]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
README: tiny markdown syntax error
[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         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER)) {
31             foreach ($aMatches as $aMatch) {
32                 $sLabel = trim($aMatch[1]);
33                 if ($oNormalizer !== null) {
34                     $sTrans = pg_escape_string($oNormalizer->transliterate($sLabel));
35                 } else {
36                     $sTrans = null;
37                 }
38                 $sClass = trim($aMatch[2]);
39                 $sType = trim($aMatch[3]);
40                 // hack around a bug where building=yes was imported with
41                 // quotes into the wiki
42                 $sType = preg_replace('/&quot;/', '', $sType);
43                 // sanity check, in case somebody added garbage in the wiki
44                 if (preg_match('/^\\w+$/', $sClass) < 1
45                     || preg_match('/^\\w+$/', $sType) < 1
46                 ) {
47                     trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
48                     exit;
49                 }
50                 // blacklisting: disallow certain class/type combinations
51                 if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
52                     // fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
53                     continue;
54                 }
55                 // whitelisting: if class is in whitelist, allow only tags in the list
56                 if (isset($aTagsWhitelist[$sClass]) && !in_array($sType, $aTagsWhitelist[$sClass])) {
57                     // fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
58                     continue;
59                 }
60                 $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
61
62                 switch (trim($aMatch[4])) {
63                     case 'near':
64                         echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sTrans', '$sClass', '$sType', 'near');\n";
65                         break;
66                     case 'in':
67                         echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sTrans', '$sClass', '$sType', 'in');\n";
68                         break;
69                     default:
70                         echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sTrans', '$sClass', '$sType');\n";
71                         break;
72                 }
73             }
74         }
75     }
76
77     echo 'create index idx_placex_classtype on placex (class, type);';
78
79     foreach ($aPairs as $aPair) {
80         echo 'create table place_classtype_'.pg_escape_string($aPair[0]).'_'.pg_escape_string($aPair[1]);
81         if (CONST_Tablespace_Aux_Data)
82             echo ' tablespace '.CONST_Tablespace_Aux_Data;
83         echo ' as select place_id as place_id,st_centroid(geometry) as centroid from placex where ';
84         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."'";
85         echo ";\n";
86
87         echo 'CREATE INDEX idx_place_classtype_'.pg_escape_string($aPair[0]).'_'.pg_escape_string($aPair[1]).'_centroid ';
88         echo 'ON place_classtype_'.pg_escape_string($aPair[0]).'_'.pg_escape_string($aPair[1]).' USING GIST (centroid)';
89         if (CONST_Tablespace_Aux_Index)
90             echo ' tablespace '.CONST_Tablespace_Aux_Index;
91         echo ";\n";
92
93         echo 'CREATE INDEX idx_place_classtype_'.pg_escape_string($aPair[0]).'_'.pg_escape_string($aPair[1]).'_place_id ';
94         echo 'ON place_classtype_'.pg_escape_string($aPair[0]).'_'.pg_escape_string($aPair[1]).' USING btree(place_id)';
95         if (CONST_Tablespace_Aux_Index)
96             echo ' tablespace '.CONST_Tablespace_Aux_Index;
97         echo ";\n";
98
99         echo 'GRANT SELECT ON place_classtype_'.pg_escape_string($aPair[0]).'_'.pg_escape_string($aPair[1]).' TO "'.CONST_Database_Web_User."\";\n";
100     }
101
102     echo 'drop index idx_placex_classtype;';
103 }