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

@ -48,7 +48,7 @@ var listURIPrefixesPHP string
// server is an optional server to fetch prefixes from.
// server may be nil.
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 {
return nil, err
}
@ -61,26 +61,49 @@ func (prefixes *Prefixes) All(ctx context.Context, server *phpx.Server) ([]strin
return append(uris, uris2...), nil
}
func (wisski *Prefixes) triplestore(ctx context.Context, server *phpx.Server) (prefixes []string, err error) {
// get all the ugly prefixes
err = wisski.dependencies.PHP.ExecScript(ctx, server, &prefixes, listURIPrefixesPHP, "list_prefixes")
// getLivePrefixes get the list of prefixes found within the live system
func (prefixes *Prefixes) getLivePrefixes(ctx context.Context, server *phpx.Server) (pfs []string, err error) {
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 {
return nil, err
}
// filter out sequential prefixes
prefixes = collection.NonSequential(prefixes, func(prev, now string) bool {
return strings.HasPrefix(now, prev)
})
// sort the prefixes, and remove duplicates
slices.Sort(pfs)
pfs = collection.Deduplicate(pfs)
// load the list of blocked prefixes
blocks, err := wisski.blocked()
blocks, err := prefixes.blocked()
if err != nil {
return nil, err
}
// 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) {

View file

@ -1,16 +1,16 @@
<?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.
*/
function list_prefixes() {
function list_triplestore_prefixes() {
$prefixes = [];
$storage = \Drupal::entityTypeManager()->getStorage('wisski_salz_adapter');
foreach ($storage->loadMultiple() as $adapter) {
// load all the prefixes from the triplestore
$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
$conf = $engine->getConfiguration();
@ -22,7 +22,29 @@ function list_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!
if (!method_exists($engine, 'directQuery')) return NULL;