+ datafile = data_path / 'wikimedia-importance.sql.gz'
+
+ if not datafile.exists():
+ return 1
+
+ pre_code = """BEGIN;
+ DROP TABLE IF EXISTS "wikipedia_article";
+ DROP TABLE IF EXISTS "wikipedia_redirect"
+ """
+ post_code = "COMMIT"
+ execute_file(dsn, datafile, ignore_errors=ignore_errors,
+ pre_code=pre_code, post_code=post_code)
+
+ return 0
+
+
+def recompute_importance(conn):
+ """ Recompute wikipedia links and importance for all entries in placex.
+ This is a long-running operations that must not be executed in
+ parallel with updates.
+ """
+ with conn.cursor() as cur:
+ cur.execute('ALTER TABLE placex DISABLE TRIGGER ALL')
+ cur.execute("""
+ UPDATE placex SET (wikipedia, importance) =
+ (SELECT wikipedia, importance
+ FROM compute_importance(extratags, country_code, osm_type, osm_id))
+ """)
+ cur.execute("""
+ UPDATE placex s SET wikipedia = d.wikipedia, importance = d.importance
+ FROM placex d
+ WHERE s.place_id = d.linked_place_id and d.wikipedia is not null
+ and (s.wikipedia is null or s.importance < d.importance);
+ """)
+
+ cur.execute('ALTER TABLE placex ENABLE TRIGGER ALL')
+ conn.commit()
+
+
+def _quote_php_variable(var_type, config, conf_name):
+ if var_type == bool:
+ return 'true' if config.get_bool(conf_name) else 'false'
+
+ if var_type == int:
+ return getattr(config, conf_name)
+
+ if not getattr(config, conf_name):
+ return 'false'
+
+ quoted = getattr(config, conf_name).replace("'", "\\'")
+ return f"'{quoted}'"
+
+
+def setup_website(basedir, config, conn):
+ """ 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', '{0}');
+ @define('CONST_TokenizerDir', '{2}');
+ @define('CONST_NominatimVersion', '{1[0]}.{1[1]}.{1[2]}-{1[3]}');
+
+ """.format(config.lib_dir.php, NOMINATIM_VERSION,
+ config.project_dir / 'tokenizer'))
+
+ for php_name, conf_name, var_type in PHP_CONST_DEFS:
+ varout = _quote_php_variable(var_type, config, conf_name)
+
+ template += f"@define('CONST_{php_name}', {varout});\n"
+
+ template += f"\nrequire_once('{config.lib_dir.php}/website/{{}}');\n"
+
+ search_name_table_exists = bool(conn and conn.table_exists('search_name'))
+
+ for script in WEBSITE_SCRIPTS:
+ if not search_name_table_exists and script == 'search.php':
+ (basedir / script).write_text(template.format('reverse-only-search.php'), 'utf-8')
+ else:
+ (basedir / script).write_text(template.format(script), 'utf-8')