]> git.openstreetmap.org Git - nominatim.git/commitdiff
move creation of website scripts to setup script
authorSarah Hoffmann <lonvia@denofr.de>
Thu, 17 Dec 2020 13:58:09 +0000 (14:58 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Sat, 19 Dec 2020 13:33:04 +0000 (14:33 +0100)
Instead of creating the website wrapper scripts with cmake,
they are now created when --setup-website is called. The
setup of the configuration constants is directly embedded
into the scripts. This means we can get rid of the separate
settings-frontend.php. More importantly however, it means
that it is now possible to set up multiple website directories
from the same build directory.

CMakeLists.txt
cmake/website.tmpl [deleted file]
lib/lib.php
lib/setup/SetupClass.php
utils/query.php

index d1dd69ba6ef6877edd9d44a83f9c92bac456e008..b4f4ddb8107e82d44a3ee897d54a61e347ac385d 100644 (file)
@@ -114,26 +114,10 @@ if (BUILD_IMPORTER)
 endif()
 
 #-----------------------------------------------------------------------------
-# webserver scripts (API only)
+# Targets for running a development webserver from the build directory.
 #-----------------------------------------------------------------------------
 
 if (BUILD_API)
-   set(WEBSITESCRIPTS
-       deletable.php
-       details.php
-       lookup.php
-       polygons.php
-       reverse.php
-       search.php
-       status.php
-   )
-
-   foreach (script_source ${WEBSITESCRIPTS})
-       configure_file(${PROJECT_SOURCE_DIR}/cmake/website.tmpl
-                      ${PROJECT_BINARY_DIR}/website/${script_source})
-   endforeach()
-
-
    add_custom_target(serve
            php -S 127.0.0.1:8088
            WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/website
diff --git a/cmake/website.tmpl b/cmake/website.tmpl
deleted file mode 100755 (executable)
index 8194a8e..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-@define('CONST_Debug', (isset($_GET['debug']) && $_GET['debug']));
-@define('CONST_LibDir', '@CMAKE_SOURCE_DIR@/lib');
-@define('CONST_DataDir', '@CMAKE_SOURCE_DIR@');
-@define('CONST_InstallDir', '@CMAKE_BINARY_DIR@');
-require_once(CONST_InstallDir.'/settings/settings-frontend.php');
-
-require_once('@CMAKE_SOURCE_DIR@/website/@script_source@');
index 74abe6a28cc03cb778463cb96d789d8267a99870..b4c415d9d228d9db553fe678a3b0c34ca7838b4f 100644 (file)
@@ -134,6 +134,24 @@ function addQuotes($s)
     return "'".$s."'";
 }
 
+function fwriteConstDef($rFile, $sConstName, $value)
+{
+    $sEscapedValue;
+
+    if (is_bool($value)) {
+        $sEscapedValue = $value ? 'true' : 'false';
+    } elseif (is_numeric($value)) {
+        $sEscapedValue = strval($value);
+    } elseif (!$value) {
+        $sEscapedValue = 'false';
+    } else {
+        $sEscapedValue = addQuotes(str_replace("'", "\\'", (string)$value));
+    }
+
+    fwrite($rFile, "@define('CONST_$sConstName', $sEscapedValue);\n");
+}
+
+
 function parseLatLon($sQuery)
 {
     $sFound    = null;
index 99cda584e5c68d589e62335cbecbffd046924008..fd2b83dea96151088975ba5a8e620bcc27b09803 100755 (executable)
@@ -706,33 +706,60 @@ 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
-if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SETTINGS'));
-
-@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);
+
+            fwrite($rFile, "if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SETTINGS'));\n\n");
+
+            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);
+        }
     }
 
     /**
index d002f898c83d9cf708e00cb8dccf638570fd8c4b..429b30ff30117427e30f9b4ce51fe015bc965300 100644 (file)
@@ -35,6 +35,7 @@ $aCMDOptions
 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
 
 loadSettings($aCMDResult['project-dir'] ?? getcwd());
+
 @define('CONST_Database_DSN', getSetting('DATABASE_DSN'));
 @define('CONST_Default_Language', getSetting('DEFAULT_LANGUAGE', false));
 @define('CONST_Log_DB', getSettingBool('LOG_DB'));