]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/admin/setup.php
0b0084957d6ecef3081409ce3a6e21da7cb6cfb9
[nominatim.git] / lib-php / admin / setup.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/SetupClass.php');
6 require_once(CONST_LibDir.'/setup_functions.php');
7 ini_set('memory_limit', '800M');
8
9 use Nominatim\Setup\SetupFunctions as SetupFunctions;
10
11 // (long-opt, short-opt, min-occurs, max-occurs, num-arguments, num-arguments, type, help)
12 $aCMDOptions
13 = array(
14    'Create and setup nominatim search system',
15    array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
16    array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
17    array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
18
19    array('osm-file', '', 0, 1, 1, 1, 'realpath', 'File to import'),
20    array('threads', '', 0, 1, 1, 1, 'int', 'Number of threads (where possible)'),
21
22    array('all', '', 0, 1, 0, 0, 'bool', 'Do the complete process'),
23
24    array('create-db', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
25    array('setup-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
26    array('import-data', '', 0, 1, 0, 0, 'bool', 'Import a osm file'),
27    array('osm2pgsql-cache', '', 0, 1, 1, 1, 'int', 'Cache size used by osm2pgsql'),
28    array('reverse-only', '', 0, 1, 0, 0, 'bool', 'Do not create search tables and indexes'),
29    array('create-functions', '', 0, 1, 0, 0, 'bool', 'Create functions'),
30    array('enable-diff-updates', '', 0, 1, 0, 0, 'bool', 'Turn on the code required to make diff updates work'),
31    array('enable-debug-statements', '', 0, 1, 0, 0, 'bool', 'Include debug warning statements in pgsql commands'),
32    array('ignore-errors', '', 0, 1, 0, 0, 'bool', 'Continue import even when errors in SQL are present (EXPERT)'),
33    array('create-tables', '', 0, 1, 0, 0, 'bool', 'Create main tables'),
34    array('create-partition-tables', '', 0, 1, 0, 0, 'bool', 'Create required partition tables'),
35    array('create-partition-functions', '', 0, 1, 0, 0, 'bool', 'Create required partition triggers'),
36    array('no-partitions', '', 0, 1, 0, 0, 'bool', 'Do not partition search indices (speeds up import of single country extracts)'),
37    array('import-wikipedia-articles', '', 0, 1, 0, 0, 'bool', 'Import wikipedia article dump'),
38    array('load-data', '', 0, 1, 0, 0, 'bool', 'Copy data to live tables from import table'),
39    array('disable-token-precalc', '', 0, 1, 0, 0, 'bool', 'Disable name precalculation (EXPERT)'),
40    array('import-tiger-data', '', 0, 1, 0, 0, 'bool', 'Import tiger data (not included in \'all\')'),
41    array('calculate-postcodes', '', 0, 1, 0, 0, 'bool', 'Calculate postcode centroids'),
42    array('index', '', 0, 1, 0, 0, 'bool', 'Index the data'),
43    array('index-noanalyse', '', 0, 1, 0, 0, 'bool', 'Do not perform analyse operations during index (EXPERT)'),
44    array('create-search-indices', '', 0, 1, 0, 0, 'bool', 'Create additional indices required for search and update'),
45    array('create-country-names', '', 0, 1, 0, 0, 'bool', 'Create default list of searchable country names'),
46    array('drop', '', 0, 1, 0, 0, 'bool', 'Drop tables needed for updates, making the database readonly (EXPERIMENTAL)'),
47    array('setup-website', '', 0, 1, 0, 0, 'bool', 'Used to compile environment variables for the website'),
48    array('project-dir', '', 0, 1, 1, 1, 'realpath', 'Base directory of the Nominatim installation (default: .)'),
49   );
50
51 // $aCMDOptions passed to getCmdOpt by reference
52 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
53
54 loadSettings($aCMDResult['project-dir'] ?? getcwd());
55 setupHTTPProxy();
56
57 $bDidSomething = false;
58
59 $oNominatimCmd = new \Nominatim\Shell(getSetting('NOMINATIM_TOOL'));
60
61 // by default, use all but one processor, but never more than 15.
62 $iInstances = max(1, $aCMDResult['threads'] ?? (min(16, getProcessorCount()) - 1));
63
64 function run($oCmd)
65 {
66     global $iInstances;
67     global $aCMDResult;
68     $oCmd->addParams('--threads', $iInstances);
69     if ($aCMDResult['ignore-errors'] ?? false) {
70         $oCmd->addParams('--ignore-errors');
71     }
72     if ($aCMDResult['quiet'] ?? false) {
73         $oCmd->addParams('--quiet');
74     }
75     if ($aCMDResult['verbose'] ?? false) {
76         $oCmd->addParams('--verbose');
77     }
78     $oCmd->run(true);
79 }
80
81
82 //*******************************************************
83 // Making some sanity check:
84 // Check if osm-file is set and points to a valid file
85 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
86     // to remain in /lib/setup_functions.php function
87     checkInFile($aCMDResult['osm-file']);
88 }
89
90 // ******************************************************
91 // instantiate Setup class
92 $oSetup = new SetupFunctions($aCMDResult);
93
94 // *******************************************************
95 // go through complete process if 'all' is selected or start selected functions
96 if ($aCMDResult['create-db'] || $aCMDResult['all']) {
97     $bDidSomething = true;
98     run((clone($oNominatimCmd))->addParams('transition', '--create-db'));
99 }
100
101 if ($aCMDResult['setup-db'] || $aCMDResult['all']) {
102     $bDidSomething = true;
103     $oCmd = (clone($oNominatimCmd))->addParams('transition', '--setup-db');
104
105     if ($aCMDResult['no-partitions'] ?? false) {
106         $oCmd->addParams('--no-partitions');
107     }
108
109     run($oCmd);
110 }
111
112 if ($aCMDResult['import-data'] || $aCMDResult['all']) {
113     $bDidSomething = true;
114     $oCmd = (clone($oNominatimCmd))
115         ->addParams('transition', '--import-data')
116         ->addParams('--osm-file', $aCMDResult['osm-file']);
117     if ($aCMDResult['drop'] ?? false) {
118         $oCmd->addParams('--drop');
119     }
120
121     run($oCmd);
122 }
123
124 if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
125     $bDidSomething = true;
126     $oSetup->createSqlFunctions();
127 }
128
129 if ($aCMDResult['create-tables'] || $aCMDResult['all']) {
130     $bDidSomething = true;
131     $oCmd = (clone($oNominatimCmd))->addParams('transition', '--create-tables');
132
133     if ($aCMDResult['reverse-only'] ?? false) {
134         $oCmd->addParams('--reverse-only');
135     }
136
137     run($oCmd);
138 }
139
140 if ($aCMDResult['create-partition-tables'] || $aCMDResult['all']) {
141     $bDidSomething = true;
142     run((clone($oNominatimCmd))->addParams('transition', '--create-partition-tables'));
143 }
144
145 if ($aCMDResult['create-partition-functions'] || $aCMDResult['all']) {
146     $bDidSomething = true;
147     $oSetup->createSqlFunctions(); // also create partition functions
148 }
149
150 if ($aCMDResult['import-wikipedia-articles'] || $aCMDResult['all']) {
151     $bDidSomething = true;
152     // ignore errors!
153     (clone($oNominatimCmd))->addParams('refresh', '--wiki-data')->run();
154 }
155
156 if ($aCMDResult['load-data'] || $aCMDResult['all']) {
157     $bDidSomething = true;
158     run((clone($oNominatimCmd))->addParams('transition', '--load-data'));
159 }
160
161 if ($aCMDResult['import-tiger-data']) {
162     $bDidSomething = true;
163     $sTigerPath = getSetting('TIGER_DATA_PATH', CONST_InstallDir.'/tiger');
164     run((clone($oNominatimCmd))->addParams('transition', '--tiger-data', $sTigerPath));
165 }
166
167 if ($aCMDResult['calculate-postcodes'] || $aCMDResult['all']) {
168     $bDidSomething = true;
169     $oSetup->calculatePostcodes($aCMDResult['all']);
170 }
171
172 if ($aCMDResult['index'] || $aCMDResult['all']) {
173     $bDidSomething = true;
174     $oCmd = (clone($oNominatimCmd))->addParams('transition', '--index');
175     if ($aCMDResult['index-noanalyse'] ?? false) {
176         $oCmd->addParams('--no-analyse');
177     }
178
179     run($oCmd);
180 }
181
182 if ($aCMDResult['drop']) {
183     $bDidSomething = true;
184     run((clone($oNominatimCmd))->addParams('freeze'));
185 }
186
187 if ($aCMDResult['create-search-indices'] || $aCMDResult['all']) {
188     $bDidSomething = true;
189
190     $oCmd = (clone($oNominatimCmd))->addParams('transition', '--create-search-indices');
191
192     if ($aCMDResult['drop'] ?? false) {
193         $oCmd->addParams('--drop');
194     }
195
196     run($oCmd);
197 }
198
199 if ($aCMDResult['create-country-names'] || $aCMDResult['all']) {
200     $bDidSomething = true;
201     $oSetup->createCountryNames($aCMDResult);
202 }
203
204 if ($aCMDResult['setup-website'] || $aCMDResult['all']) {
205     $bDidSomething = true;
206     run((clone($oNominatimCmd))->addParams('refresh', '--website'));
207 }
208
209 // ******************************************************
210 // If we did something, repeat the warnings
211 if (!$bDidSomething) {
212     showUsage($aCMDOptions, true);
213 } else {
214     echo "Summary of warnings:\n\n";
215     repeatWarnings();
216     echo "\n";
217     info('Setup finished.');
218 }