]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
add normalized version of special search terms on import
[nominatim.git] / utils / specialphrases.php
1 #!/usr/bin/php -Cq
2 <?php
3
4 require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5 require_once(CONST_BasePath.'/lib/init-cmd.php');
6 ini_set('memory_limit', '800M');
7 ini_set('display_errors', 'stderr');
8
9 $aCMDOptions
10 = array(
11    "Import and export special phrases",
12    array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
13    array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
14    array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
15    array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create import script for search phrases '),
16   );
17 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
18
19 include(CONST_InstallPath.'/settings/phrase_settings.php');
20
21 if ($aCMDResult['wiki-import']) {
22     $oNormalizer = Transliterator::createFromRules(":: NFD (); [:Nonspacing Mark:] >;  :: lower (); [[:Punctuation:][:Space:]]+ > ' '; :: NFC ();");
23     $aPairs = array();
24
25     $sLanguageIn = CONST_Languages ? CONST_Languages :
26         ('af,ar,br,ca,cs,de,en,es,et,eu,fa,fi,fr,gl,hr,hu,'.
27          'ia,is,it,ja,mk,nl,no,pl,ps,pt,ru,sk,sl,sv,uk,vi');
28
29     foreach (explode(',', $sLanguageIn) as $sLanguage) {
30         $sURL = 'http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
31         $sWikiPageXML = file_get_contents($sURL);
32         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER)) {
33             foreach ($aMatches as $aMatch) {
34                 $sLabel = trim($aMatch[1]);
35                 $sTrans = pg_escape_string($oNormalizer->transliterate($sLabel));
36                 $sClass = trim($aMatch[2]);
37                 $sType = trim($aMatch[3]);
38                 // hack around a bug where building=yes was imported with
39                 // quotes into the wiki
40                 $sType = preg_replace('/&quot;/', '', $sType);
41                 // sanity check, in case somebody added garbage in the wiki
42                 if (preg_match('/^\\w+$/', $sClass) < 1
43                     || preg_match('/^\\w+$/', $sType) < 1
44                 ) {
45                     trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
46                     exit;
47                 }
48                 // blacklisting: disallow certain class/type combinations
49                 if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
50                     // fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
51                     continue;
52                 }
53                 // whitelisting: if class is in whitelist, allow only tags in the list
54                 if (isset($aTagsWhitelist[$sClass]) && !in_array($sType, $aTagsWhitelist[$sClass])) {
55                     // fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
56                     continue;
57                 }
58                 $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
59
60                 switch (trim($aMatch[4])) {
61                     case 'near':
62                         echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sTrans', '$sClass', '$sType', 'near');\n";
63                         break;
64                     case 'in':
65                         echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sTrans', '$sClass', '$sType', 'in');\n";
66                         break;
67                     default:
68                         echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sTrans', '$sClass', '$sType');\n";
69                         break;
70                 }
71             }
72         }
73     }
74
75     echo "create index idx_placex_classtype on placex (class, type);";
76
77     foreach ($aPairs as $aPair) {
78         echo "create table place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1]);
79         if (CONST_Tablespace_Aux_Data)
80             echo " tablespace ".CONST_Tablespace_Aux_Data;
81         echo " as select place_id as place_id,st_centroid(geometry) as centroid from placex where ";
82         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."'";
83         echo ";\n";
84
85         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_centroid ";
86         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING GIST (centroid)";
87         if (CONST_Tablespace_Aux_Index)
88             echo " tablespace ".CONST_Tablespace_Aux_Index;
89         echo ";\n";
90
91         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_place_id ";
92         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING btree(place_id)";
93         if (CONST_Tablespace_Aux_Index)
94             echo " tablespace ".CONST_Tablespace_Aux_Index;
95         echo ";\n";
96
97         echo "GRANT SELECT ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1]).' TO "'.CONST_Database_Web_User."\";\n";
98     }
99
100     echo "drop index idx_placex_classtype;";
101 }