]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/setup/SetupClass.php
add a function for the intial indexing run
[nominatim.git] / lib-php / setup / SetupClass.php
1 <?php
2
3 namespace Nominatim\Setup;
4
5 require_once(CONST_LibDir.'/Shell.php');
6
7 class SetupFunctions
8 {
9     protected $iInstances;
10     protected $aDSNInfo;
11     protected $bQuiet;
12     protected $bVerbose;
13     protected $sIgnoreErrors;
14     protected $bEnableDiffUpdates;
15     protected $bEnableDebugStatements;
16     protected $bNoPartitions;
17     protected $bDrop;
18     protected $oDB = null;
19     protected $oNominatimCmd;
20
21     public function __construct(array $aCMDResult)
22     {
23         // by default, use all but one processor, but never more than 15.
24         $this->iInstances = isset($aCMDResult['threads'])
25             ? $aCMDResult['threads']
26             : (min(16, getProcessorCount()) - 1);
27
28         if ($this->iInstances < 1) {
29             $this->iInstances = 1;
30             warn('resetting threads to '.$this->iInstances);
31         }
32
33         // parse database string
34         $this->aDSNInfo = \Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
35         if (!isset($this->aDSNInfo['port'])) {
36             $this->aDSNInfo['port'] = 5432;
37         }
38
39         // setting member variables based on command line options stored in $aCMDResult
40         $this->bQuiet = isset($aCMDResult['quiet']) && $aCMDResult['quiet'];
41         $this->bVerbose = $aCMDResult['verbose'];
42
43         //setting default values which are not set by the update.php array
44         if (isset($aCMDResult['ignore-errors'])) {
45             $this->sIgnoreErrors = $aCMDResult['ignore-errors'];
46         } else {
47             $this->sIgnoreErrors = false;
48         }
49         if (isset($aCMDResult['enable-debug-statements'])) {
50             $this->bEnableDebugStatements = $aCMDResult['enable-debug-statements'];
51         } else {
52             $this->bEnableDebugStatements = false;
53         }
54         if (isset($aCMDResult['no-partitions'])) {
55             $this->bNoPartitions = $aCMDResult['no-partitions'];
56         } else {
57             $this->bNoPartitions = false;
58         }
59         if (isset($aCMDResult['enable-diff-updates'])) {
60             $this->bEnableDiffUpdates = $aCMDResult['enable-diff-updates'];
61         } else {
62             $this->bEnableDiffUpdates = false;
63         }
64
65         $this->bDrop = isset($aCMDResult['drop']) && $aCMDResult['drop'];
66
67         $this->oNominatimCmd = new \Nominatim\Shell(getSetting('NOMINATIM_TOOL'));
68         if ($this->bQuiet) {
69             $this->oNominatimCmd->addParams('--quiet');
70         }
71         if ($this->bVerbose) {
72             $this->oNominatimCmd->addParams('--verbose');
73         }
74     }
75
76     public function createFunctions()
77     {
78         info('Create Functions');
79
80         // Try accessing the C module, so we know early if something is wrong
81         $this->checkModulePresence(); // raises exception on failure
82
83         $this->createSqlFunctions();
84     }
85
86     public function createTables($bReverseOnly = false)
87     {
88         info('Create Tables');
89
90         $sTemplate = file_get_contents(CONST_SqlDir.'/tables.sql');
91         $sTemplate = $this->replaceSqlPatterns($sTemplate);
92
93         $this->pgsqlRunScript($sTemplate, false);
94
95         if ($bReverseOnly) {
96             $this->dropTable('search_name');
97         }
98
99         (clone($this->oNominatimCmd))->addParams('refresh', '--address-levels')->run();
100     }
101
102     public function createTableTriggers()
103     {
104         info('Create Tables');
105
106         $sTemplate = file_get_contents(CONST_SqlDir.'/table-triggers.sql');
107         $sTemplate = $this->replaceSqlPatterns($sTemplate);
108
109         $this->pgsqlRunScript($sTemplate, false);
110     }
111
112     public function createPartitionTables()
113     {
114         info('Create Partition Tables');
115
116         $sTemplate = file_get_contents(CONST_SqlDir.'/partition-tables.src.sql');
117         $sTemplate = $this->replaceSqlPatterns($sTemplate);
118
119         $this->pgsqlRunPartitionScript($sTemplate);
120     }
121
122     public function loadData($bDisableTokenPrecalc)
123     {
124         info('Drop old Data');
125
126         $oDB = $this->db();
127
128         $oDB->exec('TRUNCATE word');
129         echo '.';
130         $oDB->exec('TRUNCATE placex');
131         echo '.';
132         $oDB->exec('TRUNCATE location_property_osmline');
133         echo '.';
134         $oDB->exec('TRUNCATE place_addressline');
135         echo '.';
136         $oDB->exec('TRUNCATE location_area');
137         echo '.';
138         if (!$this->dbReverseOnly()) {
139             $oDB->exec('TRUNCATE search_name');
140             echo '.';
141         }
142         $oDB->exec('TRUNCATE search_name_blank');
143         echo '.';
144         $oDB->exec('DROP SEQUENCE seq_place');
145         echo '.';
146         $oDB->exec('CREATE SEQUENCE seq_place start 100000');
147         echo '.';
148
149         $sSQL = 'select distinct partition from country_name';
150         $aPartitions = $oDB->getCol($sSQL);
151
152         if (!$this->bNoPartitions) $aPartitions[] = 0;
153         foreach ($aPartitions as $sPartition) {
154             $oDB->exec('TRUNCATE location_road_'.$sPartition);
155             echo '.';
156         }
157
158         // used by getorcreate_word_id to ignore frequent partial words
159         $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
160         $sSQL .= '$$ SELECT '.getSetting('MAX_WORD_FREQUENCY').' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
161         $oDB->exec($sSQL);
162         echo ".\n";
163
164         // pre-create the word list
165         if (!$bDisableTokenPrecalc) {
166             info('Loading word list');
167             $this->pgsqlRunScriptFile(CONST_DataDir.'/words.sql');
168         }
169
170         info('Load Data');
171         $sColumns = 'osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry';
172
173         $aDBInstances = array();
174         $iLoadThreads = max(1, $this->iInstances - 1);
175         for ($i = 0; $i < $iLoadThreads; $i++) {
176             // https://secure.php.net/manual/en/function.pg-connect.php
177             $DSN = getSetting('DATABASE_DSN');
178             $DSN = preg_replace('/^pgsql:/', '', $DSN);
179             $DSN = preg_replace('/;/', ' ', $DSN);
180             $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW);
181             pg_ping($aDBInstances[$i]);
182         }
183
184         for ($i = 0; $i < $iLoadThreads; $i++) {
185             $sSQL = "INSERT INTO placex ($sColumns) SELECT $sColumns FROM place WHERE osm_id % $iLoadThreads = $i";
186             $sSQL .= " and not (class='place' and type='houses' and osm_type='W'";
187             $sSQL .= "          and ST_GeometryType(geometry) = 'ST_LineString')";
188             $sSQL .= ' and ST_IsValid(geometry)';
189             if ($this->bVerbose) echo "$sSQL\n";
190             if (!pg_send_query($aDBInstances[$i], $sSQL)) {
191                 fail(pg_last_error($aDBInstances[$i]));
192             }
193         }
194
195         // last thread for interpolation lines
196         // https://secure.php.net/manual/en/function.pg-connect.php
197         $DSN = getSetting('DATABASE_DSN');
198         $DSN = preg_replace('/^pgsql:/', '', $DSN);
199         $DSN = preg_replace('/;/', ' ', $DSN);
200         $aDBInstances[$iLoadThreads] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW);
201         pg_ping($aDBInstances[$iLoadThreads]);
202         $sSQL = 'insert into location_property_osmline';
203         $sSQL .= ' (osm_id, address, linegeo)';
204         $sSQL .= ' SELECT osm_id, address, geometry from place where ';
205         $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
206         if ($this->bVerbose) echo "$sSQL\n";
207         if (!pg_send_query($aDBInstances[$iLoadThreads], $sSQL)) {
208             fail(pg_last_error($aDBInstances[$iLoadThreads]));
209         }
210
211         $bFailed = false;
212         for ($i = 0; $i <= $iLoadThreads; $i++) {
213             while (($hPGresult = pg_get_result($aDBInstances[$i])) !== false) {
214                 $resultStatus = pg_result_status($hPGresult);
215                 // PGSQL_EMPTY_QUERY, PGSQL_COMMAND_OK, PGSQL_TUPLES_OK,
216                 // PGSQL_COPY_OUT, PGSQL_COPY_IN, PGSQL_BAD_RESPONSE,
217                 // PGSQL_NONFATAL_ERROR and PGSQL_FATAL_ERROR
218                 // echo 'Query result ' . $i . ' is: ' . $resultStatus . "\n";
219                 if ($resultStatus != PGSQL_COMMAND_OK && $resultStatus != PGSQL_TUPLES_OK) {
220                     $resultError = pg_result_error($hPGresult);
221                     echo '-- error text ' . $i . ': ' . $resultError . "\n";
222                     $bFailed = true;
223                 }
224             }
225         }
226         if ($bFailed) {
227             fail('SQL errors loading placex and/or location_property_osmline tables');
228         }
229
230         for ($i = 0; $i < $this->iInstances; $i++) {
231             pg_close($aDBInstances[$i]);
232         }
233
234         echo "\n";
235         info('Reanalysing database');
236         $this->pgsqlRunScript('ANALYSE');
237
238         $sDatabaseDate = getDatabaseDate($oDB);
239         $oDB->exec('TRUNCATE import_status');
240         if (!$sDatabaseDate) {
241             warn('could not determine database date.');
242         } else {
243             $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
244             $oDB->exec($sSQL);
245             echo "Latest data imported from $sDatabaseDate.\n";
246         }
247     }
248
249     public function importTigerData($sTigerPath)
250     {
251         info('Import Tiger data');
252
253         $aFilenames = glob($sTigerPath.'/*.sql');
254         info('Found '.count($aFilenames).' SQL files in path '.$sTigerPath);
255         if (empty($aFilenames)) {
256             warn('Tiger data import selected but no files found in path '.$sTigerPath);
257             return;
258         }
259         $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_start.sql');
260         $sTemplate = $this->replaceSqlPatterns($sTemplate);
261
262         $this->pgsqlRunScript($sTemplate, false);
263
264         $aDBInstances = array();
265         for ($i = 0; $i < $this->iInstances; $i++) {
266             // https://secure.php.net/manual/en/function.pg-connect.php
267             $DSN = getSetting('DATABASE_DSN');
268             $DSN = preg_replace('/^pgsql:/', '', $DSN);
269             $DSN = preg_replace('/;/', ' ', $DSN);
270             $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW | PGSQL_CONNECT_ASYNC);
271             pg_ping($aDBInstances[$i]);
272         }
273
274         foreach ($aFilenames as $sFile) {
275             echo $sFile.': ';
276             $hFile = fopen($sFile, 'r');
277             $sSQL = fgets($hFile, 100000);
278             $iLines = 0;
279             while (true) {
280                 for ($i = 0; $i < $this->iInstances; $i++) {
281                     if (!pg_connection_busy($aDBInstances[$i])) {
282                         while (pg_get_result($aDBInstances[$i]));
283                         $sSQL = fgets($hFile, 100000);
284                         if (!$sSQL) break 2;
285                         if (!pg_send_query($aDBInstances[$i], $sSQL)) fail(pg_last_error($aDBInstances[$i]));
286                         $iLines++;
287                         if ($iLines == 1000) {
288                             echo '.';
289                             $iLines = 0;
290                         }
291                     }
292                 }
293                 usleep(10);
294             }
295             fclose($hFile);
296
297             $bAnyBusy = true;
298             while ($bAnyBusy) {
299                 $bAnyBusy = false;
300                 for ($i = 0; $i < $this->iInstances; $i++) {
301                     if (pg_connection_busy($aDBInstances[$i])) $bAnyBusy = true;
302                 }
303                 usleep(10);
304             }
305             echo "\n";
306         }
307
308         for ($i = 0; $i < $this->iInstances; $i++) {
309             pg_close($aDBInstances[$i]);
310         }
311
312         info('Creating indexes on Tiger data');
313         $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_finish.sql');
314         $sTemplate = $this->replaceSqlPatterns($sTemplate);
315
316         $this->pgsqlRunScript($sTemplate, false);
317     }
318
319     public function calculatePostcodes($bCMDResultAll)
320     {
321         info('Calculate Postcodes');
322         $this->pgsqlRunScriptFile(CONST_SqlDir.'/postcode_tables.sql');
323
324         $sPostcodeFilename = CONST_InstallDir.'/gb_postcode_data.sql.gz';
325         if (file_exists($sPostcodeFilename)) {
326             $this->pgsqlRunScriptFile($sPostcodeFilename);
327         } else {
328             warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
329         }
330
331         $sPostcodeFilename = CONST_InstallDir.'/us_postcode_data.sql.gz';
332         if (file_exists($sPostcodeFilename)) {
333             $this->pgsqlRunScriptFile($sPostcodeFilename);
334         } else {
335             warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
336         }
337
338
339         $this->db()->exec('TRUNCATE location_postcode');
340
341         $sSQL  = 'INSERT INTO location_postcode';
342         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
343         $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
344         $sSQL .= "       upper(trim (both ' ' from address->'postcode')) as pc,";
345         $sSQL .= '       ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
346         $sSQL .= '  FROM placex';
347         $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
348         $sSQL .= '       AND geometry IS NOT null';
349         $sSQL .= ' GROUP BY country_code, pc';
350         $this->db()->exec($sSQL);
351
352         // only add postcodes that are not yet available in OSM
353         $sSQL  = 'INSERT INTO location_postcode';
354         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
355         $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
356         $sSQL .= '       ST_SetSRID(ST_Point(x,y),4326)';
357         $sSQL .= '  FROM us_postcode WHERE postcode NOT IN';
358         $sSQL .= '        (SELECT postcode FROM location_postcode';
359         $sSQL .= "          WHERE country_code = 'us')";
360         $this->db()->exec($sSQL);
361
362         // add missing postcodes for GB (if available)
363         $sSQL  = 'INSERT INTO location_postcode';
364         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
365         $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
366         $sSQL .= '  FROM gb_postcode WHERE postcode NOT IN';
367         $sSQL .= '           (SELECT postcode FROM location_postcode';
368         $sSQL .= "             WHERE country_code = 'gb')";
369         $this->db()->exec($sSQL);
370
371         if (!$bCMDResultAll) {
372             $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
373             $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
374             $this->db()->exec($sSQL);
375         }
376
377         $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
378         $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
379         $this->db()->exec($sSQL);
380     }
381
382     public function createSearchIndices()
383     {
384         info('Create Search indices');
385
386         $sSQL = 'SELECT relname FROM pg_class, pg_index ';
387         $sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid';
388         $aInvalidIndices = $this->db()->getCol($sSQL);
389
390         foreach ($aInvalidIndices as $sIndexName) {
391             info("Cleaning up invalid index $sIndexName");
392             $this->db()->exec("DROP INDEX $sIndexName;");
393         }
394
395         $sTemplate = file_get_contents(CONST_SqlDir.'/indices.src.sql');
396         if (!$this->bDrop) {
397             $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_updates.src.sql');
398         }
399         if (!$this->dbReverseOnly()) {
400             $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_search.src.sql');
401         }
402         $sTemplate = $this->replaceSqlPatterns($sTemplate);
403
404         $this->pgsqlRunScript($sTemplate);
405     }
406
407     public function createCountryNames()
408     {
409         info('Create search index for default country names');
410
411         $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
412         $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
413         $this->pgsqlRunScript('select count(*) from (select getorcreate_country(make_standard_name(country_code), country_code) from country_name where country_code is not null) as x');
414         $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
415         $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
416             .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
417         $sLanguages = getSetting('LANGUAGES');
418         if ($sLanguages) {
419             $sSQL .= 'in ';
420             $sDelim = '(';
421             foreach (explode(',', $sLanguages) as $sLang) {
422                 $sSQL .= $sDelim."'name:$sLang'";
423                 $sDelim = ',';
424             }
425             $sSQL .= ')';
426         } else {
427             // all include all simple name tags
428             $sSQL .= "like 'name:%'";
429         }
430         $sSQL .= ') v';
431         $this->pgsqlRunScript($sSQL);
432     }
433
434     /**
435      * Return the connection to the database.
436      *
437      * @return Database object.
438      *
439      * Creates a new connection if none exists yet. Otherwise reuses the
440      * already established connection.
441      */
442     private function db()
443     {
444         if (is_null($this->oDB)) {
445             $this->oDB = new \Nominatim\DB();
446             $this->oDB->connect();
447         }
448
449         return $this->oDB;
450     }
451
452     private function pgsqlRunScript($sScript, $bfatal = true)
453     {
454         runSQLScript(
455             $sScript,
456             $bfatal,
457             $this->bVerbose,
458             $this->sIgnoreErrors
459         );
460     }
461
462     private function createSqlFunctions()
463     {
464         $oCmd = (clone($this->oNominatimCmd))
465                 ->addParams('refresh', '--functions');
466
467         if (!$this->bEnableDiffUpdates) {
468             $oCmd->addParams('--no-diff-updates');
469         }
470
471         if ($this->bEnableDebugStatements) {
472             $oCmd->addParams('--enable-debug-statements');
473         }
474
475         $oCmd->run(!$this->sIgnoreErrors);
476     }
477
478     private function pgsqlRunPartitionScript($sTemplate)
479     {
480         $sSQL = 'select distinct partition from country_name';
481         $aPartitions = $this->db()->getCol($sSQL);
482         if (!$this->bNoPartitions) $aPartitions[] = 0;
483
484         preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
485         foreach ($aMatches as $aMatch) {
486             $sResult = '';
487             foreach ($aPartitions as $sPartitionName) {
488                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
489             }
490             $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
491         }
492
493         $this->pgsqlRunScript($sTemplate);
494     }
495
496     private function pgsqlRunScriptFile($sFilename)
497     {
498         if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
499
500         $oCmd = (new \Nominatim\Shell('psql'))
501                 ->addParams('--port', $this->aDSNInfo['port'])
502                 ->addParams('--dbname', $this->aDSNInfo['database']);
503
504         if (!$this->bVerbose) {
505             $oCmd->addParams('--quiet');
506         }
507         if (isset($this->aDSNInfo['hostspec'])) {
508             $oCmd->addParams('--host', $this->aDSNInfo['hostspec']);
509         }
510         if (isset($this->aDSNInfo['username'])) {
511             $oCmd->addParams('--username', $this->aDSNInfo['username']);
512         }
513         if (isset($this->aDSNInfo['password'])) {
514             $oCmd->addEnvPair('PGPASSWORD', $this->aDSNInfo['password']);
515         }
516         $ahGzipPipes = null;
517         if (preg_match('/\\.gz$/', $sFilename)) {
518             $aDescriptors = array(
519                              0 => array('pipe', 'r'),
520                              1 => array('pipe', 'w'),
521                              2 => array('file', '/dev/null', 'a')
522                             );
523             $oZcatCmd = new \Nominatim\Shell('zcat', $sFilename);
524
525             $hGzipProcess = proc_open($oZcatCmd->escapedCmd(), $aDescriptors, $ahGzipPipes);
526             if (!is_resource($hGzipProcess)) fail('unable to start zcat');
527             $aReadPipe = $ahGzipPipes[1];
528             fclose($ahGzipPipes[0]);
529         } else {
530             $oCmd->addParams('--file', $sFilename);
531             $aReadPipe = array('pipe', 'r');
532         }
533         $aDescriptors = array(
534                          0 => $aReadPipe,
535                          1 => array('pipe', 'w'),
536                          2 => array('file', '/dev/null', 'a')
537                         );
538         $ahPipes = null;
539
540         $hProcess = proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
541         if (!is_resource($hProcess)) fail('unable to start pgsql');
542         // TODO: error checking
543         while (!feof($ahPipes[1])) {
544             echo fread($ahPipes[1], 4096);
545         }
546         fclose($ahPipes[1]);
547         $iReturn = proc_close($hProcess);
548         if ($iReturn > 0) {
549             fail("pgsql returned with error code ($iReturn)");
550         }
551         if ($ahGzipPipes) {
552             fclose($ahGzipPipes[1]);
553             proc_close($hGzipProcess);
554         }
555     }
556
557     private function replaceSqlPatterns($sSql)
558     {
559         $sSql = str_replace('{www-user}', getSetting('DATABASE_WEBUSER'), $sSql);
560
561         $aPatterns = array(
562                       '{ts:address-data}' => getSetting('TABLESPACE_ADDRESS_DATA'),
563                       '{ts:address-index}' => getSetting('TABLESPACE_ADDRESS_INDEX'),
564                       '{ts:search-data}' => getSetting('TABLESPACE_SEARCH_DATA'),
565                       '{ts:search-index}' =>  getSetting('TABLESPACE_SEARCH_INDEX'),
566                       '{ts:aux-data}' =>  getSetting('TABLESPACE_AUX_DATA'),
567                       '{ts:aux-index}' =>  getSetting('TABLESPACE_AUX_INDEX')
568         );
569
570         foreach ($aPatterns as $sPattern => $sTablespace) {
571             if ($sTablespace) {
572                 $sSql = str_replace($sPattern, 'TABLESPACE "'.$sTablespace.'"', $sSql);
573             } else {
574                 $sSql = str_replace($sPattern, '', $sSql);
575             }
576         }
577
578         return $sSql;
579     }
580
581     /**
582      * Drop table with the given name if it exists.
583      *
584      * @param string $sName Name of table to remove.
585      *
586      * @return null
587      */
588     private function dropTable($sName)
589     {
590         if ($this->bVerbose) echo "Dropping table $sName\n";
591         $this->db()->deleteTable($sName);
592     }
593
594     /**
595      * Check if the database is in reverse-only mode.
596      *
597      * @return True if there is no search_name table and infrastructure.
598      */
599     private function dbReverseOnly()
600     {
601         return !($this->db()->tableExists('search_name'));
602     }
603
604     /**
605      * Try accessing the C module, so we know early if something is wrong.
606      *
607      * Raises Nominatim\DatabaseError on failure
608      */
609     private function checkModulePresence()
610     {
611         $sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
612         $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
613         $sSQL .= $sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
614         $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
615
616         $oDB = new \Nominatim\DB();
617         $oDB->connect();
618         $oDB->exec($sSQL, null, 'Database server failed to load '.$sModulePath.'/nominatim.so module');
619     }
620 }