]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
fix array-related errors according to PSR2 coding style guide
[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('countries', '', 0, 1, 0, 0, 'bool', 'Create import script for country codes and names'),
16    array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create import script for search phrases '),
17   );
18 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
19
20 include(CONST_InstallPath.'/settings/phrase_settings.php');
21
22
23 if ($aCMDResult['countries']) {
24     echo "select getorcreate_country(make_standard_name('uk'), 'gb');\n";
25     echo "select getorcreate_country(make_standard_name('united states'), 'us');\n";
26     echo "select count(*) from (select getorcreate_country(make_standard_name(country_code), country_code) from country_name where country_code is not null) as x;\n";
27
28     echo "select count(*) from (select getorcreate_country(make_standard_name(get_name_by_language(country_name.name,ARRAY['name'])), country_code) from country_name where get_name_by_language(country_name.name, ARRAY['name']) is not null) as x;\n";
29     foreach ($aLanguageIn as $sLanguage) {
30         echo "select count(*) from (select getorcreate_country(make_standard_name(get_name_by_language(country_name.name,ARRAY['name:".$sLanguage."'])), country_code) from country_name where get_name_by_language(country_name.name, ARRAY['name:".$sLanguage."']) is not null) as x;\n";
31     }
32 }
33
34 if ($aCMDResult['wiki-import']) {
35     $aPairs = array();
36
37     foreach ($aLanguageIn as $sLanguage) {
38         $sURL = 'http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
39         $sWikiPageXML = file_get_contents($sURL);
40         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER)) {
41             foreach ($aMatches as $aMatch) {
42                 $sLabel = trim($aMatch[1]);
43                 $sClass = trim($aMatch[2]);
44                 $sType = trim($aMatch[3]);
45                 # hack around a bug where building=yes was imported with
46                 # quotes into the wiki
47                 $sType = preg_replace('/&quot;/', '', $sType);
48                 # sanity check, in case somebody added garbage in the wiki
49                 if (preg_match('/^\\w+$/', $sClass) < 1 ||
50                     preg_match('/^\\w+$/', $sType) < 1) {
51                     trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
52                     exit;
53                 }
54                 # blacklisting: disallow certain class/type combinations
55                 if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
56                     # fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
57                     continue;
58                 }
59                 # whitelisting: if class is in whitelist, allow only tags in the list
60                 if (isset($aTagsWhitelist[$sClass]) && !in_array($sType, $aTagsWhitelist[$sClass])) {
61                     # fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
62                     continue;
63                 }
64                 $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
65
66                 switch (trim($aMatch[4])) {
67                     case 'near':
68                         echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'near');\n";
69                         break;
70                     case 'in':
71                         echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'in');\n";
72                         break;
73                     default:
74                         echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType');\n";
75                         break;
76                 }
77             }
78         }
79     }
80
81     echo "create index idx_placex_classtype on placex (class, type);";
82
83     foreach ($aPairs as $aPair) {
84         echo "create table place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1]);
85         if (CONST_Tablespace_Aux_Data)
86             echo " tablespace ".CONST_Tablespace_Aux_Data;
87         echo " as select place_id as place_id,st_centroid(geometry) as centroid from placex where ";
88         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."'";
89         echo ";\n";
90
91         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_centroid ";
92         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING GIST (centroid)";
93         if (CONST_Tablespace_Aux_Index)
94             echo " tablespace ".CONST_Tablespace_Aux_Index;
95         echo ";\n";
96
97         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_place_id ";
98         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING btree(place_id)";
99         if (CONST_Tablespace_Aux_Index)
100             echo " tablespace ".CONST_Tablespace_Aux_Index;
101         echo ";\n";
102
103         echo "GRANT SELECT ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1]).' TO "'.CONST_Database_Web_User."\";\n";
104     }
105
106     echo "drop index idx_placex_classtype;";
107 }