3 namespace Nominatim\Setup;
5 require_once(CONST_LibDir.'/Shell.php');
13 protected $sIgnoreErrors;
14 protected $bEnableDiffUpdates;
15 protected $bEnableDebugStatements;
17 protected $oDB = null;
18 protected $oNominatimCmd;
20 public function __construct(array $aCMDResult)
22 // by default, use all but one processor, but never more than 15.
23 $this->iInstances = isset($aCMDResult['threads'])
24 ? $aCMDResult['threads']
25 : (min(16, getProcessorCount()) - 1);
27 if ($this->iInstances < 1) {
28 $this->iInstances = 1;
29 warn('resetting threads to '.$this->iInstances);
32 // parse database string
33 $this->aDSNInfo = \Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
34 if (!isset($this->aDSNInfo['port'])) {
35 $this->aDSNInfo['port'] = 5432;
38 // setting member variables based on command line options stored in $aCMDResult
39 $this->bQuiet = isset($aCMDResult['quiet']) && $aCMDResult['quiet'];
40 $this->bVerbose = $aCMDResult['verbose'];
42 //setting default values which are not set by the update.php array
43 if (isset($aCMDResult['ignore-errors'])) {
44 $this->sIgnoreErrors = $aCMDResult['ignore-errors'];
46 $this->sIgnoreErrors = false;
48 if (isset($aCMDResult['enable-debug-statements'])) {
49 $this->bEnableDebugStatements = $aCMDResult['enable-debug-statements'];
51 $this->bEnableDebugStatements = false;
53 if (isset($aCMDResult['enable-diff-updates'])) {
54 $this->bEnableDiffUpdates = $aCMDResult['enable-diff-updates'];
56 $this->bEnableDiffUpdates = false;
59 $this->bDrop = isset($aCMDResult['drop']) && $aCMDResult['drop'];
61 $this->oNominatimCmd = new \Nominatim\Shell(getSetting('NOMINATIM_TOOL'));
63 $this->oNominatimCmd->addParams('--quiet');
65 if ($this->bVerbose) {
66 $this->oNominatimCmd->addParams('--verbose');
70 public function createFunctions()
72 info('Create Functions');
74 // Try accessing the C module, so we know early if something is wrong
75 $this->checkModulePresence(); // raises exception on failure
77 $this->createSqlFunctions();
80 public function createTables($bReverseOnly = false)
82 info('Create Tables');
84 $sTemplate = file_get_contents(CONST_SqlDir.'/tables.sql');
85 $sTemplate = $this->replaceSqlPatterns($sTemplate);
87 $this->pgsqlRunScript($sTemplate, false);
90 $this->dropTable('search_name');
93 (clone($this->oNominatimCmd))->addParams('refresh', '--address-levels')->run();
96 public function createTableTriggers()
98 info('Create Tables');
100 $sTemplate = file_get_contents(CONST_SqlDir.'/table-triggers.sql');
101 $sTemplate = $this->replaceSqlPatterns($sTemplate);
103 $this->pgsqlRunScript($sTemplate, false);
106 public function createPartitionTables()
108 info('Create Partition Tables');
110 $sTemplate = file_get_contents(CONST_SqlDir.'/partition-tables.src.sql');
111 $sTemplate = $this->replaceSqlPatterns($sTemplate);
113 $this->pgsqlRunPartitionScript($sTemplate);
116 public function importTigerData($sTigerPath)
118 info('Import Tiger data');
120 $aFilenames = glob($sTigerPath.'/*.sql');
121 info('Found '.count($aFilenames).' SQL files in path '.$sTigerPath);
122 if (empty($aFilenames)) {
123 warn('Tiger data import selected but no files found in path '.$sTigerPath);
126 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_start.sql');
127 $sTemplate = $this->replaceSqlPatterns($sTemplate);
129 $this->pgsqlRunScript($sTemplate, false);
131 $aDBInstances = array();
132 for ($i = 0; $i < $this->iInstances; $i++) {
133 // https://secure.php.net/manual/en/function.pg-connect.php
134 $DSN = getSetting('DATABASE_DSN');
135 $DSN = preg_replace('/^pgsql:/', '', $DSN);
136 $DSN = preg_replace('/;/', ' ', $DSN);
137 $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW | PGSQL_CONNECT_ASYNC);
138 pg_ping($aDBInstances[$i]);
141 foreach ($aFilenames as $sFile) {
143 $hFile = fopen($sFile, 'r');
144 $sSQL = fgets($hFile, 100000);
147 for ($i = 0; $i < $this->iInstances; $i++) {
148 if (!pg_connection_busy($aDBInstances[$i])) {
149 while (pg_get_result($aDBInstances[$i]));
150 $sSQL = fgets($hFile, 100000);
152 if (!pg_send_query($aDBInstances[$i], $sSQL)) fail(pg_last_error($aDBInstances[$i]));
154 if ($iLines == 1000) {
167 for ($i = 0; $i < $this->iInstances; $i++) {
168 if (pg_connection_busy($aDBInstances[$i])) $bAnyBusy = true;
175 for ($i = 0; $i < $this->iInstances; $i++) {
176 pg_close($aDBInstances[$i]);
179 info('Creating indexes on Tiger data');
180 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_finish.sql');
181 $sTemplate = $this->replaceSqlPatterns($sTemplate);
183 $this->pgsqlRunScript($sTemplate, false);
186 public function calculatePostcodes($bCMDResultAll)
188 info('Calculate Postcodes');
189 $this->pgsqlRunScriptFile(CONST_SqlDir.'/postcode_tables.sql');
191 $sPostcodeFilename = CONST_InstallDir.'/gb_postcode_data.sql.gz';
192 if (file_exists($sPostcodeFilename)) {
193 $this->pgsqlRunScriptFile($sPostcodeFilename);
195 warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
198 $sPostcodeFilename = CONST_InstallDir.'/us_postcode_data.sql.gz';
199 if (file_exists($sPostcodeFilename)) {
200 $this->pgsqlRunScriptFile($sPostcodeFilename);
202 warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
206 $this->db()->exec('TRUNCATE location_postcode');
208 $sSQL = 'INSERT INTO location_postcode';
209 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
210 $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
211 $sSQL .= " upper(trim (both ' ' from address->'postcode')) as pc,";
212 $sSQL .= ' ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
213 $sSQL .= ' FROM placex';
214 $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
215 $sSQL .= ' AND geometry IS NOT null';
216 $sSQL .= ' GROUP BY country_code, pc';
217 $this->db()->exec($sSQL);
219 // only add postcodes that are not yet available in OSM
220 $sSQL = 'INSERT INTO location_postcode';
221 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
222 $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
223 $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
224 $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
225 $sSQL .= ' (SELECT postcode FROM location_postcode';
226 $sSQL .= " WHERE country_code = 'us')";
227 $this->db()->exec($sSQL);
229 // add missing postcodes for GB (if available)
230 $sSQL = 'INSERT INTO location_postcode';
231 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
232 $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
233 $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
234 $sSQL .= ' (SELECT postcode FROM location_postcode';
235 $sSQL .= " WHERE country_code = 'gb')";
236 $this->db()->exec($sSQL);
238 if (!$bCMDResultAll) {
239 $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
240 $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
241 $this->db()->exec($sSQL);
244 $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
245 $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
246 $this->db()->exec($sSQL);
249 public function createSearchIndices()
251 info('Create Search indices');
253 $sSQL = 'SELECT relname FROM pg_class, pg_index ';
254 $sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid';
255 $aInvalidIndices = $this->db()->getCol($sSQL);
257 foreach ($aInvalidIndices as $sIndexName) {
258 info("Cleaning up invalid index $sIndexName");
259 $this->db()->exec("DROP INDEX $sIndexName;");
262 $sTemplate = file_get_contents(CONST_SqlDir.'/indices.src.sql');
264 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_updates.src.sql');
266 if (!$this->dbReverseOnly()) {
267 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_search.src.sql');
269 $sTemplate = $this->replaceSqlPatterns($sTemplate);
271 $this->pgsqlRunScript($sTemplate);
274 public function createCountryNames()
276 info('Create search index for default country names');
278 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
279 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
280 $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');
281 $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
282 $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
283 .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
284 $sLanguages = getSetting('LANGUAGES');
288 foreach (explode(',', $sLanguages) as $sLang) {
289 $sSQL .= $sDelim."'name:$sLang'";
294 // all include all simple name tags
295 $sSQL .= "like 'name:%'";
298 $this->pgsqlRunScript($sSQL);
302 * Return the connection to the database.
304 * @return Database object.
306 * Creates a new connection if none exists yet. Otherwise reuses the
307 * already established connection.
309 private function db()
311 if (is_null($this->oDB)) {
312 $this->oDB = new \Nominatim\DB();
313 $this->oDB->connect();
319 private function pgsqlRunScript($sScript, $bfatal = true)
329 private function createSqlFunctions()
331 $oCmd = (clone($this->oNominatimCmd))
332 ->addParams('refresh', '--functions');
334 if (!$this->bEnableDiffUpdates) {
335 $oCmd->addParams('--no-diff-updates');
338 if ($this->bEnableDebugStatements) {
339 $oCmd->addParams('--enable-debug-statements');
342 $oCmd->run(!$this->sIgnoreErrors);
345 private function pgsqlRunPartitionScript($sTemplate)
347 $sSQL = 'select distinct partition from country_name order by partition';
348 $aPartitions = $this->db()->getCol($sSQL);
349 if ($aPartitions[0] != 0) $aPartitions[] = 0;
351 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
352 foreach ($aMatches as $aMatch) {
354 foreach ($aPartitions as $sPartitionName) {
355 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
357 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
360 $this->pgsqlRunScript($sTemplate);
363 private function pgsqlRunScriptFile($sFilename)
365 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
367 $oCmd = (new \Nominatim\Shell('psql'))
368 ->addParams('--port', $this->aDSNInfo['port'])
369 ->addParams('--dbname', $this->aDSNInfo['database']);
371 if (!$this->bVerbose) {
372 $oCmd->addParams('--quiet');
374 if (isset($this->aDSNInfo['hostspec'])) {
375 $oCmd->addParams('--host', $this->aDSNInfo['hostspec']);
377 if (isset($this->aDSNInfo['username'])) {
378 $oCmd->addParams('--username', $this->aDSNInfo['username']);
380 if (isset($this->aDSNInfo['password'])) {
381 $oCmd->addEnvPair('PGPASSWORD', $this->aDSNInfo['password']);
384 if (preg_match('/\\.gz$/', $sFilename)) {
385 $aDescriptors = array(
386 0 => array('pipe', 'r'),
387 1 => array('pipe', 'w'),
388 2 => array('file', '/dev/null', 'a')
390 $oZcatCmd = new \Nominatim\Shell('zcat', $sFilename);
392 $hGzipProcess = proc_open($oZcatCmd->escapedCmd(), $aDescriptors, $ahGzipPipes);
393 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
394 $aReadPipe = $ahGzipPipes[1];
395 fclose($ahGzipPipes[0]);
397 $oCmd->addParams('--file', $sFilename);
398 $aReadPipe = array('pipe', 'r');
400 $aDescriptors = array(
402 1 => array('pipe', 'w'),
403 2 => array('file', '/dev/null', 'a')
407 $hProcess = proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
408 if (!is_resource($hProcess)) fail('unable to start pgsql');
409 // TODO: error checking
410 while (!feof($ahPipes[1])) {
411 echo fread($ahPipes[1], 4096);
414 $iReturn = proc_close($hProcess);
416 fail("pgsql returned with error code ($iReturn)");
419 fclose($ahGzipPipes[1]);
420 proc_close($hGzipProcess);
424 private function replaceSqlPatterns($sSql)
426 $sSql = str_replace('{www-user}', getSetting('DATABASE_WEBUSER'), $sSql);
429 '{ts:address-data}' => getSetting('TABLESPACE_ADDRESS_DATA'),
430 '{ts:address-index}' => getSetting('TABLESPACE_ADDRESS_INDEX'),
431 '{ts:search-data}' => getSetting('TABLESPACE_SEARCH_DATA'),
432 '{ts:search-index}' => getSetting('TABLESPACE_SEARCH_INDEX'),
433 '{ts:aux-data}' => getSetting('TABLESPACE_AUX_DATA'),
434 '{ts:aux-index}' => getSetting('TABLESPACE_AUX_INDEX')
437 foreach ($aPatterns as $sPattern => $sTablespace) {
439 $sSql = str_replace($sPattern, 'TABLESPACE "'.$sTablespace.'"', $sSql);
441 $sSql = str_replace($sPattern, '', $sSql);
449 * Drop table with the given name if it exists.
451 * @param string $sName Name of table to remove.
455 private function dropTable($sName)
457 if ($this->bVerbose) echo "Dropping table $sName\n";
458 $this->db()->deleteTable($sName);
462 * Check if the database is in reverse-only mode.
464 * @return True if there is no search_name table and infrastructure.
466 private function dbReverseOnly()
468 return !($this->db()->tableExists('search_name'));
472 * Try accessing the C module, so we know early if something is wrong.
474 * Raises Nominatim\DatabaseError on failure
476 private function checkModulePresence()
478 $sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
479 $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
480 $sSQL .= $sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
481 $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
483 $oDB = new \Nominatim\DB();
485 $oDB->exec($sSQL, null, 'Database server failed to load '.$sModulePath.'/nominatim.so module');