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 = getSetting('TABLESPACE_AUX_DATA');
113 if ($sql_tablespace) {
114 $sql_tablespace = ' TABLESPACE '.$sql_tablespace;
118 'CREATE TABLE place_classtype_%s_%s'
121 . ' SELECT place_id AS place_id,st_centroid(geometry) AS centroid FROM placex'
122 . " WHERE class = '%s' AND type = '%s'"
124 pg_escape_string($aPair[0]),
125 pg_escape_string($aPair[1]),
126 pg_escape_string($aPair[0]),
127 pg_escape_string($aPair[1])
131 'CREATE INDEX idx_place_classtype_%s_%s_centroid'
132 . ' ON place_classtype_%s_%s USING GIST (centroid)'
135 pg_escape_string($aPair[0]),
136 pg_escape_string($aPair[1]),
137 pg_escape_string($aPair[0]),
138 pg_escape_string($aPair[1])
142 'CREATE INDEX idx_place_classtype_%s_%s_place_id'
143 . ' ON place_classtype_%s_%s USING btree(place_id)'
146 pg_escape_string($aPair[0]),
147 pg_escape_string($aPair[1]),
148 pg_escape_string($aPair[0]),
149 pg_escape_string($aPair[1])
153 'GRANT SELECT ON place_classtype_%s_%s TO "%s"'
155 pg_escape_string($aPair[0]),
156 pg_escape_string($aPair[1]),
157 getSetting('DATABASE_WEBUSER')
161 echo 'DROP INDEX idx_placex_classtype;';