Passed
Pull Request — master (#1412)
by Jakob
03:19
created

migrate-config.php (22 issues)

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
$handle is of type resource, thus it always evaluated to false.
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
The constant DEFAULT_ENDPOINT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56
$dialect = defined('DEFAULT_SPARQL_DIALECT') ? DEFAULT_SPARQL_DIALECT : "?";
0 ignored issues
show
The constant DEFAULT_SPARQL_DIALECT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57
$collationEnabled = defined('SPARQL_COLLATION_ENABLED') ? (SPARQL_COLLATION_ENABLED ? "true" : "false") : "?";
0 ignored issues
show
The constant SPARQL_COLLATION_ENABLED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
$sparqlTimeout = defined('SPARQL_TIMEOUT') ? SPARQL_TIMEOUT : "?";
0 ignored issues
show
The constant SPARQL_TIMEOUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
$httpTimeout = defined('HTTP_TIMEOUT') ? HTTP_TIMEOUT : "?";
0 ignored issues
show
The constant HTTP_TIMEOUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60
$serviceName = defined('SERVICE_NAME') ? SERVICE_NAME : "?";
0 ignored issues
show
The constant SERVICE_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
$baseHref = defined('BASE_HREF') ? BASE_HREF : "?";
0 ignored issues
show
The constant BASE_HREF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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
The constant SEARCH_RESULTS_SIZE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
$transitiveLimit = defined('DEFAULT_TRANSITIVE_LIMIT') ? DEFAULT_TRANSITIVE_LIMIT : "?";
0 ignored issues
show
The constant DEFAULT_TRANSITIVE_LIMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
$logCaughtExceptions = defined('LOG_CAUGHT_EXCEPTIONS') ? (LOG_CAUGHT_EXCEPTIONS ? "true" : "false") : "?";
0 ignored issues
show
The constant LOG_CAUGHT_EXCEPTIONS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
$logBrowserConsole = defined('LOG_BROWSER_CONSOLE') ? (LOG_BROWSER_CONSOLE ? "true" : "false") : "?";
0 ignored issues
show
The constant LOG_BROWSER_CONSOLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
72
$logFileName = defined('LOG_FILE_NAME') && !empty(LOG_FILE_NAME) ? LOG_FILE_NAME : "?";
0 ignored issues
show
The constant LOG_FILE_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
$templateCache = defined('TEMPLATE_CACHE') ? TEMPLATE_CACHE : "?";
0 ignored issues
show
The constant TEMPLATE_CACHE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
74
$customCss = defined('CUSTOM_CSS') ? CUSTOM_CSS : "?";
0 ignored issues
show
The constant CUSTOM_CSS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
$feedbackAddress = defined('FEEDBACK_ADDRESS') ? FEEDBACK_ADDRESS : "?";
0 ignored issues
show
The constant FEEDBACK_ADDRESS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
$feedbackSender = defined('FEEDBACK_SENDER') ? FEEDBACK_SENDER : "?";
0 ignored issues
show
The constant FEEDBACK_SENDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
77
$feedbackEnvelopeSender = defined('FEEDBACK_ENVELOPE_SENDER') ? FEEDBACK_ENVELOPE_SENDER : "?";
0 ignored issues
show
The constant FEEDBACK_ENVELOPE_SENDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
78
$uiLanguageDropdown = defined('UI_LANGUAGE_DROPDOWN') ? (UI_LANGUAGE_DROPDOWN ? "true" : "false") : "?";
0 ignored issues
show
The constant UI_LANGUAGE_DROPDOWN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
79
$uiHoneypotEnabled = defined('UI_HONEYPOT_ENABLED') ? (UI_HONEYPOT_ENABLED ? "true" : "false") : "?";
0 ignored issues
show
The constant UI_HONEYPOT_ENABLED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
80
$uiHoneypotTime = defined('UI_HONEYPOT_TIME') ? UI_HONEYPOT_TIME : "?";
0 ignored issues
show
The constant UI_HONEYPOT_TIME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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
The constant GLOBAL_PLUGINS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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