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