4 require_once(dirname(dirname(__FILE__)).'/lib/init-cmd.php');
5 ini_set('memory_limit', '800M');
8 "Create and setup nominatim search system",
9 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
10 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
11 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
13 array('osm-file', '', 0, 1, 1, 1, 'realpath', 'File to import'),
14 array('threads', '', 0, 1, 1, 1, 'int', 'Number of threads (where possible)'),
16 array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
18 array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
19 array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
20 array('import-data', '', 0, 1, 0, 0, 'bool', 'Import a osm file'),
21 array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
22 array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
23 array('enable-diff-updates', '', 0, 1, 0, 0, 'bool', 'Turn on the code required to make diff updates work'),
24 array('enable-debug-statements', '', 0, 1, 0, 0, 'bool', 'Include debug warning statements in pgsql commands'),
25 array('create-minimal-tables', '', 0, 1, 0, 0, 'bool', 'Create minimal main tables'),
26 array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
27 array('create-partition-tables', '', 0, 1, 0, 0, 'bool', 'Create required partition tables'),
28 array('create-partition-functions', '', 0, 1, 0, 0, 'bool', 'Create required partition triggers'),
29 array('no-partitions', '', 0, 1, 0, 0, 'bool', "Do not partition search indices (speeds up import of single country extracts)"),
30 array('import-wikipedia-articles', '', 0, 1, 0, 0, 'bool', 'Import wikipedia article dump'),
31 array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
32 array('disable-token-precalc', '', 0, 1, 0, 0, 'bool', 'Disable name precalculation (EXPERT)'),
33 array('import-tiger-data', '', 0, 1, 0, 0, 'bool', 'Import tiger data (not included in \'all\')'),
34 array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
35 array('create-roads', '', 0, 1, 0, 0, 'bool', ''),
36 array('osmosis-init', '', 0, 1, 0, 0, 'bool', 'Generate default osmosis configuration'),
37 array('index', '', 0, 1, 0, 0, 'bool', 'Index the data'),
38 array('index-noanalyse', '', 0, 1, 0, 0, 'bool', 'Do not perform analyse operations during index (EXPERT)'),
39 array('index-output', '', 0, 1, 1, 1, 'string', 'File to dump index information to'),
40 array('create-search-indices', '', 0, 1, 0, 0, 'bool', 'Create additional indices required for search and update'),
41 array('create-website', '', 0, 1, 1, 1, 'realpath', 'Create symlinks to setup web directory'),
43 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
45 $bDidSomething = false;
47 // Check if osm-file is set and points to a valid file if --all or --import-data is given
48 if ($aCMDResult['import-data'] || $aCMDResult['all'])
50 if (!isset($aCMDResult['osm-file']))
52 fail('missing --osm-file for data import');
55 if (!file_exists($aCMDResult['osm-file']))
57 fail('the path supplied to --osm-file does not exist');
60 if (!is_readable($aCMDResult['osm-file']))
62 fail('osm-file "'.$aCMDResult['osm-file'].'" not readable');
67 // This is a pretty hard core default - the number of processors in the box - 1
68 $iInstances = isset($aCMDResult['threads'])?$aCMDResult['threads']:(getProcessorCount()-1);
72 echo "WARNING: resetting threads to $iInstances\n";
74 if ($iInstances > getProcessorCount())
76 $iInstances = getProcessorCount();
77 echo "WARNING: resetting threads to $iInstances\n";
80 // Assume we can steal all the cache memory in the box (unless told otherwise)
81 $iCacheMemory = (isset($aCMDResult['osm2pgsql-cache'])?$aCMDResult['osm2pgsql-cache']:getCacheMemoryMB());
82 if ($iCacheMemory > getTotalMemoryMB())
84 $iCacheMemory = getCacheMemoryMB();
85 echo "WARNING: resetting cache memory to $iCacheMemory\n";
88 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
89 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
91 if ($aCMDResult['create-db'] || $aCMDResult['all'])
94 $bDidSomething = true;
95 $oDB =& DB::connect(CONST_Database_DSN, false);
96 if (!PEAR::isError($oDB))
98 fail('database already exists ('.CONST_Database_DSN.')');
100 passthruCheckReturn('createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
103 if ($aCMDResult['setup-db'] || $aCMDResult['all'])
106 $bDidSomething = true;
107 // TODO: path detection, detection memory, etc.
111 $sVersionString = $oDB->getOne('select version()');
112 preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[.]([0-9]+) #', $sVersionString, $aMatches);
113 if (CONST_Postgresql_Version != $aMatches[1].'.'.$aMatches[2])
115 echo "ERROR: PostgreSQL version is not correct. Expected ".CONST_Postgresql_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n";
119 passthru('createlang plpgsql -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
120 $pgver = (float) CONST_Postgresql_Version;
122 pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/hstore.sql');
123 pgsqlRunScriptFile(CONST_BasePath.'/sql/hstore_compatability_9_0.sql');
125 pgsqlRunScript('CREATE EXTENSION hstore');
128 $fPostgisVersion = (float) CONST_Postgis_Version;
129 if ($fPostgisVersion < 2.0) {
130 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql');
131 pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql');
133 pgsqlRunScript('CREATE EXTENSION postgis');
135 if ($fPostgisVersion < 2.1) {
136 // Function was renamed in 2.1 and throws an annoying deprecation warning
137 pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
139 $sVersionString = $oDB->getOne('select postgis_full_version()');
140 preg_match('#POSTGIS="([0-9]+)[.]([0-9]+)[.]([0-9]+)( r([0-9]+))?"#', $sVersionString, $aMatches);
141 if (CONST_Postgis_Version != $aMatches[1].'.'.$aMatches[2])
143 echo "ERROR: PostGIS version is not correct. Expected ".CONST_Postgis_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n";
147 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
148 pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
149 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
150 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
151 if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz'))
153 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz');
157 echo "WARNING: external UK postcode table not found.\n";
159 pgsqlRunScriptFile(CONST_BasePath.'/data/us_statecounty.sql');
160 pgsqlRunScriptFile(CONST_BasePath.'/data/us_state.sql');
161 pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
163 if ($aCMDResult['no-partitions'])
165 pgsqlRunScript('update country_name set partition = 0');
169 if ($aCMDResult['import-data'] || $aCMDResult['all'])
172 $bDidSomething = true;
174 $osm2pgsql = CONST_Osm2pgsql_Binary;
175 if (!file_exists($osm2pgsql))
177 echo "Please download and build osm2pgsql.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
178 fail("osm2pgsql not found in '$osm2pgsql'");
181 if (!is_null(CONST_Osm2pgsql_Flatnode_File))
183 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
185 $osm2pgsql .= ' --tablespace-slim-index ssd --tablespace-main-index ssd --tablespace-main-data ssd --tablespace-slim-data data';
186 $osm2pgsql .= ' -lsc -O gazetteer --hstore';
187 $osm2pgsql .= ' -C 18000';
188 $osm2pgsql .= ' -P '.$aDSNInfo['port'];
189 $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
190 passthruCheckReturn($osm2pgsql);
193 $x = $oDB->getRow('select * from place limit 1');
194 if (PEAR::isError($x)) {
195 fail($x->getMessage());
197 if (!$x) fail('No Data');
200 if ($aCMDResult['create-functions'] || $aCMDResult['all'])
203 $bDidSomething = true;
204 if (!file_exists(CONST_BasePath.'/module/nominatim.so')) fail("nominatim module not built");
205 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
206 $sTemplate = str_replace('{modulepath}', CONST_BasePath.'/module', $sTemplate);
207 if ($aCMDResult['enable-diff-updates']) $sTemplate = str_replace('RETURN NEW; -- @DIFFUPDATES@', '--', $sTemplate);
208 if ($aCMDResult['enable-debug-statements']) $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
209 if (CONST_Limit_Reindexing) $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
210 pgsqlRunScript($sTemplate);
213 if ($aCMDResult['create-minimal-tables'])
215 echo "Minimal Tables\n";
216 $bDidSomething = true;
217 pgsqlRunScriptFile(CONST_BasePath.'/sql/tables-minimal.sql');
221 // Backstop the import process - easliest possible import id
222 $sScript .= "insert into import_npi_log values (18022);\n";
224 $hFile = @fopen(CONST_BasePath.'/settings/partitionedtags.def', "r");
225 if (!$hFile) fail('unable to open list of partitions: '.CONST_BasePath.'/settings/partitionedtags.def');
227 while (($sLine = fgets($hFile, 4096)) !== false && $sLine && substr($sLine,0,1) !='#')
229 list($sClass, $sType) = explode(' ', trim($sLine));
230 $sScript .= "create table place_classtype_".$sClass."_".$sType." as ";
231 $sScript .= "select place_id as place_id,geometry as centroid from placex limit 0;\n";
233 $sScript .= "CREATE INDEX idx_place_classtype_".$sClass."_".$sType."_centroid ";
234 $sScript .= "ON place_classtype_".$sClass."_".$sType." USING GIST (centroid);\n";
236 $sScript .= "CREATE INDEX idx_place_classtype_".$sClass."_".$sType."_place_id ";
237 $sScript .= "ON place_classtype_".$sClass."_".$sType." USING btree(place_id);\n";
240 pgsqlRunScript($sScript);
243 if ($aCMDResult['create-tables'] || $aCMDResult['all'])
246 $bDidSomething = true;
247 pgsqlRunScriptFile(CONST_BasePath.'/sql/tables.sql');
249 // re-run the functions
250 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
251 $sTemplate = str_replace('{modulepath}',CONST_BasePath.'/module', $sTemplate);
252 pgsqlRunScript($sTemplate);
255 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all'])
257 echo "Partition Tables\n";
258 $bDidSomething = true;
260 $sSQL = 'select distinct partition from country_name';
261 $aPartitions = $oDB->getCol($sSQL);
262 if (PEAR::isError($aPartitions))
264 fail($aPartitions->getMessage());
266 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
268 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
269 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
270 foreach($aMatches as $aMatch)
273 foreach($aPartitions as $sPartitionName)
275 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
277 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
280 pgsqlRunScript($sTemplate);
284 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all'])
286 echo "Partition Functions\n";
287 $bDidSomething = true;
289 $sSQL = 'select distinct partition from country_name';
290 $aPartitions = $oDB->getCol($sSQL);
291 if (PEAR::isError($aPartitions))
293 fail($aPartitions->getMessage());
295 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
297 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
298 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
299 foreach($aMatches as $aMatch)
302 foreach($aPartitions as $sPartitionName)
304 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
306 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
309 pgsqlRunScript($sTemplate);
312 if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all'])
314 $bDidSomething = true;
315 $sWikiArticlesFile = CONST_BasePath.'/data/wikipedia_article.sql.bin';
316 $sWikiRedirectsFile = CONST_BasePath.'/data/wikipedia_redirect.sql.bin';
317 if (file_exists($sWikiArticlesFile))
319 echo "Importing wikipedia articles...";
320 pgsqlRunDropAndRestore($sWikiArticlesFile);
325 echo "WARNING: wikipedia article dump file not found - places will have default importance\n";
327 if (file_exists($sWikiRedirectsFile))
329 echo "Importing wikipedia redirects...";
330 pgsqlRunDropAndRestore($sWikiRedirectsFile);
335 echo "WARNING: wikipedia redirect dump file not found - some place importance values may be missing\n";
340 if ($aCMDResult['load-data'] || $aCMDResult['all'])
342 echo "Drop old Data\n";
343 $bDidSomething = true;
346 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
348 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
350 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
352 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
354 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
356 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
358 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
360 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
362 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
365 $sSQL = 'select distinct partition from country_name';
366 $aPartitions = $oDB->getCol($sSQL);
367 if (PEAR::isError($aPartitions))
369 fail($aPartitions->getMessage());
371 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
372 foreach($aPartitions as $sPartition)
374 if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection));
378 // used by getorcreate_word_id to ignore frequent partial words
379 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));
382 // pre-create the word list
383 if (!$aCMDResult['disable-token-precalc'])
385 echo "Loading word list\n";
386 pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
390 $aDBInstances = array();
391 for($i = 0; $i < $iInstances; $i++)
393 $aDBInstances[$i] =& getDB(true);
394 $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
395 $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
396 $sSQL .= 'geometry) select * from place where osm_id % '.$iInstances.' = '.$i;
397 if ($aCMDResult['verbose']) echo "$sSQL\n";
398 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
404 for($i = 0; $i < $iInstances; $i++)
406 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
412 echo "Reanalysing database...\n";
413 pgsqlRunScript('ANALYSE');
416 if ($aCMDResult['create-roads'])
418 $bDidSomething = true;
421 $aDBInstances = array();
422 for($i = 0; $i < $iInstances; $i++)
424 $aDBInstances[$i] =& getDB(true);
425 if (!pg_query($aDBInstances[$i]->connection, 'set enable_bitmapscan = off')) fail(pg_last_error($oDB->connection));
426 $sSQL = 'select count(*) from (select insertLocationRoad(partition, place_id, calculated_country_code, geometry) from ';
427 $sSQL .= 'placex where osm_id % '.$iInstances.' = '.$i.' and rank_search between 26 and 27 and class = \'highway\') as x ';
428 if ($aCMDResult['verbose']) echo "$sSQL\n";
429 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
435 for($i = 0; $i < $iInstances; $i++)
437 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
445 if ($aCMDResult['import-tiger-data'])
447 $bDidSomething = true;
449 pgsqlRunScriptFile(CONST_BasePath.'/sql/tiger_import_start.sql');
451 $aDBInstances = array();
452 for($i = 0; $i < $iInstances; $i++)
454 $aDBInstances[$i] =& getDB(true);
457 foreach(glob(CONST_BasePath.'/data/tiger2011/*.sql') as $sFile)
460 $hFile = fopen($sFile, "r");
461 $sSQL = fgets($hFile, 100000);
466 for($i = 0; $i < $iInstances; $i++)
468 if (!pg_connection_busy($aDBInstances[$i]->connection))
470 while(pg_get_result($aDBInstances[$i]->connection));
471 $sSQL = fgets($hFile, 100000);
473 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
491 for($i = 0; $i < $iInstances; $i++)
493 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
500 echo "Creating indexes\n";
501 pgsqlRunScriptFile(CONST_BasePath.'/sql/tiger_import_finish.sql');
504 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all'])
506 $bDidSomething = true;
508 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
509 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
510 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,calculated_country_code,";
511 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select calculated_country_code,postcode,";
512 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
513 $sSQL .= "from placex where postcode is not null and calculated_country_code not in ('ie') group by calculated_country_code,postcode) as x";
514 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
516 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
517 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
518 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
519 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
522 if ($aCMDResult['osmosis-init'] || $aCMDResult['all'])
524 $bDidSomething = true;
527 if (!file_exists(CONST_Osmosis_Binary))
529 echo "Please download osmosis.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
530 if (!$aCMDResult['all'])
532 fail("osmosis not found in '".CONST_Osmosis_Binary."'");
537 if (file_exists(CONST_BasePath.'/settings/configuration.txt'))
539 echo "settings/configuration.txt already exists\n";
543 passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_BasePath.'/settings');
544 // update osmosis configuration.txt with our settings
545 passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_BasePath.'/settings/configuration.txt');
546 passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_BasePath.'/settings/configuration.txt');
549 // Find the last node in the DB
550 $iLastOSMID = $oDB->getOne("select max(id) from planet_osm_nodes");
552 // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed)
553 $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1";
554 $sLastNodeXML = file_get_contents($sLastNodeURL);
555 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);
556 $iLastNodeTimestamp = strtotime($aLastNodeDate[1]) - (3*60*60);
558 // Search for the correct state file - uses file timestamps so need to sort by date descending
559 $sRepURL = CONST_Replication_Url."/";
560 $sRep = file_get_contents($sRepURL."?C=M;O=D");
561 // download.geofabrik.de: <a href="000/">000/</a></td><td align="right">26-Feb-2013 11:53 </td>
562 // planet.openstreetmap.org: <a href="273/">273/</a> 22-Mar-2013 07:41 -
563 preg_match_all('#<a href="[0-9]{3}/">([0-9]{3}/)</a>.*(([0-9]{2})-([A-z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}))#', $sRep, $aRepMatches, PREG_SET_ORDER);
564 $aPrevRepMatch = false;
565 foreach($aRepMatches as $aRepMatch)
567 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
568 $aPrevRepMatch = $aRepMatch;
570 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
572 $sRepURL .= $aRepMatch[1];
573 $sRep = file_get_contents($sRepURL."?C=M;O=D");
574 preg_match_all('#<a href="[0-9]{3}/">([0-9]{3}/)</a>.*(([0-9]{2})-([A-z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}))#', $sRep, $aRepMatches, PREG_SET_ORDER);
575 $aPrevRepMatch = false;
576 foreach($aRepMatches as $aRepMatch)
578 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
579 $aPrevRepMatch = $aRepMatch;
581 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
583 $sRepURL .= $aRepMatch[1];
584 $sRep = file_get_contents($sRepURL."?C=M;O=D");
585 preg_match_all('#<a href="[0-9]{3}.state.txt">([0-9]{3}).state.txt</a>.*(([0-9]{2})-([A-z]{3})-([0-9]{4}) ([0-9]{2}):([0-9]{2}))#', $sRep, $aRepMatches, PREG_SET_ORDER);
586 $aPrevRepMatch = false;
587 foreach($aRepMatches as $aRepMatch)
589 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
590 $aPrevRepMatch = $aRepMatch;
592 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
594 $sRepURL .= $aRepMatch[1].'.state.txt';
595 echo "Getting state file: $sRepURL\n";
596 $sStateFile = file_get_contents($sRepURL);
597 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
598 file_put_contents(CONST_BasePath.'/settings/state.txt', $sStateFile);
599 echo "Updating DB status\n";
600 pg_query($oDB->connection, 'TRUNCATE import_status');
601 $sSQL = "INSERT INTO import_status VALUES('".$aRepMatch[2]."')";
602 pg_query($oDB->connection, $sSQL);
606 if ($aCMDResult['index'] || $aCMDResult['all'])
608 $bDidSomething = true;
610 if (isset($aCMDResult['index-output'])) $sOutputFile = ' -F '.$aCMDResult['index-output'];
611 $sBaseCmd = CONST_BasePath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
612 passthruCheckReturn($sBaseCmd.' -R 4');
613 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
614 passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
615 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
616 passthruCheckReturn($sBaseCmd.' -r 26');
619 if ($aCMDResult['create-search-indices'] || $aCMDResult['all'])
621 echo "Search indices\n";
622 $bDidSomething = true;
624 $sSQL = 'select distinct partition from country_name';
625 $aPartitions = $oDB->getCol($sSQL);
626 if (PEAR::isError($aPartitions))
628 fail($aPartitions->getMessage());
630 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
632 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
633 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
634 foreach($aMatches as $aMatch)
637 foreach($aPartitions as $sPartitionName)
639 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
641 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
644 pgsqlRunScript($sTemplate);
647 if (isset($aCMDResult['create-website']))
649 $bDidSomething = true;
650 $sTargetDir = $aCMDResult['create-website'];
651 if (!is_dir($sTargetDir))
653 echo "You must create the website directory before calling this function.\n";
654 fail("Target directory does not exist.");
657 @symlink(CONST_BasePath.'/website/details.php', $sTargetDir.'/details.php');
658 @symlink(CONST_BasePath.'/website/reverse.php', $sTargetDir.'/reverse.php');
659 @symlink(CONST_BasePath.'/website/search.php', $sTargetDir.'/search.php');
660 @symlink(CONST_BasePath.'/website/search.php', $sTargetDir.'/index.php');
661 @symlink(CONST_BasePath.'/website/deletable.php', $sTargetDir.'/deletable.php');
662 @symlink(CONST_BasePath.'/website/polygons.php', $sTargetDir.'/polygons.php');
663 @symlink(CONST_BasePath.'/website/status.php', $sTargetDir.'/status.php');
664 @symlink(CONST_BasePath.'/website/images', $sTargetDir.'/images');
665 @symlink(CONST_BasePath.'/website/js', $sTargetDir.'/js');
666 @symlink(CONST_BasePath.'/website/css', $sTargetDir.'/css');
667 echo "Symlinks created\n";
669 $sTestFile = @file_get_contents(CONST_Website_BaseURL.'js/tiles.js');
672 echo "\nWARNING: Unable to access the website at ".CONST_Website_BaseURL."\n";
673 echo "You may want to update settings/local.php with @define('CONST_Website_BaseURL', 'http://[HOST]/[PATH]/');\n";
679 showUsage($aCMDOptions, true);
682 function pgsqlRunScriptFile($sFilename)
684 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
686 // Convert database DSN to psql parameters
687 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
688 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
689 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
692 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')
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]);
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]))
723 echo fread($ahPipes[1], 4096);
727 proc_close($hProcess);
730 fclose($ahGzipPipes[1]);
731 proc_close($hGzipProcess);
736 function pgsqlRunScript($sScript)
738 // Convert database DSN to psql parameters
739 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
740 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
741 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
742 $aDescriptors = array(
743 0 => array('pipe', 'r'),
748 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
749 if (!is_resource($hProcess)) fail('unable to start pgsql');
751 while(strlen($sScript))
753 $written = fwrite($ahPipes[0], $sScript);
754 $sScript = substr($sScript, $written);
757 proc_close($hProcess);
760 function pgsqlRunRestoreData($sDumpFile)
762 // Convert database DSN to psql parameters
763 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
764 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
765 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
767 $aDescriptors = array(
768 0 => array('pipe', 'r'),
769 1 => array('pipe', 'w'),
770 2 => array('file', '/dev/null', 'a')
773 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
774 if (!is_resource($hProcess)) fail('unable to start pg_restore');
778 // TODO: error checking
779 while(!feof($ahPipes[1]))
781 echo fread($ahPipes[1], 4096);
785 proc_close($hProcess);
788 function pgsqlRunDropAndRestore($sDumpFile)
790 // Convert database DSN to psql parameters
791 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
792 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
793 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
795 $aDescriptors = array(
796 0 => array('pipe', 'r'),
797 1 => array('pipe', 'w'),
798 2 => array('file', '/dev/null', 'a')
801 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
802 if (!is_resource($hProcess)) fail('unable to start pg_restore');
806 // TODO: error checking
807 while(!feof($ahPipes[1]))
809 echo fread($ahPipes[1], 4096);
813 proc_close($hProcess);
816 function passthruCheckReturn($cmd)
819 passthru($cmd, $result);
820 if ($result != 0) fail('Error executing external command: '.$cmd);