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('drop', '', 0, 1, 0, 0, 'bool', 'Drop tables needed for updates, making the database readonly (EXPERIMENTAL)'),
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']) {
49 if (!isset($aCMDResult['osm-file'])) {
50 fail('missing --osm-file for data import');
53 if (!file_exists($aCMDResult['osm-file'])) {
54 fail('the path supplied to --osm-file does not exist');
57 if (!is_readable($aCMDResult['osm-file'])) {
58 fail('osm-file "'.$aCMDResult['osm-file'].'" not readable');
63 // This is a pretty hard core default - the number of processors in the box - 1
64 $iInstances = isset($aCMDResult['threads'])?$aCMDResult['threads']:(getProcessorCount()-1);
65 if ($iInstances < 1) {
67 echo "WARNING: resetting threads to $iInstances\n";
69 if ($iInstances > getProcessorCount()) {
70 $iInstances = getProcessorCount();
71 echo "WARNING: resetting threads to $iInstances\n";
74 // Assume we can steal all the cache memory in the box (unless told otherwise)
75 if (isset($aCMDResult['osm2pgsql-cache'])) {
76 $iCacheMemory = $aCMDResult['osm2pgsql-cache'];
78 $iCacheMemory = getCacheMemoryMB();
81 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
82 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
84 if ($aCMDResult['create-db'] || $aCMDResult['all']) {
86 $bDidSomething = true;
87 $oDB = DB::connect(CONST_Database_DSN, false);
88 if (!PEAR::isError($oDB)) {
89 fail('database already exists ('.CONST_Database_DSN.')');
91 passthruCheckReturn('createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
94 if ($aCMDResult['setup-db'] || $aCMDResult['all']) {
96 $bDidSomething = true;
97 // TODO: path detection, detection memory, etc.
101 $fPostgresVersion = getPostgresVersion($oDB);
102 echo 'Postgres version found: '.$fPostgresVersion."\n";
104 if ($fPostgresVersion < 9.1) {
105 fail("Minimum supported version of Postgresql is 9.1.");
108 pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
109 pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
111 // For extratags and namedetails the hstore_to_json converter is
112 // needed which is only available from Postgresql 9.3+. For older
113 // versions add a dummy function that returns nothing.
114 $iNumFunc = chksql($oDB->getOne("select count(*) from pg_proc where proname = 'hstore_to_json'"));
116 if ($iNumFunc == 0) {
117 pgsqlRunScript("create function hstore_to_json(dummy hstore) returns text AS 'select null::text' language sql immutable");
118 echo "WARNING: Postgresql is too old. extratags and namedetails API not available.";
121 $fPostgisVersion = getPostgisVersion($oDB);
122 echo 'Postgis version found: '.$fPostgisVersion."\n";
124 if ($fPostgisVersion < 2.1) {
125 // Function was renamed in 2.1 and throws an annoying deprecation warning
126 pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
129 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
130 pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
131 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
132 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
133 if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz')) {
134 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz');
136 echo "WARNING: external UK postcode table not found.\n";
138 if (CONST_Use_Extra_US_Postcodes) {
139 pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
142 if ($aCMDResult['no-partitions']) {
143 pgsqlRunScript('update country_name set partition = 0');
146 // the following will be needed by create_functions later but
147 // is only defined in the subsequently called create_tables.
148 // Create dummies here that will be overwritten by the proper
149 // versions in create-tables.
150 pgsqlRunScript('CREATE TABLE place_boundingbox ()');
151 pgsqlRunScript('create type wikipedia_article_match as ()');
154 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
156 $bDidSomething = true;
158 $osm2pgsql = CONST_Osm2pgsql_Binary;
159 if (!file_exists($osm2pgsql)) {
160 echo "Please download and build osm2pgsql.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
161 fail("osm2pgsql not found in '$osm2pgsql'");
164 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
165 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
167 if (CONST_Tablespace_Osm2pgsql_Data)
168 $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
169 if (CONST_Tablespace_Osm2pgsql_Index)
170 $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
171 if (CONST_Tablespace_Place_Data)
172 $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
173 if (CONST_Tablespace_Place_Index)
174 $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
175 $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
176 $osm2pgsql .= ' -C '.$iCacheMemory;
177 $osm2pgsql .= ' -P '.$aDSNInfo['port'];
178 $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
179 passthruCheckReturn($osm2pgsql);
182 if (!chksql($oDB->getRow('select * from place limit 1'))) {
187 if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
189 $bDidSomething = true;
190 if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) fail("nominatim module not built");
191 create_sql_functions($aCMDResult);
194 if ($aCMDResult['create-tables'] || $aCMDResult['all']) {
195 $bDidSomething = true;
198 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
199 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
200 $sTemplate = replace_tablespace(
202 CONST_Tablespace_Address_Data,
205 $sTemplate = replace_tablespace(
206 '{ts:address-index}',
207 CONST_Tablespace_Address_Index,
210 $sTemplate = replace_tablespace(
212 CONST_Tablespace_Search_Data,
215 $sTemplate = replace_tablespace(
217 CONST_Tablespace_Search_Index,
220 $sTemplate = replace_tablespace(
222 CONST_Tablespace_Aux_Data,
225 $sTemplate = replace_tablespace(
227 CONST_Tablespace_Aux_Index,
230 pgsqlRunScript($sTemplate, false);
232 // re-run the functions
234 create_sql_functions($aCMDResult);
237 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all']) {
238 echo "Partition Tables\n";
239 $bDidSomething = true;
241 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
242 $sTemplate = replace_tablespace(
244 CONST_Tablespace_Address_Data,
247 $sTemplate = replace_tablespace(
248 '{ts:address-index}',
249 CONST_Tablespace_Address_Index,
252 $sTemplate = replace_tablespace(
254 CONST_Tablespace_Search_Data,
257 $sTemplate = replace_tablespace(
259 CONST_Tablespace_Search_Index,
262 $sTemplate = replace_tablespace(
264 CONST_Tablespace_Aux_Data,
267 $sTemplate = replace_tablespace(
269 CONST_Tablespace_Aux_Index,
273 pgsqlRunPartitionScript($sTemplate);
277 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all']) {
278 echo "Partition Functions\n";
279 $bDidSomething = true;
281 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
283 pgsqlRunPartitionScript($sTemplate);
286 if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all']) {
287 $bDidSomething = true;
288 $sWikiArticlesFile = CONST_BasePath.'/data/wikipedia_article.sql.bin';
289 $sWikiRedirectsFile = CONST_BasePath.'/data/wikipedia_redirect.sql.bin';
290 if (file_exists($sWikiArticlesFile)) {
291 echo "Importing wikipedia articles...";
292 pgsqlRunDropAndRestore($sWikiArticlesFile);
295 echo "WARNING: wikipedia article dump file not found - places will have default importance\n";
297 if (file_exists($sWikiRedirectsFile)) {
298 echo "Importing wikipedia redirects...";
299 pgsqlRunDropAndRestore($sWikiRedirectsFile);
302 echo "WARNING: wikipedia redirect dump file not found - some place importance values may be missing\n";
307 if ($aCMDResult['load-data'] || $aCMDResult['all']) {
308 echo "Drop old Data\n";
309 $bDidSomething = true;
312 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
314 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
316 if (!pg_query($oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($oDB->connection));
318 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
320 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
322 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
324 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
326 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
328 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
330 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
333 $sSQL = 'select distinct partition from country_name';
334 $aPartitions = chksql($oDB->getCol($sSQL));
335 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
336 foreach ($aPartitions as $sPartition) {
337 if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection));
341 // used by getorcreate_word_id to ignore frequent partial words
342 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));
345 // pre-create the word list
346 if (!$aCMDResult['disable-token-precalc']) {
347 echo "Loading word list\n";
348 pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
352 $aDBInstances = array();
353 $iLoadThreads = max(1, $iInstances - 1);
354 for ($i = 0; $i < $iLoadThreads; $i++) {
355 $aDBInstances[$i] =& getDB(true);
356 $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
357 $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
358 $sSQL .= 'geometry) select * from place where osm_id % '.$iLoadThreads.' = '.$i;
359 $sSQL .= " and not (class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString')";
360 if ($aCMDResult['verbose']) echo "$sSQL\n";
361 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
363 // last thread for interpolation lines
364 $aDBInstances[$iLoadThreads] =& getDB(true);
365 $sSQL = 'select insert_osmline (osm_id, housenumber, street, addr_place, postcode, country_code, ';
366 $sSQL .= 'geometry) from place where ';
367 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
368 if ($aCMDResult['verbose']) echo "$sSQL\n";
369 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
374 for ($i = 0; $i <= $iLoadThreads; $i++) {
375 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
381 echo "Reanalysing database...\n";
382 pgsqlRunScript('ANALYSE');
385 if ($aCMDResult['import-tiger-data']) {
386 $bDidSomething = true;
388 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
389 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
390 $sTemplate = replace_tablespace(
392 CONST_Tablespace_Aux_Data,
395 $sTemplate = replace_tablespace(
397 CONST_Tablespace_Aux_Index,
400 pgsqlRunScript($sTemplate, false);
402 $aDBInstances = array();
403 for ($i = 0; $i < $iInstances; $i++) {
404 $aDBInstances[$i] =& getDB(true);
407 foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
409 $hFile = fopen($sFile, "r");
410 $sSQL = fgets($hFile, 100000);
414 for ($i = 0; $i < $iInstances; $i++) {
415 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
416 while (pg_get_result($aDBInstances[$i]->connection));
417 $sSQL = fgets($hFile, 100000);
419 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
421 if ($iLines == 1000) {
435 for ($i = 0; $i < $iInstances; $i++) {
436 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
443 echo "Creating indexes\n";
444 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
445 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
446 $sTemplate = replace_tablespace(
448 CONST_Tablespace_Aux_Data,
451 $sTemplate = replace_tablespace(
453 CONST_Tablespace_Aux_Index,
456 pgsqlRunScript($sTemplate, false);
459 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) {
460 $bDidSomething = true;
462 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
463 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
464 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,calculated_country_code,";
465 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select calculated_country_code,postcode,";
466 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
467 $sSQL .= "from placex where postcode is not null group by calculated_country_code,postcode) as x";
468 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
470 if (CONST_Use_Extra_US_Postcodes) {
471 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
472 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
473 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
474 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
478 if ($aCMDResult['osmosis-init'] || ($aCMDResult['all'] && !$aCMDResult['drop'])) { // no use doing osmosis-init when dropping update tables
479 $bDidSomething = true;
482 if (!file_exists(CONST_Osmosis_Binary)) {
483 echo "Please download osmosis.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
484 if (!$aCMDResult['all']) {
485 fail("osmosis not found in '".CONST_Osmosis_Binary."'");
488 if (file_exists(CONST_InstallPath.'/settings/configuration.txt')) {
489 echo "settings/configuration.txt already exists\n";
491 passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_InstallPath.'/settings');
492 // update osmosis configuration.txt with our settings
493 passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_InstallPath.'/settings/configuration.txt');
494 passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_InstallPath.'/settings/configuration.txt');
497 // Find the last node in the DB
498 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
500 // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed)
501 $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1";
502 $sLastNodeXML = file_get_contents($sLastNodeURL);
503 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);
504 $iLastNodeTimestamp = strtotime($aLastNodeDate[1]) - (3*60*60);
506 // Search for the correct state file - uses file timestamps so need to sort by date descending
507 $sRepURL = CONST_Replication_Url."/";
508 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
509 // download.geofabrik.de: <a href="000/">000/</a></td><td align="right">26-Feb-2013 11:53 </td>
510 // planet.openstreetmap.org: <a href="273/">273/</a> 2013-03-11 07:41 -
511 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);
513 $aPrevRepMatch = false;
514 foreach ($aRepMatches as $aRepMatch) {
515 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
516 $aPrevRepMatch = $aRepMatch;
518 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
520 $sRepURL .= $aRepMatch[1];
521 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
522 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);
523 $aPrevRepMatch = false;
524 foreach ($aRepMatches as $aRepMatch) {
525 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
526 $aPrevRepMatch = $aRepMatch;
528 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
530 $sRepURL .= $aRepMatch[1];
531 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
532 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);
533 $aPrevRepMatch = false;
534 foreach ($aRepMatches as $aRepMatch) {
535 if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
536 $aPrevRepMatch = $aRepMatch;
538 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
540 $sRepURL .= $aRepMatch[1].'.state.txt';
541 echo "Getting state file: $sRepURL\n";
542 $sStateFile = file_get_contents($sRepURL);
543 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
544 file_put_contents(CONST_InstallPath.'/settings/state.txt', $sStateFile);
545 echo "Updating DB status\n";
546 pg_query($oDB->connection, 'TRUNCATE import_status');
547 $sSQL = "INSERT INTO import_status VALUES('".$aRepMatch[2]."')";
548 pg_query($oDB->connection, $sSQL);
550 if (!$aCMDResult['all']) {
551 fail("Cannot read state file directory.");
557 if ($aCMDResult['index'] || $aCMDResult['all']) {
558 $bDidSomething = true;
560 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
561 passthruCheckReturn($sBaseCmd.' -R 4');
562 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
563 passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
564 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
565 passthruCheckReturn($sBaseCmd.' -r 26');
568 if ($aCMDResult['create-search-indices'] || $aCMDResult['all']) {
569 echo "Search indices\n";
570 $bDidSomething = true;
572 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
573 $sTemplate = replace_tablespace(
574 '{ts:address-index}',
575 CONST_Tablespace_Address_Index,
578 $sTemplate = replace_tablespace(
580 CONST_Tablespace_Search_Index,
583 $sTemplate = replace_tablespace(
585 CONST_Tablespace_Aux_Index,
589 pgsqlRunScript($sTemplate);
592 if ($aCMDResult['drop']) {
593 // The implementation is potentially a bit dangerous because it uses
594 // a positive selection of tables to keep, and deletes everything else.
595 // Including any tables that the unsuspecting user might have manually
596 // created. USE AT YOUR OWN PERIL.
597 $bDidSomething = true;
599 // tables we want to keep. everything else goes.
600 $aKeepTables = array(
605 "location_property*",
619 $aDropTables = array();
620 $aHaveTables = chksql($oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
622 foreach ($aHaveTables as $sTable) {
624 foreach ($aKeepTables as $sKeep) {
625 if (fnmatch($sKeep, $sTable)) {
630 if (!$bFound) array_push($aDropTables, $sTable);
633 foreach ($aDropTables as $sDrop) {
634 if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
635 @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
636 // ignore warnings/errors as they might be caused by a table having
637 // been deleted already by CASCADE
640 if (!is_null(CONST_Osm2pgsql_Flatnode_File)) {
641 if ($aCMDResult['verbose']) echo "deleting ".CONST_Osm2pgsql_Flatnode_File."\n";
642 unlink(CONST_Osm2pgsql_Flatnode_File);
646 if (!$bDidSomething) {
647 showUsage($aCMDOptions, true);
649 echo "Setup finished.\n";
652 function pgsqlRunScriptFile($sFilename)
654 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
656 // Convert database DSN to psql parameters
657 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
658 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
659 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
662 if (preg_match('/\\.gz$/', $sFilename)) {
663 $aDescriptors = array(
664 0 => array('pipe', 'r'),
665 1 => array('pipe', 'w'),
666 2 => array('file', '/dev/null', 'a')
668 $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
669 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
670 $aReadPipe = $ahGzipPipes[1];
671 fclose($ahGzipPipes[0]);
673 $sCMD .= ' -f '.$sFilename;
674 $aReadPipe = array('pipe', 'r');
677 $aDescriptors = array(
679 1 => array('pipe', 'w'),
680 2 => array('file', '/dev/null', 'a')
683 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
684 if (!is_resource($hProcess)) fail('unable to start pgsql');
687 // TODO: error checking
688 while (!feof($ahPipes[1])) {
689 echo fread($ahPipes[1], 4096);
693 $iReturn = proc_close($hProcess);
695 fail("pgsql returned with error code ($iReturn)");
698 fclose($ahGzipPipes[1]);
699 proc_close($hGzipProcess);
704 function pgsqlRunScript($sScript, $bfatal = true)
707 // Convert database DSN to psql parameters
708 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
709 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
710 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
711 if ($bfatal && !$aCMDResult['ignore-errors'])
712 $sCMD .= ' -v ON_ERROR_STOP=1';
713 $aDescriptors = array(
714 0 => array('pipe', 'r'),
719 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
720 if (!is_resource($hProcess)) fail('unable to start pgsql');
722 while (strlen($sScript)) {
723 $written = fwrite($ahPipes[0], $sScript);
724 if ($written <= 0) break;
725 $sScript = substr($sScript, $written);
728 $iReturn = proc_close($hProcess);
729 if ($bfatal && $iReturn > 0) {
730 fail("pgsql returned with error code ($iReturn)");
734 function pgsqlRunPartitionScript($sTemplate)
739 $sSQL = 'select distinct partition from country_name';
740 $aPartitions = chksql($oDB->getCol($sSQL));
741 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
743 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
744 foreach ($aMatches as $aMatch) {
746 foreach ($aPartitions as $sPartitionName) {
747 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
749 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
752 pgsqlRunScript($sTemplate);
755 function pgsqlRunRestoreData($sDumpFile)
757 // Convert database DSN to psql parameters
758 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
759 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
760 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
762 $aDescriptors = array(
763 0 => array('pipe', 'r'),
764 1 => array('pipe', 'w'),
765 2 => array('file', '/dev/null', 'a')
768 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
769 if (!is_resource($hProcess)) fail('unable to start pg_restore');
773 // TODO: error checking
774 while (!feof($ahPipes[1])) {
775 echo fread($ahPipes[1], 4096);
779 $iReturn = proc_close($hProcess);
782 function pgsqlRunDropAndRestore($sDumpFile)
784 // Convert database DSN to psql parameters
785 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
786 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
787 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
789 $aDescriptors = array(
790 0 => array('pipe', 'r'),
791 1 => array('pipe', 'w'),
792 2 => array('file', '/dev/null', 'a')
795 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
796 if (!is_resource($hProcess)) fail('unable to start pg_restore');
800 // TODO: error checking
801 while (!feof($ahPipes[1])) {
802 echo fread($ahPipes[1], 4096);
806 $iReturn = proc_close($hProcess);
809 function passthruCheckReturn($cmd)
812 passthru($cmd, $result);
813 if ($result != 0) fail('Error executing external command: '.$cmd);
816 function replace_tablespace($sTemplate, $sTablespace, $sSql)
819 $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
821 $sSql = str_replace($sTemplate, '', $sSql);
827 function create_sql_functions($aCMDResult)
829 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
830 $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
831 if ($aCMDResult['enable-diff-updates']) {
832 $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
834 if ($aCMDResult['enable-debug-statements']) {
835 $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
837 if (CONST_Limit_Reindexing) {
838 $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
840 if (!CONST_Use_US_Tiger_Data) {
841 $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
843 if (!CONST_Use_Aux_Location_data) {
844 $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
846 pgsqlRunScript($sTemplate);