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