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 // Function was renamed in 2.1 and throws an annoying deprecation warning
128 pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
131 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
132 pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
133 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
134 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
135 if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz')) {
136 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz');
138 echo "WARNING: external UK postcode table not found.\n";
140 if (CONST_Use_Extra_US_Postcodes) {
141 pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
144 if ($aCMDResult['no-partitions']) {
145 pgsqlRunScript('update country_name set partition = 0');
148 // the following will be needed by create_functions later but
149 // is only defined in the subsequently called create_tables.
150 // Create dummies here that will be overwritten by the proper
151 // versions in create-tables.
152 pgsqlRunScript('CREATE TABLE place_boundingbox ()');
153 pgsqlRunScript('create type wikipedia_article_match as ()');
156 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
158 $bDidSomething = true;
160 $osm2pgsql = CONST_Osm2pgsql_Binary;
161 if (!file_exists($osm2pgsql)) {
162 echo "Please download and build osm2pgsql.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
163 fail("osm2pgsql not found in '$osm2pgsql'");
166 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
167 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
169 if (CONST_Tablespace_Osm2pgsql_Data)
170 $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
171 if (CONST_Tablespace_Osm2pgsql_Index)
172 $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
173 if (CONST_Tablespace_Place_Data)
174 $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
175 if (CONST_Tablespace_Place_Index)
176 $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
177 $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
178 $osm2pgsql .= ' -C '.$iCacheMemory;
179 $osm2pgsql .= ' -P '.$aDSNInfo['port'];
180 $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
181 passthruCheckReturn($osm2pgsql);
184 if (!chksql($oDB->getRow('select * from place limit 1'))) {
189 if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
191 $bDidSomething = true;
192 if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) fail("nominatim module not built");
193 create_sql_functions($aCMDResult);
196 if ($aCMDResult['create-tables'] || $aCMDResult['all']) {
197 $bDidSomething = true;
200 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
201 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
202 $sTemplate = replace_tablespace(
204 CONST_Tablespace_Address_Data,
207 $sTemplate = replace_tablespace(
208 '{ts:address-index}',
209 CONST_Tablespace_Address_Index,
212 $sTemplate = replace_tablespace(
214 CONST_Tablespace_Search_Data,
217 $sTemplate = replace_tablespace(
219 CONST_Tablespace_Search_Index,
222 $sTemplate = replace_tablespace(
224 CONST_Tablespace_Aux_Data,
227 $sTemplate = replace_tablespace(
229 CONST_Tablespace_Aux_Index,
232 pgsqlRunScript($sTemplate, false);
234 // re-run the functions
236 create_sql_functions($aCMDResult);
239 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all']) {
240 echo "Partition Tables\n";
241 $bDidSomething = true;
243 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
244 $sTemplate = replace_tablespace(
246 CONST_Tablespace_Address_Data,
249 $sTemplate = replace_tablespace(
250 '{ts:address-index}',
251 CONST_Tablespace_Address_Index,
254 $sTemplate = replace_tablespace(
256 CONST_Tablespace_Search_Data,
259 $sTemplate = replace_tablespace(
261 CONST_Tablespace_Search_Index,
264 $sTemplate = replace_tablespace(
266 CONST_Tablespace_Aux_Data,
269 $sTemplate = replace_tablespace(
271 CONST_Tablespace_Aux_Index,
275 pgsqlRunPartitionScript($sTemplate);
279 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all']) {
280 echo "Partition Functions\n";
281 $bDidSomething = true;
283 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
285 pgsqlRunPartitionScript($sTemplate);
288 if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all']) {
289 $bDidSomething = true;
290 $sWikiArticlesFile = CONST_BasePath.'/data/wikipedia_article.sql.bin';
291 $sWikiRedirectsFile = CONST_BasePath.'/data/wikipedia_redirect.sql.bin';
292 if (file_exists($sWikiArticlesFile)) {
293 echo "Importing wikipedia articles...";
294 pgsqlRunDropAndRestore($sWikiArticlesFile);
297 echo "WARNING: wikipedia article dump file not found - places will have default importance\n";
299 if (file_exists($sWikiRedirectsFile)) {
300 echo "Importing wikipedia redirects...";
301 pgsqlRunDropAndRestore($sWikiRedirectsFile);
304 echo "WARNING: wikipedia redirect dump file not found - some place importance values may be missing\n";
309 if ($aCMDResult['load-data'] || $aCMDResult['all']) {
310 echo "Drop old Data\n";
311 $bDidSomething = true;
314 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
316 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
318 if (!pg_query($oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($oDB->connection));
320 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
322 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
324 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
326 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
328 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
330 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
332 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
335 $sSQL = 'select distinct partition from country_name';
336 $aPartitions = chksql($oDB->getCol($sSQL));
337 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
338 foreach ($aPartitions as $sPartition) {
339 if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection));
343 // used by getorcreate_word_id to ignore frequent partial words
344 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));
347 // pre-create the word list
348 if (!$aCMDResult['disable-token-precalc']) {
349 echo "Loading word list\n";
350 pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
354 $aDBInstances = array();
355 $iLoadThreads = max(1, $iInstances - 1);
356 for ($i = 0; $i < $iLoadThreads; $i++) {
357 $aDBInstances[$i] =& getDB(true);
358 $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
359 $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
360 $sSQL .= 'geometry) select * from place where osm_id % '.$iLoadThreads.' = '.$i;
361 $sSQL .= " and not (class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString')";
362 if ($aCMDResult['verbose']) echo "$sSQL\n";
363 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
365 // last thread for interpolation lines
366 $aDBInstances[$iLoadThreads] =& getDB(true);
367 $sSQL = 'select insert_osmline (osm_id, housenumber, street, addr_place, postcode, country_code, ';
368 $sSQL .= 'geometry) from place where ';
369 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
370 if ($aCMDResult['verbose']) echo "$sSQL\n";
371 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
376 for ($i = 0; $i <= $iLoadThreads; $i++) {
377 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
383 echo "Reanalysing database...\n";
384 pgsqlRunScript('ANALYSE');
387 if ($aCMDResult['import-tiger-data']) {
388 $bDidSomething = true;
390 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
391 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
392 $sTemplate = replace_tablespace(
394 CONST_Tablespace_Aux_Data,
397 $sTemplate = replace_tablespace(
399 CONST_Tablespace_Aux_Index,
402 pgsqlRunScript($sTemplate, false);
404 $aDBInstances = array();
405 for ($i = 0; $i < $iInstances; $i++) {
406 $aDBInstances[$i] =& getDB(true);
409 foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
411 $hFile = fopen($sFile, "r");
412 $sSQL = fgets($hFile, 100000);
416 for ($i = 0; $i < $iInstances; $i++) {
417 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
418 while (pg_get_result($aDBInstances[$i]->connection));
419 $sSQL = fgets($hFile, 100000);
421 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
423 if ($iLines == 1000) {
437 for ($i = 0; $i < $iInstances; $i++) {
438 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
445 echo "Creating indexes\n";
446 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
447 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
448 $sTemplate = replace_tablespace(
450 CONST_Tablespace_Aux_Data,
453 $sTemplate = replace_tablespace(
455 CONST_Tablespace_Aux_Index,
458 pgsqlRunScript($sTemplate, false);
461 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) {
462 $bDidSomething = true;
464 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
465 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
466 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,calculated_country_code,";
467 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select calculated_country_code,postcode,";
468 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
469 $sSQL .= "from placex where postcode is not null group by calculated_country_code,postcode) as x";
470 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
472 if (CONST_Use_Extra_US_Postcodes) {
473 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
474 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
475 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
476 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
480 if ($aCMDResult['osmosis-init'] || ($aCMDResult['all'] && !$aCMDResult['drop'])) { // no use doing osmosis-init when dropping update tables
481 $bDidSomething = true;
484 if (!file_exists(CONST_Osmosis_Binary)) {
485 echo "Please download osmosis.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
486 if (!$aCMDResult['all']) {
487 fail("osmosis not found in '".CONST_Osmosis_Binary."'");
490 if (file_exists(CONST_InstallPath.'/settings/configuration.txt')) {
491 echo "settings/configuration.txt already exists\n";
493 passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_InstallPath.'/settings');
494 // update osmosis configuration.txt with our settings
495 passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_InstallPath.'/settings/configuration.txt');
496 passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_InstallPath.'/settings/configuration.txt');
499 // Find the last node in the DB
500 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
502 // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed)
503 $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1";
504 $sLastNodeXML = file_get_contents($sLastNodeURL);
505 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);
506 $iLastNodeTimestamp = strtotime($aLastNodeDate[1]) - (3*60*60);
508 // Search for the correct state file - uses file timestamps so need to sort by date descending
509 $sRepURL = CONST_Replication_Url."/";
510 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
511 // download.geofabrik.de: <a href="000/">000/</a></td><td align="right">26-Feb-2013 11:53 </td>
512 // planet.openstreetmap.org: <a href="273/">273/</a> 2013-03-11 07:41 -
513 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);
515 $aPrevRepMatch = false;
516 foreach ($aRepMatches as $aRepMatch) {
517 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
518 $aPrevRepMatch = $aRepMatch;
520 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
522 $sRepURL .= $aRepMatch[1];
523 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
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 $aPrevRepMatch = false;
526 foreach ($aRepMatches as $aRepMatch) {
527 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
528 $aPrevRepMatch = $aRepMatch;
530 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
532 $sRepURL .= $aRepMatch[1];
533 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
534 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);
535 $aPrevRepMatch = false;
536 foreach ($aRepMatches as $aRepMatch) {
537 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
538 $aPrevRepMatch = $aRepMatch;
540 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
542 $sRepURL .= $aRepMatch[1].'.state.txt';
543 echo "Getting state file: $sRepURL\n";
544 $sStateFile = file_get_contents($sRepURL);
545 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
546 file_put_contents(CONST_InstallPath.'/settings/state.txt', $sStateFile);
547 echo "Updating DB status\n";
548 pg_query($oDB->connection, 'TRUNCATE import_status');
549 $sSQL = "INSERT INTO import_status VALUES('".$aRepMatch[2]."')";
550 pg_query($oDB->connection, $sSQL);
552 if (!$aCMDResult['all']) {
553 fail("Cannot read state file directory.");
559 if ($aCMDResult['index'] || $aCMDResult['all']) {
560 $bDidSomething = true;
562 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
563 passthruCheckReturn($sBaseCmd.' -R 4');
564 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
565 passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
566 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
567 passthruCheckReturn($sBaseCmd.' -r 26');
570 if ($aCMDResult['create-search-indices'] || $aCMDResult['all']) {
571 echo "Search indices\n";
572 $bDidSomething = true;
574 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
575 $sTemplate = replace_tablespace(
576 '{ts:address-index}',
577 CONST_Tablespace_Address_Index,
580 $sTemplate = replace_tablespace(
582 CONST_Tablespace_Search_Index,
585 $sTemplate = replace_tablespace(
587 CONST_Tablespace_Aux_Index,
591 pgsqlRunScript($sTemplate);
594 if ($aCMDResult['create-country-names'] || $aCMDResult['all']) {
595 echo 'Creating search index for default country names';
596 $bDidSomething = true;
598 pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
599 pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
600 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");
601 pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
603 $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 ';
604 if (CONST_Languages) {
607 foreach (explode(',', CONST_Languages) as $sLang) {
608 $sSQL .= $sDelim."'name:$sLang'";
613 // all include all simple name tags
614 $sSQL .= "like 'name:%'";
617 pgsqlRunScript($sSQL);
620 if ($aCMDResult['drop']) {
621 // The implementation is potentially a bit dangerous because it uses
622 // a positive selection of tables to keep, and deletes everything else.
623 // Including any tables that the unsuspecting user might have manually
624 // created. USE AT YOUR OWN PERIL.
625 $bDidSomething = true;
627 // tables we want to keep. everything else goes.
628 $aKeepTables = array(
633 "location_property*",
647 $aDropTables = array();
648 $aHaveTables = chksql($oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
650 foreach ($aHaveTables as $sTable) {
652 foreach ($aKeepTables as $sKeep) {
653 if (fnmatch($sKeep, $sTable)) {
658 if (!$bFound) array_push($aDropTables, $sTable);
661 foreach ($aDropTables as $sDrop) {
662 if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
663 @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
664 // ignore warnings/errors as they might be caused by a table having
665 // been deleted already by CASCADE
668 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
669 if ($aCMDResult['verbose']) echo "deleting ".CONST_Osm2pgsql_Flatnode_File."\n";
670 unlink(CONST_Osm2pgsql_Flatnode_File);
674 if (!$bDidSomething) {
675 showUsage($aCMDOptions, true);
677 echo "Setup finished.\n";
681 function pgsqlRunScriptFile($sFilename)
683 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
685 // Convert database DSN to psql parameters
686 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
687 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
688 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
691 if (preg_match('/\\.gz$/', $sFilename)) {
692 $aDescriptors = array(
693 0 => array('pipe', 'r'),
694 1 => array('pipe', 'w'),
695 2 => array('file', '/dev/null', 'a')
697 $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
698 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
699 $aReadPipe = $ahGzipPipes[1];
700 fclose($ahGzipPipes[0]);
702 $sCMD .= ' -f '.$sFilename;
703 $aReadPipe = array('pipe', 'r');
706 $aDescriptors = array(
708 1 => array('pipe', 'w'),
709 2 => array('file', '/dev/null', 'a')
712 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
713 if (!is_resource($hProcess)) fail('unable to start pgsql');
716 // TODO: error checking
717 while (!feof($ahPipes[1])) {
718 echo fread($ahPipes[1], 4096);
722 $iReturn = proc_close($hProcess);
724 fail("pgsql returned with error code ($iReturn)");
727 fclose($ahGzipPipes[1]);
728 proc_close($hGzipProcess);
732 function pgsqlRunScript($sScript, $bfatal = true)
735 // Convert database DSN to psql parameters
736 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
737 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
738 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
739 if ($bfatal && !$aCMDResult['ignore-errors'])
740 $sCMD .= ' -v ON_ERROR_STOP=1';
741 $aDescriptors = array(
742 0 => array('pipe', 'r'),
747 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
748 if (!is_resource($hProcess)) fail('unable to start pgsql');
750 while (strlen($sScript)) {
751 $written = fwrite($ahPipes[0], $sScript);
752 if ($written <= 0) break;
753 $sScript = substr($sScript, $written);
756 $iReturn = proc_close($hProcess);
757 if ($bfatal && $iReturn > 0) {
758 fail("pgsql returned with error code ($iReturn)");
762 function pgsqlRunPartitionScript($sTemplate)
767 $sSQL = 'select distinct partition from country_name';
768 $aPartitions = chksql($oDB->getCol($sSQL));
769 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
771 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
772 foreach ($aMatches as $aMatch) {
774 foreach ($aPartitions as $sPartitionName) {
775 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
777 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
780 pgsqlRunScript($sTemplate);
783 function pgsqlRunRestoreData($sDumpFile)
785 // Convert database DSN to psql parameters
786 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
787 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
788 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
790 $aDescriptors = array(
791 0 => array('pipe', 'r'),
792 1 => array('pipe', 'w'),
793 2 => array('file', '/dev/null', 'a')
796 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
797 if (!is_resource($hProcess)) fail('unable to start pg_restore');
801 // TODO: error checking
802 while (!feof($ahPipes[1])) {
803 echo fread($ahPipes[1], 4096);
807 $iReturn = proc_close($hProcess);
810 function pgsqlRunDropAndRestore($sDumpFile)
812 // Convert database DSN to psql parameters
813 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
814 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
815 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
817 $aDescriptors = array(
818 0 => array('pipe', 'r'),
819 1 => array('pipe', 'w'),
820 2 => array('file', '/dev/null', 'a')
823 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
824 if (!is_resource($hProcess)) fail('unable to start pg_restore');
828 // TODO: error checking
829 while (!feof($ahPipes[1])) {
830 echo fread($ahPipes[1], 4096);
834 $iReturn = proc_close($hProcess);
837 function passthruCheckReturn($cmd)
840 passthru($cmd, $result);
841 if ($result != 0) fail('Error executing external command: '.$cmd);
844 function replace_tablespace($sTemplate, $sTablespace, $sSql)
847 $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
849 $sSql = str_replace($sTemplate, '', $sSql);
855 function create_sql_functions($aCMDResult)
857 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
858 $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
859 if ($aCMDResult['enable-diff-updates']) {
860 $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
862 if ($aCMDResult['enable-debug-statements']) {
863 $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
865 if (CONST_Limit_Reindexing) {
866 $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
868 if (!CONST_Use_US_Tiger_Data) {
869 $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
871 if (!CONST_Use_Aux_Location_data) {
872 $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
874 pgsqlRunScript($sTemplate);