From 389138abfe11eef3207f63d432865b00119beb3c Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Fri, 19 Feb 2021 17:51:06 +0100 Subject: [PATCH] port setup-website to python --- lib-php/lib.php | 18 ----- lib-php/setup/SetupClass.php | 43 +----------- nominatim/clicmd/refresh.py | 5 +- nominatim/tools/refresh.py | 66 +++++++++++++++++ test/python/test_cli.py | 2 +- .../test_tools_refresh_setup_website.py | 70 +++++++++++++++++++ 6 files changed, 141 insertions(+), 63 deletions(-) create mode 100644 test/python/test_tools_refresh_setup_website.py diff --git a/lib-php/lib.php b/lib-php/lib.php index 89b54363..a1f528fa 100644 --- a/lib-php/lib.php +++ b/lib-php/lib.php @@ -132,24 +132,6 @@ 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; diff --git a/lib-php/setup/SetupClass.php b/lib-php/setup/SetupClass.php index 4946e070..a423e12c 100755 --- a/lib-php/setup/SetupClass.php +++ b/lib-php/setup/SetupClass.php @@ -667,48 +667,7 @@ class SetupFunctions */ public function setupWebsite() { - 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, "oNominatimCmd))->addParams('refresh', '--website')->run(); } /** diff --git a/nominatim/clicmd/refresh.py b/nominatim/clicmd/refresh.py index 8e69caca..ffbe628b 100644 --- a/nominatim/clicmd/refresh.py +++ b/nominatim/clicmd/refresh.py @@ -82,7 +82,8 @@ class UpdateRefresh: run_legacy_script('update.php', '--recompute-importance', nominatim_env=args, throw_on_fail=True) if args.website: - run_legacy_script('setup.php', '--setup-website', - nominatim_env=args, throw_on_fail=True) + webdir = args.project_dir / 'website' + LOG.warning('Setting up website directory at %s', webdir) + refresh.setup_website(webdir, args.phplib_dir, args.config) return 0 diff --git a/nominatim/tools/refresh.py b/nominatim/tools/refresh.py index 1fcb1577..f09c0ced 100644 --- a/nominatim/tools/refresh.py +++ b/nominatim/tools/refresh.py @@ -2,12 +2,16 @@ Functions for bringing auxiliary data in the database up-to-date. """ import json +import logging import re +from textwrap import dedent from psycopg2.extras import execute_values from ..db.utils import execute_file +LOG = logging.getLogger() + def update_postcodes(conn, sql_dir): """ Recalculate postcode centroids and add, remove and update entries in the location_postcode table. `conn` is an opne connection to the database. @@ -165,3 +169,65 @@ def create_functions(conn, config, sql_dir, cur.execute(sql) conn.commit() + + +WEBSITE_SCRIPTS = ( + 'deletable.php', + 'details.php', + 'lookup.php', + 'polygons.php', + 'reverse.php', + 'search.php', + 'status.php' +) + +# constants needed by PHP scripts: PHP name, config name, type +PHP_CONST_DEFS = ( + ('Database_DSN', 'DATABASE_DSN', str), + ('Default_Language', 'DEFAULT_LANGUAGE', str), + ('Log_DB', 'LOG_DB', bool), + ('Log_File', 'LOG_FILE', str), + ('Max_Word_Frequency', 'MAX_WORD_FREQUENCY', int), + ('NoAccessControl', 'CORS_NOACCESSCONTROL', bool), + ('Places_Max_ID_count', 'LOOKUP_MAX_COUNT', int), + ('PolygonOutput_MaximumTypes', 'POLYGON_OUTPUT_MAX_TYPES', int), + ('Search_BatchMode', 'SEARCH_BATCH_MODE', bool), + ('Search_NameOnlySearchFrequencyThreshold', 'SEARCH_NAME_ONLY_THRESHOLD', str), + ('Term_Normalization_Rules', 'TERM_NORMALIZATION', str), + ('Use_Aux_Location_data', 'USE_AUX_LOCATION_DATA', bool), + ('Use_US_Tiger_Data', 'USE_US_TIGER_DATA', bool), + ('MapIcon_URL', 'MAPICON_URL', str), +) + + +def setup_website(basedir, phplib_dir, config): + """ Create the website script stubs. + """ + if not basedir.exists(): + LOG.info('Creating website directory.') + basedir.mkdir() + + template = dedent("""\ +