3 namespace Nominatim\Setup;
7 protected $iCacheMemory;
9 protected $sModulePath;
12 protected $sIgnoreErrors;
13 protected $bEnableDiffUpdates;
14 protected $bEnableDebugStatements;
15 protected $bNoPartitions;
16 protected $oDB = null;
18 public function __construct(array $aCMDResult)
20 // by default, use all but one processor, but never more than 15.
21 $this->iInstances = isset($aCMDResult['threads'])
22 ? $aCMDResult['threads']
23 : (min(16, getProcessorCount()) - 1);
25 if ($this->iInstances < 1) {
26 $this->iInstances = 1;
27 warn('resetting threads to '.$this->iInstances);
30 // Assume we can steal all the cache memory in the box (unless told otherwise)
31 if (isset($aCMDResult['osm2pgsql-cache'])) {
32 $this->iCacheMemory = $aCMDResult['osm2pgsql-cache'];
34 $this->iCacheMemory = getCacheMemoryMB();
37 $this->sModulePath = CONST_Database_Module_Path;
38 info('module path: ' . $this->sModulePath);
40 // parse database string
41 $this->aDSNInfo = array_filter(\DB::parseDSN(CONST_Database_DSN));
42 if (!isset($this->aDSNInfo['port'])) {
43 $this->aDSNInfo['port'] = 5432;
46 // setting member variables based on command line options stored in $aCMDResult
47 $this->sVerbose = $aCMDResult['verbose'];
49 //setting default values which are not set by the update.php array
50 if (isset($aCMDResult['ignore-errors'])) {
51 $this->sIgnoreErrors = $aCMDResult['ignore-errors'];
53 $this->sIgnoreErrors = false;
55 if (isset($aCMDResult['enable-debug-statements'])) {
56 $this->bEnableDebugStatements = $aCMDResult['enable-debug-statements'];
58 $this->bEnableDebugStatements = false;
60 if (isset($aCMDResult['no-partitions'])) {
61 $this->bNoPartitions = $aCMDResult['no-partitions'];
63 $this->bNoPartitions = false;
65 if (isset($aCMDResult['enable-diff-updates'])) {
66 $this->bEnableDiffUpdates = $aCMDResult['enable-diff-updates'];
68 $this->bEnableDiffUpdates = false;
72 public function createDB()
75 $sDB = \DB::connect(CONST_Database_DSN, false);
76 if (!\PEAR::isError($sDB)) {
77 fail('database already exists ('.CONST_Database_DSN.')');
80 $sCreateDBCmd = 'createdb -E UTF-8 -p '.$this->aDSNInfo['port'].' '.$this->aDSNInfo['database'];
81 if (isset($this->aDSNInfo['username'])) {
82 $sCreateDBCmd .= ' -U '.$this->aDSNInfo['username'];
85 if (isset($this->aDSNInfo['hostspec'])) {
86 $sCreateDBCmd .= ' -h '.$this->aDSNInfo['hostspec'];
89 $result = $this->runWithPgEnv($sCreateDBCmd);
90 if ($result != 0) fail('Error executing external command: '.$sCreateDBCmd);
93 public function connect()
95 $this->oDB =& getDB();
98 public function setupDB()
102 $fPostgresVersion = getPostgresVersion($this->oDB);
103 echo 'Postgres version found: '.$fPostgresVersion."\n";
105 if ($fPostgresVersion < 9.01) {
106 fail('Minimum supported version of Postgresql is 9.1.');
109 $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
110 $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
112 // For extratags and namedetails the hstore_to_json converter is
113 // needed which is only available from Postgresql 9.3+. For older
114 // versions add a dummy function that returns nothing.
115 $iNumFunc = chksql($this->oDB->getOne("select count(*) from pg_proc where proname = 'hstore_to_json'"));
117 if ($iNumFunc == 0) {
118 $this->pgsqlRunScript("create function hstore_to_json(dummy hstore) returns text AS 'select null::text' language sql immutable");
119 warn('Postgresql is too old. extratags and namedetails API not available.');
123 $fPostgisVersion = getPostgisVersion($this->oDB);
124 echo 'Postgis version found: '.$fPostgisVersion."\n";
126 if ($fPostgisVersion < 2.1) {
127 // Functions were renamed in 2.1 and throw an annoying deprecation warning
128 $this->pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint');
129 $this->pgsqlRunScript('ALTER FUNCTION ST_Line_Locate_Point(geometry, geometry) RENAME TO ST_LineLocatePoint');
131 if ($fPostgisVersion < 2.2) {
132 $this->pgsqlRunScript('ALTER FUNCTION ST_Distance_Spheroid(geometry, geometry, spheroid) RENAME TO ST_DistanceSpheroid');
135 $i = chksql($this->oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'"));
137 echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n";
138 echo "\n createuser ".CONST_Database_Web_User."\n\n";
142 // Try accessing the C module, so we know early if something is wrong
143 if (!checkModulePresence()) {
144 fail('error loading nominatim.so module');
147 if (!file_exists(CONST_ExtraDataPath.'/country_osm_grid.sql.gz')) {
148 echo 'Error: you need to download the country_osm_grid first:';
149 echo "\n wget -O ".CONST_ExtraDataPath."/country_osm_grid.sql.gz https://www.nominatim.org/data/country_grid.sql.gz\n";
152 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
153 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_osm_grid.sql.gz');
154 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
156 $sPostcodeFilename = CONST_BasePath.'/data/gb_postcode_data.sql.gz';
157 if (file_exists($sPostcodeFilename)) {
158 $this->pgsqlRunScriptFile($sPostcodeFilename);
160 warn('optional external UK postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
163 if (CONST_Use_Extra_US_Postcodes) {
164 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode.sql');
167 if ($this->bNoPartitions) {
168 $this->pgsqlRunScript('update country_name set partition = 0');
171 // the following will be needed by createFunctions later but
172 // is only defined in the subsequently called createTables
173 // Create dummies here that will be overwritten by the proper
174 // versions in create-tables.
175 $this->pgsqlRunScript('CREATE TABLE IF NOT EXISTS place_boundingbox ()');
176 $this->pgsqlRunScript('CREATE TYPE wikipedia_article_match AS ()', false);
179 public function importData($sOSMFile)
183 $osm2pgsql = CONST_Osm2pgsql_Binary;
184 if (!file_exists($osm2pgsql)) {
185 echo "Check CONST_Osm2pgsql_Binary in your local settings file.\n";
186 echo "Normally you should not need to set this manually.\n";
187 fail("osm2pgsql not found in '$osm2pgsql'");
190 if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
191 $osm2pgsql .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
194 if (CONST_Tablespace_Osm2pgsql_Data)
195 $osm2pgsql .= ' --tablespace-slim-data '.CONST_Tablespace_Osm2pgsql_Data;
196 if (CONST_Tablespace_Osm2pgsql_Index)
197 $osm2pgsql .= ' --tablespace-slim-index '.CONST_Tablespace_Osm2pgsql_Index;
198 if (CONST_Tablespace_Place_Data)
199 $osm2pgsql .= ' --tablespace-main-data '.CONST_Tablespace_Place_Data;
200 if (CONST_Tablespace_Place_Index)
201 $osm2pgsql .= ' --tablespace-main-index '.CONST_Tablespace_Place_Index;
202 $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
203 $osm2pgsql .= ' -C '.$this->iCacheMemory;
204 $osm2pgsql .= ' -P '.$this->aDSNInfo['port'];
205 if (isset($this->aDSNInfo['username'])) {
206 $osm2pgsql .= ' -U '.$this->aDSNInfo['username'];
208 if (isset($this->aDSNInfo['hostspec'])) {
209 $osm2pgsql .= ' -H '.$this->aDSNInfo['hostspec'];
211 $osm2pgsql .= ' -d '.$this->aDSNInfo['database'].' '.$sOSMFile;
213 $this->runWithPgEnv($osm2pgsql);
215 if (!$this->sIgnoreErrors && !chksql($this->oDB->getRow('select * from place limit 1'))) {
220 public function createFunctions()
222 info('Create Functions');
224 // Try accessing the C module, so we know eif something is wrong
225 // update.php calls this function
226 if (!checkModulePresence()) {
227 fail('error loading nominatim.so module');
229 $this->createSqlFunctions();
232 public function createTables($bReverseOnly = false)
234 info('Create Tables');
236 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
237 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
238 $sTemplate = $this->replaceTablespace(
240 CONST_Tablespace_Address_Data,
243 $sTemplate = $this->replaceTablespace(
244 '{ts:address-index}',
245 CONST_Tablespace_Address_Index,
248 $sTemplate = $this->replaceTablespace(
250 CONST_Tablespace_Search_Data,
253 $sTemplate = $this->replaceTablespace(
255 CONST_Tablespace_Search_Index,
258 $sTemplate = $this->replaceTablespace(
260 CONST_Tablespace_Aux_Data,
263 $sTemplate = $this->replaceTablespace(
265 CONST_Tablespace_Aux_Index,
269 $this->pgsqlRunScript($sTemplate, false);
272 $this->pgExec('DROP TABLE search_name');
276 public function createPartitionTables()
278 info('Create Partition Tables');
280 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
281 $sTemplate = $this->replaceTablespace(
283 CONST_Tablespace_Address_Data,
287 $sTemplate = $this->replaceTablespace(
288 '{ts:address-index}',
289 CONST_Tablespace_Address_Index,
293 $sTemplate = $this->replaceTablespace(
295 CONST_Tablespace_Search_Data,
299 $sTemplate = $this->replaceTablespace(
301 CONST_Tablespace_Search_Index,
305 $sTemplate = $this->replaceTablespace(
307 CONST_Tablespace_Aux_Data,
311 $sTemplate = $this->replaceTablespace(
313 CONST_Tablespace_Aux_Index,
317 $this->pgsqlRunPartitionScript($sTemplate);
320 public function createPartitionFunctions()
322 info('Create Partition Functions');
324 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
325 $this->pgsqlRunPartitionScript($sTemplate);
328 public function importWikipediaArticles()
330 $sWikiArticlesFile = CONST_Wikipedia_Data_Path.'/wikipedia_article.sql.bin';
331 $sWikiRedirectsFile = CONST_Wikipedia_Data_Path.'/wikipedia_redirect.sql.bin';
332 if (file_exists($sWikiArticlesFile)) {
333 info('Importing wikipedia articles');
334 $this->pgsqlRunDropAndRestore($sWikiArticlesFile);
336 warn('wikipedia article dump file not found - places will have default importance');
338 if (file_exists($sWikiRedirectsFile)) {
339 info('Importing wikipedia redirects');
340 $this->pgsqlRunDropAndRestore($sWikiRedirectsFile);
342 warn('wikipedia redirect dump file not found - some place importance values may be missing');
346 public function loadData($bDisableTokenPrecalc)
348 info('Drop old Data');
350 $this->pgExec('TRUNCATE word');
352 $this->pgExec('TRUNCATE placex');
354 $this->pgExec('TRUNCATE location_property_osmline');
356 $this->pgExec('TRUNCATE place_addressline');
358 $this->pgExec('TRUNCATE place_boundingbox');
360 $this->pgExec('TRUNCATE location_area');
362 if (!$this->dbReverseOnly()) {
363 $this->pgExec('TRUNCATE search_name');
366 $this->pgExec('TRUNCATE search_name_blank');
368 $this->pgExec('DROP SEQUENCE seq_place');
370 $this->pgExec('CREATE SEQUENCE seq_place start 100000');
373 $sSQL = 'select distinct partition from country_name';
374 $aPartitions = chksql($this->oDB->getCol($sSQL));
375 if (!$this->bNoPartitions) $aPartitions[] = 0;
376 foreach ($aPartitions as $sPartition) {
377 $this->pgExec('TRUNCATE location_road_'.$sPartition);
381 // used by getorcreate_word_id to ignore frequent partial words
382 $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
383 $sSQL .= '$$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
384 $this->pgExec($sSQL);
387 // pre-create the word list
388 if (!$bDisableTokenPrecalc) {
389 info('Loading word list');
390 $this->pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
394 $sColumns = 'osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry';
395 $aDBInstances = array();
396 $iLoadThreads = max(1, $this->iInstances - 1);
397 for ($i = 0; $i < $iLoadThreads; $i++) {
398 $aDBInstances[$i] =& getDB(true);
399 $sSQL = "INSERT INTO placex ($sColumns) SELECT $sColumns FROM place WHERE osm_id % $iLoadThreads = $i";
400 $sSQL .= " and not (class='place' and type='houses' and osm_type='W'";
401 $sSQL .= " and ST_GeometryType(geometry) = 'ST_LineString')";
402 $sSQL .= ' and ST_IsValid(geometry)';
403 if ($this->sVerbose) echo "$sSQL\n";
404 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) {
405 fail(pg_last_error($aDBInstances[$i]->connection));
409 // last thread for interpolation lines
410 $aDBInstances[$iLoadThreads] =& getDB(true);
411 $sSQL = 'insert into location_property_osmline';
412 $sSQL .= ' (osm_id, address, linegeo)';
413 $sSQL .= ' SELECT osm_id, address, geometry from place where ';
414 $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
415 if ($this->sVerbose) echo "$sSQL\n";
416 if (!pg_send_query($aDBInstances[$iLoadThreads]->connection, $sSQL)) {
417 fail(pg_last_error($aDBInstances[$iLoadThreads]->connection));
421 for ($i = 0; $i <= $iLoadThreads; $i++) {
422 while (($hPGresult = pg_get_result($aDBInstances[$i]->connection)) !== false) {
423 $resultStatus = pg_result_status($hPGresult);
424 // PGSQL_EMPTY_QUERY, PGSQL_COMMAND_OK, PGSQL_TUPLES_OK,
425 // PGSQL_COPY_OUT, PGSQL_COPY_IN, PGSQL_BAD_RESPONSE,
426 // PGSQL_NONFATAL_ERROR and PGSQL_FATAL_ERROR
427 // echo 'Query result ' . $i . ' is: ' . $resultStatus . "\n";
428 if ($resultStatus != PGSQL_COMMAND_OK && $resultStatus != PGSQL_TUPLES_OK) {
429 $resultError = pg_result_error($hPGresult);
430 echo '-- error text ' . $i . ': ' . $resultError . "\n";
436 fail('SQL errors loading placex and/or location_property_osmline tables');
439 info('Reanalysing database');
440 $this->pgsqlRunScript('ANALYSE');
442 $sDatabaseDate = getDatabaseDate($this->oDB);
443 pg_query($this->oDB->connection, 'TRUNCATE import_status');
444 if ($sDatabaseDate === false) {
445 warn('could not determine database date.');
447 $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
448 pg_query($this->oDB->connection, $sSQL);
449 echo "Latest data imported from $sDatabaseDate.\n";
453 public function importTigerData()
455 info('Import Tiger data');
457 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
458 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
459 $sTemplate = $this->replaceTablespace(
461 CONST_Tablespace_Aux_Data,
464 $sTemplate = $this->replaceTablespace(
466 CONST_Tablespace_Aux_Index,
469 $this->pgsqlRunScript($sTemplate, false);
471 $aDBInstances = array();
472 for ($i = 0; $i < $this->iInstances; $i++) {
473 $aDBInstances[$i] =& getDB(true);
476 foreach (glob(CONST_Tiger_Data_Path.'/*.sql') as $sFile) {
478 $hFile = fopen($sFile, 'r');
479 $sSQL = fgets($hFile, 100000);
482 for ($i = 0; $i < $this->iInstances; $i++) {
483 if (!pg_connection_busy($aDBInstances[$i]->connection)) {
484 while (pg_get_result($aDBInstances[$i]->connection));
485 $sSQL = fgets($hFile, 100000);
487 if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($this->oDB->connection));
489 if ($iLines == 1000) {
502 for ($i = 0; $i < $this->iInstances; $i++) {
503 if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
510 info('Creating indexes on Tiger data');
511 $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
512 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
513 $sTemplate = $this->replaceTablespace(
515 CONST_Tablespace_Aux_Data,
518 $sTemplate = $this->replaceTablespace(
520 CONST_Tablespace_Aux_Index,
523 $this->pgsqlRunScript($sTemplate, false);
526 public function calculatePostcodes($bCMDResultAll)
528 info('Calculate Postcodes');
529 $this->pgExec('TRUNCATE location_postcode');
531 $sSQL = 'INSERT INTO location_postcode';
532 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
533 $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
534 $sSQL .= " upper(trim (both ' ' from address->'postcode')) as pc,";
535 $sSQL .= ' ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
536 $sSQL .= ' FROM placex';
537 $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
538 $sSQL .= ' AND geometry IS NOT null';
539 $sSQL .= ' GROUP BY country_code, pc';
540 $this->pgExec($sSQL);
542 if (CONST_Use_Extra_US_Postcodes) {
543 // only add postcodes that are not yet available in OSM
544 $sSQL = 'INSERT INTO location_postcode';
545 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
546 $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
547 $sSQL .= ' ST_SetSRID(ST_Point(x,y),4326)';
548 $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
549 $sSQL .= ' (SELECT postcode FROM location_postcode';
550 $sSQL .= " WHERE country_code = 'us')";
551 $this->pgExec($sSQL);
554 // add missing postcodes for GB (if available)
555 $sSQL = 'INSERT INTO location_postcode';
556 $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
557 $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
558 $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
559 $sSQL .= ' (SELECT postcode FROM location_postcode';
560 $sSQL .= " WHERE country_code = 'gb')";
561 $this->pgExec($sSQL);
563 if (!$bCMDResultAll) {
564 $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
565 $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
566 $this->pgExec($sSQL);
569 $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
570 $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
571 $this->pgExec($sSQL);
574 public function index($bIndexNoanalyse)
577 $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$this->aDSNInfo['database'].' -P '
578 .$this->aDSNInfo['port'].' -t '.$this->iInstances.$sOutputFile;
579 if (isset($this->aDSNInfo['hostspec'])) {
580 $sBaseCmd .= ' -H '.$this->aDSNInfo['hostspec'];
582 if (isset($this->aDSNInfo['username'])) {
583 $sBaseCmd .= ' -U '.$this->aDSNInfo['username'];
586 info('Index ranks 0 - 4');
587 $iStatus = $this->runWithPgEnv($sBaseCmd.' -R 4');
589 fail('error status ' . $iStatus . ' running nominatim!');
591 if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
593 info('Index ranks 5 - 25');
594 $iStatus = $this->runWithPgEnv($sBaseCmd.' -r 5 -R 25');
596 fail('error status ' . $iStatus . ' running nominatim!');
598 if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
600 info('Index ranks 26 - 30');
601 $iStatus = $this->runWithPgEnv($sBaseCmd.' -r 26');
603 fail('error status ' . $iStatus . ' running nominatim!');
606 info('Index postcodes');
607 $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
608 $this->pgExec($sSQL);
611 public function createSearchIndices()
613 info('Create Search indices');
615 $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
616 if (!$this->dbReverseOnly()) {
617 $sTemplate .= file_get_contents(CONST_BasePath.'/sql/indices_search.src.sql');
619 $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
620 $sTemplate = $this->replaceTablespace(
621 '{ts:address-index}',
622 CONST_Tablespace_Address_Index,
625 $sTemplate = $this->replaceTablespace(
627 CONST_Tablespace_Search_Index,
630 $sTemplate = $this->replaceTablespace(
632 CONST_Tablespace_Aux_Index,
635 $this->pgsqlRunScript($sTemplate);
638 public function createCountryNames()
640 info('Create search index for default country names');
642 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
643 $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
644 $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');
645 $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
646 $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
647 .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
648 if (CONST_Languages) {
651 foreach (explode(',', CONST_Languages) as $sLang) {
652 $sSQL .= $sDelim."'name:$sLang'";
657 // all include all simple name tags
658 $sSQL .= "like 'name:%'";
661 $this->pgsqlRunScript($sSQL);
664 public function drop()
666 info('Drop tables only required for updates');
668 // The implementation is potentially a bit dangerous because it uses
669 // a positive selection of tables to keep, and deletes everything else.
670 // Including any tables that the unsuspecting user might have manually
671 // created. USE AT YOUR OWN PERIL.
672 // tables we want to keep. everything else goes.
673 $aKeepTables = array(
679 'location_property*',
691 $aDropTables = array();
692 $aHaveTables = chksql($this->oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'"));
694 foreach ($aHaveTables as $sTable) {
696 foreach ($aKeepTables as $sKeep) {
697 if (fnmatch($sKeep, $sTable)) {
702 if (!$bFound) array_push($aDropTables, $sTable);
704 foreach ($aDropTables as $sDrop) {
705 if ($this->sVerbose) echo "Dropping table $sDrop\n";
706 @pg_query($this->oDB->connection, "DROP TABLE $sDrop CASCADE");
707 // ignore warnings/errors as they might be caused by a table having
708 // been deleted already by CASCADE
711 if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
712 if (file_exists(CONST_Osm2pgsql_Flatnode_File)) {
713 if ($this->sVerbose) echo 'Deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
714 unlink(CONST_Osm2pgsql_Flatnode_File);
719 private function pgsqlRunDropAndRestore($sDumpFile)
721 $sCMD = 'pg_restore -p '.$this->aDSNInfo['port'].' -d '.$this->aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
722 if (isset($this->aDSNInfo['hostspec'])) {
723 $sCMD .= ' -h '.$this->aDSNInfo['hostspec'];
725 if (isset($this->aDSNInfo['username'])) {
726 $sCMD .= ' -U '.$this->aDSNInfo['username'];
729 $this->runWithPgEnv($sCMD);
732 private function pgsqlRunScript($sScript, $bfatal = true)
742 private function createSqlFunctions()
744 $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
745 $sTemplate = str_replace('{modulepath}', $this->sModulePath, $sTemplate);
746 if ($this->bEnableDiffUpdates) {
747 $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
749 if ($this->bEnableDebugStatements) {
750 $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
752 if (CONST_Limit_Reindexing) {
753 $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
755 if (!CONST_Use_US_Tiger_Data) {
756 $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
758 if (!CONST_Use_Aux_Location_data) {
759 $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
762 $sReverseOnly = $this->dbReverseOnly() ? 'true' : 'false';
763 $sTemplate = str_replace('%REVERSE-ONLY%', $sReverseOnly, $sTemplate);
765 $this->pgsqlRunScript($sTemplate);
768 private function pgsqlRunPartitionScript($sTemplate)
770 $sSQL = 'select distinct partition from country_name';
771 $aPartitions = chksql($this->oDB->getCol($sSQL));
772 if (!$this->bNoPartitions) $aPartitions[] = 0;
774 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
775 foreach ($aMatches as $aMatch) {
777 foreach ($aPartitions as $sPartitionName) {
778 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
780 $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
783 $this->pgsqlRunScript($sTemplate);
786 private function pgsqlRunScriptFile($sFilename)
788 if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
790 $sCMD = 'psql -p '.$this->aDSNInfo['port'].' -d '.$this->aDSNInfo['database'];
791 if (!$this->sVerbose) {
794 if (isset($this->aDSNInfo['hostspec'])) {
795 $sCMD .= ' -h '.$this->aDSNInfo['hostspec'];
797 if (isset($this->aDSNInfo['username'])) {
798 $sCMD .= ' -U '.$this->aDSNInfo['username'];
801 if (isset($this->aDSNInfo['password'])) {
802 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
805 if (preg_match('/\\.gz$/', $sFilename)) {
806 $aDescriptors = array(
807 0 => array('pipe', 'r'),
808 1 => array('pipe', 'w'),
809 2 => array('file', '/dev/null', 'a')
811 $hGzipProcess = proc_open('zcat '.$sFilename, $aDescriptors, $ahGzipPipes);
812 if (!is_resource($hGzipProcess)) fail('unable to start zcat');
813 $aReadPipe = $ahGzipPipes[1];
814 fclose($ahGzipPipes[0]);
816 $sCMD .= ' -f '.$sFilename;
817 $aReadPipe = array('pipe', 'r');
819 $aDescriptors = array(
821 1 => array('pipe', 'w'),
822 2 => array('file', '/dev/null', 'a')
825 $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
826 if (!is_resource($hProcess)) fail('unable to start pgsql');
827 // TODO: error checking
828 while (!feof($ahPipes[1])) {
829 echo fread($ahPipes[1], 4096);
832 $iReturn = proc_close($hProcess);
834 fail("pgsql returned with error code ($iReturn)");
837 fclose($ahGzipPipes[1]);
838 proc_close($hGzipProcess);
842 private function replaceTablespace($sTemplate, $sTablespace, $sSql)
845 $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
847 $sSql = str_replace($sTemplate, '', $sSql);
852 private function runWithPgEnv($sCmd)
856 if (isset($this->aDSNInfo['password'])) {
857 $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
860 return runWithEnv($sCmd, $aProcEnv);
864 * Execute the SQL command on the open database.
866 * @param string $sSQL SQL command to execute.
870 * @pre connect() must have been called.
872 private function pgExec($sSQL)
874 if (!pg_query($this->oDB->connection, $sSQL)) {
875 fail(pg_last_error($this->oDB->connection));
880 * Check if the database is in reverse-only mode.
882 * @return True if there is no search_name table and infrastructure.
884 private function dbReverseOnly()
886 $sSQL = "SELECT count(*) FROM pg_tables WHERE tablename = 'search_name'";
887 return !(chksql($this->oDB->getOne($sSQL)));