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 importTigerData($sTigerPath)
82 info('Import Tiger data');
84 $aFilenames = glob($sTigerPath.'/*.sql');
85 info('Found '.count($aFilenames).' SQL files in path '.$sTigerPath);
86 if (empty($aFilenames)) {
87 warn('Tiger data import selected but no files found in path '.$sTigerPath);
90 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_start.sql');
91 $sTemplate = $this->replaceSqlPatterns($sTemplate);
93 $this->pgsqlRunScript($sTemplate, false);
95 $aDBInstances = array();
96 for ($i = 0; $i < $this->iInstances; $i++) {
97 // https://secure.php.net/manual/en/function.pg-connect.php
98 $DSN = getSetting('DATABASE_DSN');
99 $DSN = preg_replace('/^pgsql:/', '', $DSN);
100 $DSN = preg_replace('/;/', ' ', $DSN);
101 $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW | PGSQL_CONNECT_ASYNC);
102 pg_ping($aDBInstances[$i]);
105 foreach ($aFilenames as $sFile) {
107 $hFile = fopen($sFile, 'r');
108 $sSQL = fgets($hFile, 100000);
111 for ($i = 0; $i < $this->iInstances; $i++) {
112 if (!pg_connection_busy($aDBInstances[$i])) {
113 while (pg_get_result($aDBInstances[$i]));
114 $sSQL = fgets($hFile, 100000);
116 if (!pg_send_query($aDBInstances[$i], $sSQL)) fail(pg_last_error($aDBInstances[$i]));
118 if ($iLines == 1000) {
131 for ($i = 0; $i < $this->iInstances; $i++) {
132 if (pg_connection_busy($aDBInstances[$i])) $bAnyBusy = true;
139 for ($i = 0; $i < $this->iInstances; $i++) {
140 pg_close($aDBInstances[$i]);
143 info('Creating indexes on Tiger data');
144 $sTemplate = file_get_contents(CONST_SqlDir.'/tiger_import_finish.sql');
145 $sTemplate = $this->replaceSqlPatterns($sTemplate);
147 $this->pgsqlRunScript($sTemplate, false);
150 public function calculatePostcodes($bCMDResultAll)
152 info('Calculate Postcodes');
153 $this->pgsqlRunScriptFile(CONST_SqlDir.'/postcode_tables.sql');
155 $sPostcodeFilename = CONST_InstallDir.'/gb_postcode_data.sql.gz';
156 if (file_exists($sPostcodeFilename)) {
157 $this->pgsqlRunScriptFile($sPostcodeFilename);
159 warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
162 $sPostcodeFilename = CONST_InstallDir.'/us_postcode_data.sql.gz';
163 if (file_exists($sPostcodeFilename)) {
164 $this->pgsqlRunScriptFile($sPostcodeFilename);
166 warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
170 $this->db()->exec('TRUNCATE location_postcode');
172 $sSQL = 'INSERT INTO location_postcode';
173 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
174 $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
175 $sSQL .= " upper(trim (both ' ' from address->'postcode')) as pc,";
176 $sSQL .= ' ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
177 $sSQL .= ' FROM placex';
178 $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
179 $sSQL .= ' AND geometry IS NOT null';
180 $sSQL .= ' GROUP BY country_code, pc';
181 $this->db()->exec($sSQL);
183 // only add postcodes that are not yet available in OSM
184 $sSQL = 'INSERT INTO location_postcode';
185 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
186 $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
187 $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
188 $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
189 $sSQL .= ' (SELECT postcode FROM location_postcode';
190 $sSQL .= " WHERE country_code = 'us')";
191 $this->db()->exec($sSQL);
193 // add missing postcodes for GB (if available)
194 $sSQL = 'INSERT INTO location_postcode';
195 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
196 $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
197 $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
198 $sSQL .= ' (SELECT postcode FROM location_postcode';
199 $sSQL .= " WHERE country_code = 'gb')";
200 $this->db()->exec($sSQL);
202 if (!$bCMDResultAll) {
203 $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
204 $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
205 $this->db()->exec($sSQL);
208 $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
209 $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
210 $this->db()->exec($sSQL);
213 public function createSearchIndices()
215 info('Create Search indices');
217 $sSQL = 'SELECT relname FROM pg_class, pg_index ';
218 $sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid';
219 $aInvalidIndices = $this->db()->getCol($sSQL);
221 foreach ($aInvalidIndices as $sIndexName) {
222 info("Cleaning up invalid index $sIndexName");
223 $this->db()->exec("DROP INDEX $sIndexName;");
226 $sTemplate = file_get_contents(CONST_SqlDir.'/indices.src.sql');
228 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_updates.src.sql');
230 if (!$this->dbReverseOnly()) {
231 $sTemplate .= file_get_contents(CONST_SqlDir.'/indices_search.src.sql');
233 $sTemplate = $this->replaceSqlPatterns($sTemplate);
235 $this->pgsqlRunScript($sTemplate);
238 public function createCountryNames()
240 info('Create search index for default country names');
242 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
243 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
244 $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');
245 $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
246 $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
247 .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
248 $sLanguages = getSetting('LANGUAGES');
252 foreach (explode(',', $sLanguages) as $sLang) {
253 $sSQL .= $sDelim."'name:$sLang'";
258 // all include all simple name tags
259 $sSQL .= "like 'name:%'";
262 $this->pgsqlRunScript($sSQL);
266 * Return the connection to the database.
268 * @return Database object.
270 * Creates a new connection if none exists yet. Otherwise reuses the
271 * already established connection.
273 private function db()
275 if (is_null($this->oDB)) {
276 $this->oDB = new \Nominatim\DB();
277 $this->oDB->connect();
283 private function pgsqlRunScript($sScript, $bfatal = true)
293 private function createSqlFunctions()
295 $oCmd = (clone($this->oNominatimCmd))
296 ->addParams('refresh', '--functions');
298 if (!$this->bEnableDiffUpdates) {
299 $oCmd->addParams('--no-diff-updates');
302 if ($this->bEnableDebugStatements) {
303 $oCmd->addParams('--enable-debug-statements');
306 $oCmd->run(!$this->sIgnoreErrors);
309 private function pgsqlRunScriptFile($sFilename)
311 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
313 $oCmd = (new \Nominatim\Shell('psql'))
314 ->addParams('--port', $this->aDSNInfo['port'])
315 ->addParams('--dbname', $this->aDSNInfo['database']);
317 if (!$this->bVerbose) {
318 $oCmd->addParams('--quiet');
320 if (isset($this->aDSNInfo['hostspec'])) {
321 $oCmd->addParams('--host', $this->aDSNInfo['hostspec']);
323 if (isset($this->aDSNInfo['username'])) {
324 $oCmd->addParams('--username', $this->aDSNInfo['username']);
326 if (isset($this->aDSNInfo['password'])) {
327 $oCmd->addEnvPair('PGPASSWORD', $this->aDSNInfo['password']);
330 if (preg_match('/\\.gz$/', $sFilename)) {
331 $aDescriptors = array(
332 0 => array('pipe', 'r'),
333 1 => array('pipe', 'w'),
334 2 => array('file', '/dev/null', 'a')
336 $oZcatCmd = new \Nominatim\Shell('zcat', $sFilename);
338 $hGzipProcess = proc_open($oZcatCmd->escapedCmd(), $aDescriptors, $ahGzipPipes);
339 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
340 $aReadPipe = $ahGzipPipes[1];
341 fclose($ahGzipPipes[0]);
343 $oCmd->addParams('--file', $sFilename);
344 $aReadPipe = array('pipe', 'r');
346 $aDescriptors = array(
348 1 => array('pipe', 'w'),
349 2 => array('file', '/dev/null', 'a')
353 $hProcess = proc_open($oCmd->escapedCmd(), $aDescriptors, $ahPipes, null, $oCmd->aEnv);
354 if (!is_resource($hProcess)) fail('unable to start pgsql');
355 // TODO: error checking
356 while (!feof($ahPipes[1])) {
357 echo fread($ahPipes[1], 4096);
360 $iReturn = proc_close($hProcess);
362 fail("pgsql returned with error code ($iReturn)");
365 fclose($ahGzipPipes[1]);
366 proc_close($hGzipProcess);
370 private function replaceSqlPatterns($sSql)
372 $sSql = str_replace('{www-user}', getSetting('DATABASE_WEBUSER'), $sSql);
375 '{ts:address-data}' => getSetting('TABLESPACE_ADDRESS_DATA'),
376 '{ts:address-index}' => getSetting('TABLESPACE_ADDRESS_INDEX'),
377 '{ts:search-data}' => getSetting('TABLESPACE_SEARCH_DATA'),
378 '{ts:search-index}' => getSetting('TABLESPACE_SEARCH_INDEX'),
379 '{ts:aux-data}' => getSetting('TABLESPACE_AUX_DATA'),
380 '{ts:aux-index}' => getSetting('TABLESPACE_AUX_INDEX')
383 foreach ($aPatterns as $sPattern => $sTablespace) {
385 $sSql = str_replace($sPattern, 'TABLESPACE "'.$sTablespace.'"', $sSql);
387 $sSql = str_replace($sPattern, '', $sSql);
395 * Drop table with the given name if it exists.
397 * @param string $sName Name of table to remove.
401 private function dropTable($sName)
403 if ($this->bVerbose) echo "Dropping table $sName\n";
404 $this->db()->deleteTable($sName);
408 * Check if the database is in reverse-only mode.
410 * @return True if there is no search_name table and infrastructure.
412 private function dbReverseOnly()
414 return !($this->db()->tableExists('search_name'));
418 * Try accessing the C module, so we know early if something is wrong.
420 * Raises Nominatim\DatabaseError on failure
422 private function checkModulePresence()
424 $sModulePath = getSetting('DATABASE_MODULE_PATH', CONST_InstallDir.'/module');
425 $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
426 $sSQL .= $sModulePath . "/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
427 $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
429 $oDB = new \Nominatim\DB();
431 $oDB->exec($sSQL, null, 'Database server failed to load '.$sModulePath.'/nominatim.so module');