Move WissKI Parts to new ingredients system

This commit is contained in:
Tom Wiesing 2022-10-18 10:44:39 +02:00
parent b5b1ce2340
commit 42b8cbd865
No known key found for this signature in database
83 changed files with 1016 additions and 646 deletions

View file

@ -0,0 +1,44 @@
package extras
import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
"golang.org/x/exp/slices"
)
type Pathbuilder struct {
ingredient.Base
PHP *php.PHP
}
//go:embed pathbuilder.php
var pathbuilderPHP string
// All returns the ids of all pathbuilders in consistent order.
//
// server is the server to fetch the pathbuilders from, any may be nil.
func (pathbuilder *Pathbuilder) All(server *php.Server) (ids []string, err error) {
err = pathbuilder.PHP.ExecScript(server, &ids, pathbuilderPHP, "all_list")
slices.Sort(ids)
return
}
// Get returns a single pathbuilder as xml.
// If it does not exist, it returns the empty string and nil error.
//
// server is the server to fetch the pathbuilders from, any may be nil.
func (pathbuilder *Pathbuilder) Get(server *php.Server, id string) (xml string, err error) {
err = pathbuilder.PHP.ExecScript(server, &xml, pathbuilderPHP, "one_xml", id)
return
}
// GetAll returns all pathbuilders serialized as xml
//
// server is the server to fetch the pathbuilders from, any may be nil.
func (pathbuilder *Pathbuilder) GetAll(server *php.Server) (pathbuilders map[string]string, err error) {
err = pathbuilder.PHP.ExecScript(server, &pathbuilders, pathbuilderPHP, "all_xml")
return
}

View file

@ -0,0 +1,77 @@
<?php
use Drupal\wisski_pathbuilder\Entity\WisskiPathEntity;
/** all_xml lists all pathbuilders, and returns the corresponding xml */
function all_xml(): object {
$all = \Drupal::entityTypeManager()->getStorage('wisski_pathbuilder')->loadMultiple();
return (object)array_map("entity_to_xml", $all);
}
/** all_list lists the ids of all pathbuilders */
function all_list(): Array {
return array_keys(\Drupal::entityQuery('wisski_pathbuilder')->execute());
}
/** one_xml serializes a single pathbuilder as xml */
function one_xml(string $id): string {
$pb = \Drupal::entityTypeManager()->getStorage('wisski_pathbuilder')->load($id);
if ($pb === NULL) {
return "";
}
return entity_to_xml($pb);
}
// =================================================================================
// =================================================================================
function entity_to_xml($pb) {
$xml = new \SimpleXMLElement("<pathbuilderinterface></pathbuilderinterface>");
$paths = $pb->getAllPaths();
foreach ($paths as $key => $path) {
$id = $path->getID();
$path = $pb->getPbPath($id);
$pathChild = $xml->addChild("path");
$pathObject = WisskiPathEntity::load($id);
foreach ($path as $subkey => $value) {
if (in_array($subkey, ['relativepath'])) {
continue;
}
if ($subkey == "parent") {
$subkey = "group_id";
}
$pathChild->addChild($subkey, htmlspecialchars($value));
}
$pathArray = $pathChild->addChild('path_array');
foreach ($pathObject->getPathArray() as $subkey => $value) {
$pathArray->addChild($subkey % 2 == 0 ? 'x' : 'y', $value);
}
$pathChild->addChild('datatype_property', htmlspecialchars($pathObject->getDatatypeProperty()));
$pathChild->addChild('short_name', htmlspecialchars($pathObject->getShortName()));
$pathChild->addChild('disamb', htmlspecialchars($pathObject->getDisamb()));
$pathChild->addChild('description', htmlspecialchars($pathObject->getDescription()));
$pathChild->addChild('uuid', htmlspecialchars($pathObject->uuid()));
if ($pathObject->getType() == "Group" || $pathObject->getType() == "Smartgroup") {
$pathChild->addChild('is_group', "1");
} else {
$pathChild->addChild('is_group', "0");
}
$pathChild->addChild('name', htmlspecialchars($pathObject->getName()));
}
// turn it into XML
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = TRUE;
return $dom->saveXML();
}

View file

