2 @define('CONST_LibDir', dirname(dirname(__FILE__)));
4 require_once(CONST_LibDir.'/init-cmd.php');
5 ini_set('memory_limit', '800M');
6 ini_set('display_errors', 'stderr');
10 'Import and export special phrases',
11 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
12 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
13 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
14 array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create import script for search phrases '),
15 array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
17 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
19 loadSettings($aCMDResult['project-dir'] ?? getcwd());
22 include(getSettingConfig('PHRASE_CONFIG', 'phrase_settings.php'));
24 if ($aCMDResult['wiki-import']) {
25 $oNormalizer = Transliterator::createFromRules(getSetting('TERM_NORMALIZATION'));
28 $sLanguageIn = getSetting(
30 'af,ar,br,ca,cs,de,en,es,et,eu,fa,fi,fr,gl,hr,hu,'.
31 'ia,is,it,ja,mk,nl,no,pl,ps,pt,ru,sk,sl,sv,uk,vi'
34 foreach (explode(',', $sLanguageIn) as $sLanguage) {
35 $sURL = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
36 $sWikiPageXML = file_get_contents($sURL);
39 '#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#',
47 foreach ($aMatches as $aMatch) {
48 $sLabel = trim($aMatch[1]);
49 if ($oNormalizer !== null) {
50 $sTrans = pg_escape_string($oNormalizer->transliterate($sLabel));
54 $sClass = trim($aMatch[2]);
55 $sType = trim($aMatch[3]);
56 // hack around a bug where building=yes was imported with
57 // quotes into the wiki
58 $sType = preg_replace('/("|")/', '', $sType);
59 // sanity check, in case somebody added garbage in the wiki
60 if (preg_match('/^\\w+$/', $sClass) < 1
61 || preg_match('/^\\w+$/', $sType) < 1
63 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
66 // blacklisting: disallow certain class/type combinations
67 if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
68 // fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
71 // whitelisting: if class is in whitelist, allow only tags in the list
72 if (isset($aTagsWhitelist[$sClass]) && !in_array($sType, $aTagsWhitelist[$sClass])) {
73 // fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
76 $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
78 switch (trim($aMatch[4])) {
81 "SELECT getorcreate_amenityoperator(make_standard_name('%s'), '%s', '%s', '%s', 'near');\n",
82 pg_escape_string($sLabel),
90 "SELECT getorcreate_amenityoperator(make_standard_name('%s'), '%s', '%s', '%s', 'in');\n",
91 pg_escape_string($sLabel),
99 "SELECT getorcreate_amenity(make_standard_name('%s'), '%s', '%s', '%s');\n",
100 pg_escape_string($sLabel),
110 echo 'CREATE INDEX idx_placex_classtype ON placex (class, type);';
112 foreach ($aPairs as $aPair) {
113 $sql_tablespace = getSetting('TABLESPACE_AUX_DATA');
114 if ($sql_tablespace) {
115 $sql_tablespace = ' TABLESPACE '.$sql_tablespace;
119 'CREATE TABLE place_classtype_%s_%s'
122 . ' SELECT place_id AS place_id,st_centroid(geometry) AS centroid FROM placex'
123 . " WHERE class = '%s' AND type = '%s'"
125 pg_escape_string($aPair[0]),
126 pg_escape_string($aPair[1]),
127 pg_escape_string($aPair[0]),
128 pg_escape_string($aPair[1])
132 'CREATE INDEX idx_place_classtype_%s_%s_centroid'
133 . ' ON place_classtype_%s_%s USING GIST (centroid)'
136 pg_escape_string($aPair[0]),
137 pg_escape_string($aPair[1]),
138 pg_escape_string($aPair[0]),
139 pg_escape_string($aPair[1])
143 'CREATE INDEX idx_place_classtype_%s_%s_place_id'
144 . ' ON place_classtype_%s_%s USING btree(place_id)'
147 pg_escape_string($aPair[0]),
148 pg_escape_string($aPair[1]),
149 pg_escape_string($aPair[0]),
150 pg_escape_string($aPair[1])
154 'GRANT SELECT ON place_classtype_%s_%s TO "%s"'
156 pg_escape_string($aPair[0]),
157 pg_escape_string($aPair[1]),
158 getSetting('DATABASE_WEBUSER')
162 echo 'DROP INDEX idx_placex_classtype;';