]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/setup/SetupClass.php
Merge pull request #1245 from lonvia/address-levels-from-json
[nominatim.git] / lib / setup / SetupClass.php
index dde834f7d100766fe740aa9ec5aa68772a204b0b..013668fa5fd7a2b40190ec20b285588e7adab7cf 100755 (executable)
@@ -2,6 +2,8 @@
 
 namespace Nominatim\Setup;
 
+require_once(CONST_BasePath.'/lib/setup/AddressLevelParser.php');
+
 class SetupFunctions
 {
     protected $iCacheMemory;
@@ -150,7 +152,6 @@ class SetupFunctions
             exit(1);
         }
         $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
-        $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_naturalearthdata.sql');
         $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql.gz');
         $this->pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
 
@@ -230,7 +231,7 @@ class SetupFunctions
         $this->createSqlFunctions();
     }
 
-    public function createTables()
+    public function createTables($bReverseOnly = false)
     {
         info('Create Tables');
 
@@ -268,6 +269,13 @@ class SetupFunctions
         );
 
         $this->pgsqlRunScript($sTemplate, false);
+
+        if ($bReverseOnly) {
+            $this->pgExec('DROP TABLE search_name');
+        }
+
+        $oAlParser = new AddressLevelParser(CONST_Address_Level_Config);
+        $oAlParser->createTable($this->oDB, 'address_levels');
     }
 
     public function createPartitionTables()
@@ -356,8 +364,10 @@ class SetupFunctions
         echo '.';
         $this->pgExec('TRUNCATE location_area');
         echo '.';
-        $this->pgExec('TRUNCATE search_name');
-        echo '.';
+        if (!$this->dbReverseOnly()) {
+            $this->pgExec('TRUNCATE search_name');
+            echo '.';
+        }
         $this->pgExec('TRUNCATE search_name_blank');
         echo '.';
         $this->pgExec('DROP SEQUENCE seq_place');
@@ -608,6 +618,9 @@ class SetupFunctions
         info('Create Search indices');
 
         $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
+        if (!$this->dbReverseOnly()) {
+            $sTemplate .= file_get_contents(CONST_BasePath.'/sql/indices_search.src.sql');
+        }
         $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
         $sTemplate = $this->replaceTablespace(
             '{ts:address-index}',
@@ -694,15 +707,17 @@ class SetupFunctions
             if (!$bFound) array_push($aDropTables, $sTable);
         }
         foreach ($aDropTables as $sDrop) {
-            if ($this->sVerbose) echo "dropping table $sDrop\n";
+            if ($this->sVerbose) echo "Dropping table $sDrop\n";
             @pg_query($this->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 ($sVerbose) echo 'deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
-            unlink(CONST_Osm2pgsql_Flatnode_File);
+            if (file_exists(CONST_Osm2pgsql_Flatnode_File)) {
+                if ($this->sVerbose) echo 'Deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
+                unlink(CONST_Osm2pgsql_Flatnode_File);
+            }
         }
     }
 
@@ -748,6 +763,10 @@ class SetupFunctions
         if (!CONST_Use_Aux_Location_data) {
             $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
         }
+
+        $sReverseOnly = $this->dbReverseOnly() ? 'true' : 'false';
+        $sTemplate = str_replace('%REVERSE-ONLY%', $sReverseOnly, $sTemplate);
+
         $this->pgsqlRunScript($sTemplate);
     }
 
@@ -861,4 +880,15 @@ class SetupFunctions
             fail(pg_last_error($this->oDB->connection));
         }
     }
+
+    /**
+     * Check if the database is in reverse-only mode.
+     *
+     * @return True if there is no search_name table and infrastructure.
+     */
+    private function dbReverseOnly()
+    {
+        $sSQL = "SELECT count(*) FROM pg_tables WHERE tablename = 'search_name'";
+        return !(chksql($this->oDB->getOne($sSQL)));
+    }
 }