4         require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
 
   5         require_once(CONST_BasePath.'/lib/init-cmd.php');
 
   6         ini_set('memory_limit', '800M');
 
   9                 "Create and setup nominatim search system",
 
  10                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
 
  11                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
 
  12                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
 
  14                 array('osm-file', '', 0, 1, 1, 1, 'realpath', 'File to import'),
 
  15                 array('threads', '', 0, 1, 1, 1, 'int', 'Number of threads (where possible)'),
 
  17                 array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
 
  19                 array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
 
  20                 array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
 
  21                 array('import-data', '', 0, 1, 0, 0, 'bool', 'Import a osm file'),
 
  22                 array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
 
  23                 array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
 
  24                 array('enable-diff-updates', '', 0, 1, 0, 0, 'bool', 'Turn on the code required to make diff updates work'),
 
  25                 array('enable-debug-statements', '', 0, 1, 0, 0, 'bool', 'Include debug warning statements in pgsql commands'),
 
  26                 array('ignore-errors', '', 0, 1, 0, 0, 'bool', 'Continue import even when errors in SQL are present (EXPERT)'),
 
  27                 array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
 
  28                 array('create-partition-tables', '', 0, 1, 0, 0, 'bool', 'Create required partition tables'),
 
  29                 array('create-partition-functions', '', 0, 1, 0, 0, 'bool', 'Create required partition triggers'),
 
  30                 array('no-partitions', '', 0, 1, 0, 0, 'bool', "Do not partition search indices (speeds up import of single country extracts)"),
 
  31                 array('import-wikipedia-articles', '', 0, 1, 0, 0, 'bool', 'Import wikipedia article dump'),
 
  32                 array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
 
  33                 array('disable-token-precalc', '', 0, 1, 0, 0, 'bool', 'Disable name precalculation (EXPERT)'),
 
  34                 array('import-tiger-data', '', 0, 1, 0, 0, 'bool', 'Import tiger data (not included in \'all\')'),
 
  35                 array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
 
  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('create-search-indices', '', 0, 1, 0, 0, 'bool', 'Create additional indices required for search and update'),
 
  40                 array('create-website', '', 0, 1, 1, 1, 'realpath', 'Create symlinks to setup web directory'),
 
  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'])
 
  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         if (isset($aCMDResult['osm2pgsql-cache']))
 
  83                 $iCacheMemory = $aCMDResult['osm2pgsql-cache'];
 
  87                 $iCacheMemory = getCacheMemoryMB();
 
  90         $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
  91         if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
 
  93         $fPostgisVersion = (float) CONST_Postgis_Version;
 
  95         if ($aCMDResult['create-db'] || $aCMDResult['all'])
 
  98                 $bDidSomething = true;
 
  99                 $oDB =& DB::connect(CONST_Database_DSN, false);
 
 100                 if (!PEAR::isError($oDB))
 
 102                         fail('database already exists ('.CONST_Database_DSN.')');
 
 104                 passthruCheckReturn('createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
 
 107         if ($aCMDResult['setup-db'] || $aCMDResult['all'])
 
 110                 $bDidSomething = true;
 
 111                 // TODO: path detection, detection memory, etc.
 
 115                 $sVersionString = $oDB->getOne('select version()');
 
 116                 preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[^0-9]#', $sVersionString, $aMatches);
 
 117                 if (CONST_Postgresql_Version != $aMatches[1].'.'.$aMatches[2])
 
 119                         echo "ERROR: PostgreSQL version is not correct.  Expected ".CONST_Postgresql_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n";
 
 123                 passthru('createlang plpgsql -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
 
 124                 $pgver = (float) CONST_Postgresql_Version;
 
 126                         pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/hstore.sql');
 
 127                         pgsqlRunScriptFile(CONST_BasePath.'/sql/hstore_compatability_9_0.sql');
 
 129                         pgsqlRunScript('CREATE EXTENSION hstore');
 
 132                 if ($fPostgisVersion < 2.0) {
 
 133                         pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql');
 
 134                         pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql');
 
 136                         pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
 
 138                 if ($fPostgisVersion < 2.1) {
 
 139                         // Function was renamed in 2.1 and throws an annoying deprecation warning
 
 140                         pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
 
 142                 $sVersionString = $oDB->getOne('select postgis_full_version()');
 
 143                 preg_match('#POSTGIS="([0-9]+)[.]([0-9]+)[.]([0-9]+)( r([0-9]+))?"#', $sVersionString, $aMatches);
 
 144                 if (CONST_Postgis_Version != $aMatches[1].'.'.$aMatches[2])
 
 146                         echo "ERROR: PostGIS version is not correct.  Expected ".CONST_Postgis_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n";
 
 150                 pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
 
 151                 pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
 
 152                 pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql');
 
 153                 pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
 
 154                 if (file_exists(CONST_BasePath.'/data/gb_postcode_data.sql.gz'))
 
 156                         pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_data.sql.gz');
 
 160                         echo "WARNING: external UK postcode table not found.\n";
 
 162                 pgsqlRunScriptFile(CONST_BasePath.'/data/us_statecounty.sql');
 
 163                 pgsqlRunScriptFile(CONST_BasePath.'/data/us_state.sql');
 
 164                 pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
 
 166                 if ($aCMDResult['no-partitions'])
 
 168                         pgsqlRunScript('update country_name set partition = 0');
 
 171                 // the following will be needed by create_functions later but
 
 172                 // is only defined in the subsequently called create_tables.
 
 173                 // Create dummies here that will be overwritten by the proper
 
 174                 // versions in create-tables.
 
 175                 pgsqlRunScript('CREATE TABLE place_boundingbox ()');
 
 176                 pgsqlRunScript('create type wikipedia_article_match as ()');
 
 179         if ($aCMDResult['import-data'] || $aCMDResult['all'])
 
 182                 $bDidSomething = true;
 
 184                 $osm2pgsql = CONST_Osm2pgsql_Binary;
 
 185                 if (!file_exists($osm2pgsql))
 
 187                         echo "Please download and build osm2pgsql.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
 
 188                         fail("osm2pgsql not found in '$osm2pgsql'");
 
 191                 if (!is_null(CONST_Osm2pgsql_Flatnode_File))
 
 193                         $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
 
 195                 if (CONST_Tablespace_Osm2pgsql_Data)
 
 196                         $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
 
 197                 if (CONST_Tablespace_Osm2pgsql_Index)
 
 198                         $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
 
 199                 if (CONST_Tablespace_Place_Data)
 
 200                         $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
 
 201                 if (CONST_Tablespace_Place_Index)
 
 202                         $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
 
 203                 $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
 
 204                 $osm2pgsql .= ' -C '.$iCacheMemory;
 
 205                 $osm2pgsql .= ' -P '.$aDSNInfo['port'];
 
 206                 $osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
 
 207                 passthruCheckReturn($osm2pgsql);
 
 210                 $x = $oDB->getRow('select * from place limit 1');
 
 211                 if (PEAR::isError($x)) {
 
 212                         fail($x->getMessage());
 
 214                 if (!$x) fail('No Data');
 
 217         if ($aCMDResult['create-functions'] || $aCMDResult['all'])
 
 220                 $bDidSomething = true;
 
 221                 if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) fail("nominatim module not built");
 
 222                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
 
 223                 $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
 
 224                 if ($aCMDResult['enable-diff-updates']) $sTemplate = str_replace('RETURN NEW; -- @DIFFUPDATES@', '--', $sTemplate);
 
 225                 if ($aCMDResult['enable-debug-statements']) $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
 
 226                 if (CONST_Limit_Reindexing) $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
 
 227                 pgsqlRunScript($sTemplate);
 
 229                 if ($fPostgisVersion < 2.0) {
 
 230                         echo "Helper functions for postgis < 2.0\n";
 
 231                         $sTemplate = file_get_contents(CONST_BasePath.'/sql/postgis_15_aux.sql');
 
 233                         echo "Helper functions for postgis >= 2.0\n";
 
 234                         $sTemplate = file_get_contents(CONST_BasePath.'/sql/postgis_20_aux.sql');
 
 236                 pgsqlRunScript($sTemplate);
 
 239         if ($aCMDResult['create-tables'] || $aCMDResult['all'])
 
 241                 $bDidSomething = true;
 
 244                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
 
 245                 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
 
 246                 $sTemplate = replace_tablespace('{ts:address-data}',
 
 247                                                 CONST_Tablespace_Address_Data, $sTemplate);
 
 248                 $sTemplate = replace_tablespace('{ts:address-index}',
 
 249                                                 CONST_Tablespace_Address_Index, $sTemplate);
 
 250                 $sTemplate = replace_tablespace('{ts:search-data}',
 
 251                                                 CONST_Tablespace_Search_Data, $sTemplate);
 
 252                 $sTemplate = replace_tablespace('{ts:search-index}',
 
 253                                                 CONST_Tablespace_Search_Index, $sTemplate);
 
 254                 $sTemplate = replace_tablespace('{ts:aux-data}',
 
 255                                                 CONST_Tablespace_Aux_Data, $sTemplate);
 
 256                 $sTemplate = replace_tablespace('{ts:aux-index}',
 
 257                                                 CONST_Tablespace_Aux_Index, $sTemplate);
 
 258                 pgsqlRunScript($sTemplate, false);
 
 260                 // re-run the functions
 
 262                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
 
 263                 $sTemplate = str_replace('{modulepath}',
 
 264                                              CONST_InstallPath.'/module', $sTemplate);
 
 265                 pgsqlRunScript($sTemplate);
 
 268         if ($aCMDResult['create-partition-tables'] || $aCMDResult['all'])
 
 270                 echo "Partition Tables\n";
 
 271                 $bDidSomething = true;
 
 273                 $sSQL = 'select distinct partition from country_name';
 
 274                 $aPartitions = $oDB->getCol($sSQL);
 
 275                 if (PEAR::isError($aPartitions))
 
 277                         fail($aPartitions->getMessage());
 
 279                 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
 
 281                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
 
 282                 $sTemplate = replace_tablespace('{ts:address-data}',
 
 283                                                 CONST_Tablespace_Address_Data, $sTemplate);
 
 284                 $sTemplate = replace_tablespace('{ts:address-index}',
 
 285                                                 CONST_Tablespace_Address_Index, $sTemplate);
 
 286                 $sTemplate = replace_tablespace('{ts:search-data}',
 
 287                                                 CONST_Tablespace_Search_Data, $sTemplate);
 
 288                 $sTemplate = replace_tablespace('{ts:search-index}',
 
 289                                                 CONST_Tablespace_Search_Index, $sTemplate);
 
 290                 $sTemplate = replace_tablespace('{ts:aux-data}',
 
 291                                                 CONST_Tablespace_Aux_Data, $sTemplate);
 
 292                 $sTemplate = replace_tablespace('{ts:aux-index}',
 
 293                                                 CONST_Tablespace_Aux_Index, $sTemplate);
 
 294                 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
 
 295                 foreach($aMatches as $aMatch)
 
 298                         foreach($aPartitions as $sPartitionName)
 
 300                                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
 
 302                         $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
 
 305                 pgsqlRunScript($sTemplate);
 
 309         if ($aCMDResult['create-partition-functions'] || $aCMDResult['all'])
 
 311                 echo "Partition Functions\n";
 
 312                 $bDidSomething = true;
 
 314                 $sSQL = 'select distinct partition from country_name';
 
 315                 $aPartitions = $oDB->getCol($sSQL);
 
 316                 if (PEAR::isError($aPartitions))
 
 318                         fail($aPartitions->getMessage());
 
 320                 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
 
 322                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
 
 323                 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
 
 324                 foreach($aMatches as $aMatch)
 
 327                         foreach($aPartitions as $sPartitionName)
 
 329                                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
 
 331                         $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
 
 334                 pgsqlRunScript($sTemplate);
 
 337         if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all'])
 
 339                 $bDidSomething = true;
 
 340                 $sWikiArticlesFile = CONST_BasePath.'/data/wikipedia_article.sql.bin';
 
 341                 $sWikiRedirectsFile = CONST_BasePath.'/data/wikipedia_redirect.sql.bin';
 
 342                 if (file_exists($sWikiArticlesFile))
 
 344                         echo "Importing wikipedia articles...";
 
 345                         pgsqlRunDropAndRestore($sWikiArticlesFile);
 
 350                         echo "WARNING: wikipedia article dump file not found - places will have default importance\n";
 
 352                 if (file_exists($sWikiRedirectsFile))
 
 354                         echo "Importing wikipedia redirects...";
 
 355                         pgsqlRunDropAndRestore($sWikiRedirectsFile);
 
 360                         echo "WARNING: wikipedia redirect dump file not found - some place importance values may be missing\n";
 
 365         if ($aCMDResult['load-data'] || $aCMDResult['all'])
 
 367                 echo "Drop old Data\n";
 
 368                 $bDidSomething = true;
 
 371                 if (!pg_query($oDB->connection, 'TRUNCATE word')) fail(pg_last_error($oDB->connection));
 
 373                 if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
 
 375                 if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
 
 377                 if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
 
 379                 if (!pg_query($oDB->connection, 'TRUNCATE location_area')) fail(pg_last_error($oDB->connection));
 
 381                 if (!pg_query($oDB->connection, 'TRUNCATE search_name')) fail(pg_last_error($oDB->connection));
 
 383                 if (!pg_query($oDB->connection, 'TRUNCATE search_name_blank')) fail(pg_last_error($oDB->connection));
 
 385                 if (!pg_query($oDB->connection, 'DROP SEQUENCE seq_place')) fail(pg_last_error($oDB->connection));
 
 387                 if (!pg_query($oDB->connection, 'CREATE SEQUENCE seq_place start 100000')) fail(pg_last_error($oDB->connection));
 
 390                 $sSQL = 'select distinct partition from country_name';
 
 391                 $aPartitions = $oDB->getCol($sSQL);
 
 392                 if (PEAR::isError($aPartitions))
 
 394                         fail($aPartitions->getMessage());
 
 396                 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
 
 397                 foreach($aPartitions as $sPartition)
 
 399                         if (!pg_query($oDB->connection, 'TRUNCATE location_road_'.$sPartition)) fail(pg_last_error($oDB->connection));
 
 403                 // used by getorcreate_word_id to ignore frequent partial words
 
 404                 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));
 
 407                 // pre-create the word list
 
 408                 if (!$aCMDResult['disable-token-precalc'])
 
 410                         echo "Loading word list\n";
 
 411                         pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
 
 415                 $aDBInstances = array();
 
 416                 for($i = 0; $i < $iInstances; $i++)
 
 418                         $aDBInstances[$i] =& getDB(true);
 
 419                         $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
 
 420                         $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
 
 421                         $sSQL .= 'geometry) select * from place where osm_id % '.$iInstances.' = '.$i;
 
 422                         if ($aCMDResult['verbose']) echo "$sSQL\n";
 
 423                         if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 429                         for($i = 0; $i < $iInstances; $i++)
 
 431                                 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
 
 437                 echo "Reanalysing database...\n";
 
 438                 pgsqlRunScript('ANALYSE');
 
 441         if ($aCMDResult['import-tiger-data'])
 
 443                 $bDidSomething = true;
 
 445                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
 
 446                 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
 
 447                 $sTemplate = replace_tablespace('{ts:aux-data}',
 
 448                                                 CONST_Tablespace_Aux_Data, $sTemplate);
 
 449                 $sTemplate = replace_tablespace('{ts:aux-index}',
 
 450                                                 CONST_Tablespace_Aux_Index, $sTemplate);
 
 451                 pgsqlRunScript($sTemplate, false);
 
 453                 $aDBInstances = array();
 
 454                 for($i = 0; $i < $iInstances; $i++)
 
 456                         $aDBInstances[$i] =& getDB(true);
 
 459                 foreach(glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile)
 
 462                         $hFile = fopen($sFile, "r");
 
 463                         $sSQL = fgets($hFile, 100000);
 
 468                                 for($i = 0; $i < $iInstances; $i++)
 
 470                                         if (!pg_connection_busy($aDBInstances[$i]->connection))
 
 472                                                 while(pg_get_result($aDBInstances[$i]->connection));
 
 473                                                 $sSQL = fgets($hFile, 100000);
 
 475                                                 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 493                                 for($i = 0; $i < $iInstances; $i++)
 
 495                                         if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
 
 502                 echo "Creating indexes\n";
 
 503                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
 
 504                 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
 
 505                 $sTemplate = replace_tablespace('{ts:aux-data}',
 
 506                                                 CONST_Tablespace_Aux_Data, $sTemplate);
 
 507                 $sTemplate = replace_tablespace('{ts:aux-index}',
 
 508                                                 CONST_Tablespace_Aux_Index, $sTemplate);
 
 509                 pgsqlRunScript($sTemplate, false);
 
 512         if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all'])
 
 514                 $bDidSomething = true;
 
 516                 if (!pg_query($oDB->connection, 'DELETE from placex where osm_type=\'P\'')) fail(pg_last_error($oDB->connection));
 
 517                 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
 
 518                 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,calculated_country_code,";
 
 519                 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from (select calculated_country_code,postcode,";
 
 520                 $sSQL .= "avg(st_x(st_centroid(geometry))) as x,avg(st_y(st_centroid(geometry))) as y ";
 
 521                 $sSQL .= "from placex where postcode is not null group by calculated_country_code,postcode) as x";
 
 522                 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 524                 $sSQL = "insert into placex (osm_type,osm_id,class,type,postcode,calculated_country_code,geometry) ";
 
 525                 $sSQL .= "select 'P',nextval('seq_postcodes'),'place','postcode',postcode,'us',";
 
 526                 $sSQL .= "ST_SetSRID(ST_Point(x,y),4326) as geometry from us_postcode";
 
 527                 if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
 
 530         if ($aCMDResult['osmosis-init'] || ($aCMDResult['all'] && !$aCMDResult['drop'])) // no use doing osmosis-init when dropping update tables
 
 532                 $bDidSomething = true;
 
 535                 if (!file_exists(CONST_Osmosis_Binary))
 
 537                         echo "Please download osmosis.\nIf it is already installed, check the path in your local settings (settings/local.php) file.\n";
 
 538                         if (!$aCMDResult['all'])
 
 540                                 fail("osmosis not found in '".CONST_Osmosis_Binary."'");
 
 545                         if (file_exists(CONST_InstallPath.'/settings/configuration.txt'))
 
 547                                 echo "settings/configuration.txt already exists\n";
 
 551                                 passthru(CONST_Osmosis_Binary.' --read-replication-interval-init '.CONST_InstallPath.'/settings');
 
 552                                 // update osmosis configuration.txt with our settings
 
 553                                 passthru("sed -i 's!baseUrl=.*!baseUrl=".CONST_Replication_Url."!' ".CONST_InstallPath.'/settings/configuration.txt');
 
 554                                 passthru("sed -i 's:maxInterval = .*:maxInterval = ".CONST_Replication_MaxInterval.":' ".CONST_InstallPath.'/settings/configuration.txt');
 
 557                         // Find the last node in the DB
 
 558                         $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
 
 560                         // Lookup the timestamp that node was created (less 3 hours for margin for changsets to be closed)
 
 561                         $sLastNodeURL = 'http://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID."/1";
 
 562                         $sLastNodeXML = file_get_contents($sLastNodeURL);
 
 563                         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);
 
 564                         $iLastNodeTimestamp = strtotime($aLastNodeDate[1]) - (3*60*60);
 
 566                         // Search for the correct state file - uses file timestamps so need to sort by date descending
 
 567                         $sRepURL = CONST_Replication_Url."/";
 
 568                         $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
 
 569                         // download.geofabrik.de:    <a href="000/">000/</a></td><td align="right">26-Feb-2013 11:53  </td>
 
 570                         // planet.openstreetmap.org: <a href="273/">273/</a>                    2013-03-11 07:41    -
 
 571                         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);
 
 574                                 $aPrevRepMatch = false;
 
 575                                 foreach($aRepMatches as $aRepMatch)
 
 577                                         if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
 
 578                                         $aPrevRepMatch = $aRepMatch;
 
 580                                 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
 
 582                                 $sRepURL .= $aRepMatch[1];
 
 583                                 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
 
 584                                 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);
 
 585                                 $aPrevRepMatch = false;
 
 586                                 foreach($aRepMatches as $aRepMatch)
 
 588                                         if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
 
 589                                         $aPrevRepMatch = $aRepMatch;
 
 591                                 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
 
 593                                 $sRepURL .= $aRepMatch[1];
 
 594                                 $sRep = file_get_contents($sRepURL."?C=M;O=D;F=1");
 
 595                                 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);
 
 596                                 $aPrevRepMatch = false;
 
 597                                 foreach($aRepMatches as $aRepMatch)
 
 599                                         if (strtotime($aRepMatch[2]) < $iLastNodeTimestamp) break;
 
 600                                         $aPrevRepMatch = $aRepMatch;
 
 602                                 if ($aPrevRepMatch) $aRepMatch = $aPrevRepMatch;
 
 604                                 $sRepURL .= $aRepMatch[1].'.state.txt';
 
 605                                 echo "Getting state file: $sRepURL\n";
 
 606                                 $sStateFile = file_get_contents($sRepURL);
 
 607                                 if (!$sStateFile || strlen($sStateFile) > 1000) fail("unable to obtain state file");
 
 608                                 file_put_contents(CONST_InstallPath.'/settings/state.txt', $sStateFile);
 
 609                                 echo "Updating DB status\n";
 
 610                                 pg_query($oDB->connection, 'TRUNCATE import_status');
 
 611                                 $sSQL = "INSERT INTO import_status VALUES('".$aRepMatch[2]."')";
 
 612                                 pg_query($oDB->connection, $sSQL);
 
 616                                 if (!$aCMDResult['all'])
 
 618                                         fail("Cannot read state file directory.");
 
 624         if ($aCMDResult['index'] || $aCMDResult['all'])
 
 626                 $bDidSomething = true;
 
 628                 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
 
 629                 passthruCheckReturn($sBaseCmd.' -R 4');
 
 630                 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
 
 631                 passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
 
 632                 if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
 
 633                 passthruCheckReturn($sBaseCmd.' -r 26');
 
 636         if ($aCMDResult['create-search-indices'] || $aCMDResult['all'])
 
 638                 echo "Search indices\n";
 
 639                 $bDidSomething = true;
 
 641                 $sSQL = 'select distinct partition from country_name';
 
 642                 $aPartitions = $oDB->getCol($sSQL);
 
 643                 if (PEAR::isError($aPartitions))
 
 645                         fail($aPartitions->getMessage());
 
 647                 if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
 
 649                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
 
 650                 $sTemplate = replace_tablespace('{ts:address-index}',
 
 651                                                 CONST_Tablespace_Address_Index, $sTemplate);
 
 652                 $sTemplate = replace_tablespace('{ts:search-index}',
 
 653                                                 CONST_Tablespace_Search_Index, $sTemplate);
 
 654                 $sTemplate = replace_tablespace('{ts:aux-index}',
 
 655                                                 CONST_Tablespace_Aux_Index, $sTemplate);
 
 656                 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
 
 657                 foreach($aMatches as $aMatch)
 
 660                         foreach($aPartitions as $sPartitionName)
 
 662                                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
 
 664                         $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
 
 667                 pgsqlRunScript($sTemplate);
 
 670         if (isset($aCMDResult['create-website']))
 
 672                 $bDidSomething = true;
 
 673                 $sTargetDir = $aCMDResult['create-website'];
 
 674                 if (!is_dir($sTargetDir))
 
 676                         echo "You must create the website directory before calling this function.\n";
 
 677                         fail("Target directory does not exist.");
 
 680                 @symlink(CONST_InstallPath.'/website/details.php', $sTargetDir.'/details.php');
 
 681                 @symlink(CONST_InstallPath.'/website/reverse.php', $sTargetDir.'/reverse.php');
 
 682                 @symlink(CONST_InstallPath.'/website/search.php', $sTargetDir.'/search.php');
 
 683                 @symlink(CONST_InstallPath.'/website/search.php', $sTargetDir.'/index.php');
 
 684                 @symlink(CONST_InstallPath.'/website/lookup.php', $sTargetDir.'/lookup.php');
 
 685                 @symlink(CONST_InstallPath.'/website/deletable.php', $sTargetDir.'/deletable.php');
 
 686                 @symlink(CONST_InstallPath.'/website/polygons.php', $sTargetDir.'/polygons.php');
 
 687                 @symlink(CONST_InstallPath.'/website/status.php', $sTargetDir.'/status.php');
 
 688                 @symlink(CONST_BasePath.'/website/images', $sTargetDir.'/images');
 
 689                 @symlink(CONST_BasePath.'/website/js', $sTargetDir.'/js');
 
 690                 @symlink(CONST_BasePath.'/website/css', $sTargetDir.'/css');
 
 691                 echo "Symlinks created\n";
 
 693                 $sTestFile = @file_get_contents(CONST_Website_BaseURL.'js/tiles.js');
 
 696                         echo "\nWARNING: Unable to access the website at ".CONST_Website_BaseURL."\n";
 
 697                         echo "You may want to update settings/local.php with @define('CONST_Website_BaseURL', 'http://[HOST]/[PATH]/');\n";
 
 701         if ($aCMDResult['drop'])
 
 703                 // The implementation is potentially a bit dangerous because it uses
 
 704                 // a positive selection of tables to keep, and deletes everything else.
 
 705                 // Including any tables that the unsuspecting user might have manually
 
 706                 // created. USE AT YOUR OWN PERIL.
 
 707                 $bDidSomething = true;
 
 709                 // tables we want to keep. everything else goes.
 
 710                 $aKeepTables = array(
 
 715                    "location_property*",
 
 729                 $aDropTables = array();
 
 730                 $aHaveTables = $oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'");
 
 731                 if (PEAR::isError($aHaveTables))
 
 733                         fail($aPartitions->getMessage());
 
 735                 foreach($aHaveTables as $sTable)
 
 738                         foreach ($aKeepTables as $sKeep)
 
 740                                 if (fnmatch($sKeep, $sTable))
 
 746                         if (!$bFound) array_push($aDropTables, $sTable);
 
 749                 foreach ($aDropTables as $sDrop)
 
 751                         if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
 
 752                         @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
 
 753                         // ignore warnings/errors as they might be caused by a table having
 
 754                         // been deleted already by CASCADE
 
 757                 if (!is_null(CONST_Osm2pgsql_Flatnode_File))
 
 759                         if ($aCMDResult['verbose']) echo "deleting ".CONST_Osm2pgsql_Flatnode_File."\n";
 
 760                         unlink(CONST_Osm2pgsql_Flatnode_File);
 
 766                 showUsage($aCMDOptions, true);
 
 770                 echo "Setup finished.\n";
 
 773         function pgsqlRunScriptFile($sFilename)
 
 775                 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
 
 777                 // Convert database DSN to psql parameters
 
 778                 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
 779                 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
 
 780                 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
 
 783                 if (preg_match('/\\.gz$/', $sFilename))
 
 785                         $aDescriptors = array(
 
 786                                 0 => array('pipe', 'r'),
 
 787                                 1 => array('pipe', 'w'),
 
 788                                 2 => array('file', '/dev/null', 'a')
 
 790                         $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
 
 791                         if (!is_resource($hGzipProcess)) fail('unable to start zcat');
 
 792                         $aReadPipe = $ahGzipPipes[1];
 
 793                         fclose($ahGzipPipes[0]);
 
 797                         $sCMD .= ' -f '.$sFilename;
 
 798                         $aReadPipe = array('pipe', 'r');
 
 801                 $aDescriptors = array(
 
 803                         1 => array('pipe', 'w'),
 
 804                         2 => array('file', '/dev/null', 'a')
 
 807                 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
 
 808                 if (!is_resource($hProcess)) fail('unable to start pgsql');
 
 811                 // TODO: error checking
 
 812                 while(!feof($ahPipes[1]))
 
 814                         echo fread($ahPipes[1], 4096);
 
 818                 $iReturn = proc_close($hProcess);
 
 821                         fail("pgsql returned with error code ($iReturn)");
 
 825                         fclose($ahGzipPipes[1]);
 
 826                         proc_close($hGzipProcess);
 
 831         function pgsqlRunScript($sScript, $bfatal = true)
 
 834                 // Convert database DSN to psql parameters
 
 835                 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
 836                 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
 
 837                 $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
 
 838                 if ($bfatal && !$aCMDResult['ignore-errors'])
 
 839                         $sCMD .= ' -v ON_ERROR_STOP=1';
 
 840                 $aDescriptors = array(
 
 841                         0 => array('pipe', 'r'),
 
 846                 $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
 
 847                 if (!is_resource($hProcess)) fail('unable to start pgsql');
 
 849                 while(strlen($sScript))
 
 851                         $written = fwrite($ahPipes[0], $sScript);
 
 852                         if ($written <= 0) break;
 
 853                         $sScript = substr($sScript, $written);
 
 856                 $iReturn = proc_close($hProcess);
 
 857                 if ($bfatal && $iReturn > 0)
 
 859                         fail("pgsql returned with error code ($iReturn)");
 
 863         function pgsqlRunRestoreData($sDumpFile)
 
 865                 // Convert database DSN to psql parameters
 
 866                 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
 867                 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
 
 868                 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
 
 870                 $aDescriptors = array(
 
 871                         0 => array('pipe', 'r'),
 
 872                         1 => array('pipe', 'w'),
 
 873                         2 => array('file', '/dev/null', 'a')
 
 876                 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
 
 877                 if (!is_resource($hProcess)) fail('unable to start pg_restore');
 
 881                 // TODO: error checking
 
 882                 while(!feof($ahPipes[1]))
 
 884                         echo fread($ahPipes[1], 4096);
 
 888                 $iReturn = proc_close($hProcess);
 
 891         function pgsqlRunDropAndRestore($sDumpFile)
 
 893                 // Convert database DSN to psql parameters
 
 894                 $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
 
 895                 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
 
 896                 $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
 
 898                 $aDescriptors = array(
 
 899                         0 => array('pipe', 'r'),
 
 900                         1 => array('pipe', 'w'),
 
 901                         2 => array('file', '/dev/null', 'a')
 
 904                 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
 
 905                 if (!is_resource($hProcess)) fail('unable to start pg_restore');
 
 909                 // TODO: error checking
 
 910                 while(!feof($ahPipes[1]))
 
 912                         echo fread($ahPipes[1], 4096);
 
 916                 $iReturn = proc_close($hProcess);
 
 919         function passthruCheckReturn($cmd)
 
 922                 passthru($cmd, $result);
 
 923                 if ($result != 0) fail('Error executing external command: '.$cmd);
 
 926         function replace_tablespace($sTemplate, $sTablespace, $sSql)
 
 929                         $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"',
 
 932                         $sSql = str_replace($sTemplate, '', $sSql);