]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / utils / specialphrases.php
1 <?php
2
3 require_once(CONST_BasePath.'/lib/init-cmd.php');
4 ini_set('memory_limit', '800M');
5 ini_set('display_errors', 'stderr');
6
7 $aCMDOptions
8 = array(
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   );
15 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
16
17 include(CONST_Phrase_Config);
18
19 if ($aCMDResult['wiki-import']) {
20     $oNormalizer = Transliterator::createFromRules(CONST_Term_Normalization_Rules);
21     $aPairs = array();
22
23     $sLanguageIn = CONST_Languages ? CONST_Languages :
24         ('af,ar,br,ca,cs,de,en,es,et,eu,fa,fi,fr,gl,hr,hu,'.
25          'ia,is,it,ja,mk,nl,no,pl,ps,pt,ru,sk,sl,sv,uk,vi');
26
27     foreach (explode(',', $sLanguageIn) as $sLanguage) {
28         $sURL = 'https://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
29         $sWikiPageXML = file_get_contents($sURL);
30
31         if (!preg_match_all(
32             '#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#',
33             $sWikiPageXML,
34             $aMatches,
35             PREG_SET_ORDER
36         )) {
37             continue;
38         }
39
40         foreach ($aMatches as $aMatch) {
41             $sLabel = trim($aMatch[1]);
42             if ($oNormalizer !== null) {
43                 $sTrans = pg_escape_string($oNormalizer->transliterate($sLabel));
44             } else {
45                 $sTrans = null;
46             }
47             $sClass = trim($aMatch[2]);
48             $sType = trim($aMatch[3]);
49             // hack around a bug where building=yes was imported with
50             // quotes into the wiki
51             $sType = preg_replace('/(&quot;|")/', '', $sType);
52             // sanity check, in case somebody added garbage in the wiki
53             if (preg_match('/^\\w+$/', $sClass) < 1
54                 || preg_match('/^\\w+$/', $sType) < 1
55             ) {
56                 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
57                 exit;
58             }
59             // blacklisting: disallow certain class/type combinations
60             if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
61                 // fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
62                 continue;
63             }
64             // whitelisting: if class is in whitelist, allow only tags in the list
65             if (isset($aTagsWhitelist[$sClass]) && !in_array($sType, $aTagsWhitelist[$sClass])) {
66                 // fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
67                 continue;
68             }
69             $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
70
71             switch (trim($aMatch[4])) {
72                 case 'near':
73                     printf(
74                         "SELECT getorcreate_amenityoperator(make_standard_name('%s'), '%s', '%s', '%s', 'near');\n",
75                         pg_escape_string($sLabel),
76                         $sTrans,
77                         $sClass,
78                         $sType
79                     );
80                     break;
81                 case 'in':
82                     printf(
83                         "SELECT getorcreate_amenityoperator(make_standard_name('%s'), '%s', '%s', '%s', 'in');\n",
84                         pg_escape_string($sLabel),
85                         $sTrans,
86                         $sClass,
87                         $sType
88                     );
89                     break;
90                 default:
91                     printf(
92                         "SELECT getorcreate_amenity(make_standard_name('%s'), '%s', '%s', '%s');\n",
93                         pg_escape_string($sLabel),
94                         $sTrans,
95                         $sClass,
96                         $sType
97                     );
98                     break;
99             }
100         }
101     }
102
103     echo 'CREATE INDEX idx_placex_classtype ON placex (class, type);';
104
105     foreach ($aPairs as $aPair) {
106         $sql_tablespace = CONST_Tablespace_Aux_Data ? ' TABLESPACE '.CONST_Tablespace_Aux_Data : '';
107
108         printf(
109             'CREATE TABLE place_classtype_%s_%s'
110             . $sql_tablespace
111             . ' AS'
112             . ' SELECT place_id AS place_id,st_centroid(geometry) AS centroid FROM placex'
113             . " WHERE class = '%s' AND type = '%s'"
114             . ";\n",
115             pg_escape_string($aPair[0]),
116             pg_escape_string($aPair[1]),
117             pg_escape_string($aPair[0]),
118             pg_escape_string($aPair[1])
119         );
120
121         printf(
122             'CREATE INDEX idx_place_classtype_%s_%s_centroid'
123             . ' ON place_classtype_%s_%s USING GIST (centroid)'
124             . $sql_tablespace
125             . ";\n",
126             pg_escape_string($aPair[0]),
127             pg_escape_string($aPair[1]),
128             pg_escape_string($aPair[0]),
129             pg_escape_string($aPair[1])
130         );
131
132         printf(
133             'CREATE INDEX idx_place_classtype_%s_%s_place_id'
134             . ' ON place_classtype_%s_%s USING btree(place_id)'
135             . $sql_tablespace
136             . ";\n",
137             pg_escape_string($aPair[0]),
138             pg_escape_string($aPair[1]),
139             pg_escape_string($aPair[0]),
140             pg_escape_string($aPair[1])
141         );
142
143         printf(
144             'GRANT SELECT ON place_classtype_%s_%s TO "%s"'
145             . ";\n",
146             pg_escape_string($aPair[0]),
147             pg_escape_string($aPair[1]),
148             CONST_Database_Web_User
149         );
150     }
151
152     echo 'DROP INDEX idx_placex_classtype;';
153 }