NatLibFi /
Skosmos
| 1 | <?php |
||
| 2 | |||
| 3 | /* Converts old config.inc and vocabulary.ttl configuration files, into new config.ttl */ |
||
| 4 | |||
| 5 | // run only in cli command line mode |
||
| 6 | if (php_sapi_name() !== "cli") { |
||
| 7 | throw new \Exception("This tool can run only in command line mode!"); |
||
| 8 | } |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Parse the vocabularies file, and return it in two sections, the |
||
| 12 | * prefixes, and the rest of the configuration. |
||
| 13 | * @param string $vocabulariesFile vocabularies file location |
||
| 14 | * @return array |
||
| 15 | */ |
||
| 16 | function parse_vocabularies_file($vocabulariesFile) |
||
| 17 | { |
||
| 18 | if (!is_file($vocabulariesFile)) { |
||
| 19 | throw new \Exception("Invalid vocabularies file: $vocabulariesFile"); |
||
| 20 | } |
||
| 21 | $prefixes = ""; |
||
| 22 | $config = ""; |
||
| 23 | $handle = fopen($vocabulariesFile, "r"); |
||
| 24 | if (!$handle) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | throw new \Exception("Failed to open vocabularies file: $vocabulariesFile"); |
||
| 26 | } |
||
| 27 | $prefixPrefix = '@prefix'; |
||
| 28 | while (($line = fgets($handle)) !== false) { |
||
| 29 | if ($prefixPrefix === substr(trim($line), 0, strlen($prefixPrefix))) { |
||
| 30 | $prefixes .= "$line"; |
||
| 31 | } else { |
||
| 32 | $config .= "$line"; |
||
| 33 | } |
||
| 34 | } |
||
| 35 | fclose($handle); |
||
| 36 | return ["prefixes" => $prefixes, 'config' => $config]; |
||
| 37 | } |
||
| 38 | |||
| 39 | // print usage if no args |
||
| 40 | if (!isset($argc) || $argc !== 3) { |
||
| 41 | throw new \Exception("Usage: php migrate-config config.inc vocabularies.ttl > config.ttl"); |
||
| 42 | } |
||
| 43 | |||
| 44 | $configFile = $argv[1]; |
||
| 45 | $vocabulariesFile = $argv[2]; |
||
| 46 | |||
| 47 | # parse the file into an array with the keys "prefixes" and "config" |
||
| 48 | $vocabs = parse_vocabularies_file($vocabulariesFile); |
||
| 49 | |||
| 50 | # read the old style config file and use the constants to set variables for use in the template |
||
| 51 | if (!is_file($configFile)) { |
||
| 52 | throw new \Exception("Invalid configuration file: $configFile"); |
||
| 53 | } |
||
| 54 | include($configFile); |
||
| 55 | $endpoint = defined('DEFAULT_ENDPOINT') ? DEFAULT_ENDPOINT : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 56 | $dialect = defined('DEFAULT_SPARQL_DIALECT') ? DEFAULT_SPARQL_DIALECT : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 57 | $collationEnabled = defined('SPARQL_COLLATION_ENABLED') ? (SPARQL_COLLATION_ENABLED ? "true" : "false") : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 58 | $sparqlTimeout = defined('SPARQL_TIMEOUT') ? SPARQL_TIMEOUT : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 59 | $httpTimeout = defined('HTTP_TIMEOUT') ? HTTP_TIMEOUT : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 60 | $serviceName = defined('SERVICE_NAME') ? SERVICE_NAME : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 61 | $baseHref = defined('BASE_HREF') ? BASE_HREF : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 62 | $languages = ""; |
||
| 63 | if (isset($LANGUAGES) && !is_null($LANGUAGES) && is_array($LANGUAGES) && !empty($LANGUAGES)) { |
||
| 64 | foreach ($LANGUAGES as $code => $name) { |
||
| 65 | $languages .= " [ rdfs:label \"$code\" ; rdf:value \"$name\" ]\n"; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | $searchResultsSize = defined('SEARCH_RESULTS_SIZE') ? SEARCH_RESULTS_SIZE : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 69 | $transitiveLimit = defined('DEFAULT_TRANSITIVE_LIMIT') ? DEFAULT_TRANSITIVE_LIMIT : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 70 | $logCaughtExceptions = defined('LOG_CAUGHT_EXCEPTIONS') ? (LOG_CAUGHT_EXCEPTIONS ? "true" : "false") : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 71 | $logBrowserConsole = defined('LOG_BROWSER_CONSOLE') ? (LOG_BROWSER_CONSOLE ? "true" : "false") : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 72 | $logFileName = defined('LOG_FILE_NAME') && !empty(LOG_FILE_NAME) ? LOG_FILE_NAME : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 73 | $templateCache = defined('TEMPLATE_CACHE') ? TEMPLATE_CACHE : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 74 | $customCss = defined('CUSTOM_CSS') ? CUSTOM_CSS : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 75 | $feedbackAddress = defined('FEEDBACK_ADDRESS') ? FEEDBACK_ADDRESS : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 76 | $feedbackSender = defined('FEEDBACK_SENDER') ? FEEDBACK_SENDER : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 77 | $feedbackEnvelopeSender = defined('FEEDBACK_ENVELOPE_SENDER') ? FEEDBACK_ENVELOPE_SENDER : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 78 | $uiLanguageDropdown = defined('UI_LANGUAGE_DROPDOWN') ? (UI_LANGUAGE_DROPDOWN ? "true" : "false") : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 79 | $uiHoneypotEnabled = defined('UI_HONEYPOT_ENABLED') ? (UI_HONEYPOT_ENABLED ? "true" : "false") : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 80 | $uiHoneypotTime = defined('UI_HONEYPOT_TIME') ? UI_HONEYPOT_TIME : "?"; |
||
|
0 ignored issues
–
show
|
|||
| 81 | $globalPluginsArray = []; |
||
| 82 | $globalPlugins = ""; |
||
| 83 | if (defined('GLOBAL_PLUGINS') && !is_null(GLOBAL_PLUGINS) && is_string(GLOBAL_PLUGINS) && !empty(trim(GLOBAL_PLUGINS))) { |
||
|
0 ignored issues
–
show
|
|||
| 84 | foreach (explode(' ', GLOBAL_PLUGINS) as $pluginName) { |
||
| 85 | $globalPluginsArray[] = "\"$pluginName\""; |
||
| 86 | } |
||
| 87 | $globalPlugins = " " . implode(', ', $globalPluginsArray) . " "; |
||
| 88 | } |
||
| 89 | |||
| 90 | # print the prefixes |
||
| 91 | echo $vocabs['prefixes']; |
||
| 92 | |||
| 93 | # print the global config using a string template |
||
| 94 | $globalConfig = <<<EOT |
||
| 95 | |||
| 96 | # Skosmos main configuration |
||
| 97 | |||
| 98 | :config a skosmos:Configuration ; |
||
| 99 | # SPARQL endpoint |
||
| 100 | # a local Fuseki server is usually on localhost:3030 |
||
| 101 | skosmos:sparqlEndpoint <$endpoint> ; |
||
| 102 | # sparql-query extension, or "Generic" for plain SPARQL 1.1 |
||
| 103 | # set to "JenaText" instead if you use Fuseki with jena-text index |
||
| 104 | skosmos:sparqlDialect "$dialect" ; |
||
| 105 | # whether to enable collation in sparql queries |
||
| 106 | skosmos:sparqlCollationEnabled $collationEnabled ; |
||
| 107 | # HTTP client configuration |
||
| 108 | skosmos:sparqlTimeout $sparqlTimeout ; |
||
| 109 | skosmos:httpTimeout $httpTimeout ; |
||
| 110 | # customize the service name |
||
| 111 | skosmos:serviceName "$serviceName" ; |
||
| 112 | # customize the base element. Set this if the automatic base url detection doesn't work. For example setups behind a proxy. |
||
| 113 | skosmos:baseHref "$baseHref" ; |
||
| 114 | # interface languages available, and the corresponding system locales |
||
| 115 | skosmos:languages ( |
||
| 116 | $languages ) ; |
||
| 117 | # how many results (maximum) to load at a time on the search results page |
||
| 118 | skosmos:searchResultsSize $searchResultsSize ; |
||
| 119 | # how many items (maximum) to retrieve in transitive property queries |
||
| 120 | skosmos:transitiveLimit $transitiveLimit ; |
||
| 121 | # whether or not to log caught exceptions |
||
| 122 | skosmos:logCaughtExceptions $logCaughtExceptions ; |
||
| 123 | # set to TRUE to enable logging into browser console |
||
| 124 | skosmos:logBrowserConsole $logBrowserConsole ; |
||
| 125 | # set to a logfile path to enable logging into log file |
||
| 126 | skosmos:logFileName "$logFileName" ; |
||
| 127 | # a default location for Twig template rendering |
||
| 128 | skosmos:templateCache "$templateCache" ; |
||
| 129 | # customize the css by adding your own stylesheet |
||
| 130 | skosmos:customCss "$customCss" ; |
||
| 131 | # default email address where to send the feedback |
||
| 132 | skosmos:feedbackAddress "$feedbackAddress" ; |
||
| 133 | # email address to set as the sender for feedback messages |
||
| 134 | skosmos:feedbackSender "$feedbackSender" ; |
||
| 135 | # email address to set as the envelope sender for feedback messages |
||
| 136 | skosmos:feedbackEnvelopeSender "$feedbackEnvelopeSender" ; |
||
| 137 | # whether to display the ui language selection as a dropdown (useful for cases where there are more than 3 languages) |
||
| 138 | skosmos:uiLanguageDropdown $uiLanguageDropdown ; |
||
| 139 | # whether to enable the spam honey pot or not, enabled by default |
||
| 140 | skosmos:uiHoneypotEnabled $uiHoneypotEnabled ; |
||
| 141 | # default time a user must wait before submitting a form |
||
| 142 | skosmos:uiHoneypotTime $uiHoneypotTime ; |
||
| 143 | # plugins to activate for the whole installation (including all vocabularies) |
||
| 144 | skosmos:globalPlugins ($globalPlugins) . |
||
| 145 | |||
| 146 | EOT; |
||
| 147 | |||
| 148 | echo preg_replace('/(\\s*)(.*\\?[\\"]?[\s]*;.*)/', "\\1# \\2", $globalConfig); |
||
| 149 | |||
| 150 | echo "\n# Skosmos vocabularies\n"; |
||
| 151 | |||
| 152 | # print the vocabulary-specific configuration |
||
| 153 | echo $vocabs['config']; |
||
| 154 |