+
+$oOsm2pgsqlCmd = (new \Nominatim\Shell(CONST_Osm2pgsql_Binary))
+ ->addParams('--hstore')
+ ->addParams('--latlong')
+ ->addParams('--append')
+ ->addParams('--slim')
+ ->addParams('--number-processes', 1)
+ ->addParams('--cache', $iCacheMemory)
+ ->addParams('--output', 'gazetteer')
+ ->addParams('--style', CONST_Import_Style)
+ ->addParams('--database', $aDSNInfo['database'])
+ ->addParams('--port', $aDSNInfo['port']);
+
+if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $oOsm2pgsqlCmd->addParams('--host', $aDSNInfo['hostspec']);
+}
+if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $oOsm2pgsqlCmd->addParams('--user', $aDSNInfo['username']);
+}
+if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $oOsm2pgsqlCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
+}
+if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
+ $oOsm2pgsqlCmd->addParams('--flat-nodes', CONST_Osm2pgsql_Flatnode_File);
+}
+
+
+$oIndexCmd = (new \Nominatim\Shell(CONST_BasePath.'/nominatim/nominatim.py'))
+ ->addParams('--database', $aDSNInfo['database'])
+ ->addParams('--port', $aDSNInfo['port'])
+ ->addParams('--threads', $aResult['index-instances']);
+
+if ($aResult['verbose']) {
+ $oIndexCmd->addParams('--verbose');
+}
+if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $oIndexCmd->addParams('--host', $aDSNInfo['hostspec']);
+}
+if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $oIndexCmd->addParams('--username', $aDSNInfo['username']);
+}
+if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $oIndexCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
+}
+
+
+if ($aResult['init-updates']) {
+ // sanity check that the replication URL is correct
+ $sBaseState = file_get_contents(CONST_Replication_Url.'/state.txt');
+ if ($sBaseState === false) {
+ echo "\nCannot find state.txt file at the configured replication URL.\n";
+ echo "Does the URL point to a directory containing OSM update data?\n\n";
+ fail('replication URL not reachable.');
+ }
+ // sanity check for pyosmium-get-changes
+ if (!CONST_Pyosmium_Binary) {
+ echo "\nCONST_Pyosmium_Binary not configured.\n";
+ echo "You need to install pyosmium and set up the path to pyosmium-get-changes\n";
+ echo "in your local settings file.\n\n";
+ fail('CONST_Pyosmium_Binary not configured');
+ }
+
+ $aOutput = 0;
+ $oCMD = new \Nominatim\Shell(CONST_Pyosmium_Binary, '--help');
+ exec($oCMD->escapedCmd(), $aOutput, $iRet);
+
+ if ($iRet != 0) {
+ echo "Cannot execute pyosmium-get-changes.\n";
+ echo "Make sure you have pyosmium installed correctly\n";
+ echo "and have set up CONST_Pyosmium_Binary to point to pyosmium-get-changes.\n";
+ fail('pyosmium-get-changes not found or not usable');
+ }
+
+ if (!$aResult['no-update-functions']) {
+ // instantiate setupClass to use the function therein
+ $cSetup = new SetupFunctions(array(
+ 'enable-diff-updates' => true,
+ 'verbose' => $aResult['verbose']
+ ));
+ $cSetup->connect();
+ $cSetup->createFunctions();
+ }
+
+ $sDatabaseDate = getDatabaseDate($oDB);
+ if (!$sDatabaseDate) {
+ fail('Cannot determine date of database.');
+ }
+ $sWindBack = strftime('%Y-%m-%dT%H:%M:%SZ', strtotime($sDatabaseDate) - (3*60*60));
+
+ // get the appropriate state id
+ $aOutput = 0;
+ $oCMD = (new \Nominatim\Shell(CONST_Pyosmium_Binary))
+ ->addParams('--start-date', $sWindBack)
+ ->addParams('--server', CONST_Replication_Url);
+
+ exec($oCMD->escapedCmd(), $aOutput, $iRet);
+ if ($iRet != 0 || $aOutput[0] == 'None') {
+ fail('Error running pyosmium tools');
+ }
+
+ $oDB->exec('TRUNCATE import_status');
+ $sSQL = "INSERT INTO import_status (lastimportdate, sequence_id, indexed) VALUES('";
+ $sSQL .= $sDatabaseDate."',".$aOutput[0].', true)';
+
+ try {
+ $oDB->exec($sSQL);
+ } catch (\Nominatim\DatabaseError $e) {
+ fail('Could not enter sequence into database.');
+ }
+
+ echo "Done. Database updates will start at sequence $aOutput[0] ($sWindBack)\n";
+}
+
+if ($aResult['check-for-updates']) {
+ $aLastState = $oDB->getRow('SELECT sequence_id FROM import_status');
+
+ if (!$aLastState['sequence_id']) {
+ fail('Updates not set up. Please run ./utils/update.php --init-updates.');
+ }
+
+ $oCmd = (new \Nominatim\Shell(CONST_BasePath.'/utils/check_server_for_updates.py'))
+ ->addParams(CONST_Replication_Url)
+ ->addParams($aLastState['sequence_id']);
+ $iRet = $oCmd->run();
+
+ exit($iRet);