3 namespace Nominatim\Setup;
5 require_once(CONST_LibDir.'/Shell.php');
13 protected $sIgnoreErrors;
14 protected $bEnableDiffUpdates;
15 protected $bEnableDebugStatements;
16 protected $bNoPartitions;
18 protected $oDB = null;
19 protected $oNominatimCmd;
21 public function __construct(array $aCMDResult)
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);
28 if ($this->iInstances < 1) {
29 $this->iInstances = 1;
30 warn('resetting threads to '.$this->iInstances);
33 // parse database string
34 $this->aDSNInfo = \Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
35 if (!isset($this->aDSNInfo['port'])) {
36 $this->aDSNInfo['port'] = 5432;
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'];
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'];
47 $this->sIgnoreErrors = false;
49 if (isset($aCMDResult['enable-debug-statements'])) {
50 $this->bEnableDebugStatements = $aCMDResult['enable-debug-statements'];
52 $this->bEnableDebugStatements = false;
54 if (isset($aCMDResult['no-partitions'])) {
55 $this->bNoPartitions = $aCMDResult['no-partitions'];
57 $this->bNoPartitions = false;
59 if (isset($aCMDResult['enable-diff-updates'])) {
60 $this->bEnableDiffUpdates = $aCMDResult['enable-diff-updates'];
62 $this->bEnableDiffUpdates = false;
65 $this->bDrop = isset($aCMDResult['drop']) && $aCMDResult['drop'];
67 $this->oNominatimCmd = new \Nominatim\Shell(getSetting('NOMINATIM_TOOL'));
69 $this->oNominatimCmd->addParams('--quiet');
71 if ($this->bVerbose) {
72 $this->oNominatimCmd->addParams('--verbose');
74 $this->oNominatimCmd->addParams('--threads', $this->iInstances);
77 public function createFunctions()
79 info('Create Functions');
81 // Try accessing the C module, so we know early if something is wrong
82 $this->checkModulePresence(); // raises exception on failure
84 $this->createSqlFunctions();
87 public function createTables($bReverseOnly = false)
89 info('Create Tables');
91 $sTemplate = file_get_contents(CONST_SqlDir.'/tables.sql');
92 $sTemplate = $this->replaceSqlPatterns($sTemplate);
94 $this->pgsqlRunScript($sTemplate, false);
97 $this->dropTable('search_name');
100 (clone($this->oNominatimCmd))->addParams('refresh', '--address-levels')->run();
103 public function createTableTriggers()
105 info('Create Tables');
107 $sTemplate = file_get_contents(CONST_SqlDir.'/table-triggers.sql');
108 $sTemplate = $this->replaceSqlPatterns($sTemplate);
110 $this->pgsqlRunScript($sTemplate, false);
113 public function createPartitionTables()
115 info('Create Partition Tables');
117 $sTemplate = file_get_contents(CONST_SqlDir.'/partition-tables.src.sql');
118 $sTemplate = $this->replaceSqlPatterns($sTemplate);
120 $this->pgsqlRunPartitionScript($sTemplate);
123 public function createPartitionFunctions()
125 info('Create Partition Functions');
126 $this->createSqlFunctions(); // also create partition functions
129 public function loadData($bDisableTokenPrecalc)
131 info('Drop old Data');
135 $oDB->exec('TRUNCATE word');
137 $oDB->exec('TRUNCATE placex');
139 $oDB->exec('TRUNCATE location_property_osmline');
141 $oDB->exec('TRUNCATE place_addressline');
143 $oDB->exec('TRUNCATE location_area');
145 if (!$this->dbReverseOnly()) {
146 $oDB->exec('TRUNCATE search_name');
149 $oDB->exec('TRUNCATE search_name_blank');
151 $oDB->exec('DROP SEQUENCE seq_place');
153 $oDB->exec('CREATE SEQUENCE seq_place start 100000');
156 $sSQL = 'select distinct partition from country_name';
157 $aPartitions = $oDB->getCol($sSQL);
159 if (!$this->bNoPartitions) $aPartitions[] = 0;
160 foreach ($aPartitions as $sPartition) {
161 $oDB->exec('TRUNCATE location_road_'.$sPartition);
165 // used by getorcreate_word_id to ignore frequent partial words
166 $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
167 $sSQL .= '$$ SELECT '.getSetting('MAX_WORD_FREQUENCY').' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
171 // pre-create the word list
172 if (!$bDisableTokenPrecalc) {
173 info('Loading word list');
174 $this->pgsqlRunScriptFile(CONST_DataDir.'/words.sql');
178 $sColumns = 'osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry';
180 $aDBInstances = array();
181 $iLoadThreads = max(1, $this->iInstances - 1);
182 for ($i = 0; $i < $iLoadThreads; $i++) {
183 // https://secure.php.net/manual/en/function.pg-connect.php
184 $DSN = getSetting('DATABASE_DSN');
185 $DSN = preg_replace('/^pgsql:/', '', $DSN);
186 $DSN = preg_replace('/;/', ' ', $DSN);
187 $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW);
188 pg_ping($aDBInstances[$i]);
191 for ($i = 0; $i < $iLoadThreads; $i++) {
192 $sSQL = "INSERT INTO placex ($sColumns) SELECT $sColumns FROM place WHERE osm_id % $iLoadThreads = $i";
193 $sSQL .= " and not (class='place' and type='houses' and osm_type='W'";
194 $sSQL .= " and ST_GeometryType(geometry) = 'ST_LineString')";
195 $sSQL .= ' and ST_IsValid(geometry)';
196 if ($this->bVerbose) echo "$sSQL\n";
197 if (!pg_send_query($aDBInstances[$i], $sSQL)) {
198 fail(pg_last_error($aDBInstances[$i]));
202 // last thread for interpolation lines
203 // https://secure.php.net/manual/en/function.pg-connect.php
204 $DSN = getSetting('DATABASE_DSN');
205 $DSN = preg_replace('/^pgsql:/', '', $DSN);
206 $DSN = preg_replace('/;/', ' ', $DSN);
207 $aDBInstances[$iLoadThreads] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW);
208 pg_ping($aDBInstances[$iLoadThreads]);
209 $sSQL = 'insert into location_property_osmline';
210 $sSQL .= ' (osm_id, address, linegeo)';
211 $sSQL .= ' SELECT osm_id, address, geometry from place where ';
212 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
213 if ($this->bVerbose) echo "$sSQL\n";
214 if (!pg_send_query($aDBInstances[$iLoadThreads], $sSQL)) {
215 fail(pg_last_error($aDBInstances[$iLoadThreads]));
219 for ($i = 0; $i <= $iLoadThreads; $i++) {
220 while (($hPGresult = pg_get_result($aDBInstances[$i])) !== false) {
221 $resultStatus = pg_result_status($hPGresult);
222 // PGSQL_EMPTY_QUERY, PGSQL_COMMAND_OK, PGSQL_TUPLES_OK,
223 // PGSQL_COPY_OUT, PGSQL_COPY_IN, PGSQL_BAD_RESPONSE,
224 // PGSQL_NONFATAL_ERROR and PGSQL_FATAL_ERROR
225 // echo 'Query result ' . $i . ' is: ' . $resultStatus . "\n";
226 if ($resultStatus != PGSQL_COMMAND_OK && $resultStatus != PGSQL_TUPLES_OK) {
227 $resultError = pg_result_error($hPGresult);
228 echo '-- error text ' . $i . ': ' . $resultError . "\n";
234 fail('SQL errors loading placex and/or location_property_osmline tables');
237 for ($i = 0; $i < $this->iInstances; $i++) {
238 pg_close($aDBInstances[$i]);
242 info('Reanalysing database');
243 $this->pgsqlRunScript('ANALYSE');
245 $sDatabaseDate = getDatabaseDate($oDB);
246 $oDB->exec('TRUNCATE import_status');
247 if (!$sDatabaseDate) {
248 warn('could not determine database date.');
250 $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
252 echo "Latest data imported from $sDatabaseDate.\n";
256 public function importTigerData($sTigerPath)
258 info('Import Tiger data');
260 $aFilenames = glob($sTigerPath.'/*.sql');
261 info('Found '.count($aFilenames).' SQL files in path '.$sTigerPath);
262 if (empty($aFilenames)) {
263 warn('Tiger data import selected but no files found in path '.$sTigerPath);
266 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_start.sql');
267 $sTemplate = $this->replaceSqlPatterns($sTemplate);
269 $this->pgsqlRunScript($sTemplate, false);
271 $aDBInstances = array();
272 for ($i = 0; $i < $this->iInstances; $i++) {
273 // https://secure.php.net/manual/en/function.pg-connect.php
274 $DSN = getSetting('DATABASE_DSN');
275 $DSN = preg_replace('/^pgsql:/', '', $DSN);
276 $DSN = preg_replace('/;/', ' ', $DSN);
277 $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW | PGSQL_CONNECT_ASYNC);
278 pg_ping($aDBInstances[$i]);
281 foreach ($aFilenames as $sFile) {
283 $hFile = fopen($sFile, 'r');
284 $sSQL = fgets($hFile, 100000);
287 for ($i = 0; $i < $this->iInstances; $i++) {
288 if (!pg_connection_busy($aDBInstances[$i])) {
289 while (pg_get_result($aDBInstances[$i]));
290 $sSQL = fgets($hFile, 100000);
292 if (!pg_send_query($aDBInstances[$i], $sSQL)) fail(pg_last_error($aDBInstances[$i]));
294 if ($iLines == 1000) {
307 for ($i = 0; $i < $this->iInstances; $i++) {
308 if (pg_connection_busy($aDBInstances[$i])) $bAnyBusy = true;
315 for ($i = 0; $i < $this->iInstances; $i++) {
316 pg_close($aDBInstances[$i]);
319 info('Creating indexes on Tiger data');
320 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_finish.sql');
321 $sTemplate = $this->replaceSqlPatterns($sTemplate);
323 $this->pgsqlRunScript($sTemplate, false);
326 public function calculatePostcodes($bCMDResultAll)
328 info('Calculate Postcodes');
329 $this->pgsqlRunScriptFile(CONST_SqlDir.'/postcode_tables.sql');
331 $sPostcodeFilename = CONST_InstallDir.'/gb_postcode_data.sql.gz';
332 if (file_exists($sPostcodeFilename)) {
333 $this->pgsqlRunScriptFile($sPostcodeFilename);
335 warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
338 $sPostcodeFilename = CONST_InstallDir.'/us_postcode_data.sql.gz';
339 if (file_exists($sPostcodeFilename)) {
340 $this->pgsqlRunScriptFile($sPostcodeFilename);
342 warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
346 $this->db()->exec('TRUNCATE location_postcode');
348 $sSQL = 'INSERT INTO location_postcode';
349 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
350 $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
351 $sSQL .= " upper(trim (both ' ' from address->'postcode')) as pc,";
352 $sSQL .= ' ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
353 $sSQL .= ' FROM placex';
354 $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
355 $sSQL .= ' AND geometry IS NOT null';
356 $sSQL .= ' GROUP BY country_code, pc';
357 $this->db()->exec($sSQL);
359 // only add postcodes that are not yet available in OSM
360 $sSQL = 'INSERT INTO location_postcode';
361 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
362 $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
363 $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
364 $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
365 $sSQL .= ' (SELECT postcode FROM location_postcode';
366 $sSQL .= " WHERE country_code = 'us')";
367 $this->db()->exec($sSQL);
369 // add missing postcodes for GB (if available)
370 $sSQL = 'INSERT INTO location_postcode';
371 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
372 $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
373 $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
374 $sSQL .= ' (SELECT postcode FROM location_postcode';
375 $sSQL .= " WHERE country_code = 'gb')";
376 $this->db()->exec($sSQL);
378 if (!$bCMDResultAll) {
379 $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
380 $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
381 $this->db()->exec($sSQL);
384 $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
385 $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
386 $this->db()->exec($sSQL);
389 public function index($bIndexNoanalyse)
391 $this->checkModulePresence(); // raises exception on failure
393 $oBaseCmd = (clone $this->oNominatimCmd)->addParams('index');
395 info('Index ranks 0 - 4');
396 $oCmd = (clone $oBaseCmd)->addParams('--maxrank', 4);
398 $iStatus = $oCmd->run();
400 fail('error status ' . $iStatus . ' running nominatim!');
402 if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
404 info('Index administrative boundaries');
405 $oCmd = (clone $oBaseCmd)->addParams('--boundaries-only');
406 $iStatus = $oCmd->run();
408 fail('error status ' . $iStatus . ' running nominatim!');
411 info('Index ranks 5 - 25');
412 $oCmd = (clone $oBaseCmd)->addParams('--no-boundaries', '--minrank', 5, '--maxrank', 25);
413 $iStatus = $oCmd->run();
415 fail('error status ' . $iStatus . ' running nominatim!');
418 if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
420 info('Index ranks 26 - 30');
421 $oCmd = (clone $oBaseCmd)->addParams('--no-boundaries', '--minrank', 26);
422 $iStatus = $oCmd->run();
424 fail('error status ' . $iStatus . ' running nominatim!');
427 info('Index postcodes');
428 $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
429 $this->db()->exec($sSQL);
432 public function createSearchIndices()
434 info('Create Search indices');
436 $sSQL = 'SELECT relname FROM pg_class, pg_index ';
437 $sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid';
438 $aInvalidIndices = $this->db()->getCol($sSQL);
440 foreach ($aInvalidIndices as $sIndexName) {
441 info("Cleaning up invalid index $sIndexName");
442 $this->db()->exec("DROP INDEX $sIndexName;");
445 $sTemplate = file_get_contents(CONST_SqlDir.'/indices.src.sql');
447 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_updates.src.sql');
449 if (!$this->dbReverseOnly()) {
450 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_search.src.sql');
452 $sTemplate = $this->replaceSqlPatterns($sTemplate);
454 $this->pgsqlRunScript($sTemplate);
457 public function createCountryNames()
459 info('Create search index for default country names');
461 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
462 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
463 $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');
464 $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
465 $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
466 .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
467 $sLanguages = getSetting('LANGUAGES');
471 foreach (explode(',', $sLanguages) as $sLang) {
472 $sSQL .= $sDelim."'name:$sLang'";
477 // all include all simple name tags
478 $sSQL .= "like 'name:%'";
481 $this->pgsqlRunScript($sSQL);
485 * Return the connection to the database.
487 * @return Database object.
489 * Creates a new connection if none exists yet. Otherwise reuses the
490 * already established connection.
492 private function db()
494 if (is_null($this->oDB)) {
495 $this->oDB = new \Nominatim\DB();
496 $this->oDB->connect();
502 private function pgsqlRunScript($sScript, $bfatal = true)
512 private function createSqlFunctions()
514 $oCmd = (clone($this->oNominatimCmd))
515 ->addParams('refresh', '--functions');
517 if (!$this->bEnableDiffUpdates) {
518 $oCmd->addParams('--no-diff-updates');
521 if ($this->bEnableDebugStatements) {
522 $oCmd->addParams('--enable-debug-statements');
525 $oCmd->run(!$this->sIgnoreErrors);
528 private function pgsqlRunPartitionScript($sTemplate)
530 $sSQL = 'select distinct partition from country_name';
531 $aPartitions = $this->db()->getCol($sSQL);
532 if (!$this->bNoPartitions) $aPartitions[] = 0;
534 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
535 foreach ($aMatches as $aMatch) {
537 foreach ($aPartitions as $sPartitionName) {
538 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
540 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
543 $this->pgsqlRunScript($sTemplate);
546 private function pgsqlRunScriptFile($sFilename)
548 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
550 $oCmd = (new \Nominatim\Shell('psql'))
551 ->addParams('--port', $this->aDSNInfo['port'])
552 ->addParams('--dbname', $this->aDSNInfo['database']);
554 if (!$this->bVerbose) {
555 $oCmd->addParams('--quiet');
557 if (isset($this->aDSNInfo['hostspec'])) {
558 $oCmd->addParams('--host', $this->aDSNInfo['hostspec']);
560 if (isset($this->aDSNInfo['username'])) {
561 $oCmd->addParams('--username', $this->aDSNInfo['username']);
563 if (isset($this->aDSNInfo['password'])) {
564 $oCmd->addEnvPair('PGPASSWORD', $this->aDSNInfo['password']);
567 if (preg_match('/\\.gz$/', $sFilename)) {
568 $aDescriptors = array(
569 0 => array('pipe', 'r'),
570 1 => array('pipe', 'w'),
571 2 => array('file', '/dev/null', 'a')
573 $oZcatCmd = new \Nominatim\Shell('zcat', $sFilename);
575 $hGzipProcess = proc_open($oZcatCmd->escapedCmd(), $aDescriptors, $ahGzipPipes);
576 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
577 $aReadPipe = $ahGzipPipes[1];
578 fclose($ahGzipPipes[0]);
580 $oCmd->addParams('--file', $sFilename);
581 $aReadPipe = array('pipe', 'r');
583 $aDescriptors = array(
585 1 => array('pipe', 'w'),
586 2 => array('file', '/dev/null', 'a')
590 $hProcess = proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
591 if (!is_resource($hProcess)) fail('unable to start pgsql');
592 // TODO: error checking
593 while (!feof($ahPipes[1])) {
594 echo fread($ahPipes[1], 4096);
597 $iReturn = proc_close($hProcess);
599 fail("pgsql returned with error code ($iReturn)");
602 fclose($ahGzipPipes[1]);
603 proc_close($hGzipProcess);
607 private function replaceSqlPatterns($sSql)
609 $sSql = str_replace('{www-user}', getSetting('DATABASE_WEBUSER'), $sSql);
612 '{ts:address-data}' => getSetting('TABLESPACE_ADDRESS_DATA'),
613 '{ts:address-index}' => getSetting('TABLESPACE_ADDRESS_INDEX'),
614 '{ts:search-data}' => getSetting('TABLESPACE_SEARCH_DATA'),
615 '{ts:search-index}' => getSetting('TABLESPACE_SEARCH_INDEX'),
616 '{ts:aux-data}' => getSetting('TABLESPACE_AUX_DATA'),
617 '{ts:aux-index}' => getSetting('TABLESPACE_AUX_INDEX')
620 foreach ($aPatterns as $sPattern => $sTablespace) {
622 $sSql = str_replace($sPattern, 'TABLESPACE "'.$sTablespace.'"', $sSql);
624 $sSql = str_replace($sPattern, '', $sSql);
632 * Drop table with the given name if it exists.
634 * @param string $sName Name of table to remove.
638 private function dropTable($sName)
640 if ($this->bVerbose) echo "Dropping table $sName\n";
641 $this->db()->deleteTable($sName);
645 * Check if the database is in reverse-only mode.
647 * @return True if there is no search_name table and infrastructure.
649 private function dbReverseOnly()
651 return !($this->db()->tableExists('search_name'));
655 * Try accessing the C module, so we know early if something is wrong.
657 * Raises Nominatim\DatabaseError on failure
659 private function checkModulePresence()
661 $sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
662 $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
663 $sSQL .= $sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
664 $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
666 $oDB = new \Nominatim\DB();
668 $oDB->exec($sSQL, null, 'Database server failed to load '.$sModulePath.'/nominatim.so module');