- info('Index ranks 0 - 4');
- $iStatus = runWithEnv($sBaseCmd.' -R 4', $aProcEnv);
- if ($iStatus != 0) {
- fail('error status ' . $iStatus . ' running nominatim!');
- }
- if (!$aCMDResult['index-noanalyse']) pgsqlRunScript($aCMDResult, 'ANALYSE');
- info('Index ranks 5 - 25');
- $iStatus = runWithEnv($sBaseCmd.' -r 5 -R 25', $aProcEnv);
- if ($iStatus != 0) {
- fail('error status ' . $iStatus . ' running nominatim!');
- }
- if (!$aCMDResult['index-noanalyse']) pgsqlRunScript($aCMDResult, 'ANALYSE');
- info('Index ranks 26 - 30');
- $iStatus = runWithEnv($sBaseCmd.' -r 26', $aProcEnv);
- if ($iStatus != 0) {
- fail('error status ' . $iStatus . ' running nominatim!');
- }
-
- info('Index postcodes');
- $oDB =& getDB();
- $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
- if (!pg_query($oDB->connection, $sSQL)) fail(pg_last_error($oDB->connection));
-}
-
-function createSearchIndices($aCMDResult)
-{
- info('Create Search indices');
-
- $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
- $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
- $sTemplate = replaceTablespace(
- '{ts:address-index}',
- CONST_Tablespace_Address_Index,
- $sTemplate
- );
- $sTemplate = replaceTablespace(
- '{ts:search-index}',
- CONST_Tablespace_Search_Index,
- $sTemplate
- );
- $sTemplate = replaceTablespace(
- '{ts:aux-index}',
- CONST_Tablespace_Aux_Index,
- $sTemplate
- );
-
- pgsqlRunScript($aCMDResult, $sTemplate);
-}
-
-function createCountryNames($aCMDResult)
-{
- info('Create search index for default country names');
-
- pgsqlRunScript($aCMDResult, "select getorcreate_country(make_standard_name('uk'), 'gb')");
- pgsqlRunScript($aCMDResult, "select getorcreate_country(make_standard_name('united states'), 'us')");
- pgsqlRunScript($aCMDResult, 'select count(*) from (select getorcreate_country(make_standard_name(country_code), country_code) from country_name where country_code is not null) as x');
- pgsqlRunScript($aCMDResult, "select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
-
- $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 ';
- if (CONST_Languages) {
- $sSQL .= 'in ';
- $sDelim = '(';
- foreach (explode(',', CONST_Languages) as $sLang) {
- $sSQL .= $sDelim."'name:$sLang'";
- $sDelim = ',';
- }
- $sSQL .= ')';
- } else {
- // all include all simple name tags
- $sSQL .= "like 'name:%'";
- }
- $sSQL .= ') v';
- pgsqlRunScript($aCMDResult, $sSQL);
-}
-
-function drop($aCMDResult)
-{
- info('Drop tables only required for updates');
- // The implementation is potentially a bit dangerous because it uses
- // a positive selection of tables to keep, and deletes everything else.
- // Including any tables that the unsuspecting user might have manually
- // created. USE AT YOUR OWN PERIL.
-
-
- // tables we want to keep. everything else goes.
- $aKeepTables = array(
- '*columns',
- 'import_polygon_*',
- 'import_status',
- 'place_addressline',
- 'location_postcode',
- 'location_property*',
- 'placex',
- 'search_name',
- 'seq_*',
- 'word',
- 'query_log',
- 'new_query_log',
- 'spatial_ref_sys',
- 'country_name',
- 'place_classtype_*'
- );
-
- $oDB =& getDB();
- $aDropTables = array();
- $aHaveTables = chksql($oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
-
- foreach ($aHaveTables as $sTable) {
- $bFound = false;
- foreach ($aKeepTables as $sKeep) {
- if (fnmatch($sKeep, $sTable)) {
- $bFound = true;
- break;
- }
- }
- if (!$bFound) array_push($aDropTables, $sTable);
- }
-
- foreach ($aDropTables as $sDrop) {
- if ($aCMDResult['verbose']) echo "dropping table $sDrop\n";
- @pg_query($oDB->connection, "DROP TABLE $sDrop CASCADE");
- // ignore warnings/errors as they might be caused by a table having
- // been deleted already by CASCADE
- }
-
- if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
- if ($aCMDResult['verbose']) echo 'deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
- unlink(CONST_Osm2pgsql_Flatnode_File);
- }
-}
-
-function didSomething($bDidSomething)
-{
- if (!$bDidSomething) {
- showUsage($aCMDOptions, true);
- } else {
- echo "Summary of warnings:\n\n";
- repeatWarnings();
- echo "\n";
- info('Setup finished.');
- }
-}
-
-// *********************************
-
-function pgsqlRunScriptFile($aCMDResult, $sFilename)
-{
- if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
- // Convert database DSN to psql parameters
- $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
- if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
- $sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
- if (!$aCMDResult['verbose']) {
- $sCMD .= ' -q';
- }
- if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
- $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
- }
- if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
- $sCMD .= ' -U ' . $aDSNInfo['username'];
- }
- $aProcEnv = null;
- if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
- $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
- }
- $ahGzipPipes = null;
- if (preg_match('/\\.gz$/', $sFilename)) {
- $aDescriptors = array(
- 0 => array('pipe', 'r'),
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
- if (!is_resource($hGzipProcess)) fail('unable to start zcat');
- $aReadPipe = $ahGzipPipes[1];
- fclose($ahGzipPipes[0]);
- } else {
- $sCMD .= ' -f '.$sFilename;
- $aReadPipe = array('pipe', 'r');
- }
- $aDescriptors = array(
- 0 => $aReadPipe,
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $ahPipes = null;
- $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
- if (!is_resource($hProcess)) fail('unable to start pgsql');
- // TODO: error checking
- while (!feof($ahPipes[1])) {
- echo fread($ahPipes[1], 4096);
- }
- fclose($ahPipes[1]);
- $iReturn = proc_close($hProcess);
- if ($iReturn > 0) {
- fail("pgsql returned with error code ($iReturn)");
- }
- if ($ahGzipPipes) {
- fclose($ahGzipPipes[1]);
- proc_close($hGzipProcess);
- }
-}
-
-function pgsqlRunScript($aCMDResult, $sScript, $bfatal = true)
-{
- runSQLScript(
- $sScript,
- $bfatal,
- $aCMDResult['verbose'],
- $aCMDResult['ignore-errors']
- );
-}
-
-function pgsqlRunPartitionScript($aCMDResult, $sTemplate)
-{
- global $aCMDResult;
- $oDB =& getDB();
-
- $sSQL = 'select distinct partition from country_name';
- $aPartitions = chksql($oDB->getCol($sSQL));
- if (!$aCMDResult['no-partitions']) $aPartitions[] = 0;
-
- preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
- foreach ($aMatches as $aMatch) {
- $sResult = '';
- foreach ($aPartitions as $sPartitionName) {
- $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
- }
- $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
- }
-
- pgsqlRunScript($aCMDResult, $sTemplate);
-}
-
-function pgsqlRunRestoreData($sDumpFile)
-{
- // Convert database DSN to psql parameters
- $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
- if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
- $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc -a '.$sDumpFile;
-
- $aDescriptors = array(
- 0 => array('pipe', 'r'),
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $ahPipes = null;
- $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
- if (!is_resource($hProcess)) fail('unable to start pg_restore');
-
- fclose($ahPipes[0]);
-
- // TODO: error checking
- while (!feof($ahPipes[1])) {
- echo fread($ahPipes[1], 4096);
- }
- fclose($ahPipes[1]);
-
- $iReturn = proc_close($hProcess);
-}
-
-function pgsqlRunDropAndRestore($sDumpFile)
-{
- // Convert database DSN to psql parameters
- $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
- if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
- $sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
- if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
- $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
- }
- if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
- $sCMD .= ' -U ' . $aDSNInfo['username'];
- }
- $aProcEnv = null;
- if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
- $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
- }
-
- $iReturn = runWithEnv($sCMD, $aProcEnv);
-}
-
-function passthpassthruCheckReturn($sCmd)
-{
- $iResult = -1;
- passthru($sCmd, $iResult);
-}
-
-function replaceTablespace($sTemplate, $sTablespace, $sSql)
-{
- if ($sTablespace) {
- $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
- } else {
- $sSql = str_replace($sTemplate, '', $sSql);
- }
-
- return $sSql;
-}
-
-function createSqlFunctions($aCMDResult)
-{
- $sModulePath = CONST_Database_Module_Path;
- $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
- $sTemplate = str_replace('{modulepath}', $sModulePath, $sTemplate);
- if ($aCMDResult['enable-diff-updates']) {
- $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
- }
- if ($aCMDResult['enable-debug-statements']) {
- $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
- }
- if (CONST_Limit_Reindexing) {
- $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
- }
- if (!CONST_Use_US_Tiger_Data) {
- $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
- }
- if (!CONST_Use_Aux_Location_data) {
- $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
- }
- pgsqlRunScript($aCMDResult, $sTemplate);
-}
-
-function checkModulePresence()
-{
- // Try accessing the C module, so we know early if something is wrong
- // and can simply error out.
- $sModulePath = CONST_Database_Module_Path;
- $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
- $sSQL .= $sModulePath."/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
- $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
-
- $oDB =& getDB();
- $oResult = $oDB->query($sSQL);
-
- $bResult = true;
-
- if (PEAR::isError($oResult)) {
- echo "\nERROR: Failed to load nominatim module. Reason:\n";
- echo $oResult->userinfo."\n\n";
- $bResult = false;
- }
-
- return $bResult;
-}
-
-function createSetupArgvArray()
-{
- $aCMDOptions
- = array(
- 'Create and setup nominatim search system',
- array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
- array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
- array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
-
- array('osm-file', '', 0, 1, 1, 1, 'realpath', 'File to import'),
- array('threads', '', 0, 1, 1, 1, 'int', 'Number of threads (where possible)'),
-
- array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
-
- array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
- array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
- array('import-data', '', 0, 1, 0, 0, 'bool', 'Import a osm file'),
- array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
- array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
- array('enable-diff-updates', '', 0, 1, 0, 0, 'bool', 'Turn on the code required to make diff updates work'),
- array('enable-debug-statements', '', 0, 1, 0, 0, 'bool', 'Include debug warning statements in pgsql commands'),
- array('ignore-errors', '', 0, 1, 0, 0, 'bool', 'Continue import even when errors in SQL are present (EXPERT)'),
- array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
- array('create-partition-tables', '', 0, 1, 0, 0, 'bool', 'Create required partition tables'),
- array('create-partition-functions', '', 0, 1, 0, 0, 'bool', 'Create required partition triggers'),
- array('no-partitions', '', 0, 1, 0, 0, 'bool', 'Do not partition search indices (speeds up import of single country extracts)'),
- array('import-wikipedia-articles', '', 0, 1, 0, 0, 'bool', 'Import wikipedia article dump'),
- array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
- array('disable-token-precalc', '', 0, 1, 0, 0, 'bool', 'Disable name precalculation (EXPERT)'),
- array('import-tiger-data', '', 0, 1, 0, 0, 'bool', 'Import tiger data (not included in \'all\')'),
- array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
- array('osmosis-init', '', 0, 1, 0, 0, 'bool', 'Generate default osmosis configuration'),
- array('index', '', 0, 1, 0, 0, 'bool', 'Index the data'),
- array('index-noanalyse', '', 0, 1, 0, 0, 'bool', 'Do not perform analyse operations during index (EXPERT)'),
- array('create-search-indices', '', 0, 1, 0, 0, 'bool', 'Create additional indices required for search and update'),
- array('create-country-names', '', 0, 1, 0, 0, 'bool', 'Create default list of searchable country names'),
- array('drop', '', 0, 1, 0, 0, 'bool', 'Drop tables needed for updates, making the database readonly (EXPERIMENTAL)'),
- );
- return $aCMDOptions;