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');
76 public function createFunctions()
78 info('Create Functions');
80 // Try accessing the C module, so we know early if something is wrong
81 $this->checkModulePresence(); // raises exception on failure
83 $this->createSqlFunctions();
86 public function createTables($bReverseOnly = false)
88 info('Create Tables');
90 $sTemplate = file_get_contents(CONST_SqlDir.'/tables.sql');
91 $sTemplate = $this->replaceSqlPatterns($sTemplate);
93 $this->pgsqlRunScript($sTemplate, false);
96 $this->dropTable('search_name');
99 (clone($this->oNominatimCmd))->addParams('refresh', '--address-levels')->run();
102 public function createTableTriggers()
104 info('Create Tables');
106 $sTemplate = file_get_contents(CONST_SqlDir.'/table-triggers.sql');
107 $sTemplate = $this->replaceSqlPatterns($sTemplate);
109 $this->pgsqlRunScript($sTemplate, false);
112 public function createPartitionTables()
114 info('Create Partition Tables');
116 $sTemplate = file_get_contents(CONST_SqlDir.'/partition-tables.src.sql');
117 $sTemplate = $this->replaceSqlPatterns($sTemplate);
119 $this->pgsqlRunPartitionScript($sTemplate);
122 public function importTigerData($sTigerPath)
124 info('Import Tiger data');
126 $aFilenames = glob($sTigerPath.'/*.sql');
127 info('Found '.count($aFilenames).' SQL files in path '.$sTigerPath);
128 if (empty($aFilenames)) {
129 warn('Tiger data import selected but no files found in path '.$sTigerPath);
132 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_start.sql');
133 $sTemplate = $this->replaceSqlPatterns($sTemplate);
135 $this->pgsqlRunScript($sTemplate, false);
137 $aDBInstances = array();
138 for ($i = 0; $i < $this->iInstances; $i++) {
139 // https://secure.php.net/manual/en/function.pg-connect.php
140 $DSN = getSetting('DATABASE_DSN');
141 $DSN = preg_replace('/^pgsql:/', '', $DSN);
142 $DSN = preg_replace('/;/', ' ', $DSN);
143 $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW | PGSQL_CONNECT_ASYNC);
144 pg_ping($aDBInstances[$i]);
147 foreach ($aFilenames as $sFile) {
149 $hFile = fopen($sFile, 'r');
150 $sSQL = fgets($hFile, 100000);
153 for ($i = 0; $i < $this->iInstances; $i++) {
154 if (!pg_connection_busy($aDBInstances[$i])) {
155 while (pg_get_result($aDBInstances[$i]));
156 $sSQL = fgets($hFile, 100000);
158 if (!pg_send_query($aDBInstances[$i], $sSQL)) fail(pg_last_error($aDBInstances[$i]));
160 if ($iLines == 1000) {
173 for ($i = 0; $i < $this->iInstances; $i++) {
174 if (pg_connection_busy($aDBInstances[$i])) $bAnyBusy = true;
181 for ($i = 0; $i < $this->iInstances; $i++) {
182 pg_close($aDBInstances[$i]);
185 info('Creating indexes on Tiger data');
186 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_finish.sql');
187 $sTemplate = $this->replaceSqlPatterns($sTemplate);
189 $this->pgsqlRunScript($sTemplate, false);
192 public function calculatePostcodes($bCMDResultAll)
194 info('Calculate Postcodes');
195 $this->pgsqlRunScriptFile(CONST_SqlDir.'/postcode_tables.sql');
197 $sPostcodeFilename = CONST_InstallDir.'/gb_postcode_data.sql.gz';
198 if (file_exists($sPostcodeFilename)) {
199 $this->pgsqlRunScriptFile($sPostcodeFilename);
201 warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
204 $sPostcodeFilename = CONST_InstallDir.'/us_postcode_data.sql.gz';
205 if (file_exists($sPostcodeFilename)) {
206 $this->pgsqlRunScriptFile($sPostcodeFilename);
208 warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
212 $this->db()->exec('TRUNCATE location_postcode');
214 $sSQL = 'INSERT INTO location_postcode';
215 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
216 $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
217 $sSQL .= " upper(trim (both ' ' from address->'postcode')) as pc,";
218 $sSQL .= ' ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
219 $sSQL .= ' FROM placex';
220 $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
221 $sSQL .= ' AND geometry IS NOT null';
222 $sSQL .= ' GROUP BY country_code, pc';
223 $this->db()->exec($sSQL);
225 // only add postcodes that are not yet available in OSM
226 $sSQL = 'INSERT INTO location_postcode';
227 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
228 $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
229 $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
230 $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
231 $sSQL .= ' (SELECT postcode FROM location_postcode';
232 $sSQL .= " WHERE country_code = 'us')";
233 $this->db()->exec($sSQL);
235 // add missing postcodes for GB (if available)
236 $sSQL = 'INSERT INTO location_postcode';
237 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
238 $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
239 $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
240 $sSQL .= ' (SELECT postcode FROM location_postcode';
241 $sSQL .= " WHERE country_code = 'gb')";
242 $this->db()->exec($sSQL);
244 if (!$bCMDResultAll) {
245 $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
246 $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
247 $this->db()->exec($sSQL);
250 $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
251 $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
252 $this->db()->exec($sSQL);
255 public function createSearchIndices()
257 info('Create Search indices');
259 $sSQL = 'SELECT relname FROM pg_class, pg_index ';
260 $sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid';
261 $aInvalidIndices = $this->db()->getCol($sSQL);
263 foreach ($aInvalidIndices as $sIndexName) {
264 info("Cleaning up invalid index $sIndexName");
265 $this->db()->exec("DROP INDEX $sIndexName;");
268 $sTemplate = file_get_contents(CONST_SqlDir.'/indices.src.sql');
270 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_updates.src.sql');
272 if (!$this->dbReverseOnly()) {
273 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_search.src.sql');
275 $sTemplate = $this->replaceSqlPatterns($sTemplate);
277 $this->pgsqlRunScript($sTemplate);
280 public function createCountryNames()
282 info('Create search index for default country names');
284 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
285 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
286 $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');
287 $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
288 $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
289 .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
290 $sLanguages = getSetting('LANGUAGES');
294 foreach (explode(',', $sLanguages) as $sLang) {
295 $sSQL .= $sDelim."'name:$sLang'";
300 // all include all simple name tags
301 $sSQL .= "like 'name:%'";
304 $this->pgsqlRunScript($sSQL);
308 * Return the connection to the database.
310 * @return Database object.
312 * Creates a new connection if none exists yet. Otherwise reuses the
313 * already established connection.
315 private function db()
317 if (is_null($this->oDB)) {
318 $this->oDB = new \Nominatim\DB();
319 $this->oDB->connect();
325 private function pgsqlRunScript($sScript, $bfatal = true)
335 private function createSqlFunctions()
337 $oCmd = (clone($this->oNominatimCmd))
338 ->addParams('refresh', '--functions');
340 if (!$this->bEnableDiffUpdates) {
341 $oCmd->addParams('--no-diff-updates');
344 if ($this->bEnableDebugStatements) {
345 $oCmd->addParams('--enable-debug-statements');
348 $oCmd->run(!$this->sIgnoreErrors);
351 private function pgsqlRunPartitionScript($sTemplate)
353 $sSQL = 'select distinct partition from country_name';
354 $aPartitions = $this->db()->getCol($sSQL);
355 if (!$this->bNoPartitions) $aPartitions[] = 0;
357 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
358 foreach ($aMatches as $aMatch) {
360 foreach ($aPartitions as $sPartitionName) {
361 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
363 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
366 $this->pgsqlRunScript($sTemplate);
369 private function pgsqlRunScriptFile($sFilename)
371 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
373 $oCmd = (new \Nominatim\Shell('psql'))
374 ->addParams('--port', $this->aDSNInfo['port'])
375 ->addParams('--dbname', $this->aDSNInfo['database']);
377 if (!$this->bVerbose) {
378 $oCmd->addParams('--quiet');
380 if (isset($this->aDSNInfo['hostspec'])) {
381 $oCmd->addParams('--host', $this->aDSNInfo['hostspec']);
383 if (isset($this->aDSNInfo['username'])) {
384 $oCmd->addParams('--username', $this->aDSNInfo['username']);
386 if (isset($this->aDSNInfo['password'])) {
387 $oCmd->addEnvPair('PGPASSWORD', $this->aDSNInfo['password']);
390 if (preg_match('/\\.gz$/', $sFilename)) {
391 $aDescriptors = array(
392 0 => array('pipe', 'r'),
393 1 => array('pipe', 'w'),
394 2 => array('file', '/dev/null', 'a')
396 $oZcatCmd = new \Nominatim\Shell('zcat', $sFilename);
398 $hGzipProcess = proc_open($oZcatCmd->escapedCmd(), $aDescriptors, $ahGzipPipes);
399 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
400 $aReadPipe = $ahGzipPipes[1];
401 fclose($ahGzipPipes[0]);
403 $oCmd->addParams('--file', $sFilename);
404 $aReadPipe = array('pipe', 'r');
406 $aDescriptors = array(
408 1 => array('pipe', 'w'),
409 2 => array('file', '/dev/null', 'a')
413 $hProcess = proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
414 if (!is_resource($hProcess)) fail('unable to start pgsql');
415 // TODO: error checking
416 while (!feof($ahPipes[1])) {
417 echo fread($ahPipes[1], 4096);
420 $iReturn = proc_close($hProcess);
422 fail("pgsql returned with error code ($iReturn)");
425 fclose($ahGzipPipes[1]);
426 proc_close($hGzipProcess);
430 private function replaceSqlPatterns($sSql)
432 $sSql = str_replace('{www-user}', getSetting('DATABASE_WEBUSER'), $sSql);
435 '{ts:address-data}' => getSetting('TABLESPACE_ADDRESS_DATA'),
436 '{ts:address-index}' => getSetting('TABLESPACE_ADDRESS_INDEX'),
437 '{ts:search-data}' => getSetting('TABLESPACE_SEARCH_DATA'),
438 '{ts:search-index}' => getSetting('TABLESPACE_SEARCH_INDEX'),
439 '{ts:aux-data}' => getSetting('TABLESPACE_AUX_DATA'),
440 '{ts:aux-index}' => getSetting('TABLESPACE_AUX_INDEX')
443 foreach ($aPatterns as $sPattern => $sTablespace) {
445 $sSql = str_replace($sPattern, 'TABLESPACE "'.$sTablespace.'"', $sSql);
447 $sSql = str_replace($sPattern, '', $sSql);
455 * Drop table with the given name if it exists.
457 * @param string $sName Name of table to remove.
461 private function dropTable($sName)
463 if ($this->bVerbose) echo "Dropping table $sName\n";
464 $this->db()->deleteTable($sName);
468 * Check if the database is in reverse-only mode.
470 * @return True if there is no search_name table and infrastructure.
472 private function dbReverseOnly()
474 return !($this->db()->tableExists('search_name'));
478 * Try accessing the C module, so we know early if something is wrong.
480 * Raises Nominatim\DatabaseError on failure
482 private function checkModulePresence()
484 $sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
485 $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
486 $sSQL .= $sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
487 $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
489 $oDB = new \Nominatim\DB();
491 $oDB->exec($sSQL, null, 'Database server failed to load '.$sModulePath.'/nominatim.so module');