]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/admin/update.php
fix more missing braces on one-liners
[nominatim.git] / lib-php / admin / update.php
1 <?php
2 @define('CONST_LibDir', dirname(dirname(__FILE__)));
3
4 require_once(CONST_LibDir.'/init-cmd.php');
5 require_once(CONST_LibDir.'/setup_functions.php');
6
7 ini_set('memory_limit', '800M');
8
9 // (long-opt, short-opt, min-occurs, max-occurs, num-arguments, num-arguments, type, help)
10 $aCMDOptions
11 = array(
12    'Import / update / index osm data',
13    array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
14    array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
15    array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
16
17    array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Update postcode centroid table'),
18
19    array('import-file', '', 0, 1, 1, 1, 'realpath', 'Re-import data from an OSM file'),
20    array('import-diff', '', 0, 1, 1, 1, 'realpath', 'Import a diff (osc) file from local file system'),
21    array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
22
23    array('import-node', '', 0, 1, 1, 1, 'int', 'Re-import node'),
24    array('import-way', '', 0, 1, 1, 1, 'int', 'Re-import way'),
25    array('import-relation', '', 0, 1, 1, 1, 'int', 'Re-import relation'),
26    array('import-from-main-api', '', 0, 1, 0, 0, 'bool', 'Use OSM API instead of Overpass to download objects'),
27
28    array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
29   );
30
31 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aResult, true, true);
32
33 loadSettings($aCMDResult['project-dir'] ?? getcwd());
34 setupHTTPProxy();
35
36 date_default_timezone_set('Etc/UTC');
37
38 $oDB = new Nominatim\DB();
39 $oDB->connect();
40 $fPostgresVersion = $oDB->getPostgresVersion();
41
42 $aDSNInfo = Nominatim\DB::parseDSN(getSetting('DATABASE_DSN'));
43 if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) {
44     $aDSNInfo['port'] = 5432;
45 }
46
47 // cache memory to be used by osm2pgsql, should not be more than the available memory
48 $iCacheMemory = (isset($aResult['osm2pgsql-cache'])?$aResult['osm2pgsql-cache']:2000);
49 if ($iCacheMemory + 500 > getTotalMemoryMB()) {
50     $iCacheMemory = getCacheMemoryMB();
51     echo "WARNING: resetting cache memory to $iCacheMemory\n";
52 }
53
54 $oOsm2pgsqlCmd = (new \Nominatim\Shell(getOsm2pgsqlBinary()))
55                  ->addParams('--hstore')
56                  ->addParams('--latlong')
57                  ->addParams('--append')
58                  ->addParams('--slim')
59                  ->addParams('--with-forward-dependencies', 'false')
60                  ->addParams('--log-progress', 'true')
61                  ->addParams('--number-processes', 1)
62                  ->addParams('--cache', $iCacheMemory)
63                  ->addParams('--output', 'gazetteer')
64                  ->addParams('--style', getImportStyle())
65                  ->addParams('--database', $aDSNInfo['database'])
66                  ->addParams('--port', $aDSNInfo['port']);
67
68 if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
69     $oOsm2pgsqlCmd->addParams('--host', $aDSNInfo['hostspec']);
70 }
71 if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
72     $oOsm2pgsqlCmd->addParams('--user', $aDSNInfo['username']);
73 }
74 if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
75     $oOsm2pgsqlCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
76 }
77 if (getSetting('FLATNODE_FILE')) {
78     $oOsm2pgsqlCmd->addParams('--flat-nodes', getSetting('FLATNODE_FILE'));
79 }
80 if ($fPostgresVersion >= 11.0) {
81     $oOsm2pgsqlCmd->addEnvPair(
82         'PGOPTIONS',
83         '-c jit=off -c max_parallel_workers_per_gather=0'
84     );
85 }
86
87 if (isset($aResult['import-diff']) || isset($aResult['import-file'])) {
88     // import diffs and files directly (e.g. from osmosis --rri)
89     $sNextFile = isset($aResult['import-diff']) ? $aResult['import-diff'] : $aResult['import-file'];
90
91     if (!file_exists($sNextFile)) {
92         fail("Cannot open $sNextFile\n");
93     }
94
95     // Import the file
96     $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sNextFile);
97     echo $oCMD->escapedCmd()."\n";
98     $iRet = $oCMD->run();
99
100     if ($iRet) {
101         fail("Error from osm2pgsql, $iRet\n");
102     }
103
104     // Don't update the import status - we don't know what this file contains
105 }
106
107 $sTemporaryFile = CONST_InstallDir.'/osmosischange.osc';
108 $bHaveDiff = false;
109 $bUseOSMApi = isset($aResult['import-from-main-api']) && $aResult['import-from-main-api'];
110 $sContentURL = '';
111 if (isset($aResult['import-node']) && $aResult['import-node']) {
112     if ($bUseOSMApi) {
113         $sContentURL = 'https://www.openstreetmap.org/api/0.6/node/'.$aResult['import-node'];
114     } else {
115         $sContentURL = 'https://overpass-api.de/api/interpreter?data=node('.$aResult['import-node'].');out%20meta;';
116     }
117 }
118
119 if (isset($aResult['import-way']) && $aResult['import-way']) {
120     if ($bUseOSMApi) {
121         $sContentURL = 'https://www.openstreetmap.org/api/0.6/way/'.$aResult['import-way'].'/full';
122     } else {
123         $sContentURL = 'https://overpass-api.de/api/interpreter?data=(way('.$aResult['import-way'].');%3E;);out%20meta;';
124     }
125 }
126
127 if (isset($aResult['import-relation']) && $aResult['import-relation']) {
128     if ($bUseOSMApi) {
129         $sContentURL = 'https://www.openstreetmap.org/api/0.6/relation/'.$aResult['import-relation'].'/full';
130     } else {
131         $sContentURL = 'https://overpass-api.de/api/interpreter?data=(rel(id:'.$aResult['import-relation'].');%3E;);out%20meta;';
132     }
133 }
134
135 if ($sContentURL) {
136     file_put_contents($sTemporaryFile, file_get_contents($sContentURL));
137     $bHaveDiff = true;
138 }
139
140 if ($bHaveDiff) {
141     // import generated change file
142
143     $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sTemporaryFile);
144     echo $oCMD->escapedCmd()."\n";
145
146     $iRet = $oCMD->run();
147     if ($iRet) {
148         fail("osm2pgsql exited with error level $iRet\n");
149     }
150 }