]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/admin/update.php
Merge pull request #2390 from lonvia/responsible-disclosure
[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']) $aDSNInfo['port'] = 5432;
44
45 // cache memory to be used by osm2pgsql, should not be more than the available memory
46 $iCacheMemory = (isset($aResult['osm2pgsql-cache'])?$aResult['osm2pgsql-cache']:2000);
47 if ($iCacheMemory + 500 > getTotalMemoryMB()) {
48     $iCacheMemory = getCacheMemoryMB();
49     echo "WARNING: resetting cache memory to $iCacheMemory\n";
50 }
51
52 $oOsm2pgsqlCmd = (new \Nominatim\Shell(getOsm2pgsqlBinary()))
53                  ->addParams('--hstore')
54                  ->addParams('--latlong')
55                  ->addParams('--append')
56                  ->addParams('--slim')
57                  ->addParams('--with-forward-dependencies', 'false')
58                  ->addParams('--log-progress', 'true')
59                  ->addParams('--number-processes', 1)
60                  ->addParams('--cache', $iCacheMemory)
61                  ->addParams('--output', 'gazetteer')
62                  ->addParams('--style', getImportStyle())
63                  ->addParams('--database', $aDSNInfo['database'])
64                  ->addParams('--port', $aDSNInfo['port']);
65
66 if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
67     $oOsm2pgsqlCmd->addParams('--host', $aDSNInfo['hostspec']);
68 }
69 if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
70     $oOsm2pgsqlCmd->addParams('--user', $aDSNInfo['username']);
71 }
72 if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
73     $oOsm2pgsqlCmd->addEnvPair('PGPASSWORD', $aDSNInfo['password']);
74 }
75 if (getSetting('FLATNODE_FILE')) {
76     $oOsm2pgsqlCmd->addParams('--flat-nodes', getSetting('FLATNODE_FILE'));
77 }
78 if ($fPostgresVersion >= 11.0) {
79     $oOsm2pgsqlCmd->addEnvPair(
80         'PGOPTIONS',
81         '-c jit=off -c max_parallel_workers_per_gather=0'
82     );
83 }
84
85 if (isset($aResult['import-diff']) || isset($aResult['import-file'])) {
86     // import diffs and files directly (e.g. from osmosis --rri)
87     $sNextFile = isset($aResult['import-diff']) ? $aResult['import-diff'] : $aResult['import-file'];
88
89     if (!file_exists($sNextFile)) {
90         fail("Cannot open $sNextFile\n");
91     }
92
93     // Import the file
94     $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sNextFile);
95     echo $oCMD->escapedCmd()."\n";
96     $iRet = $oCMD->run();
97
98     if ($iRet) {
99         fail("Error from osm2pgsql, $iRet\n");
100     }
101
102     // Don't update the import status - we don't know what this file contains
103 }
104
105 $sTemporaryFile = CONST_InstallDir.'/osmosischange.osc';
106 $bHaveDiff = false;
107 $bUseOSMApi = isset($aResult['import-from-main-api']) && $aResult['import-from-main-api'];
108 $sContentURL = '';
109 if (isset($aResult['import-node']) && $aResult['import-node']) {
110     if ($bUseOSMApi) {
111         $sContentURL = 'https://www.openstreetmap.org/api/0.6/node/'.$aResult['import-node'];
112     } else {
113         $sContentURL = 'https://overpass-api.de/api/interpreter?data=node('.$aResult['import-node'].');out%20meta;';
114     }
115 }
116
117 if (isset($aResult['import-way']) && $aResult['import-way']) {
118     if ($bUseOSMApi) {
119         $sContentURL = 'https://www.openstreetmap.org/api/0.6/way/'.$aResult['import-way'].'/full';
120     } else {
121         $sContentURL = 'https://overpass-api.de/api/interpreter?data=(way('.$aResult['import-way'].');%3E;);out%20meta;';
122     }
123 }
124
125 if (isset($aResult['import-relation']) && $aResult['import-relation']) {
126     if ($bUseOSMApi) {
127         $sContentURL = 'https://www.openstreetmap.org/api/0.6/relation/'.$aResult['import-relation'].'/full';
128     } else {
129         $sContentURL = 'https://overpass-api.de/api/interpreter?data=(rel(id:'.$aResult['import-relation'].');%3E;);out%20meta;';
130     }
131 }
132
133 if ($sContentURL) {
134     file_put_contents($sTemporaryFile, file_get_contents($sContentURL));
135     $bHaveDiff = true;
136 }
137
138 if ($bHaveDiff) {
139     // import generated change file
140
141     $oCMD = (clone $oOsm2pgsqlCmd)->addParams($sTemporaryFile);
142     echo $oCMD->escapedCmd()."\n";
143
144     $iRet = $oCMD->run();
145     if ($iRet) {
146         fail("osm2pgsql exited with error level $iRet\n");
147     }
148 }