]> git.openstreetmap.org Git - nominatim.git/blob - lib/setup/SetupClass.php
fix loading of data without wikipedia files
[nominatim.git] / lib / setup / SetupClass.php
1 <?php
2
3 namespace Nominatim\Setup;
4
5 require_once(CONST_BasePath.'/lib/setup/AddressLevelParser.php');
6
7 class SetupFunctions
8 {
9     protected $iCacheMemory;
10     protected $iInstances;
11     protected $sModulePath;
12     protected $aDSNInfo;
13     protected $bVerbose;
14     protected $sIgnoreErrors;
15     protected $bEnableDiffUpdates;
16     protected $bEnableDebugStatements;
17     protected $bNoPartitions;
18     protected $oDB = null;
19
20     public function __construct(array $aCMDResult)
21     {
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);
26
27         if ($this->iInstances < 1) {
28             $this->iInstances = 1;
29             warn('resetting threads to '.$this->iInstances);
30         }
31
32         // Assume we can steal all the cache memory in the box (unless told otherwise)
33         if (isset($aCMDResult['osm2pgsql-cache'])) {
34             $this->iCacheMemory = $aCMDResult['osm2pgsql-cache'];
35         } else {
36             $this->iCacheMemory = getCacheMemoryMB();
37         }
38
39         $this->sModulePath = CONST_Database_Module_Path;
40         info('module path: ' . $this->sModulePath);
41
42         // parse database string
43         $this->aDSNInfo = \Nominatim\DB::parseDSN(CONST_Database_DSN);
44         if (!isset($this->aDSNInfo['port'])) {
45             $this->aDSNInfo['port'] = 5432;
46         }
47
48         // setting member variables based on command line options stored in $aCMDResult
49         $this->bVerbose = $aCMDResult['verbose'];
50
51         //setting default values which are not set by the update.php array
52         if (isset($aCMDResult['ignore-errors'])) {
53             $this->sIgnoreErrors = $aCMDResult['ignore-errors'];
54         } else {
55             $this->sIgnoreErrors = false;
56         }
57         if (isset($aCMDResult['enable-debug-statements'])) {
58             $this->bEnableDebugStatements = $aCMDResult['enable-debug-statements'];
59         } else {
60             $this->bEnableDebugStatements = false;
61         }
62         if (isset($aCMDResult['no-partitions'])) {
63             $this->bNoPartitions = $aCMDResult['no-partitions'];
64         } else {
65             $this->bNoPartitions = false;
66         }
67         if (isset($aCMDResult['enable-diff-updates'])) {
68             $this->bEnableDiffUpdates = $aCMDResult['enable-diff-updates'];
69         } else {
70             $this->bEnableDiffUpdates = false;
71         }
72     }
73
74     public function createDB()
75     {
76         info('Create DB');
77         $oDB = new \Nominatim\DB;
78
79         if ($oDB->databaseExists()) {
80             fail('database already exists ('.CONST_Database_DSN.')');
81         }
82
83         $sCreateDBCmd = 'createdb -E UTF-8'
84             .' -p '.escapeshellarg($this->aDSNInfo['port'])
85             .' '.escapeshellarg($this->aDSNInfo['database']);
86         if (isset($this->aDSNInfo['username'])) {
87             $sCreateDBCmd .= ' -U '.escapeshellarg($this->aDSNInfo['username']);
88         }
89
90         if (isset($this->aDSNInfo['hostspec'])) {
91             $sCreateDBCmd .= ' -h '.escapeshellarg($this->aDSNInfo['hostspec']);
92         }
93
94         $result = $this->runWithPgEnv($sCreateDBCmd);
95         if ($result != 0) fail('Error executing external command: '.$sCreateDBCmd);
96     }
97
98     public function connect()
99     {
100         $this->oDB = new \Nominatim\DB();
101         $this->oDB->connect();
102     }
103
104     public function setupDB()
105     {
106         info('Setup DB');
107
108         $fPostgresVersion = $this->oDB->getPostgresVersion();
109         echo 'Postgres version found: '.$fPostgresVersion."\n";
110
111         if ($fPostgresVersion < 9.03) {
112             fail('Minimum supported version of Postgresql is 9.3.');
113         }
114
115         $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
116         $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
117
118         $fPostgisVersion = $this->oDB->getPostgisVersion();
119         echo 'Postgis version found: '.$fPostgisVersion."\n";
120
121         if ($fPostgisVersion < 2.2) {
122             echo "Minimum required Postgis version 2.2\n";
123             exit(1);
124         }
125
126         $i = $this->oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'");
127         if ($i == 0) {
128             echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n";
129             echo "\n          createuser ".CONST_Database_Web_User."\n\n";
130             exit(1);
131         }
132
133         // Try accessing the C module, so we know early if something is wrong
134         checkModulePresence(); // raises exception on failure
135
136         if (!file_exists(CONST_ExtraDataPath.'/country_osm_grid.sql.gz')) {
137             echo 'Error: you need to download the country_osm_grid first:';
138             echo "\n    wget -O ".CONST_ExtraDataPath."/country_osm_grid.sql.gz https://www.nominatim.org/data/country_grid.sql.gz\n";
139             exit(1);
140         }
141         $this->pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql');
142         $this->pgsqlRunScriptFile(CONST_ExtraDataPath.'/country_osm_grid.sql.gz');
143         $this->pgsqlRunScriptFile(CONST_BasePath.'/data/gb_postcode_table.sql');
144         $this->pgsqlRunScriptFile(CONST_BasePath.'/data/us_postcode_table.sql');
145
146         $sPostcodeFilename = CONST_BasePath.'/data/gb_postcode_data.sql.gz';
147         if (file_exists($sPostcodeFilename)) {
148             $this->pgsqlRunScriptFile($sPostcodeFilename);
149         } else {
150             warn('optional external GB postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
151         }
152
153         $sPostcodeFilename = CONST_BasePath.'/data/us_postcode_data.sql.gz';
154         if (file_exists($sPostcodeFilename)) {
155             $this->pgsqlRunScriptFile($sPostcodeFilename);
156         } else {
157             warn('optional external US postcode table file ('.$sPostcodeFilename.') not found. Skipping.');
158         }
159
160         if ($this->bNoPartitions) {
161             $this->pgsqlRunScript('update country_name set partition = 0');
162         }
163     }
164
165     public function importData($sOSMFile)
166     {
167         info('Import data');
168
169         $osm2pgsql = CONST_Osm2pgsql_Binary;
170         if (!file_exists($osm2pgsql)) {
171             echo "Check CONST_Osm2pgsql_Binary in your local settings file.\n";
172             echo "Normally you should not need to set this manually.\n";
173             fail("osm2pgsql not found in '$osm2pgsql'");
174         }
175
176         $osm2pgsql .= ' -S '.escapeshellarg(CONST_Import_Style);
177
178         if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
179             $osm2pgsql .= ' --flat-nodes '.escapeshellarg(CONST_Osm2pgsql_Flatnode_File);
180         }
181
182         if (CONST_Tablespace_Osm2pgsql_Data)
183             $osm2pgsql .= ' --tablespace-slim-data '.escapeshellarg(CONST_Tablespace_Osm2pgsql_Data);
184         if (CONST_Tablespace_Osm2pgsql_Index)
185             $osm2pgsql .= ' --tablespace-slim-index '.escapeshellarg(CONST_Tablespace_Osm2pgsql_Index);
186         if (CONST_Tablespace_Place_Data)
187             $osm2pgsql .= ' --tablespace-main-data '.escapeshellarg(CONST_Tablespace_Place_Data);
188         if (CONST_Tablespace_Place_Index)
189             $osm2pgsql .= ' --tablespace-main-index '.escapeshellarg(CONST_Tablespace_Place_Index);
190         $osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
191         $osm2pgsql .= ' -C '.escapeshellarg($this->iCacheMemory);
192         $osm2pgsql .= ' -P '.escapeshellarg($this->aDSNInfo['port']);
193         if (isset($this->aDSNInfo['username'])) {
194             $osm2pgsql .= ' -U '.escapeshellarg($this->aDSNInfo['username']);
195         }
196         if (isset($this->aDSNInfo['hostspec'])) {
197             $osm2pgsql .= ' -H '.escapeshellarg($this->aDSNInfo['hostspec']);
198         }
199         $osm2pgsql .= ' -d '.escapeshellarg($this->aDSNInfo['database']).' '.escapeshellarg($sOSMFile);
200
201         $this->runWithPgEnv($osm2pgsql);
202
203         if (!$this->sIgnoreErrors && !$this->oDB->getRow('select * from place limit 1')) {
204             fail('No Data');
205         }
206     }
207
208     public function createFunctions()
209     {
210         info('Create Functions');
211
212         // Try accessing the C module, so we know early if something is wrong
213         checkModulePresence(); // raises exception on failure
214
215         $this->createSqlFunctions();
216     }
217
218     public function createTables($bReverseOnly = false)
219     {
220         info('Create Tables');
221
222         $sTemplate = file_get_contents(CONST_BasePath.'/sql/tables.sql');
223         $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
224         $sTemplate = $this->replaceTablespace(
225             '{ts:address-data}',
226             CONST_Tablespace_Address_Data,
227             $sTemplate
228         );
229         $sTemplate = $this->replaceTablespace(
230             '{ts:address-index}',
231             CONST_Tablespace_Address_Index,
232             $sTemplate
233         );
234         $sTemplate = $this->replaceTablespace(
235             '{ts:search-data}',
236             CONST_Tablespace_Search_Data,
237             $sTemplate
238         );
239         $sTemplate = $this->replaceTablespace(
240             '{ts:search-index}',
241             CONST_Tablespace_Search_Index,
242             $sTemplate
243         );
244         $sTemplate = $this->replaceTablespace(
245             '{ts:aux-data}',
246             CONST_Tablespace_Aux_Data,
247             $sTemplate
248         );
249         $sTemplate = $this->replaceTablespace(
250             '{ts:aux-index}',
251             CONST_Tablespace_Aux_Index,
252             $sTemplate
253         );
254
255         $this->pgsqlRunScript($sTemplate, false);
256
257         if ($bReverseOnly) {
258             $this->pgExec('DROP TABLE search_name');
259         }
260
261         $oAlParser = new AddressLevelParser(CONST_Address_Level_Config);
262         $oAlParser->createTable($this->oDB, 'address_levels');
263     }
264
265     public function createPartitionTables()
266     {
267         info('Create Partition Tables');
268
269         $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-tables.src.sql');
270         $sTemplate = $this->replaceTablespace(
271             '{ts:address-data}',
272             CONST_Tablespace_Address_Data,
273             $sTemplate
274         );
275
276         $sTemplate = $this->replaceTablespace(
277             '{ts:address-index}',
278             CONST_Tablespace_Address_Index,
279             $sTemplate
280         );
281
282         $sTemplate = $this->replaceTablespace(
283             '{ts:search-data}',
284             CONST_Tablespace_Search_Data,
285             $sTemplate
286         );
287
288         $sTemplate = $this->replaceTablespace(
289             '{ts:search-index}',
290             CONST_Tablespace_Search_Index,
291             $sTemplate
292         );
293
294         $sTemplate = $this->replaceTablespace(
295             '{ts:aux-data}',
296             CONST_Tablespace_Aux_Data,
297             $sTemplate
298         );
299
300         $sTemplate = $this->replaceTablespace(
301             '{ts:aux-index}',
302             CONST_Tablespace_Aux_Index,
303             $sTemplate
304         );
305
306         $this->pgsqlRunPartitionScript($sTemplate);
307     }
308
309     public function createPartitionFunctions()
310     {
311         info('Create Partition Functions');
312
313         $sTemplate = file_get_contents(CONST_BasePath.'/sql/partition-functions.src.sql');
314         $this->pgsqlRunPartitionScript($sTemplate);
315     }
316
317     public function importWikipediaArticles()
318     {
319         $sWikiArticlesFile = CONST_Wikipedia_Data_Path.'/wikimedia-importance.sql.gz';
320         if (file_exists($sWikiArticlesFile)) {
321             info('Importing wikipedia articles and redirects');
322             $this->pgExec('DROP TABLE IF EXISTS wikipedia_article');
323             $this->pgExec('DROP TABLE IF EXISTS wikipedia_redirect');
324             $this->pgsqlRunScriptFile($sWikiArticlesFile);
325         } else {
326             warn('wikipedia importance dump file not found - places will have default importance');
327         }
328     }
329
330     public function loadData($bDisableTokenPrecalc)
331     {
332         info('Drop old Data');
333
334         $this->pgExec('TRUNCATE word');
335         echo '.';
336         $this->pgExec('TRUNCATE placex');
337         echo '.';
338         $this->pgExec('TRUNCATE location_property_osmline');
339         echo '.';
340         $this->pgExec('TRUNCATE place_addressline');
341         echo '.';
342         $this->pgExec('TRUNCATE location_area');
343         echo '.';
344         if (!$this->dbReverseOnly()) {
345             $this->pgExec('TRUNCATE search_name');
346             echo '.';
347         }
348         $this->pgExec('TRUNCATE search_name_blank');
349         echo '.';
350         $this->pgExec('DROP SEQUENCE seq_place');
351         echo '.';
352         $this->pgExec('CREATE SEQUENCE seq_place start 100000');
353         echo '.';
354
355         $sSQL = 'select distinct partition from country_name';
356         $aPartitions = $this->oDB->getCol($sSQL);
357
358         if (!$this->bNoPartitions) $aPartitions[] = 0;
359         foreach ($aPartitions as $sPartition) {
360             $this->pgExec('TRUNCATE location_road_'.$sPartition);
361             echo '.';
362         }
363
364         // used by getorcreate_word_id to ignore frequent partial words
365         $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
366         $sSQL .= '$$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
367         $this->pgExec($sSQL);
368         echo ".\n";
369
370         // pre-create the word list
371         if (!$bDisableTokenPrecalc) {
372             info('Loading word list');
373             $this->pgsqlRunScriptFile(CONST_BasePath.'/data/words.sql');
374         }
375
376         info('Load Data');
377         $sColumns = 'osm_type, osm_id, class, type, name, admin_level, address, extratags, geometry';
378
379         $aDBInstances = array();
380         $iLoadThreads = max(1, $this->iInstances - 1);
381         for ($i = 0; $i < $iLoadThreads; $i++) {
382             // https://secure.php.net/manual/en/function.pg-connect.php
383             $DSN = CONST_Database_DSN;
384             $DSN = preg_replace('/^pgsql:/', '', $DSN);
385             $DSN = preg_replace('/;/', ' ', $DSN);
386             $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW);
387             pg_ping($aDBInstances[$i]);
388         }
389
390         for ($i = 0; $i < $iLoadThreads; $i++) {
391             $sSQL = "INSERT INTO placex ($sColumns) SELECT $sColumns FROM place WHERE osm_id % $iLoadThreads = $i";
392             $sSQL .= " and not (class='place' and type='houses' and osm_type='W'";
393             $sSQL .= "          and ST_GeometryType(geometry) = 'ST_LineString')";
394             $sSQL .= ' and ST_IsValid(geometry)';
395             if ($this->bVerbose) echo "$sSQL\n";
396             if (!pg_send_query($aDBInstances[$i], $sSQL)) {
397                 fail(pg_last_error($aDBInstances[$i]));
398             }
399         }
400
401         // last thread for interpolation lines
402         // https://secure.php.net/manual/en/function.pg-connect.php
403         $DSN = CONST_Database_DSN;
404         $DSN = preg_replace('/^pgsql:/', '', $DSN);
405         $DSN = preg_replace('/;/', ' ', $DSN);
406         $aDBInstances[$iLoadThreads] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW);
407         pg_ping($aDBInstances[$iLoadThreads]);
408         $sSQL = 'insert into location_property_osmline';
409         $sSQL .= ' (osm_id, address, linegeo)';
410         $sSQL .= ' SELECT osm_id, address, geometry from place where ';
411         $sSQL .= "class='place' and type='houses' and osm_type='W' and ST_GeometryType(geometry) = 'ST_LineString'";
412         if ($this->bVerbose) echo "$sSQL\n";
413         if (!pg_send_query($aDBInstances[$iLoadThreads], $sSQL)) {
414             fail(pg_last_error($aDBInstances[$iLoadThreads]));
415         }
416
417         $bFailed = false;
418         for ($i = 0; $i <= $iLoadThreads; $i++) {
419             while (($hPGresult = pg_get_result($aDBInstances[$i])) !== false) {
420                 $resultStatus = pg_result_status($hPGresult);
421                 // PGSQL_EMPTY_QUERY, PGSQL_COMMAND_OK, PGSQL_TUPLES_OK,
422                 // PGSQL_COPY_OUT, PGSQL_COPY_IN, PGSQL_BAD_RESPONSE,
423                 // PGSQL_NONFATAL_ERROR and PGSQL_FATAL_ERROR
424                 // echo 'Query result ' . $i . ' is: ' . $resultStatus . "\n";
425                 if ($resultStatus != PGSQL_COMMAND_OK && $resultStatus != PGSQL_TUPLES_OK) {
426                     $resultError = pg_result_error($hPGresult);
427                     echo '-- error text ' . $i . ': ' . $resultError . "\n";
428                     $bFailed = true;
429                 }
430             }
431         }
432         if ($bFailed) {
433             fail('SQL errors loading placex and/or location_property_osmline tables');
434         }
435
436         for ($i = 0; $i < $this->iInstances; $i++) {
437             pg_close($aDBInstances[$i]);
438         }
439
440         echo "\n";
441         info('Reanalysing database');
442         $this->pgsqlRunScript('ANALYSE');
443
444         $sDatabaseDate = getDatabaseDate($this->oDB);
445         $this->oDB->exec('TRUNCATE import_status');
446         if (!$sDatabaseDate) {
447             warn('could not determine database date.');
448         } else {
449             $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
450             $this->oDB->exec($sSQL);
451             echo "Latest data imported from $sDatabaseDate.\n";
452         }
453     }
454
455     public function importTigerData()
456     {
457         info('Import Tiger data');
458
459         $aFilenames = glob(CONST_Tiger_Data_Path.'/*.sql');
460         info('Found '.count($aFilenames).' SQL files in path '.CONST_Tiger_Data_Path);
461         if (empty($aFilenames)) return;
462
463         $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_start.sql');
464         $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
465         $sTemplate = $this->replaceTablespace(
466             '{ts:aux-data}',
467             CONST_Tablespace_Aux_Data,
468             $sTemplate
469         );
470         $sTemplate = $this->replaceTablespace(
471             '{ts:aux-index}',
472             CONST_Tablespace_Aux_Index,
473             $sTemplate
474         );
475         $this->pgsqlRunScript($sTemplate, false);
476
477         $aDBInstances = array();
478         for ($i = 0; $i < $this->iInstances; $i++) {
479             // https://secure.php.net/manual/en/function.pg-connect.php
480             $DSN = CONST_Database_DSN;
481             $DSN = preg_replace('/^pgsql:/', '', $DSN);
482             $DSN = preg_replace('/;/', ' ', $DSN);
483             $aDBInstances[$i] = pg_connect($DSN, PGSQL_CONNECT_FORCE_NEW | PGSQL_CONNECT_ASYNC);
484             pg_ping($aDBInstances[$i]);
485         }
486
487         foreach ($aFilenames as $sFile) {
488             echo $sFile.': ';
489             $hFile = fopen($sFile, 'r');
490             $sSQL = fgets($hFile, 100000);
491             $iLines = 0;
492             while (true) {
493                 for ($i = 0; $i < $this->iInstances; $i++) {
494                     if (!pg_connection_busy($aDBInstances[$i])) {
495                         while (pg_get_result($aDBInstances[$i]));
496                         $sSQL = fgets($hFile, 100000);
497                         if (!$sSQL) break 2;
498                         if (!pg_send_query($aDBInstances[$i], $sSQL)) fail(pg_last_error($aDBInstances[$i]));
499                         $iLines++;
500                         if ($iLines == 1000) {
501                             echo '.';
502                             $iLines = 0;
503                         }
504                     }
505                 }
506                 usleep(10);
507             }
508             fclose($hFile);
509
510             $bAnyBusy = true;
511             while ($bAnyBusy) {
512                 $bAnyBusy = false;
513                 for ($i = 0; $i < $this->iInstances; $i++) {
514                     if (pg_connection_busy($aDBInstances[$i])) $bAnyBusy = true;
515                 }
516                 usleep(10);
517             }
518             echo "\n";
519         }
520
521         for ($i = 0; $i < $this->iInstances; $i++) {
522             pg_close($aDBInstances[$i]);
523         }
524
525         info('Creating indexes on Tiger data');
526         $sTemplate = file_get_contents(CONST_BasePath.'/sql/tiger_import_finish.sql');
527         $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
528         $sTemplate = $this->replaceTablespace(
529             '{ts:aux-data}',
530             CONST_Tablespace_Aux_Data,
531             $sTemplate
532         );
533         $sTemplate = $this->replaceTablespace(
534             '{ts:aux-index}',
535             CONST_Tablespace_Aux_Index,
536             $sTemplate
537         );
538         $this->pgsqlRunScript($sTemplate, false);
539     }
540
541     public function calculatePostcodes($bCMDResultAll)
542     {
543         info('Calculate Postcodes');
544         $this->pgExec('TRUNCATE location_postcode');
545
546         $sSQL  = 'INSERT INTO location_postcode';
547         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
548         $sSQL .= "SELECT nextval('seq_place'), 1, country_code,";
549         $sSQL .= "       upper(trim (both ' ' from address->'postcode')) as pc,";
550         $sSQL .= '       ST_Centroid(ST_Collect(ST_Centroid(geometry)))';
551         $sSQL .= '  FROM placex';
552         $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
553         $sSQL .= '       AND geometry IS NOT null';
554         $sSQL .= ' GROUP BY country_code, pc';
555         $this->pgExec($sSQL);
556
557         // only add postcodes that are not yet available in OSM
558         $sSQL  = 'INSERT INTO location_postcode';
559         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
560         $sSQL .= "SELECT nextval('seq_place'), 1, 'us', postcode,";
561         $sSQL .= '       ST_SetSRID(ST_Point(x,y),4326)';
562         $sSQL .= '  FROM us_postcode WHERE postcode NOT IN';
563         $sSQL .= '        (SELECT postcode FROM location_postcode';
564         $sSQL .= "          WHERE country_code = 'us')";
565         $this->pgExec($sSQL);
566
567         // add missing postcodes for GB (if available)
568         $sSQL  = 'INSERT INTO location_postcode';
569         $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
570         $sSQL .= "SELECT nextval('seq_place'), 1, 'gb', postcode, geometry";
571         $sSQL .= '  FROM gb_postcode WHERE postcode NOT IN';
572         $sSQL .= '           (SELECT postcode FROM location_postcode';
573         $sSQL .= "             WHERE country_code = 'gb')";
574         $this->pgExec($sSQL);
575
576         if (!$bCMDResultAll) {
577             $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
578             $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
579             $this->pgExec($sSQL);
580         }
581
582         $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
583         $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
584         $this->pgExec($sSQL);
585     }
586
587     public function index($bIndexNoanalyse)
588     {
589         $sOutputFile = '';
590         $sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i'
591             .' -d '.escapeshellarg($this->aDSNInfo['database'])
592             .' -P '.escapeshellarg($this->aDSNInfo['port'])
593             .' -t '.escapeshellarg($this->iInstances.$sOutputFile);
594         if (isset($this->aDSNInfo['hostspec'])) {
595             $sBaseCmd .= ' -H '.escapeshellarg($this->aDSNInfo['hostspec']);
596         }
597         if (isset($this->aDSNInfo['username'])) {
598             $sBaseCmd .= ' -U '.escapeshellarg($this->aDSNInfo['username']);
599         }
600
601         info('Index ranks 0 - 4');
602         $iStatus = $this->runWithPgEnv($sBaseCmd.' -R 4');
603         if ($iStatus != 0) {
604             fail('error status ' . $iStatus . ' running nominatim!');
605         }
606         if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
607
608         info('Index ranks 5 - 25');
609         $iStatus = $this->runWithPgEnv($sBaseCmd.' -r 5 -R 25');
610         if ($iStatus != 0) {
611             fail('error status ' . $iStatus . ' running nominatim!');
612         }
613         if (!$bIndexNoanalyse) $this->pgsqlRunScript('ANALYSE');
614
615         info('Index ranks 26 - 30');
616         $iStatus = $this->runWithPgEnv($sBaseCmd.' -r 26');
617         if ($iStatus != 0) {
618             fail('error status ' . $iStatus . ' running nominatim!');
619         }
620
621         info('Index postcodes');
622         $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
623         $this->pgExec($sSQL);
624     }
625
626     public function createSearchIndices()
627     {
628         info('Create Search indices');
629
630         $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
631         if (!$this->dbReverseOnly()) {
632             $sTemplate .= file_get_contents(CONST_BasePath.'/sql/indices_search.src.sql');
633         }
634         $sTemplate = str_replace('{www-user}', CONST_Database_Web_User, $sTemplate);
635         $sTemplate = $this->replaceTablespace(
636             '{ts:address-index}',
637             CONST_Tablespace_Address_Index,
638             $sTemplate
639         );
640         $sTemplate = $this->replaceTablespace(
641             '{ts:search-index}',
642             CONST_Tablespace_Search_Index,
643             $sTemplate
644         );
645         $sTemplate = $this->replaceTablespace(
646             '{ts:aux-index}',
647             CONST_Tablespace_Aux_Index,
648             $sTemplate
649         );
650         $this->pgsqlRunScript($sTemplate);
651     }
652
653     public function createCountryNames()
654     {
655         info('Create search index for default country names');
656
657         $this->pgsqlRunScript("select getorcreate_country(make_standard_name('uk'), 'gb')");
658         $this->pgsqlRunScript("select getorcreate_country(make_standard_name('united states'), 'us')");
659         $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');
660         $this->pgsqlRunScript("select count(*) from (select getorcreate_country(make_standard_name(name->'name'), country_code) from country_name where name ? 'name') as x");
661         $sSQL = 'select count(*) from (select getorcreate_country(make_standard_name(v),'
662             .'country_code) from (select country_code, skeys(name) as k, svals(name) as v from country_name) x where k ';
663         if (CONST_Languages) {
664             $sSQL .= 'in ';
665             $sDelim = '(';
666             foreach (explode(',', CONST_Languages) as $sLang) {
667                 $sSQL .= $sDelim."'name:$sLang'";
668                 $sDelim = ',';
669             }
670             $sSQL .= ')';
671         } else {
672             // all include all simple name tags
673             $sSQL .= "like 'name:%'";
674         }
675         $sSQL .= ') v';
676         $this->pgsqlRunScript($sSQL);
677     }
678
679     public function drop()
680     {
681         info('Drop tables only required for updates');
682
683         // The implementation is potentially a bit dangerous because it uses
684         // a positive selection of tables to keep, and deletes everything else.
685         // Including any tables that the unsuspecting user might have manually
686         // created. USE AT YOUR OWN PERIL.
687         // tables we want to keep. everything else goes.
688         $aKeepTables = array(
689                         '*columns',
690                         'import_polygon_*',
691                         'import_status',
692                         'place_addressline',
693                         'location_postcode',
694                         'location_property*',
695                         'placex',
696                         'search_name',
697                         'seq_*',
698                         'word',
699                         'query_log',
700                         'new_query_log',
701                         'spatial_ref_sys',
702                         'country_name',
703                         'place_classtype_*',
704                         'country_osm_grid'
705                        );
706
707         $aDropTables = array();
708         $aHaveTables = $this->oDB->getCol("SELECT tablename FROM pg_tables WHERE schemaname='public'");
709
710         foreach ($aHaveTables as $sTable) {
711             $bFound = false;
712             foreach ($aKeepTables as $sKeep) {
713                 if (fnmatch($sKeep, $sTable)) {
714                     $bFound = true;
715                     break;
716                 }
717             }
718             if (!$bFound) array_push($aDropTables, $sTable);
719         }
720         foreach ($aDropTables as $sDrop) {
721             if ($this->bVerbose) echo "Dropping table $sDrop\n";
722             $this->oDB->exec("DROP TABLE IF EXISTS $sDrop CASCADE");
723         }
724
725         if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
726             if (file_exists(CONST_Osm2pgsql_Flatnode_File)) {
727                 if ($this->bVerbose) echo 'Deleting '.CONST_Osm2pgsql_Flatnode_File."\n";
728                 unlink(CONST_Osm2pgsql_Flatnode_File);
729             }
730         }
731     }
732
733     private function pgsqlRunScript($sScript, $bfatal = true)
734     {
735         runSQLScript(
736             $sScript,
737             $bfatal,
738             $this->bVerbose,
739             $this->sIgnoreErrors
740         );
741     }
742
743     private function createSqlFunctions()
744     {
745         $sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
746         $sTemplate = str_replace('{modulepath}', $this->sModulePath, $sTemplate);
747         if ($this->bEnableDiffUpdates) {
748             $sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
749         }
750         if ($this->bEnableDebugStatements) {
751             $sTemplate = str_replace('--DEBUG:', '', $sTemplate);
752         }
753         if (CONST_Limit_Reindexing) {
754             $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate);
755         }
756         if (!CONST_Use_US_Tiger_Data) {
757             $sTemplate = str_replace('-- %NOTIGERDATA% ', '', $sTemplate);
758         }
759         if (!CONST_Use_Aux_Location_data) {
760             $sTemplate = str_replace('-- %NOAUXDATA% ', '', $sTemplate);
761         }
762
763         $sReverseOnly = $this->dbReverseOnly() ? 'true' : 'false';
764         $sTemplate = str_replace('%REVERSE-ONLY%', $sReverseOnly, $sTemplate);
765
766         $this->pgsqlRunScript($sTemplate);
767     }
768
769     private function pgsqlRunPartitionScript($sTemplate)
770     {
771         $sSQL = 'select distinct partition from country_name';
772         $aPartitions = $this->oDB->getCol($sSQL);
773         if (!$this->bNoPartitions) $aPartitions[] = 0;
774
775         preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
776         foreach ($aMatches as $aMatch) {
777             $sResult = '';
778             foreach ($aPartitions as $sPartitionName) {
779                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
780             }
781             $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
782         }
783
784         $this->pgsqlRunScript($sTemplate);
785     }
786
787     private function pgsqlRunScriptFile($sFilename)
788     {
789         if (!file_exists($sFilename)) fail('unable to find '.$sFilename);
790
791         $sCMD = 'psql'
792             .' -p '.escapeshellarg($this->aDSNInfo['port'])
793             .' -d '.escapeshellarg($this->aDSNInfo['database']);
794         if (!$this->bVerbose) {
795             $sCMD .= ' -q';
796         }
797         if (isset($this->aDSNInfo['hostspec'])) {
798             $sCMD .= ' -h '.escapeshellarg($this->aDSNInfo['hostspec']);
799         }
800         if (isset($this->aDSNInfo['username'])) {
801             $sCMD .= ' -U '.escapeshellarg($this->aDSNInfo['username']);
802         }
803         $aProcEnv = null;
804         if (isset($this->aDSNInfo['password'])) {
805             $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
806         }
807         $ahGzipPipes = null;
808         if (preg_match('/\\.gz$/', $sFilename)) {
809             $aDescriptors = array(
810                              0 => array('pipe', 'r'),
811                              1 => array('pipe', 'w'),
812                              2 => array('file', '/dev/null', 'a')
813                             );
814             $hGzipProcess = proc_open('zcat '.escapeshellarg($sFilename), $aDescriptors, $ahGzipPipes);
815             if (!is_resource($hGzipProcess)) fail('unable to start zcat');
816             $aReadPipe = $ahGzipPipes[1];
817             fclose($ahGzipPipes[0]);
818         } else {
819             $sCMD .= ' -f '.escapeshellarg($sFilename);
820             $aReadPipe = array('pipe', 'r');
821         }
822         $aDescriptors = array(
823                          0 => $aReadPipe,
824                          1 => array('pipe', 'w'),
825                          2 => array('file', '/dev/null', 'a')
826                         );
827         $ahPipes = null;
828         $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
829         if (!is_resource($hProcess)) fail('unable to start pgsql');
830         // TODO: error checking
831         while (!feof($ahPipes[1])) {
832             echo fread($ahPipes[1], 4096);
833         }
834         fclose($ahPipes[1]);
835         $iReturn = proc_close($hProcess);
836         if ($iReturn > 0) {
837             fail("pgsql returned with error code ($iReturn)");
838         }
839         if ($ahGzipPipes) {
840             fclose($ahGzipPipes[1]);
841             proc_close($hGzipProcess);
842         }
843     }
844
845     private function replaceTablespace($sTemplate, $sTablespace, $sSql)
846     {
847         if ($sTablespace) {
848             $sSql = str_replace($sTemplate, 'TABLESPACE "'.$sTablespace.'"', $sSql);
849         } else {
850             $sSql = str_replace($sTemplate, '', $sSql);
851         }
852         return $sSql;
853     }
854
855     private function runWithPgEnv($sCmd)
856     {
857         if ($this->bVerbose) {
858             echo "Execute: $sCmd\n";
859         }
860
861         $aProcEnv = null;
862
863         if (isset($this->aDSNInfo['password'])) {
864             $aProcEnv = array_merge(array('PGPASSWORD' => $this->aDSNInfo['password']), $_ENV);
865         }
866
867         return runWithEnv($sCmd, $aProcEnv);
868     }
869
870     /**
871      * Execute the SQL command on the open database.
872      *
873      * @param string $sSQL SQL command to execute.
874      *
875      * @return null
876      *
877      * @pre connect() must have been called.
878      */
879     private function pgExec($sSQL)
880     {
881         $this->oDB->exec($sSQL);
882     }
883
884     /**
885      * Check if the database is in reverse-only mode.
886      *
887      * @return True if there is no search_name table and infrastructure.
888      */
889     private function dbReverseOnly()
890     {
891         return !($this->oDB->tableExists('search_name'));
892     }
893 }