]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/setup/SetupClass.php
do not overwrite custom set module paths
[nominatim.git] / lib / setup / SetupClass.php
index 4e3d6ece4bf6b791cd1b319128097e3bcd5f00d8..dda491603c3e673576f35ab0e03802874127ee82 100755 (executable)
@@ -2,14 +2,12 @@
 
 namespace Nominatim\Setup;
 
-require_once(CONST_LibDir.'/setup/AddressLevelParser.php');
 require_once(CONST_LibDir.'/Shell.php');
 
 class SetupFunctions
 {
     protected $iCacheMemory;
     protected $iInstances;
-    protected $sModulePath;
     protected $aDSNInfo;
     protected $bQuiet;
     protected $bVerbose;
@@ -19,6 +17,7 @@ class SetupFunctions
     protected $bNoPartitions;
     protected $bDrop;
     protected $oDB = null;
+    protected $oNominatimCmd;
 
     public function __construct(array $aCMDResult)
     {
@@ -42,9 +41,6 @@ class SetupFunctions
             $this->iCacheMemory = getCacheMemoryMB();
         }
 
-        $this->sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
-        info('module path: ' . $this->sModulePath);
-
         // parse database string
         $this->aDSNInfo = \Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
         if (!isset($this->aDSNInfo['port'])) {
@@ -78,6 +74,14 @@ class SetupFunctions
         }
 
         $this->bDrop = isset($aCMDResult['drop']) && $aCMDResult['drop'];
+
+        $this->oNominatimCmd = new \Nominatim\Shell(getSetting('NOMINATIM_TOOL'));
+        if ($this->bQuiet) {
+            $this->oNominatimCmd->addParams('--quiet');
+        }
+        if ($this->bVerbose) {
+            $this->oNominatimCmd->addParams('--verbose');
+        }
     }
 
     public function createDB()
@@ -138,6 +142,27 @@ class SetupFunctions
             exit(1);
         }
 
+        if (!getSetting('DATABASE_MODULE_PATH')) {
+            // If no custom module path is set then copy the module into the
+            // project directory, but only if it is not the same file already
+            // (aka we are running from the build dir).
+            $sDest = CONST_InstallDir.'/module';
+            if ($sDest != CONST_Default_ModulePath) {
+                if (!file_exists($sDest)) {
+                    mkdir($sDest);
+                }
+                if (!copy(CONST_Default_ModulePath.'/nominatim.so', $sDest.'/nominatim.so')) {
+                    echo "Failed to copy database module to $sDest.";
+                    exit(1);
+                }
+                chmod($sDest.'/nominatim.so', 0755);
+                info("Database module installed at $sDest.");
+            } else {
+                info('Running from build directory. Leaving database module as is.');
+            }
+        } else {
+            info('Using database module from DATABASE_MODULE_PATH ('.getSetting('DATABASE_MODULE_PATH').').');
+        }
         // Try accessing the C module, so we know early if something is wrong
         $this->checkModulePresence(); // raises exception on failure
 
@@ -151,14 +176,14 @@ class SetupFunctions
         $this->pgsqlRunScriptFile(CONST_DataDir.'/data/gb_postcode_table.sql');
         $this->pgsqlRunScriptFile(CONST_DataDir.'/data/us_postcode_table.sql');
 
-        $sPostcodeFilename = CONST_DataDir.'/data/gb_postcode_data.sql.gz';
+        $sPostcodeFilename = CONST_InstallDir.'/gb_postcode_data.sql.gz';
         if (file_exists($sPostcodeFilename)) {
             $this->pgsqlRunScriptFile($sPostcodeFilename);
         } else {
             warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
         }
 
-        $sPostcodeFilename = CONST_DataDir.'/data/us_postcode_data.sql.gz';
+        $sPostcodeFilename = CONST_InstallDir.'/us_postcode_data.sql.gz';
         if (file_exists($sPostcodeFilename)) {
             $this->pgsqlRunScriptFile($sPostcodeFilename);
         } else {
@@ -253,8 +278,7 @@ class SetupFunctions
             $this->dropTable('search_name');
         }
 
