These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
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) { |
||
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 : "?"; |
||
56 | $dialect = defined('DEFAULT_SPARQL_DIALECT') ? DEFAULT_SPARQL_DIALECT : "?"; |
||
57 | $collationEnabled = defined('SPARQL_COLLATION_ENABLED') ? (SPARQL_COLLATION_ENABLED ? "true" : "false") : "?"; |
||
58 | $sparqlTimeout = defined('SPARQL_TIMEOUT') ? SPARQL_TIMEOUT : "?"; |
||
59 | $httpTimeout = defined('HTTP_TIMEOUT') ? HTTP_TIMEOUT : "?"; |
||
60 | $serviceName = defined('SERVICE_NAME') ? SERVICE_NAME : "?"; |
||
61 | $baseHref = defined('BASE_HREF') ? BASE_HREF : "?"; |
||
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 : "?"; |
||
69 | $transitiveLimit = defined('DEFAULT_TRANSITIVE_LIMIT') ? DEFAULT_TRANSITIVE_LIMIT : "?"; |
||
70 | $logCaughtExceptions = defined('LOG_CAUGHT_EXCEPTIONS') ? (LOG_CAUGHT_EXCEPTIONS ? "true" : "false") : "?"; |
||
71 | $logBrowserConsole = defined('LOG_BROWSER_CONSOLE') ? (LOG_BROWSER_CONSOLE ? "true" : "false") : "?"; |
||
72 | $logFileName = defined('LOG_FILE_NAME') ? LOG_FILE_NAME : "?"; |
||
73 | $templateCache = defined('TEMPLATE_CACHE') ? TEMPLATE_CACHE : "?"; |
||
74 | $customCss = defined('CUSTOM_CSS') ? CUSTOM_CSS : "?"; |
||
75 | $feedbackAddress = defined('FEEDBACK_ADDRESS') ? FEEDBACK_ADDRESS : "?"; |
||
76 | $feedbackSender = defined('FEEDBACK_SENDER') ? FEEDBACK_SENDER : "?"; |
||
77 | $feedbackEnvelopeSender = defined('FEEDBACK_ENVELOPE_SENDER') ? FEEDBACK_ENVELOPE_SENDER : "?"; |
||
78 | $uiLanguageDropdown = defined('UI_LANGUAGE_DROPDOWN') ? (UI_LANGUAGE_DROPDOWN ? "true" : "false") : "?"; |
||
79 | $uiHoneypotEnabled = defined('UI_HONEYPOT_ENABLED') ? (UI_HONEYPOT_ENABLED ? "true" : "false") : "?"; |
||
80 | $uiHoneypotTime = defined('UI_HONEYPOT_TIME') ? UI_HONEYPOT_TIME : "?"; |
||
81 | $globalPluginsArray = []; |
||
82 | $globalPlugins = ""; |
||
83 | if (defined('GLOBAL_PLUGINS') && !is_null(GLOBAL_PLUGINS) && is_string(GLOBAL_PLUGINS) && !empty(trim(GLOBAL_PLUGINS))) { |
||
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 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.