4 require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5 require_once(CONST_BasePath.'/lib/init-cmd.php');
6 ini_set('memory_limit', '800M');
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'),
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)'),
18 array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
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)'),
44 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
46 $bDidSomething = false;
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');
54 if (!file_exists($aCMDResult['osm-file'])) {
55 fail('the path supplied to --osm-file does not exist');
58 if (!is_readable($aCMDResult['osm-file'])) {
59 fail('osm-file "'.$aCMDResult['osm-file'].'" not readable');
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) {
68 echo "WARNING: resetting threads to $iInstances\n";
70 if ($iInstances > getProcessorCount()) {
71 $iInstances = getProcessorCount();
72 echo "WARNING: resetting threads to $iInstances\n";
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'];
79 $iCacheMemory = getCacheMemoryMB();
82 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
83 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
85 if ($aCMDResult['create-db'] || $aCMDResult['all']) {
87 $bDidSomething = true;
88 $oDB = DB::connect(CONST_Database_DSN, false);
89 if (!PEAR::isError($oDB)) {
90 fail('database already exists ('.CONST_Database_DSN.')');
92 passthruCheckReturn('createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
95 if ($aCMDResult['setup-db'] || $aCMDResult['all']) {
97 $bDidSomething = true;
99 // TODO: path detection, detection memory, etc.
103 $fPostgresVersion = getPostgresVersion($oDB);
104 echo 'Postgres version found: '.$fPostgresVersion."\n";
106 if ($fPostgresVersion < 9.1) {
107 fail("Minimum supported version of Postgresql is 9.1.");
110 pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
111 pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
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'"));
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.";
123 $fPostgisVersion = getPostgisVersion($oDB);
124 echo 'Postgis version found: '.$fPostgisVersion."\n";
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, double precision) RENAME TO ST_LineLocatePoint');
131 if ($fPostgisVersion < 2.2) {
132 pgsqlRunScript('ALTER FUNCTION ST_Distance_Spheroid(geometry, double precision) RENAME TO ST_DistanceSpheroid');
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');
142 echo "WARNING: external UK postcode table not found.\n";
144 if (CONST_Use_Extra_US_Postcodes) {
145 pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
148 if ($aCMDResult['no-partitions']) {
149 pgsqlRunScript('update country_name set partition = 0');
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 ()');
160 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
162 $bDidSomething = true;
164 $osm2pgsql = CONST_Osm2pgsql_Binary;
165 if (!file_exists($osm2pgsql)) {
166 echo "Please download and build osm2pgsql.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
167 fail("osm2pgsql not found in '$osm2pgsql'");
170 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
171 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
173 if (CONST_Tablespace_Osm2pgsql_Data)
174 $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
175 if (CONST_Tablespace_Osm2pgsql_Index)
176 $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
177 if (CONST_Tablespace_Place_Data)
178 $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
179 if (CONST_Tablespace_Place_Index)
180 $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
181 $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
182 $osm2pgsql .= ' -C '.$iCacheMemory;
183 $osm2pgsql .= ' -P '.$aDSNInfo['port'];
184 $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
185 passthruCheckReturn($osm2pgsql);
188 if (!chksql($oDB->getRow('select * from place limit 1'))) {
193 if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
195 $bDidSomething = true;
196 if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) fail("nominatim module not built");
197 create_sql_functions($aCMDResult);
200 if ($aCMDResult['create-tables'] || $aCMDResult['all']) {
201 $bDidSomething = true;
204 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
205 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
206 $sTemplate = replace_tablespace(
208 CONST_Tablespace_Address_Data,
211 $sTemplate = replace_tablespace(
212 '{ts:address-index}',
213 CONST_Tablespace_Address_Index,
216 $sTemplate = replace_tablespace(
218 CONST_Tablespace_Search_Data,
221 $sTemplate = replace_tablespace(
223 CONST_Tablespace_Search_Index,
226 $sTemplate = replace_tablespace(
228 CONST_Tablespace_Aux_Data,
231 $sTemplate = replace_tablespace(
233 CONST_Tablespace_Aux_Index,
236 pgsqlRunScript($sTemplate, false);
238 // re-run the functions
240 create_sql_functions($aCMDResult);
243 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all']) {
244 echo "Partition Tables\n";
245 $bDidSomething = true;
247 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
248 $sTemplate = replace_tablespace(
250 CONST_Tablespace_Address_Data,
253 $sTemplate = replace_tablespace(
254 '{ts:address-index}',
255 CONST_Tablespace_Address_Index,
258 $sTemplate = replace_tablespace(
260 CONST_Tablespace_Search_Data,
263 $sTemplate = replace_tablespace(
265 CONST_Tablespace_Search_Index,
268 $sTemplate = replace_tablespace(
270 CONST_Tablespace_Aux_Data,
273 $sTemplate = replace_tablespace(
275 CONST_Tablespace_Aux_Index,
279 pgsqlRunPartitionScript($sTemplate);
283 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all']) {
284 echo "Partition Functions\n";
285 $bDidSomething = true;
287 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
289 pgsqlRunPartitionScript($sTemplate);
292 if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all']) {
293 $bDidSomething = true;
294 $sWikiArticlesFile = CONST_BasePath.'/data/wikipedia_article.sql.bin';
295 $sWikiRedirectsFile = CONST_BasePath.'/data/wikipedia_redirect.sql.bin';
296 if (file_exists($sWikiArticlesFile)) {
297 echo "Importing wikipedia articles...";
298 pgsqlRunDropAndRestore($sWikiArticlesFile);
301 echo "WARNING: wikipedia article dump file not found - places will have default importance\n";
303 if (file_exists($sWikiRedirectsFile)) {
304 echo "Importing wikipedia redirects...";
305 pgsqlRunDropAndRestore($sWikiRedirectsFile);
308 echo "WARNING: wikipedia redirect dump file not found - some place importance values may be missing\n";
313 if ($aCMDResult['load-data'] || $aCMDResult['all']) {
314 echo "Drop old Data\n";
315 $bDidSomething = true;
318 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
320 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
322 if (!pg_query($oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($oDB->connection));
324 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
326 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
328 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
330 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
332 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
334 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
336 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
339 $sSQL = 'select distinct partition from country_name';
340 $aPartitions = chksql($oDB->getCol($sSQL));
341 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
342 foreach ($aPartitions as $sPartition) {
343 if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection));
347 // used by getorcreate_word_id to ignore frequent partial words
348 if (!pg_query($oDB->connection, 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS $$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE')) fail(pg_last_error($oDB->connection));
351 // pre-create the word list
352 if (!$aCMDResult['disable-token-precalc']) {
353 echo "Loading word list\n";
354 pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
358 $aDBInstances = array();
359 $iLoadThreads = max(1, $iInstances - 1);
360 for ($i = 0; $i < $iLoadThreads; $i++) {
361 $aDBInstances[$i] =& getDB(true);
362 $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
363 $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
364 $sSQL .= 'geometry) select * from place where osm_id % '.$iLoadThreads.' = '.$i;
365 $sSQL .= " and not (class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString')";
366 if ($aCMDResult['verbose']) echo "$sSQL\n";
367 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
369 // last thread for interpolation lines
370 $aDBInstances[$iLoadThreads] =& getDB(true);
371 $sSQL = 'select insert_osmline (osm_id, housenumber, street, addr_place, postcode, country_code, ';
372 $sSQL .= 'geometry) from place where ';
373 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
374 if ($aCMDResult['verbose']) echo "$sSQL\n";
375 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
380 for ($i = 0; $i <= $iLoadThreads; $i++) {
381 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
387 echo "Reanalysing database...\n";
388 pgsqlRunScript('ANALYSE');
391 if ($aCMDResult['import-tiger-data']) {
392 $bDidSomething = true;
394 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
395 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
396 $sTemplate = replace_tablespace(
398 CONST_Tablespace_Aux_Data,
401 $sTemplate = replace_tablespace(
403 CONST_Tablespace_Aux_Index,
406 pgsqlRunScript($sTemplate, false);
408 $aDBInstances = array();
409 for ($i = 0; $i < $iInstances; $i++) {
410 $aDBInstances[$i] =& getDB(true);
413 foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
415 $hFile = fopen($sFile, "r");
416 $sSQL = fgets($hFile, 100000);
420 for ($i = 0; $i < $iInstances; $i++) {
421 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
422 while (pg_get_result($aDBInstances[$i]->connection));
423 $sSQL = fgets($hFile, 100000);
425 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
427 if ($iLines == 1000) {
441 for ($i = 0; $i < $iInstances; $i++) {
442 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
449 echo "Creating indexes\n";
450 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
451 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
452 $sTemplate = replace_tablespace(
454 CONST_Tablespace_Aux_Data,
457 $sTemplate = replace_tablespace(
459 CONST_Tablespace_Aux_Index,
462 pgsqlRunScript($sTemplate, false);
465 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) {
466 $bDidSomething = true;
468 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
469 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
470 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,calculated_country_code,";
471 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select calculated_country_code,postcode,";
472 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
473 $sSQL .= "from placex where postcode is not null group by calculated_country_code,postcode) as x";
474 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
476 if (CONST_Use_Extra_US_Postcodes) {
477 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
478 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
479 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
480 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
484 if ($aCMDResult['osmosis-init'] || ($aCMDResult['all'] && !$aCMDResult['drop'])) { // no use doing osmosis-init when dropping update tables
485 $bDidSomething = true;
488 if (!file_exists(CONST_Osmosis_Binary)) {
489 echo "Please download osmosis.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
490 if (!$aCMDResult['all']) {
491 fail("osmosis not found in '".CONST_Osmosis_Binary."'");
494 if (file_exists(CONST_InstallPath.'/settings/configuration.txt')) {
495 echo "settings/configuration.txt already exists\n";
497 passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_InstallPath.'/settings');
498 // update osmosis configuration.txt with our settings
499 passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_InstallPath.'/settings/configuration.txt');
500 passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_InstallPath.'/settings/configuration.txt');
503 // Find the last node in the DB
504 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
506 // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed)
507 $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1";
508 $sLastNodeXML = file_get_contents($sLastNodeURL);
509 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);
510 $iLastNodeTimestamp = strtotime($aLastNodeDate[1]) - (3*60*60);
512 // Search for the correct state file - uses file timestamps so need to sort by date descending
513 $sRepURL = CONST_Replication_Url."/";
514 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
515 // download.geofabrik.de: <a href="000/">000/</a></td><td align="right">26-Feb-2013 11:53 </td>
516 // planet.openstreetmap.org: <a href="273/">273/</a> 2013-03-11 07:41 -
517 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);
519 $aPrevRepMatch = false;
520 foreach ($aRepMatches as $aRepMatch) {
521 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
522 $aPrevRepMatch = $aRepMatch;
524 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
526 $sRepURL .= $aRepMatch[1];
527 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
528 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);
529 $aPrevRepMatch = false;
530 foreach ($aRepMatches as $aRepMatch) {
531 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
532 $aPrevRepMatch = $aRepMatch;
534 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
536 $sRepURL .= $aRepMatch[1];
537 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
538 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);
539 $aPrevRepMatch = false;
540 foreach ($aRepMatches as $aRepMatch) {
541 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
542 $aPrevRepMatch = $aRepMatch;
544 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
546 $sRepURL .= $aRepMatch[1].'.state.txt';
547 echo "Getting state file: $sRepURL\n";
548 $sStateFile = file_get_contents($sRepURL);
549 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
550 file_put_contents(CONST_InstallPath.'/settings/state.txt', $sStateFile);
551 echo "Updating DB status\n";
552 pg_query($oDB->connection, 'TRUNCATE import_status');
553 $sSQL = "INSERT INTO import_status VALUES('".$aRepMatch[2]."')";
554 pg_query($oDB->connection, $sSQL);
556 if (!$aCMDResult['all']) {
557 fail("Cannot read state file directory.");
563 if ($aCMDResult['index'] || $aCMDResult['all']) {
564 $bDidSomething = true;
566 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
567 passthruCheckReturn($sBaseCmd.' -R 4');
568 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
569 passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
570 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
571 passthruCheckReturn($sBaseCmd.' -r 26');
574 if ($aCMDResult['create-search-indices'] || $aCMDResult['all']) {
575 echo "Search indices\n";
576 $bDidSomething = true;
578 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
579 $sTemplate = replace_tablespace(
580 '{ts:address-index}',
581 CONST_Tablespace_Address_Index,
584 $sTemplate = replace_tablespace(
586 CONST_Tablespace_Search_Index,
589 $sTemplate = replace_tablespace(
591 CONST_Tablespace_Aux_Index,
595 pgsqlRunScript($sTemplate);
598 if ($aCMDResult['create-country-names'] || $aCMDResult['all']) {
599 echo 'Creating search index for default country names';
600 $bDidSomething = true;
602 pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
603 pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
604 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");
605 pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
607 $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 ';
608 if (CONST_Languages) {
611 foreach (explode(',', CONST_Languages) as $sLang) {
612 $sSQL .= $sDelim."'name:$sLang'";
617 // all include all simple name tags
618 $sSQL .= "like 'name:%'";
621 pgsqlRunScript($sSQL);
624 if ($aCMDResult['drop']) {
625 // The implementation is potentially a bit dangerous because it uses
626 // a positive selection of tables to keep, and deletes everything else.
627 // Including any tables that the unsuspecting user might have manually
628 // created. USE AT YOUR OWN PERIL.
629 $bDidSomething = true;
631 // tables we want to keep. everything else goes.
632 $aKeepTables = array(
637 "location_property*",
651 $aDropTables = array();
652 $aHaveTables = chksql($oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
654 foreach ($aHaveTables as $sTable) {
656 foreach ($aKeepTables as $sKeep) {
657 if (fnmatch($sKeep, $sTable)) {
662 if (!$bFound) array_push($aDropTables, $sTable);
665 foreach ($aDropTables as $sDrop) {
666 if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
667 @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
668 // ignore warnings/errors as they might be caused by a table having
669 // been deleted already by CASCADE
672 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
673 if ($aCMDResult['verbose']) echo "deleting ".CONST_Osm2pgsql_Flatnode_File."\n";
674 unlink(CONST_Osm2pgsql_Flatnode_File);
678 if (!$bDidSomething) {
679 showUsage($aCMDOptions, true);
681 echo "Setup finished.\n";
685 function pgsqlRunScriptFile($sFilename)
687 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
689 // Convert database DSN to psql parameters
690 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
691 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
692 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
695 if (preg_match('/\\.gz$/', $sFilename)) {
696 $aDescriptors = array(
697 0 => array('pipe', 'r'),
698 1 => array('pipe', 'w'),
699 2 => array('file', '/dev/null', 'a')
701 $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
702 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
703 $aReadPipe = $ahGzipPipes[1];
704 fclose($ahGzipPipes[0]);
706 $sCMD .= ' -f '.$sFilename;
707 $aReadPipe = array('pipe', 'r');
710 $aDescriptors = array(
712 1 => array('pipe', 'w'),
713 2 => array('file', '/dev/null', 'a')
716 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
717 if (!is_resource($hProcess)) fail('unable to start pgsql');
720 // TODO: error checking
721 while (!feof($ahPipes[1])) {
722 echo fread($ahPipes[1], 4096);
726 $iReturn = proc_close($hProcess);
728 fail("pgsql returned with error code ($iReturn)");
731 fclose($ahGzipPipes[1]);
732 proc_close($hGzipProcess);
736 function pgsqlRunScript($sScript, $bfatal = true)
739 // Convert database DSN to psql parameters
740 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
741 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
742 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
743 if ($bfatal && !$aCMDResult['ignore-errors'])
744 $sCMD .= ' -v ON_ERROR_STOP=1';
745 $aDescriptors = array(
746 0 => array('pipe', 'r'),
751 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
752 if (!is_resource($hProcess)) fail('unable to start pgsql');
754 while (strlen($sScript)) {
755 $written = fwrite($ahPipes[0], $sScript);
756 if ($written <= 0) break;
757 $sScript = substr($sScript, $written);
760 $iReturn = proc_close($hProcess);
761 if ($bfatal && $iReturn > 0) {
762 fail("pgsql returned with error code ($iReturn)");
766 function pgsqlRunPartitionScript($sTemplate)
771 $sSQL = 'select distinct partition from country_name';
772 $aPartitions = chksql($oDB->getCol($sSQL));
773 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
775 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
776 foreach ($aMatches as $aMatch) {
778 foreach ($aPartitions as $sPartitionName) {
779 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
781 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
784 pgsqlRunScript($sTemplate);
787 function pgsqlRunRestoreData($sDumpFile)
789 // Convert database DSN to psql parameters
790 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
791 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
792 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
794 $aDescriptors = array(
795 0 => array('pipe', 'r'),
796 1 => array('pipe', 'w'),
797 2 => array('file', '/dev/null', 'a')
800 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
801 if (!is_resource($hProcess)) fail('unable to start pg_restore');
805 // TODO: error checking
806 while (!feof($ahPipes[1])) {
807 echo fread($ahPipes[1], 4096);
811 $iReturn = proc_close($hProcess);
814 function pgsqlRunDropAndRestore($sDumpFile)
816 // Convert database DSN to psql parameters
817 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
818 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
819 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
821 $aDescriptors = array(
822 0 => array('pipe', 'r'),
823 1 => array('pipe', 'w'),
824 2 => array('file', '/dev/null', 'a')
827 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
828 if (!is_resource($hProcess)) fail('unable to start pg_restore');
832 // TODO: error checking
833 while (!feof($ahPipes[1])) {
834 echo fread($ahPipes[1], 4096);
838 $iReturn = proc_close($hProcess);
841 function passthruCheckReturn($cmd)
844 passthru($cmd, $result);
845 if ($result != 0) fail('Error executing external command: '.$cmd);
848 function replace_tablespace($sTemplate, $sTablespace, $sSql)
851 $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
853 $sSql = str_replace($sTemplate, '', $sSql);
859 function create_sql_functions($aCMDResult)
861 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
862 $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
863 if ($aCMDResult['enable-diff-updates']) {
864 $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
866 if ($aCMDResult['enable-debug-statements']) {
867 $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
869 if (CONST_Limit_Reindexing) {
870 $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
872 if (!CONST_Use_US_Tiger_Data) {
873 $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
875 if (!CONST_Use_Aux_Location_data) {
876 $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
878 pgsqlRunScript($sTemplate);