3 require_once(CONST_LibDir.'/init-cmd.php');
4 ini_set('memory_limit', '800M');
5 ini_set('display_errors', 'stderr');
9 'Import and export special phrases',
10 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
11 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
12 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
13 array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create import script for search phrases '),
14 array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
16 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
18 loadSettings($aCMDResult['project-dir'] ?? getcwd());
21 include(getSettingConfig('PHRASE_CONFIG', 'phrase_settings.php'));
23 if ($aCMDResult['wiki-import']) {
24 $oNormalizer = Transliterator::createFromRules(getSetting('TERM_NORMALIZATION'));
27 $sLanguageIn = getSetting(
29 'af,ar,br,ca,cs,de,en,es,et,eu,fa,fi,fr,gl,hr,hu,'.
30 'ia,is,it,ja,mk,nl,no,pl,ps,pt,ru,sk,sl,sv,uk,vi';
33 foreach (explode(',', $sLanguageIn) as $sLanguage) {
34 $sURL = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
35 $sWikiPageXML = file_get_contents($sURL);
38 '#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#',
46 foreach ($aMatches as $aMatch) {
47 $sLabel = trim($aMatch[1]);
48 if ($oNormalizer !== null) {
49 $sTrans = pg_escape_string($oNormalizer->transliterate($sLabel));
53 $sClass = trim($aMatch[2]);
54 $sType = trim($aMatch[3]);
55 // hack around a bug where building=yes was imported with
56 // quotes into the wiki
57 $sType = preg_replace('/("|")/', '', $sType);
58 // sanity check, in case somebody added garbage in the wiki
59 if (preg_match('/^\\w+$/', $sClass) < 1
60 || preg_match('/^\\w+$/', $sType) < 1
62 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
65 // blacklisting: disallow certain class/type combinations
66 if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
67 // fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
70 // whitelisting: if class is in whitelist, allow only tags in the list
71 if (isset($aTagsWhitelist[$sClass]) && !in_array($sType, $aTagsWhitelist[$sClass])) {
72 // fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
75 $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
77 switch (trim($aMatch[4])) {
80 "SELECT getorcreate_amenityoperator(make_standard_name('%s'), '%s', '%s', '%s', 'near');\n",
81 pg_escape_string($sLabel),
89 "SELECT getorcreate_amenityoperator(make_standard_name('%s'), '%s', '%s', '%s', 'in');\n",
90 pg_escape_string($sLabel),
98 "SELECT getorcreate_amenity(make_standard_name('%s'), '%s', '%s', '%s');\n",
99 pg_escape_string($sLabel),
109 echo 'CREATE INDEX idx_placex_classtype ON placex (class, type);';
111 foreach ($aPairs as $aPair) {
112 $sql_tablespace = CONST_Tablespace_Aux_Data ? ' TABLESPACE '.CONST_Tablespace_Aux_Data : '';
115 'CREATE TABLE place_classtype_%s_%s'
118 . ' SELECT place_id AS place_id,st_centroid(geometry) AS centroid FROM placex'
119 . " WHERE class = '%s' AND type = '%s'"
121 pg_escape_string($aPair[0]),
122 pg_escape_string($aPair[1]),
123 pg_escape_string($aPair[0]),
124 pg_escape_string($aPair[1])
128 'CREATE INDEX idx_place_classtype_%s_%s_centroid'
129 . ' ON place_classtype_%s_%s USING GIST (centroid)'
132 pg_escape_string($aPair[0]),
133 pg_escape_string($aPair[1]),
134 pg_escape_string($aPair[0]),
135 pg_escape_string($aPair[1])
139 'CREATE INDEX idx_place_classtype_%s_%s_place_id'
140 . ' ON place_classtype_%s_%s USING btree(place_id)'
143 pg_escape_string($aPair[0]),
144 pg_escape_string($aPair[1]),
145 pg_escape_string($aPair[0]),
146 pg_escape_string($aPair[1])
150 'GRANT SELECT ON place_classtype_%s_%s TO "%s"'
152 pg_escape_string($aPair[0]),
153 pg_escape_string($aPair[1]),
154 getSetting('DATABASE_WEBUSER');
158 echo 'DROP INDEX idx_placex_classtype;';