-        $oAlParser = new AddressLevelParser(getSettingConfig('ADDRESS_LEVEL_CONFIG', 'address-levels.json'));
-        $oAlParser->createTable($this->db(), 'address_levels');
+        (clone($this->oNominatimCmd))->addParams('refresh', '--address-levels')->run();
     }
 
     public function createTableTriggers()
@@ -280,14 +304,12 @@ class SetupFunctions
     public function createPartitionFunctions()
     {
         info('Create Partition Functions');
-
-        $sTemplate = file_get_contents(CONST_DataDir.'/sql/partition-functions.src.sql');
-        $this->pgsqlRunPartitionScript($sTemplate);
+        $this->createSqlFunctions(); // also create partition functions
     }
 
     public function importWikipediaArticles()
     {
-        $sWikiArticlePath = getSettings('WIKIPEDIA_DATA_PATH', CONST_DataDir.'/data');
+        $sWikiArticlePath = getSetting('WIKIPEDIA_DATA_PATH', CONST_InstallDir);
         $sWikiArticlesFile = $sWikiArticlePath.'/wikimedia-importance.sql.gz';
         if (file_exists($sWikiArticlesFile)) {
             info('Importing wikipedia articles and redirects');
@@ -546,30 +568,10 @@ class SetupFunctions
     {
         $this->checkModulePresence(); // raises exception on failure
 
-        $oBaseCmd = (new \Nominatim\Shell(CONST_DataDir.'/nominatim/nominatim.py'))
-                    ->addParams('--database', $this->aDSNInfo['database'])
-                    ->addParams('--port', $this->aDSNInfo['port'])
-                    ->addParams('--threads', $this->iInstances);
-
-        if (!$this->bQuiet) {
-            $oBaseCmd->addParams('-v');
-        }
-        if ($this->bVerbose) {
-            $oBaseCmd->addParams('-v');
-        }
-        if (isset($this->aDSNInfo['hostspec'])) {
-            $oBaseCmd->addParams('--host', $this->aDSNInfo['hostspec']);
-        }
-        if (isset($this->aDSNInfo['username'])) {
-            $oBaseCmd->addParams('--user', $this->aDSNInfo['username']);
-        }
-        if (isset($this->aDSNInfo['password'])) {
-            $oBaseCmd->addEnvPair('PGPASSWORD', $this->aDSNInfo['password']);
-        }
+        $oBaseCmd = (clone $this->oNominatimCmd)->addParams('index');
 
         info('Index ranks 0 - 4');
         $oCmd = (clone $oBaseCmd)->addParams('--maxrank', 4);
-        echo $oCmd->escapedCmd();
 
         $iStatus = $oCmd->run();
         if ($iStatus != 0) {
@@ -578,14 +580,14 @@ class SetupFunctions
         if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
 
         info('Index administrative boundaries');
-        $oCmd = (clone $oBaseCmd)->addParams('-b');
+        $oCmd = (clone $oBaseCmd)->addParams('--boundaries-only');
         $iStatus = $oCmd->run();
         if ($iStatus != 0) {
             fail('error status ' . $iStatus . ' running nominatim!');
         }
 
         info('Index ranks 5 - 25');
-        $oCmd = (clone $oBaseCmd)->addParams('--minrank', 5, '--maxrank', 25);
+        $oCmd = (clone $oBaseCmd)->addParams('--no-boundaries', '--minrank', 5, '--maxrank', 25);
         $iStatus = $oCmd->run();
         if ($iStatus != 0) {
             fail('error status ' . $iStatus . ' running nominatim!');
@@ -594,7 +596,7 @@ class SetupFunctions
         if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
 
         info('Index ranks 26 - 30');
-        $oCmd = (clone $oBaseCmd)->addParams('--minrank', 26);
+        $oCmd = (clone $oBaseCmd)->addParams('--no-boundaries', '--minrank', 26);
         $iStatus = $oCmd->run();
         if ($iStatus != 0) {
             fail('error status ' . $iStatus . ' running nominatim!');
@@ -706,31 +708,57 @@ class SetupFunctions
     }
 
     /**
-     * Setup settings-frontend.php in the build/website directory
+     * Setup the directory for the API scripts.
      *
      * @return null
      */
     public function setupWebsite()
     {
-        $rOutputFile = fopen(CONST_InstallDir.'/settings/settings-frontend.php', 'w');
-
-        fwrite($rOutputFile, "<?php
-@define('CONST_Database_DSN', '".getSetting('DATABASE_DSN')."');
-@define('CONST_Default_Language', ".getSetting('DEFAULT_LANGUAGE', 'false').");
-@define('CONST_Log_DB', ".(getSettingBool('LOG_DB') ? 'true' : 'false').");
-@define('CONST_Log_File', ".getSetting('LOG_FILE', 'false').");
-@define('CONST_Max_Word_Frequency', '".getSetting('MAX_WORD_FREQUENCY')."');
-@define('CONST_NoAccessControl', ".(getSettingBool('CORS_NOACCESSCONTROL') ? 'true' : 'false').");
-@define('CONST_Places_Max_ID_count', ".getSetting('LOOKUP_MAX_COUNT').");
-@define('CONST_PolygonOutput_MaximumTypes', ".getSetting('POLYGON_OUTPUT_MAX_TYPES').");
-@define('CONST_Search_BatchMode', ".(getSettingBool('SEARCH_BATCH_MODE' ? 'true' : 'false').");
-@define('CONST_Search_NameOnlySearchFrequencyThreshold', ".getSetting('SEARCH_NAME_ONLY_THRESHOLD').");
-@define('CONST_Term_Normalization_Rules', \"".getSetting('TERM_NORMALIZATION')."\");
-@define('CONST_Use_Aux_Location_data', ".(getSettingBool('USE_AUX_LOCATION_DATA') ? 'true' : 'false').");
-@define('CONST_Use_US_Tiger_Data', ".(getSettingBool('USE_US_TIGER_DATA') ? 'true' : 'false').");
-@define('CONST_MapIcon_URL', ".(getSetting('MAPICON_URL', 'false').');
-');
-        info(CONST_InstallDir.'/settings/settings-frontend.php has been set up successfully');
+        if (!is_dir(CONST_InstallDir.'/website')) {
+            info('Creating directory for website scripts at: '.CONST_InstallDir.'/website');
+            mkdir(CONST_InstallDir.'/website');
+        }
+
+        $aScripts = array(
+          'deletable.php',
+          'details.php',
+          'lookup.php',
+          'polygons.php',
+          'reverse.php',
+          'search.php',
+          'status.php'
+        );
+
+        foreach ($aScripts as $sScript) {
+            $rFile = fopen(CONST_InstallDir.'/website/'.$sScript, 'w');
+
+            fwrite($rFile, "<?php\n\n");
+            fwrite($rFile, '@define(\'CONST_Debug\', $_GET[\'debug\'] ?? false);'."\n\n");
+
+            fwriteConstDef($rFile, 'LibDir', CONST_LibDir);
+            fwriteConstDef($rFile, 'DataDir', CONST_DataDir);
+            fwriteConstDef($rFile, 'InstallDir', CONST_InstallDir);
+            fwriteConstDef($rFile, 'Database_DSN', getSetting('DATABASE_DSN'));
+            fwriteConstDef($rFile, 'Default_Language', getSetting('DEFAULT_LANGUAGE'));
+            fwriteConstDef($rFile, 'Log_DB', getSettingBool('LOG_DB'));
+            fwriteConstDef($rFile, 'Log_File', getSetting('LOG_FILE'));
+            fwriteConstDef($rFile, 'Max_Word_Frequency', (int)getSetting('MAX_WORD_FREQUENCY'));
+            fwriteConstDef($rFile, 'NoAccessControl', getSettingBool('CORS_NOACCESSCONTROL'));
+            fwriteConstDef($rFile, 'Places_Max_ID_count', (int)getSetting('LOOKUP_MAX_COUNT'));
+            fwriteConstDef($rFile, 'PolygonOutput_MaximumTypes', getSetting('POLYGON_OUTPUT_MAX_TYPES'));
+            fwriteConstDef($rFile, 'Search_BatchMode', getSettingBool('SEARCH_BATCH_MODE'));
+            fwriteConstDef($rFile, 'Search_NameOnlySearchFrequencyThreshold', getSetting('SEARCH_NAME_ONLY_THRESHOLD'));
+            fwriteConstDef($rFile, 'Term_Normalization_Rules', getSetting('TERM_NORMALIZATION'));
+            fwriteConstDef($rFile, 'Use_Aux_Location_data', getSettingBool('USE_AUX_LOCATION_DATA'));
+            fwriteConstDef($rFile, 'Use_US_Tiger_Data', getSettingBool('USE_US_TIGER_DATA'));
+            fwriteConstDef($rFile, 'MapIcon_URL', getSetting('MAPICON_URL'));
+
+            // XXX scripts should go into the library.
+            fwrite($rFile, 'require_once(\''.CONST_DataDir.'/website/'.$sScript."');\n");
+            fclose($rFile);
+
+            chmod(CONST_InstallDir.'/website/'.$sScript, 0755);
+        }
     }
 
     /**
@@ -755,9 +783,8 @@ class SetupFunctions
     {
         $sFName = getSetting('FLATNODE_FILE');
         if ($sFName && file_exists($sFName)) {
-                if ($this->bVerbose) echo 'Deleting '.$sFName."\n";
-                unlink($sFName);
-            }
+            if ($this->bVerbose) echo 'Deleting '.$sFName."\n";
+            unlink($sFName);
         }
     }
 
@@ -773,43 +800,18 @@ class SetupFunctions
 
     private function createSqlFunctions()
     {
-        $sBasePath = CONST_DataDir.'/sql/functions/';
-        $sTemplate = file_get_contents($sBasePath.'utils.sql');
-        $sTemplate .= file_get_contents($sBasePath.'normalization.sql');
-        $sTemplate .= file_get_contents($sBasePath.'ranking.sql');
-        $sTemplate .= file_get_contents($sBasePath.'importance.sql');
-        $sTemplate .= file_get_contents($sBasePath.'address_lookup.sql');
-        $sTemplate .= file_get_contents($sBasePath.'interpolation.sql');
-        if ($this->db()->tableExists('place')) {
-            $sTemplate .= file_get_contents($sBasePath.'place_triggers.sql');
-        }
-        if ($this->db()->tableExists('placex')) {
-            $sTemplate .= file_get_contents($sBasePath.'placex_triggers.sql');
-        }
-        if ($this->db()->tableExists('location_postcode')) {
-            $sTemplate .= file_get_contents($sBasePath.'postcode_triggers.sql');
-        }
-        $sTemplate = str_replace('{modulepath}', $this->sModulePath, $sTemplate);
-        if ($this->bEnableDiffUpdates) {
-            $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
+        $oCmd = (clone($this->oNominatimCmd))
+                ->addParams('refresh', '--functions');
+
+        if (!$this->bEnableDiffUpdates) {
+            $oCmd->addParams('--no-diff-updates');
         }
+
         if ($this->bEnableDebugStatements) {
-            $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
-        }
-        if (getSettingBool('LIMIT_REINDEXING')) {
-            $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
-        }
-        if (!getSettingBool('USE_US_TIGER_DATA')) {
-            $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
+            $oCmd->addParams('--enable-debug-statements');
         }
-        if (!getSettingBool('USE_AUX_LOCATION_DATA')) {
-            $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
-        }
-
-        $sReverseOnly = $this->dbReverseOnly() ? 'true' : 'false';
-        $sTemplate = str_replace('%REVERSE-ONLY%', $sReverseOnly, $sTemplate);
 
-        $this->pgsqlRunScript($sTemplate);
+        $oCmd->run();
     }
 
     private function pgsqlRunPartitionScript($sTemplate)
@@ -945,12 +947,13 @@ class SetupFunctions
      */
     private function checkModulePresence()
     {
+        $sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
         $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
-        $sSQL .= $this->sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
+        $sSQL .= $sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
         $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
 
         $oDB = new \Nominatim\DB();
         $oDB->connect();
-        $oDB->exec($sSQL, null, 'Database server failed to load '.$this->sModulePath.'/nominatim.so module');
+        $oDB->exec($sSQL, null, 'Database server failed to load '.$sModulePath.'/nominatim.so module');
     }
 }