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, geometry) RENAME TO ST_LineLocatePoint');
131 if ($fPostgisVersion < 2.2) {
132 pgsqlRunScript('ALTER FUNCTION ST_Distance_Spheroid(geometry, geometry, spheroid) 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 "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'");
171 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
172 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
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);
189 if (!chksql($oDB->getRow('select * from place limit 1'))) {
194 if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
196 $bDidSomething = true;
197 if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) fail("nominatim module not built");
198 create_sql_functions($aCMDResult);
201 if ($aCMDResult['create-tables'] || $aCMDResult['all']) {
202 $bDidSomething = true;
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(
209 CONST_Tablespace_Address_Data,
212 $sTemplate = replace_tablespace(
213 '{ts:address-index}',
214 CONST_Tablespace_Address_Index,
217 $sTemplate = replace_tablespace(
219 CONST_Tablespace_Search_Data,
222 $sTemplate = replace_tablespace(
224 CONST_Tablespace_Search_Index,
227 $sTemplate = replace_tablespace(
229 CONST_Tablespace_Aux_Data,
232 $sTemplate = replace_tablespace(
234 CONST_Tablespace_Aux_Index,
237 pgsqlRunScript($sTemplate, false);
239 // re-run the functions
241 create_sql_functions($aCMDResult);
244 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all']) {
245 echo "Partition Tables\n";
246 $bDidSomething = true;
248 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
249 $sTemplate = replace_tablespace(
251 CONST_Tablespace_Address_Data,
254 $sTemplate = replace_tablespace(
255 '{ts:address-index}',
256 CONST_Tablespace_Address_Index,
259 $sTemplate = replace_tablespace(
261 CONST_Tablespace_Search_Data,
264 $sTemplate = replace_tablespace(
266 CONST_Tablespace_Search_Index,
269 $sTemplate = replace_tablespace(
271 CONST_Tablespace_Aux_Data,
274 $sTemplate = replace_tablespace(
276 CONST_Tablespace_Aux_Index,
280 pgsqlRunPartitionScript($sTemplate);
284 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all']) {
285 echo "Partition Functions\n";
286 $bDidSomething = true;
288 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
290 pgsqlRunPartitionScript($sTemplate);
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);
302 echo "WARNING: wikipedia article dump file not found - places will have default importance\n";
304 if (file_exists($sWikiRedirectsFile)) {
305 echo "Importing wikipedia redirects...";
306 pgsqlRunDropAndRestore($sWikiRedirectsFile);
309 echo "WARNING: wikipedia redirect dump file not found - some place importance values may be missing\n";
314 if ($aCMDResult['load-data'] || $aCMDResult['all']) {
315 echo "Drop old Data\n";
316 $bDidSomething = true;
319 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
321 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
323 if (!pg_query($oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($oDB->connection));
325 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
327 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
329 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
331 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
333 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
335 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
337 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
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));
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));
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');
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 .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
369 $sSQL .= 'geometry) 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));
374 // last thread for interpolation lines
375 $aDBInstances[$iLoadThreads] =& getDB(true);
376 $sSQL = 'insert into location_property_osmline';
377 $sSQL .= ' (osm_id, interpolationtype, street, addr_place,';
378 $sSQL .= ' postcode, calculated_country_code, linegeo)';
379 $sSQL .= ' SELECT osm_id, housenumber, street, addr_place,';
380 $sSQL .= ' postcode, country_code, geometry from place where ';
381 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
382 if ($aCMDResult['verbose']) echo "$sSQL\n";
383 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
388 for ($i = 0; $i <= $iLoadThreads; $i++) {
389 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
395 echo "Reanalysing database...\n";
396 pgsqlRunScript('ANALYSE');
399 if ($aCMDResult['import-tiger-data']) {
400 $bDidSomething = true;
402 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
403 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
404 $sTemplate = replace_tablespace(
406 CONST_Tablespace_Aux_Data,
409 $sTemplate = replace_tablespace(
411 CONST_Tablespace_Aux_Index,
414 pgsqlRunScript($sTemplate, false);
416 $aDBInstances = array();
417 for ($i = 0; $i < $iInstances; $i++) {
418 $aDBInstances[$i] =& getDB(true);
421 foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
423 $hFile = fopen($sFile, "r");
424 $sSQL = fgets($hFile, 100000);
428 for ($i = 0; $i < $iInstances; $i++) {
429 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
430 while (pg_get_result($aDBInstances[$i]->connection));
431 $sSQL = fgets($hFile, 100000);
433 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
435 if ($iLines == 1000) {
449 for ($i = 0; $i < $iInstances; $i++) {
450 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
457 echo "Creating indexes\n";
458 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
459 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
460 $sTemplate = replace_tablespace(
462 CONST_Tablespace_Aux_Data,
465 $sTemplate = replace_tablespace(
467 CONST_Tablespace_Aux_Index,
470 pgsqlRunScript($sTemplate, false);
473 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) {
474 $bDidSomething = true;
476 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
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,calculated_country_code,";
479 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select calculated_country_code,postcode,";
480 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
481 $sSQL .= "from placex where postcode is not null group by calculated_country_code,postcode) as x ";
482 $sSQL .= "where ST_Point(x,y) is not null";
483 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
485 if (CONST_Use_Extra_US_Postcodes) {
486 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
487 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
488 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
489 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
493 if ($aCMDResult['osmosis-init'] || ($aCMDResult['all'] && !$aCMDResult['drop'])) { // no use doing osmosis-init when dropping update tables
494 $bDidSomething = true;
497 if (!file_exists(CONST_Osmosis_Binary)) {
498 echo "Please download osmosis.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
499 if (!$aCMDResult['all']) {
500 fail("osmosis not found in '".CONST_Osmosis_Binary."'");
503 if (file_exists(CONST_InstallPath.'/settings/configuration.txt')) {
504 echo "settings/configuration.txt already exists\n";
506 passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_InstallPath.'/settings');
507 // update osmosis configuration.txt with our settings
508 passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_InstallPath.'/settings/configuration.txt');
509 passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_InstallPath.'/settings/configuration.txt');
512 // Find the last node in the DB
513 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
515 // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed)
516 $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1";
517 $sLastNodeXML = file_get_contents($sLastNodeURL);
518 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);
519 $iLastNodeTimestamp = strtotime($aLastNodeDate[1]) - (3*60*60);
521 // Search for the correct state file - uses file timestamps so need to sort by date descending
522 $sRepURL = CONST_Replication_Url."/";
523 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
524 // download.geofabrik.de: <a href="000/">000/</a></td><td align="right">26-Feb-2013 11:53 </td>
525 // planet.openstreetmap.org: <a href="273/">273/</a> 2013-03-11 07:41 -
526 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);
528 $aPrevRepMatch = false;
529 foreach ($aRepMatches as $aRepMatch) {
530 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
531 $aPrevRepMatch = $aRepMatch;
533 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
535 $sRepURL .= $aRepMatch[1];
536 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
537 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);
538 $aPrevRepMatch = false;
539 foreach ($aRepMatches as $aRepMatch) {
540 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
541 $aPrevRepMatch = $aRepMatch;
543 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
545 $sRepURL .= $aRepMatch[1];
546 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
547 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);
548 $aPrevRepMatch = false;
549 foreach ($aRepMatches as $aRepMatch) {
550 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
551 $aPrevRepMatch = $aRepMatch;
553 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
555 $sRepURL .= $aRepMatch[1].'.state.txt';
556 echo "Getting state file: $sRepURL\n";
557 $sStateFile = file_get_contents($sRepURL);
558 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
559 file_put_contents(CONST_InstallPath.'/settings/state.txt', $sStateFile);
560 echo "Updating DB status\n";
561 pg_query($oDB->connection, 'TRUNCATE import_status');
562 $sSQL = "INSERT INTO import_status VALUES('".$aRepMatch[2]."')";
563 pg_query($oDB->connection, $sSQL);
565 if (!$aCMDResult['all']) {
566 fail("Cannot read state file directory.");
572 if ($aCMDResult['index'] || $aCMDResult['all']) {
573 $bDidSomething = true;
575 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
576 passthruCheckReturn($sBaseCmd.' -R 4');
577 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
578 passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
579 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
580 passthruCheckReturn($sBaseCmd.' -r 26');
583 if ($aCMDResult['create-search-indices'] || $aCMDResult['all']) {
584 echo "Search indices\n";
585 $bDidSomething = true;
587 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
588 $sTemplate = replace_tablespace(
589 '{ts:address-index}',
590 CONST_Tablespace_Address_Index,
593 $sTemplate = replace_tablespace(
595 CONST_Tablespace_Search_Index,
598 $sTemplate = replace_tablespace(
600 CONST_Tablespace_Aux_Index,
604 pgsqlRunScript($sTemplate);
607 if ($aCMDResult['create-country-names'] || $aCMDResult['all']) {
608 echo 'Creating search index for default country names';
609 $bDidSomething = true;
611 pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
612 pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
613 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");
614 pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
616 $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 ';
617 if (CONST_Languages) {
620 foreach (explode(',', CONST_Languages) as $sLang) {
621 $sSQL .= $sDelim."'name:$sLang'";
626 // all include all simple name tags
627 $sSQL .= "like 'name:%'";
630 pgsqlRunScript($sSQL);
633 if ($aCMDResult['drop']) {
634 // The implementation is potentially a bit dangerous because it uses
635 // a positive selection of tables to keep, and deletes everything else.
636 // Including any tables that the unsuspecting user might have manually
637 // created. USE AT YOUR OWN PERIL.
638 $bDidSomething = true;
640 // tables we want to keep. everything else goes.
641 $aKeepTables = array(
646 "location_property*",
660 $aDropTables = array();
661 $aHaveTables = chksql($oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
663 foreach ($aHaveTables as $sTable) {
665 foreach ($aKeepTables as $sKeep) {
666 if (fnmatch($sKeep, $sTable)) {
671 if (!$bFound) array_push($aDropTables, $sTable);
674 foreach ($aDropTables as $sDrop) {
675 if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
676 @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
677 // ignore warnings/errors as they might be caused by a table having
678 // been deleted already by CASCADE
681 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
682 if ($aCMDResult['verbose']) echo "deleting ".CONST_Osm2pgsql_Flatnode_File."\n";
683 unlink(CONST_Osm2pgsql_Flatnode_File);
687 if (!$bDidSomething) {
688 showUsage($aCMDOptions, true);
690 echo "Setup finished.\n";
694 function pgsqlRunScriptFile($sFilename)
696 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
698 // Convert database DSN to psql parameters
699 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
700 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
701 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
704 if (preg_match('/\\.gz$/', $sFilename)) {
705 $aDescriptors = array(
706 0 => array('pipe', 'r'),
707 1 => array('pipe', 'w'),
708 2 => array('file', '/dev/null', 'a')
710 $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
711 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
712 $aReadPipe = $ahGzipPipes[1];
713 fclose($ahGzipPipes[0]);
715 $sCMD .= ' -f '.$sFilename;
716 $aReadPipe = array('pipe', 'r');
719 $aDescriptors = array(
721 1 => array('pipe', 'w'),
722 2 => array('file', '/dev/null', 'a')
725 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
726 if (!is_resource($hProcess)) fail('unable to start pgsql');
729 // TODO: error checking
730 while (!feof($ahPipes[1])) {
731 echo fread($ahPipes[1], 4096);
735 $iReturn = proc_close($hProcess);
737 fail("pgsql returned with error code ($iReturn)");
740 fclose($ahGzipPipes[1]);
741 proc_close($hGzipProcess);
745 function pgsqlRunScript($sScript, $bfatal = true)
748 // Convert database DSN to psql parameters
749 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
750 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
751 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
752 if ($bfatal && !$aCMDResult['ignore-errors'])
753 $sCMD .= ' -v ON_ERROR_STOP=1';
754 $aDescriptors = array(
755 0 => array('pipe', 'r'),
760 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
761 if (!is_resource($hProcess)) fail('unable to start pgsql');
763 while (strlen($sScript)) {
764 $written = fwrite($ahPipes[0], $sScript);
765 if ($written <= 0) break;
766 $sScript = substr($sScript, $written);
769 $iReturn = proc_close($hProcess);
770 if ($bfatal && $iReturn > 0) {
771 fail("pgsql returned with error code ($iReturn)");
775 function pgsqlRunPartitionScript($sTemplate)
780 $sSQL = 'select distinct partition from country_name';
781 $aPartitions = chksql($oDB->getCol($sSQL));
782 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
784 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
785 foreach ($aMatches as $aMatch) {
787 foreach ($aPartitions as $sPartitionName) {
788 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
790 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
793 pgsqlRunScript($sTemplate);
796 function pgsqlRunRestoreData($sDumpFile)
798 // Convert database DSN to psql parameters
799 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
800 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
801 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
803 $aDescriptors = array(
804 0 => array('pipe', 'r'),
805 1 => array('pipe', 'w'),
806 2 => array('file', '/dev/null', 'a')
809 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
810 if (!is_resource($hProcess)) fail('unable to start pg_restore');
814 // TODO: error checking
815 while (!feof($ahPipes[1])) {
816 echo fread($ahPipes[1], 4096);
820 $iReturn = proc_close($hProcess);
823 function pgsqlRunDropAndRestore($sDumpFile)
825 // Convert database DSN to psql parameters
826 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
827 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
828 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
830 $aDescriptors = array(
831 0 => array('pipe', 'r'),
832 1 => array('pipe', 'w'),
833 2 => array('file', '/dev/null', 'a')
836 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
837 if (!is_resource($hProcess)) fail('unable to start pg_restore');
841 // TODO: error checking
842 while (!feof($ahPipes[1])) {
843 echo fread($ahPipes[1], 4096);
847 $iReturn = proc_close($hProcess);
850 function passthruCheckReturn($cmd)
853 passthru($cmd, $result);
854 if ($result != 0) fail('Error executing external command: '.$cmd);
857 function replace_tablespace($sTemplate, $sTablespace, $sSql)
860 $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
862 $sSql = str_replace($sTemplate, '', $sSql);
868 function create_sql_functions($aCMDResult)
870 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
871 $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
872 if ($aCMDResult['enable-diff-updates']) {
873 $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
875 if ($aCMDResult['enable-debug-statements']) {
876 $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
878 if (CONST_Limit_Reindexing) {
879 $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
881 if (!CONST_Use_US_Tiger_Data) {
882 $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
884 if (!CONST_Use_Aux_Location_data) {
885 $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
887 pgsqlRunScript($sTemplate);