Add 'dangerously_use_adapter_prefixes' setting

This commit adds a setting to not scan the triplestore for prefixes, but
instead use the prefixes listed in adapaters as the only URIs to
resolve.
This commit is contained in:
Tom Wiesing 2024-04-08 14:43:40 +02:00
parent fd53a901c3
commit 7763644ebe
No known key found for this signature in database
4 changed files with 69 additions and 14 deletions

View file

@ -113,6 +113,10 @@ triplestore:
user_prefix: null user_prefix: null
data_prefix: null data_prefix: null
# if desired, turn off regularly scanning for prefixes in the triplestore.
# DANGER: Turning this on will break the global resolver.
dangerously_use_adapter_prefixes: false
# The maximum agefor backups to be kept. # The maximum agefor backups to be kept.
# Backups older than this will be removed when a new backup is made. # Backups older than this will be removed when a new backup is made.
# The default here is 720hours (== 30 days) # The default here is 720hours (== 30 days)

View file

@ -1,5 +1,7 @@
package config package config
import "github.com/FAU-CDI/wisski-distillery/internal/config/validators"
type DatabaseConfig struct { type DatabaseConfig struct {
// Credentials for the admin user. // Credentials for the admin user.
// Is automatically created if it does not exist. // Is automatically created if it does not exist.
@ -20,4 +22,8 @@ type SQLConfig struct {
type TSConfig struct { type TSConfig struct {
DatabaseConfig `yaml:",inline" recurse:"true"` DatabaseConfig `yaml:",inline" recurse:"true"`
// DangerouslyUseAdapterPrefixes inidicates if scanning for prefixes should just use prefixes declared in all adapters.
// This may not reflect what is actually in the database.
DangerouslyUseAdapterPrefixes validators.NullableBool `yaml:"dangerously_use_adapter_prefixes" default:"false" validate:"bool"`
} }

View file

@ -48,7 +48,7 @@ var listURIPrefixesPHP string
// server is an optional server to fetch prefixes from. // server is an optional server to fetch prefixes from.
// server may be nil. // server may be nil.
func (prefixes *Prefixes) All(ctx context.Context, server *phpx.Server) ([]string, error) { func (prefixes *Prefixes) All(ctx context.Context, server *phpx.Server) ([]string, error) {
uris, err := prefixes.triplestore(ctx, server) uris, err := prefixes.getLivePrefixes(ctx, server)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -61,26 +61,49 @@ func (prefixes *Prefixes) All(ctx context.Context, server *phpx.Server) ([]strin
return append(uris, uris2...), nil return append(uris, uris2...), nil
} }
func (wisski *Prefixes) triplestore(ctx context.Context, server *phpx.Server) (prefixes []string, err error) { // getLivePrefixes get the list of prefixes found within the live system
// get all the ugly prefixes func (prefixes *Prefixes) getLivePrefixes(ctx context.Context, server *phpx.Server) (pfs []string, err error) {
err = wisski.dependencies.PHP.ExecScript(ctx, server, &prefixes, listURIPrefixesPHP, "list_prefixes") useTS := !(prefixes.Config.TS.DangerouslyUseAdapterPrefixes.Set && prefixes.Config.TS.DangerouslyUseAdapterPrefixes.Value)
if useTS {
pfs, err = prefixes.getTSPrefixes(ctx, server)
} else {
// danger danger danger: Use the adapter prefixes
pfs, err = prefixes.getAdapterPrefixes(ctx, server)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
// filter out sequential prefixes // sort the prefixes, and remove duplicates
prefixes = collection.NonSequential(prefixes, func(prev, now string) bool { slices.Sort(pfs)
return strings.HasPrefix(now, prev) pfs = collection.Deduplicate(pfs)
})
// load the list of blocked prefixes // load the list of blocked prefixes
blocks, err := wisski.blocked() blocks, err := prefixes.blocked()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// filter out blocked prefixes // filter out blocked prefixes
return collection.Filter(prefixes, func(uri string) bool { return !hasAnyPrefix(uri, blocks) }), nil return collection.Filter(pfs, func(uri string) bool { return !hasAnyPrefix(uri, blocks) }), nil
}
func (wisski *Prefixes) getAdapterPrefixes(ctx context.Context, server *phpx.Server) (pfs []string, err error) {
err = wisski.dependencies.PHP.ExecScript(ctx, server, &pfs, listURIPrefixesPHP, "list_adapter_prefixes")
if err != nil {
return nil, err
}
return pfs, nil
}
func (wisski *Prefixes) getTSPrefixes(ctx context.Context, server *phpx.Server) (pfs []string, err error) {
err = wisski.dependencies.PHP.ExecScript(ctx, server, &pfs, listURIPrefixesPHP, "list_triplestore_prefixes")
if err != nil {
return nil, err
}
return pfs, nil
} }
func (prefixes *Prefixes) blocked() ([]string, error) { func (prefixes *Prefixes) blocked() ([]string, error) {

View file

@ -1,16 +1,16 @@
<?php <?php
/** /**
* list_prefixes lists all content prefixes known to this WissKI. * list_triplestore_prefixes returns the prefixes of all objects found in the triplestore.
* Prefixes are not filtered, and may contain duplicates. * Prefixes are not filtered, and may contain duplicates.
*/ */
function list_prefixes() { function list_triplestore_prefixes() {
$prefixes = []; $prefixes = [];
$storage = \Drupal::entityTypeManager()->getStorage('wisski_salz_adapter'); $storage = \Drupal::entityTypeManager()->getStorage('wisski_salz_adapter');
foreach ($storage->loadMultiple() as $adapter) { foreach ($storage->loadMultiple() as $adapter) {
// load all the prefixes from the triplestore // load all the prefixes from the triplestore
$engine = $adapter->getEngine(); $engine = $adapter->getEngine();
getTriplestorePrefixes($adapter->getEngine(), $prefixes); get_prefixes_from_engine($adapter->getEngine(), $prefixes);
// read the configuration to check if we have a default graph // read the configuration to check if we have a default graph
$conf = $engine->getConfiguration(); $conf = $engine->getConfiguration();
@ -22,7 +22,29 @@ function list_prefixes() {
return $prefixes; return $prefixes;
} }
function getTriplestorePrefixes($engine, &$prefixes) {
/**
* list_adapter_prefixes returns the prefixes of all adapters.
* Prefixes are not filtered, and may contain duplicates.
*/
function list_adapter_prefixes() {
$prefixes = [];
$storage = \Drupal::entityTypeManager()->getStorage('wisski_salz_adapter');
foreach ($storage->loadMultiple() as $adapter) {
// load all the prefixes from the triplestore
$engine = $adapter->getEngine();
// read the configuration to check if we have a default graph
$conf = $engine->getConfiguration();
if(!array_key_exists('default_graph', $conf)) {
continue;
}
$prefixes[] = $conf['default_graph'];
}
return $prefixes;
}
function get_prefixes_from_engine($engine, &$prefixes) {
// some adapters don't support a query method! // some adapters don't support a query method!
if (!method_exists($engine, 'directQuery')) return NULL; if (!method_exists($engine, 'directQuery')) return NULL;