Fetch Prefixes from Triplestore

This commit is contained in:
Tom Wiesing 2022-09-21 15:20:42 +02:00
parent a63e656f69
commit 6f2ba18227
No known key found for this signature in database
8 changed files with 209 additions and 23 deletions

View file

@ -12,6 +12,13 @@
<b>Slug:</b> <code>{{ .Info.Slug }}</code> <br />
<b>URL:</b> <a href="{{ .Info.URL }}" target="_blank" rel="noopener noreferrer">{{ .Info.URL }}</a> <br />
<hr />
<b>Resolver Prefixes:</b>
<ul>
{{ range .Info.Prefixes }}
<li><code>{{ . }}</code></li>
{{ end}}
</ul>
<hr />
<b>Running:</b> <code>{{ .Info.Running }}</code> <br />
<!-- <b>OwnerEmail:</b> <code>{{ .Instance.OwnerEmail }}</code> <br /> -->
<hr />

View file

@ -1,19 +0,0 @@
<?php
/**
* This script will list all the URIs that this system is aware of.
* This works by listing all the default graph uris of all the adapters.
*/
// iterate over all adapters
$storage = \Drupal::entityTypeManager()->getStorage('wisski_salz_adapter');
foreach ($storage->loadMultiple() as $adapter) {
// read the configuration, and check if we have a default graph
$conf = $adapter->getEngine()->getConfiguration();
if(!array_key_exists('default_graph', $conf)) {
continue;
}
// and echo it out
echo $conf['default_graph'] . "\n";
}

View file

@ -0,0 +1,48 @@
<?php
/**
* list_prefixes lists all content prefixes known to this WissKI.
* Prefixes are not filtered, and may contain duplicates.
*/
function list_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);
// 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 getTriplestorePrefixes($engine, &$prefixes) {
$results = $engine->directQuery('
select distinct ?base where {
{
select distinct ?iri where {
{
select distinct (?s as ?iri) { ?s ?p ?o }
} union {
select distinct (?o as ?iri) { ?s ?p ?o FILTER(isiri(?o)) }
}
}
}
BIND(replace(str(?iri), "/[^/]*/?$", "/") as ?base)
FILTER(!REGEX(?base, "/wisski/navigate/[\\\\d]+/$"))
} ORDER BY ?base');
if (!$results) return FALSE;
foreach($results as $result) {
$prefixes[] = $result->base->getValue();
}
return TRUE;
}

View file

@ -7,7 +7,10 @@ import (
"strings"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/slicesx"
"github.com/tkw1536/goprogram/stream"
_ "embed"
)
// NoPrefix checks if this WissKI instance is excluded from generating prefixes.
@ -16,6 +19,43 @@ func (wisski *WissKI) NoPrefix() bool {
return fsx.IsFile(wisski.instances.Environment, filepath.Join(wisski.FilesystemBase, "prefixes.skip"))
}
//go:embed php/list_uri_prefixes.php
var listURIPrefixesPHP string
// Prefixes returns the prefixes
func (wisski *WissKI) Prefixes() (prefixes []string, err error) {
// get all the ugly prefixes
err = wisski.ExecPHPScript(stream.FromEnv(), &prefixes, listURIPrefixesPHP, "list_prefixes")
if err != nil {
return nil, err
}
// filter out sequential prefixes
prefixes = slicesx.NonSequential(prefixes, func(prev, now string) bool {
return strings.HasPrefix(now, prev)
})
// filter out blocked prefixes
return slicesx.Filter(prefixes, func(uri string) bool { return !IsNonServedURI(uri) }), nil
}
// TODO: Eventually move this into a configuration file.
// But for now this is fine
var blockedURIs = []string{
"http://erlangen-crm.org/",
"http://www.w3.org/",
"xsd:",
}
func IsNonServedURI(candidate string) bool {
return slicesx.Any(
blockedURIs,
func(prefix string) bool {
return strings.HasPrefix(candidate, prefix)
},
)
}
var errPrefixExecFailed = errors.New("PrefixConfig: Failed to call list_uri_prefixes")
// PrefixConfig returns the prefix config belonging to this instance.
@ -32,10 +72,15 @@ func (wisski *WissKI) PrefixConfig() (config string, err error) {
builder.WriteString("\n")
// default prefixes
wu := stream.NewIOStream(&builder, nil, nil, 0)
code, err := wisski.Barrel().Exec(wu, "barrel", "/bin/bash", "/user_shell.sh", "-c", "drush php:script /wisskiutils/list_uri_prefixes.php")
if err != nil || code != 0 {
return "", errPrefixExecFailed
prefixes, err := wisski.Prefixes()
if err != nil {
return "", err
}
// predefined prefixes
for _, prefix := range prefixes {
builder.WriteString(prefix)
builder.WriteRune('\n')
}
// custom prefixes

View file

@ -17,6 +17,7 @@ type Info struct {
Running bool // is the instance running?
Pathbuilders map[string]string // list of pathbuilders
Prefixes []string // list of uri prefixes
}
// Info returns information about this WissKI instance.
@ -46,6 +47,10 @@ func (wisski *WissKI) Info(quick bool) (info Info, err error) {
info.LastRebuild, _ = wisski.LastRebuild()
return nil
})
group.Go(func() (err error) {
info.Prefixes, _ = wisski.Prefixes()
return nil
})
}
err = group.Wait()