port setup-website to python

This commit is contained in:
Sarah Hoffmann
2021-02-19 17:51:06 +01:00
parent a0ae4945cd
commit 389138abfe
6 changed files with 141 additions and 63 deletions

View File

@@ -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;

View File

@@ -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, "<?php\n\n");
fwrite($rFile, '@define(\'CONST_Debug\', $_GET[\'debug\'] ?? false);'."\n\n");
fwriteConstDef($rFile, 'LibDir', CONST_LibDir);
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'));
fwrite($rFile, 'require_once(\''.CONST_LibDir.'/website/'.$sScript."');\n");
fclose($rFile);
chmod(CONST_InstallDir.'/website/'.$sScript, 0755);
}
(clone($this->oNominatimCmd))->addParams('refresh', '--website')->run();
}
/**

View File

@@ -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

View File

@@ -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("""\
<?php
@define('CONST_Debug', $_GET['debug'] ?? false);
@define('CONST_LibDir', '{}');
""".format(phplib_dir))
for php_name, conf_name, var_type in PHP_CONST_DEFS:
if var_type == bool:
varout = 'true' if config.get_bool(conf_name) else 'false'
elif var_type == int:
varout = getattr(config, conf_name)
elif not getattr(config, conf_name):
varout = 'false'
else:
varout = "'{}'".format(getattr(config, conf_name).replace("'", "\\'"))
template += "@define('CONST_{}', {});\n".format(php_name, varout)
template += "\nrequire_once('{}/website/{{}}');\n".format(phplib_dir)
for script in WEBSITE_SCRIPTS:
(basedir / script).write_text(template.format(script), 'utf-8')

View File

@@ -149,7 +149,6 @@ def test_index_command(mock_func_factory, temp_db_cursor, params, do_bnds, do_ra
@pytest.mark.parametrize("command,params", [
('wiki-data', ('setup.php', '--import-wikipedia-articles')),
('importance', ('update.php', '--recompute-importance')),
('website', ('setup.php', '--setup-website')),
])
def test_refresh_legacy_command(mock_func_factory, temp_db, command, params):
mock_run_legacy = mock_func_factory(nominatim.clicmd.refresh, 'run_legacy_script')
@@ -165,6 +164,7 @@ def test_refresh_legacy_command(mock_func_factory, temp_db, command, params):
('word-counts', 'recompute_word_counts'),
('address-levels', 'load_address_levels_from_file'),
('functions', 'create_functions'),
('website', 'setup_website'),
])
def test_refresh_command(mock_func_factory, temp_db, command, func):
func_mock = mock_func_factory(nominatim.tools.refresh, func)

View File

@@ -0,0 +1,70 @@
"""
Tests for setting up the website scripts.
"""
from pathlib import Path
import subprocess
import pytest
from nominatim.tools import refresh
@pytest.fixture
def envdir(tmpdir):
(tmpdir / 'php').mkdir()
(tmpdir / 'php' / 'website').mkdir()
return tmpdir
@pytest.fixture
def test_script(envdir):
def _create_file(code):
outfile = envdir / 'php' / 'website' / 'search.php'
outfile.write_text('<?php\n{}\n'.format(code), 'utf-8')
return _create_file
def run_website_script(envdir, config):
refresh.setup_website(envdir, envdir / 'php', config)
proc = subprocess.run(['/usr/bin/env', 'php', '-Cq',
envdir / 'search.php'], check=False)
return proc.returncode
@pytest.mark.parametrize("setting,retval", (('yes', 10), ('no', 20)))
def test_setup_website_check_bool(def_config, monkeypatch, envdir, test_script,
setting, retval):
monkeypatch.setenv('NOMINATIM_CORS_NOACCESSCONTROL', setting)
test_script('exit(CONST_NoAccessControl ? 10 : 20);')
assert run_website_script(envdir, def_config) == retval
@pytest.mark.parametrize("setting", (0, 10, 99067))
def test_setup_website_check_int(def_config, monkeypatch, envdir, test_script, setting):
monkeypatch.setenv('NOMINATIM_LOOKUP_MAX_COUNT', str(setting))
test_script('exit(CONST_Places_Max_ID_count == {} ? 10 : 20);'.format(setting))
assert run_website_script(envdir, def_config) == 10
def test_setup_website_check_empty_str(def_config, monkeypatch, envdir, test_script):
monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', '')
test_script('exit(CONST_Default_Language === false ? 10 : 20);')
assert run_website_script(envdir, def_config) == 10
def test_setup_website_check_str(def_config, monkeypatch, envdir, test_script):
monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'ffde 2')
test_script('exit(CONST_Default_Language === "ffde 2" ? 10 : 20);')
assert run_website_script(envdir, def_config) == 10