]> git.openstreetmap.org Git - nominatim.git/blob - utils/setup.php
edited indices an setup file to grant select for table country_osm_grid
[nominatim.git] / utils / setup.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
8 $aCMDOptions
9 = array(
10    'Create and setup nominatim search system',
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
15    array('osm-file', '', 0, 1, 1, 1, 'realpath', 'File to import'),
16    array('threads', '', 0, 1, 1, 1, 'int', 'Number of threads (where possible)'),
17
18    array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
19
20    array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
21    array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
22    array('import-data', '', 0, 1, 0, 0, 'bool', 'Import a osm file'),
23    array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
24    array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
25    array('enable-diff-updates', '', 0, 1, 0, 0, 'bool', 'Turn on the code required to make diff updates work'),
26    array('enable-debug-statements', '', 0, 1, 0, 0, 'bool', 'Include debug warning statements in pgsql commands'),
27    array('ignore-errors', '', 0, 1, 0, 0, 'bool', 'Continue import even when errors in SQL are present (EXPERT)'),
28    array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
29    array('create-partition-tables', '', 0, 1, 0, 0, 'bool', 'Create required partition tables'),
30    array('create-partition-functions', '', 0, 1, 0, 0, 'bool', 'Create required partition triggers'),
31    array('no-partitions', '', 0, 1, 0, 0, 'bool', 'Do not partition search indices (speeds up import of single country extracts)'),
32    array('import-wikipedia-articles', '', 0, 1, 0, 0, 'bool', 'Import wikipedia article dump'),
33    array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
34    array('disable-token-precalc', '', 0, 1, 0, 0, 'bool', 'Disable name precalculation (EXPERT)'),
35    array('import-tiger-data', '', 0, 1, 0, 0, 'bool', 'Import tiger data (not included in \'all\')'),
36    array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
37    array('osmosis-init', '', 0, 1, 0, 0, 'bool', 'Generate default osmosis configuration'),
38    array('index', '', 0, 1, 0, 0, 'bool', 'Index the data'),
39    array('index-noanalyse', '', 0, 1, 0, 0, 'bool', 'Do not perform analyse operations during index (EXPERT)'),
40    array('create-search-indices', '', 0, 1, 0, 0, 'bool', 'Create additional indices required for search and update'),
41    array('create-country-names', '', 0, 1, 0, 0, 'bool', 'Create default list of searchable country names'),
42    array('drop', '', 0, 1, 0, 0, 'bool', 'Drop tables needed for updates, making the database readonly (EXPERIMENTAL)'),
43   );
44 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
45
46 $bDidSomething = false;
47
48 // Check if osm-file is set and points to a valid file if --all or --import-data is given
49 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
50     if (!isset($aCMDResult['osm-file'])) {
51         fail('missing --osm-file for data import');
52     }
53
54     if (!file_exists($aCMDResult['osm-file'])) {
55         fail('the path supplied to --osm-file does not exist');
56     }
57
58     if (!is_readable($aCMDResult['osm-file'])) {
59         fail('osm-file "'.$aCMDResult['osm-file'].'" not readable');
60     }
61 }
62
63
64 // This is a pretty hard core default - the number of processors in the box - 1
65 $iInstances = isset($aCMDResult['threads'])?$aCMDResult['threads']:(getProcessorCount()-1);
66 if ($iInstances < 1) {
67     $iInstances = 1;
68     warn("resetting threads to $iInstances");
69 }
70 if ($iInstances > getProcessorCount()) {
71     $iInstances = getProcessorCount();
72     warn("resetting threads to $iInstances");
73 }
74
75 // Assume we can steal all the cache memory in the box (unless told otherwise)
76 if (isset($aCMDResult['osm2pgsql-cache'])) {
77     $iCacheMemory = $aCMDResult['osm2pgsql-cache'];
78 } else {
79     $iCacheMemory = getCacheMemoryMB();
80 }
81
82 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
83 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
84
85 if ($aCMDResult['create-db'] || $aCMDResult['all']) {
86     info('Create DB');
87     $bDidSomething = true;
88     $oDB = DB::connect(CONST_Database_DSN, false);
89     if (!PEAR::isError($oDB)) {
90         fail('database already exists ('.CONST_Database_DSN.')');
91     }
92     passthruCheckReturn('createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
93 }
94
95 if ($aCMDResult['setup-db'] || $aCMDResult['all']) {
96     info('Setup DB');
97     $bDidSomething = true;
98
99     $oDB =& getDB();
100
101     $fPostgresVersion = getPostgresVersion($oDB);
102     echo 'Postgres version found: '.$fPostgresVersion."\n";
103
104     if ($fPostgresVersion < 9.1) {
105         fail('Minimum supported version of Postgresql is 9.1.');
106     }
107
108     pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
109     pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
110
111     // For extratags and namedetails the hstore_to_json converter is
112     // needed which is only available from Postgresql 9.3+. For older
113     // versions add a dummy function that returns nothing.
114     $iNumFunc = chksql($oDB->getOne("select count(*) from pg_proc where proname = 'hstore_to_json'"));
115
116     if ($iNumFunc == 0) {
117         pgsqlRunScript("create function hstore_to_json(dummy hstore) returns text AS 'select null::text' language sql immutable");
118         warn('Postgresql is too old. extratags and namedetails API not available.');
119     }
120
121     $fPostgisVersion = getPostgisVersion($oDB);
122     echo 'Postgis version found: '.$fPostgisVersion."\n";
123
124     if ($fPostgisVersion < 2.1) {
125         // Functions were renamed in 2.1 and throw an annoying deprecation warning
126         pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
127         pgsqlRunScript('ALTER FUNCTION ST_Line_Locate_Point(geometry, geometry) RENAME TO ST_LineLocatePoint');
128     }
129     if ($fPostgisVersion < 2.2) {
130         pgsqlRunScript('ALTER FUNCTION ST_Distance_Spheroid(geometry, geometry, spheroid) RENAME TO ST_DistanceSpheroid');
131     }
132
133     $i = chksql($oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'"));
134     if ($i == 0) {
135         echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n";
136         echo "\n          createuser ".CONST_Database_Web_User."\n\n";
137         exit(1);
138     }
139
140     // Try accessing the C module, so we know early if something is wrong
141     // and can simply error out.
142     $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
143     $sSQL .= CONST_InstallPath."/module/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
144     $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
145     $oResult = $oDB->query($sSQL);
146
147     if (PEAR::isError($oResult)) {
148         echo "\nERROR: Failed to load nominatim module. Reason:\n";
149         echo $oResult->userinfo."\n\n";
150         exit(1);
151     }
152
153     if (!file_exists(CONST_ExtraDataPath.'/country_osm_grid.sql.gz')) {
154         echo 'Error: you need to download the country_osm_grid first:';
155         echo "\n    wget -O ".CONST_ExtraDataPath."/country_osm_grid.sql.gz https://www.nominatim.org/data/country_grid.sql.gz\n";
156         exit(1);
157     }
158
159     pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
160     pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
161     pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql.gz');
162     pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
163     if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz')) {
164         pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz');
165     } else {
166         warn('external UK postcode table not found.');
167     }
168     if (CONST_Use_Extra_US_Postcodes) {
169         pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
170     }
171
172     if ($aCMDResult['no-partitions']) {
173         pgsqlRunScript('update country_name set partition = 0');
174     }
175
176     // the following will be needed by create_functions later but
177     // is only defined in the subsequently called create_tables.
178     // Create dummies here that will be overwritten by the proper
179     // versions in create-tables.
180     pgsqlRunScript('CREATE TABLE place_boundingbox ()');
181     pgsqlRunScript('create type wikipedia_article_match as ()');
182 }
183
184 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
185     info('Import data');
186     $bDidSomething = true;
187
188     $osm2pgsql = CONST_Osm2pgsql_Binary;
189     if (!file_exists($osm2pgsql)) {
190         echo "Check CONST_Osm2pgsql_Binary in your local settings file.\n";
191         echo "Normally you should not need to set this manually.\n";
192         fail("osm2pgsql not found in '$osm2pgsql'");
193     }
194
195     if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
196         $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
197     }
198     if (CONST_Tablespace_Osm2pgsql_Data)
199         $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
200     if (CONST_Tablespace_Osm2pgsql_Index)
201         $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
202     if (CONST_Tablespace_Place_Data)
203         $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
204     if (CONST_Tablespace_Place_Index)
205         $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
206     $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
207     $osm2pgsql .= ' -C '.$iCacheMemory;
208     $osm2pgsql .= ' -P '.$aDSNInfo['port'];
209     $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
210     passthruCheckReturn($osm2pgsql);
211
212     $oDB =& getDB();
213     if (!$aCMDResult['ignore-errors'] && !chksql($oDB->getRow('select * from place limit 1'))) {
214         fail('No Data');
215     }
216 }
217
218 if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
219     info('Create Functions');
220     $bDidSomething = true;
221     if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) {
222         fail('nominatim module not built');
223     }
224     create_sql_functions($aCMDResult);
225 }
226
227 if ($aCMDResult['create-tables'] || $aCMDResult['all']) {
228     info('Create Tables');
229     $bDidSomething = true;
230
231     $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
232     $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
233     $sTemplate = replace_tablespace(
234         '{ts:address-data}',
235         CONST_Tablespace_Address_Data,
236         $sTemplate
237     );
238     $sTemplate = replace_tablespace(
239         '{ts:address-index}',
240         CONST_Tablespace_Address_Index,
241         $sTemplate
242     );
243     $sTemplate = replace_tablespace(
244         '{ts:search-data}',
245         CONST_Tablespace_Search_Data,
246         $sTemplate
247     );
248     $sTemplate = replace_tablespace(
249         '{ts:search-index}',
250         CONST_Tablespace_Search_Index,
251         $sTemplate
252     );
253     $sTemplate = replace_tablespace(
254         '{ts:aux-data}',
255         CONST_Tablespace_Aux_Data,
256         $sTemplate
257     );
258     $sTemplate = replace_tablespace(
259         '{ts:aux-index}',
260         CONST_Tablespace_Aux_Index,
261         $sTemplate
262     );
263     pgsqlRunScript($sTemplate, false);
264
265     // re-run the functions
266     info('Recreate Functions');
267     create_sql_functions($aCMDResult);
268 }
269
270 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all']) {
271     info('Create Partition Tables');
272     $bDidSomething = true;
273
274     $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
275     $sTemplate = replace_tablespace(
276         '{ts:address-data}',
277         CONST_Tablespace_Address_Data,
278         $sTemplate
279     );
280     $sTemplate = replace_tablespace(
281         '{ts:address-index}',
282         CONST_Tablespace_Address_Index,
283         $sTemplate
284     );
285     $sTemplate = replace_tablespace(
286         '{ts:search-data}',
287         CONST_Tablespace_Search_Data,
288         $sTemplate
289     );
290     $sTemplate = replace_tablespace(
291         '{ts:search-index}',
292         CONST_Tablespace_Search_Index,
293         $sTemplate
294     );
295     $sTemplate = replace_tablespace(
296         '{ts:aux-data}',
297         CONST_Tablespace_Aux_Data,
298         $sTemplate
299     );
300     $sTemplate = replace_tablespace(
301         '{ts:aux-index}',
302         CONST_Tablespace_Aux_Index,
303         $sTemplate
304     );
305
306     pgsqlRunPartitionScript($sTemplate);
307 }
308
309
310 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all']) {
311     info('Create Partition Functions');
312     $bDidSomething = true;
313
314     $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
315
316     pgsqlRunPartitionScript($sTemplate);
317 }
318
319 if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all']) {
320     $bDidSomething = true;
321     $sWikiArticlesFile = CONST_Wikipedia_Data_Path.'/wikipedia_article.sql.bin';
322     $sWikiRedirectsFile = CONST_Wikipedia_Data_Path.'/wikipedia_redirect.sql.bin';
323     if (file_exists($sWikiArticlesFile)) {
324         info('Importing wikipedia articles');
325         pgsqlRunDropAndRestore($sWikiArticlesFile);
326     } else {
327         warn('wikipedia article dump file not found - places will have default importance');
328     }
329     if (file_exists($sWikiRedirectsFile)) {
330         info('Importing wikipedia redirects');
331         pgsqlRunDropAndRestore($sWikiRedirectsFile);
332     } else {
333         warn('wikipedia redirect dump file not found - some place importance values may be missing');
334     }
335 }
336
337
338 if ($aCMDResult['load-data'] || $aCMDResult['all']) {
339     info('Drop old Data');
340     $bDidSomething = true;
341
342     $oDB =& getDB();
343     if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
344     echo '.';
345     if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
346     echo '.';
347     if (!pg_query($oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($oDB->connection));
348     echo '.';
349     if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
350     echo '.';
351     if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
352     echo '.';
353     if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
354     echo '.';
355     if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
356     echo '.';
357     if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
358     echo '.';
359     if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
360     echo '.';
361     if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
362     echo '.';
363
364     $sSQL = 'select distinct partition from country_name';
365     $aPartitions = chksql($oDB->getCol($sSQL));
366     if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
367     foreach ($aPartitions as $sPartition) {
368         if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection));
369         echo '.';
370     }
371
372     // used by getorcreate_word_id to ignore frequent partial words
373     $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
374     $sSQL .= '$$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
375     if (!pg_query($oDB->connection, $sSQL)) {
376         fail(pg_last_error($oDB->connection));
377     }
378     echo ".\n";
379
380     // pre-create the word list
381     if (!$aCMDResult['disable-token-precalc']) {
382         info('Loading word list');
383         pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
384     }
385
386     info('Load Data');
387     $sColumns = 'osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry';
388
389     $aDBInstances = array();
390     $iLoadThreads = max(1, $iInstances - 1);
391     for ($i = 0; $i < $iLoadThreads; $i++) {
392         $aDBInstances[$i] =& getDB(true);
393         $sSQL = "INSERT INTO placex ($sColumns) SELECT $sColumns FROM place WHERE osm_id % $iLoadThreads = $i";
394         $sSQL .= " and not (class='place' and type='houses' and osm_type='W'";
395         $sSQL .= "          and ST_GeometryType(geometry) = 'ST_LineString')";
396         $sSQL .= ' and ST_IsValid(geometry)';
397         if ($aCMDResult['verbose']) echo "$sSQL\n";
398         if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) {
399             fail(pg_last_error($aDBInstances[$i]->connection));
400         }
401     }
402     // last thread for interpolation lines
403     $aDBInstances[$iLoadThreads] =& getDB(true);
404     $sSQL = 'insert into location_property_osmline';
405     $sSQL .= ' (osm_id, address, linegeo)';
406     $sSQL .= ' SELECT osm_id, address, geometry from place where ';
407     $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
408     if ($aCMDResult['verbose']) echo "$sSQL\n";
409     if (!pg_send_query($aDBInstances[$iLoadThreads]->connection, $sSQL)) {
410         fail(pg_last_error($aDBInstances[$iLoadThreads]->connection));
411     }
412
413     $bAnyBusy = true;
414     while ($bAnyBusy) {
415         $bAnyBusy = false;
416         for ($i = 0; $i <= $iLoadThreads; $i++) {
417             if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
418         }
419         sleep(1);
420         echo '.';
421     }
422     echo "\n";
423     info('Reanalysing database');
424     pgsqlRunScript('ANALYSE');
425
426     $sDatabaseDate = getDatabaseDate($oDB);
427     pg_query($oDB->connection, 'TRUNCATE import_status');
428     if ($sDatabaseDate === false) {
429         warn('could not determine database date.');
430     } else {
431         $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
432         pg_query($oDB->connection, $sSQL);
433         echo "Latest data imported from $sDatabaseDate.\n";
434     }
435 }
436
437 if ($aCMDResult['import-tiger-data']) {
438     info('Import Tiger data');
439     $bDidSomething = true;
440
441     $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
442     $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
443     $sTemplate = replace_tablespace(
444         '{ts:aux-data}',
445         CONST_Tablespace_Aux_Data,
446         $sTemplate
447     );
448     $sTemplate = replace_tablespace(
449         '{ts:aux-index}',
450         CONST_Tablespace_Aux_Index,
451         $sTemplate
452     );
453     pgsqlRunScript($sTemplate, false);
454
455     $aDBInstances = array();
456     for ($i = 0; $i < $iInstances; $i++) {
457         $aDBInstances[$i] =& getDB(true);
458     }
459
460     foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
461         echo $sFile.': ';
462         $hFile = fopen($sFile, 'r');
463         $sSQL = fgets($hFile, 100000);
464         $iLines = 0;
465
466         while (true) {
467             for ($i = 0; $i < $iInstances; $i++) {
468                 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
469                     while (pg_get_result($aDBInstances[$i]->connection));
470                     $sSQL = fgets($hFile, 100000);
471                     if (!$sSQL) break 2;
472                     if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
473                     $iLines++;
474                     if ($iLines == 1000) {
475                         echo '.';
476                         $iLines = 0;
477                     }
478                 }
479             }
480             usleep(10);
481         }
482
483         fclose($hFile);
484
485         $bAnyBusy = true;
486         while ($bAnyBusy) {
487             $bAnyBusy = false;
488             for ($i = 0; $i < $iInstances; $i++) {
489                 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
490             }
491             usleep(10);
492         }
493         echo "\n";
494     }
495
496     info('Creating indexes on Tiger data');
497     $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
498     $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
499     $sTemplate = replace_tablespace(
500         '{ts:aux-data}',
501         CONST_Tablespace_Aux_Data,
502         $sTemplate
503     );
504     $sTemplate = replace_tablespace(
505         '{ts:aux-index}',
506         CONST_Tablespace_Aux_Index,
507         $sTemplate
508     );
509     pgsqlRunScript($sTemplate, false);
510 }
511
512 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) {
513     info('Calculate Postcodes');
514     $bDidSomething = true;
515     $oDB =& getDB();
516     if (!pg_query($oDB->connection, 'TRUNCATE location_postcode')) {
517         fail(pg_last_error($oDB->connection));
518     }
519
520     $sSQL  = 'INSERT INTO location_postcode';
521     $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
522     $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
523     $sSQL .= "       upper(trim (both ' ' from address->'postcode')) as pc,";
524     $sSQL .= '       ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
525     $sSQL .= '  FROM placex';
526     $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
527     $sSQL .= '       AND geometry IS NOT null';
528     $sSQL .= ' GROUP BY country_code, pc';
529
530     if (!pg_query($oDB->connection, $sSQL)) {
531         fail(pg_last_error($oDB->connection));
532     }
533
534     if (CONST_Use_Extra_US_Postcodes) {
535         // only add postcodes that are not yet available in OSM
536         $sSQL  = 'INSERT INTO location_postcode';
537         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
538         $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
539         $sSQL .= '       ST_SetSRID(ST_Point(x,y),4326)';
540         $sSQL .= '  FROM us_postcode WHERE postcode NOT IN';
541         $sSQL .= '        (SELECT postcode FROM location_postcode';
542         $sSQL .= "          WHERE country_code = 'us')";
543         if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
544     }
545
546     // add missing postcodes for GB (if available)
547     $sSQL  = 'INSERT INTO location_postcode';
548     $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
549     $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
550     $sSQL .= '  FROM gb_postcode WHERE postcode NOT IN';
551     $sSQL .= '           (SELECT postcode FROM location_postcode';
552     $sSQL .= "             WHERE country_code = 'gb')";
553     if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
554
555     if (!$aCMDResult['all']) {
556         $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
557         $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
558         if (!pg_query($oDB->connection, $sSQL)) {
559             fail(pg_last_error($oDB->connection));
560         }
561     }
562     $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
563     $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
564
565     if (!pg_query($oDB->connection, $sSQL)) {
566         fail(pg_last_error($oDB->connection));
567     }
568 }
569
570 if ($aCMDResult['osmosis-init']) {
571     $bDidSomething = true;
572     echo "Command 'osmosis-init' no longer available, please use utils/update.php --init-updates.\n";
573 }
574
575 if ($aCMDResult['index'] || $aCMDResult['all']) {
576     $bDidSomething = true;
577     $sOutputFile = '';
578     $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
579     info('Index ranks 0 - 4');
580     passthruCheckReturn($sBaseCmd.' -R 4');
581     if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
582     info('Index ranks 5 - 25');
583     passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
584     if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
585     info('Index ranks 26 - 30');
586     passthruCheckReturn($sBaseCmd.' -r 26');
587
588     info('Index postcodes');
589     $oDB =& getDB();
590     $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
591     if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
592 }
593
594 if ($aCMDResult['create-search-indices'] || $aCMDResult['all']) {
595     info('Create Search indices');
596     $bDidSomething = true;
597
598     $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
599     $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
600     $sTemplate = replace_tablespace(
601         '{ts:address-index}',
602         CONST_Tablespace_Address_Index,
603         $sTemplate
604     );
605     $sTemplate = replace_tablespace(
606         '{ts:search-index}',
607         CONST_Tablespace_Search_Index,
608         $sTemplate
609     );
610     $sTemplate = replace_tablespace(
611         '{ts:aux-index}',
612         CONST_Tablespace_Aux_Index,
613         $sTemplate
614     );
615
616     pgsqlRunScript($sTemplate);
617 }
618
619 if ($aCMDResult['create-country-names'] || $aCMDResult['all']) {
620     info('Create search index for default country names');
621     $bDidSomething = true;
622
623     pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
624     pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
625     pgsqlRunScript('select count(*) from (select getorcreate_country(make_standard_name(country_code), country_code) from country_name where country_code is not null) as x');
626     pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
627
628     $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v), country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
629     if (CONST_Languages) {
630         $sSQL .= 'in ';
631         $sDelim = '(';
632         foreach (explode(',', CONST_Languages) as $sLang) {
633             $sSQL .= $sDelim."'name:$sLang'";
634             $sDelim = ',';
635         }
636         $sSQL .= ')';
637     } else {
638         // all include all simple name tags
639         $sSQL .= "like 'name:%'";
640     }
641     $sSQL .= ') v';
642     pgsqlRunScript($sSQL);
643 }
644
645 if ($aCMDResult['drop']) {
646     info('Drop tables only required for updates');
647     // The implementation is potentially a bit dangerous because it uses
648     // a positive selection of tables to keep, and deletes everything else.
649     // Including any tables that the unsuspecting user might have manually
650     // created. USE AT YOUR OWN PERIL.
651     $bDidSomething = true;
652
653     // tables we want to keep. everything else goes.
654     $aKeepTables = array(
655                     '*columns',
656                     'import_polygon_*',
657                     'import_status',
658                     'place_addressline',
659                     'location_postcode',
660                     'location_property*',
661                     'placex',
662                     'search_name',
663                     'seq_*',
664                     'word',
665                     'query_log',
666                     'new_query_log',
667                     'spatial_ref_sys',
668                     'country_name',
669                     'place_classtype_*'
670                    );
671
672     $oDB =& getDB();
673     $aDropTables = array();
674     $aHaveTables = chksql($oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
675
676     foreach ($aHaveTables as $sTable) {
677         $bFound = false;
678         foreach ($aKeepTables as $sKeep) {
679             if (fnmatch($sKeep, $sTable)) {
680                 $bFound = true;
681                 break;
682             }
683         }
684         if (!$bFound) array_push($aDropTables, $sTable);
685     }
686
687     foreach ($aDropTables as $sDrop) {
688         if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
689         @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
690         // ignore warnings/errors as they might be caused by a table having
691         // been deleted already by CASCADE
692     }
693
694     if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
695         if ($aCMDResult['verbose']) echo 'deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
696         unlink(CONST_Osm2pgsql_Flatnode_File);
697     }
698 }
699
700 if (!$bDidSomething) {
701     showUsage($aCMDOptions, true);
702 } else {
703     echo "Summary of warnings:\n\n";
704     repeatWarnings();
705     echo "\n";
706     info('Setup finished.');
707 }
708
709
710 function pgsqlRunScriptFile($sFilename)
711 {
712     global $aCMDResult;
713     if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
714
715     // Convert database DSN to psql parameters
716     $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
717     if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
718     $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
719     if (!$aCMDResult['verbose']) {
720         $sCMD .= ' -q';
721     }
722
723     $ahGzipPipes = null;
724     if (preg_match('/\\.gz$/', $sFilename)) {
725         $aDescriptors = array(
726                          0 => array('pipe', 'r'),
727                          1 => array('pipe', 'w'),
728                          2 => array('file', '/dev/null', 'a')
729                         );
730         $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
731         if (!is_resource($hGzipProcess)) fail('unable to start zcat');
732         $aReadPipe = $ahGzipPipes[1];
733         fclose($ahGzipPipes[0]);
734     } else {
735         $sCMD .= ' -f '.$sFilename;
736         $aReadPipe = array('pipe', 'r');
737     }
738
739     $aDescriptors = array(
740                      0 => $aReadPipe,
741                      1 => array('pipe', 'w'),
742                      2 => array('file', '/dev/null', 'a')
743                     );
744     $ahPipes = null;
745     $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
746     if (!is_resource($hProcess)) fail('unable to start pgsql');
747
748
749     // TODO: error checking
750     while (!feof($ahPipes[1])) {
751         echo fread($ahPipes[1], 4096);
752     }
753     fclose($ahPipes[1]);
754
755     $iReturn = proc_close($hProcess);
756     if ($iReturn > 0) {
757         fail("pgsql returned with error code ($iReturn)");
758     }
759     if ($ahGzipPipes) {
760         fclose($ahGzipPipes[1]);
761         proc_close($hGzipProcess);
762     }
763 }
764
765 function pgsqlRunScript($sScript, $bfatal = true)
766 {
767     global $aCMDResult;
768     runSQLScript(
769         $sScript,
770         $bfatal,
771         $aCMDResult['verbose'],
772         $aCMDResult['ignore-errors']
773     );
774 }
775
776 function pgsqlRunPartitionScript($sTemplate)
777 {
778     global $aCMDResult;
779     $oDB =& getDB();
780
781     $sSQL = 'select distinct partition from country_name';
782     $aPartitions = chksql($oDB->getCol($sSQL));
783     if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
784
785     preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
786     foreach ($aMatches as $aMatch) {
787         $sResult = '';
788         foreach ($aPartitions as $sPartitionName) {
789             $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
790         }
791         $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
792     }
793
794     pgsqlRunScript($sTemplate);
795 }
796
797 function pgsqlRunRestoreData($sDumpFile)
798 {
799     // Convert database DSN to psql parameters
800     $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
801     if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
802     $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
803
804     $aDescriptors = array(
805                      0 => array('pipe', 'r'),
806                      1 => array('pipe', 'w'),
807                      2 => array('file', '/dev/null', 'a')
808                     );
809     $ahPipes = null;
810     $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
811     if (!is_resource($hProcess)) fail('unable to start pg_restore');
812
813     fclose($ahPipes[0]);
814
815     // TODO: error checking
816     while (!feof($ahPipes[1])) {
817         echo fread($ahPipes[1], 4096);
818     }
819     fclose($ahPipes[1]);
820
821     $iReturn = proc_close($hProcess);
822 }
823
824 function pgsqlRunDropAndRestore($sDumpFile)
825 {
826     // Convert database DSN to psql parameters
827     $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
828     if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
829     $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
830
831     $aDescriptors = array(
832                      0 => array('pipe', 'r'),
833                      1 => array('pipe', 'w'),
834                      2 => array('file', '/dev/null', 'a')
835                     );
836     $ahPipes = null;
837     $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
838     if (!is_resource($hProcess)) fail('unable to start pg_restore');
839
840     fclose($ahPipes[0]);
841
842     // TODO: error checking
843     while (!feof($ahPipes[1])) {
844         echo fread($ahPipes[1], 4096);
845     }
846     fclose($ahPipes[1]);
847
848     $iReturn = proc_close($hProcess);
849 }
850
851 function passthruCheckReturn($cmd)
852 {
853     $result = -1;
854     passthru($cmd, $result);
855     if ($result != 0) fail('Error executing external command: '.$cmd);
856 }
857
858 function replace_tablespace($sTemplate, $sTablespace, $sSql)
859 {
860     if ($sTablespace) {
861         $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
862     } else {
863         $sSql = str_replace($sTemplate, '', $sSql);
864     }
865
866     return $sSql;
867 }
868
869 function create_sql_functions($aCMDResult)
870 {
871     $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
872     $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
873     if ($aCMDResult['enable-diff-updates']) {
874         $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
875     }
876     if ($aCMDResult['enable-debug-statements']) {
877         $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
878     }
879     if (CONST_Limit_Reindexing) {
880         $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
881     }
882     if (!CONST_Use_US_Tiger_Data) {
883         $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
884     }
885     if (!CONST_Use_Aux_Location_data) {
886         $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
887     }
888     pgsqlRunScript($sTemplate);
889 }