@ -0,0 +1,155 @@
package extras
import (
"bufio"
"path/filepath"
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/mstore"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/tkw1536/goprogram/lib/collection"
_ "embed"
)
// Prefixes implements reading and writing prefix
type Prefixes struct {
ingredient.Base
PHP *php.PHP
MStore *mstore.MStore
}
// NoPrefix checks if this WissKI instance is excluded from generating prefixes.
// TODO: Move this to the database!
func (prefixes *Prefixes) NoPrefix() bool {
return fsx.IsFile(prefixes.Malt.Environment, filepath.Join(prefixes.FilesystemBase, "prefixes.skip"))
}
//go:embed prefixes.php
var listURIPrefixesPHP string
// All returns the prefixes applying to this WissKI
//
// server is an optional server to fetch prefixes from.
// server may be nil.
func (prefixes *Prefixes) All(server *php.Server) ([]string, error) {
uris, err := prefixes.database(server)
if err != nil {
return nil, err
}
uris2, err := prefixes.filePrefixes()
if err != nil {
return nil, err
}
return append(uris, uris2...), nil
}
func (wisski *Prefixes) database(server *php.Server) (prefixes []string, err error) {
// get all the ugly prefixes
err = wisski.PHP.ExecScript(server, &prefixes, listURIPrefixesPHP, "list_prefixes")
if err != nil {
return nil, err
}
// filter out sequential prefixes
prefixes = collection.NonSequential(prefixes, func(prev, now string) bool {
return strings.HasPrefix(now, prev)
})
// load the list of blocked prefixes
blocks, err := wisski.blocked()
if err != nil {
return nil, err
}
// filter out blocked prefixes
return collection.Filter(prefixes, func(uri string) bool { return !hasAnyPrefix(uri, blocks) }), nil
}
func (prefixes *Prefixes) blocked() ([]string, error) {
// open the resolver block file
// TODO: move this to the distillery
file, err := prefixes.Malt.Environment.Open(prefixes.Malt.Config.SelfResolverBlockFile)
if err != nil {
return nil, err
}
var lines []string
// read all the lines that aren't a comment!
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "//") || strings.HasPrefix(line, "#") {
continue
}
lines = append(lines, line)
}
// check if there was an error
if err := scanner.Err(); err != nil {
return nil, err
}
// and done!
return lines, nil
}
func hasAnyPrefix(candidate string, prefixes []string) bool {
return collection.Any(
prefixes,
func(prefix string) bool {
return strings.HasPrefix(candidate, prefix)
},
)
}
func (wisski *Prefixes) filePrefixes() (prefixes []string, err error) {
path := filepath.Join(wisski.FilesystemBase, "prefixes")
if !fsx.IsFile(wisski.Malt.Environment, path) {
return nil, nil
}
file, err := wisski.Malt.Environment.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[0] == '#' {
continue
}
prefixes = append(prefixes, line)
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
return prefixes, nil
}
// CACHING
var prefix = mstore.For[string]("prefix")
// Prefixes returns the cached prefixes from the given instance
func (wisski *Prefixes) PrefixesCached() (results []string, err error) {
return prefix.GetAll(wisski.MStore)
}
// UpdatePrefixes updates the cached prefixes of this instance
func (wisski *Prefixes) UpdatePrefixes() error {
prefixes, err := wisski.All(nil)
if err != nil {
return err
}
return prefix.SetAll(wisski.MStore, prefixes...)
}

View file

@ -0,0 +1,51 @@
<?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) {
// some adapters don't support a query method!
if (!method_exists($engine, 'directQuery')) return NULL;
$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

@ -0,0 +1,26 @@
package extras
import (
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
)
type Settings struct {
ingredient.Base
PHP *php.PHP
}
//go:embed settings.php
var settingsPHP string
func (settings *Settings) Get(server *php.Server, key string) (value any, err error) {
err = settings.PHP.ExecScript(server, &value, settingsPHP, "get_setting", key)
return
}
func (settings *Settings) Set(server *php.Server, key string, value any) error {
return settings.PHP.ExecScript(server, nil, settingsPHP, "set_setting", key, value)
}

View file

@ -0,0 +1,29 @@
<?php
/** gets a setting from 'settings.php' */
function get_setting($name) {
use \Drupal\Core\Site\Settings;
return Settings::get($name);
}
/** sets a setting in 'settings.php' */
function set_setting($name, $value) {
// load install.inc
if(is_file(DRUPAL_ROOT . "/internal/")) {
include_once DRUPAL_ROOT . "/internal/core/includes/install.inc";
} else {
include_once DRUPAL_ROOT . "/core/includes/install.inc";
}
// update the provided setting
$settings["settings"][$name] = (object)[
"value" => $value,
"required" => TRUE,
];
// find the filename
$filename = DRUPAL_ROOT . "/" . \Drupal::service("site.path") . "/settings.php";
drupal_rewrite_settings($settings, $filename);
return True;
}