]> git.openstreetmap.org Git - nominatim.git/blob - utils/update.php
move deletable and polygons into default website
[nominatim.git] / utils / update.php
1 #!/usr/bin/php -Cq
2 <?php
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-cmd.php');
5         ini_set('memory_limit', '800M');
6
7         $aCMDOptions = array(
8                 "Import / update / index osm data",
9                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
10                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
11                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
12
13                 array('max-load', '', 0, 1, 1, 1, 'float', 'Maximum load average - indexing is paused if this is exceeded'),
14                 array('max-blocking', '', 0, 1, 1, 1, 'int', 'Maximum blocking processes - indexing is aborted / paused if this is exceeded'),
15
16                 array('import-osmosis', '', 0, 1, 0, 0, 'bool', 'Import using osmosis'),
17                 array('import-osmosis-all', '', 0, 1, 0, 0, 'bool', 'Import using osmosis forever'),
18                 array('no-npi', '', 0, 1, 0, 0, 'bool', 'Do not write npi index files'),
19                 array('no-index', '', 0, 1, 0, 0, 'bool', 'Do not index the new data'),
20
21                 array('import-npi-all', '', 0, 1, 0, 0, 'bool', 'Import npi pre-indexed files'),
22
23                 array('import-hourly', '', 0, 1, 0, 0, 'bool', 'Import hourly diffs'),
24                 array('import-daily', '', 0, 1, 0, 0, 'bool', 'Import daily diffs'),
25                 array('import-all', '', 0, 1, 0, 0, 'bool', 'Import all available files'),
26
27                 array('import-file', '', 0, 1, 1, 1, 'realpath', 'Re-import data from an OSM file'),
28                 array('import-diff', '', 0, 1, 1, 1, 'realpath', 'Import a diff (osc) file from local file system'),
29
30                 array('import-node', '', 0, 1, 1, 1, 'int', 'Re-import node'),
31                 array('import-way', '', 0, 1, 1, 1, 'int', 'Re-import way'),
32                 array('import-relation', '', 0, 1, 1, 1, 'int', 'Re-import relation'),
33                 array('import-from-main-api', '', 0, 1, 0, 0, 'bool', 'Use OSM API instead of Overpass to download objects'),
34
35                 array('index', '', 0, 1, 0, 0, 'bool', 'Index'),
36                 array('index-rank', '', 0, 1, 1, 1, 'int', 'Rank to start indexing from'),
37                 array('index-instances', '', 0, 1, 1, 1, 'int', 'Number of indexing instances (threads)'),
38                 array('index-estrate', '', 0, 1, 1, 1, 'int', 'Estimated indexed items per second (def:30)'),
39
40                 array('deduplicate', '', 0, 1, 0, 0, 'bool', 'Deduplicate tokens'),
41         );
42         getCmdOpt($_SERVER['argv'], $aCMDOptions, $aResult, true, true);
43
44         if ($aResult['import-hourly'] + $aResult['import-daily'] + isset($aResult['import-diff']) > 1)
45         {
46                 showUsage($aCMDOptions, true, 'Select either import of hourly or daily');
47         }
48
49         if (!isset($aResult['index-instances'])) $aResult['index-instances'] = 1;
50         if (!isset($aResult['index-rank'])) $aResult['index-rank'] = 0;
51 /*
52         // Lock to prevent multiple copies running
53         if (exec('/bin/ps uww | grep '.basename(__FILE__).' | grep -v /dev/null | grep -v grep -c', $aOutput2, $iResult) > 1)
54         {
55                 echo "Copy already running\n";
56                 exit;
57         }
58         if (!isset($aResult['max-load'])) $aResult['max-load'] = 1.9;
59         if (!isset($aResult['max-blocking'])) $aResult['max-blocking'] = 3;
60         if (getBlockingProcesses() > $aResult['max-blocking'])
61         {
62                 echo "Too many blocking processes for import\n";
63                 exit;
64         }
65 */
66
67         // Assume osm2pgsql is in the folder above
68         $sBasePath = dirname(dirname(__FILE__));
69
70         $oDB =& getDB();
71
72         $aDSNInfo = DB::parseDSN(CONST_Database_DSN);
73
74         $bFirst = true;
75         $bContinue = $aResult['import-all'];
76         while ($bContinue || $bFirst)
77         {
78                 $bFirst = false;
79
80                 if ($aResult['import-hourly'])
81                 {
82                         // Mirror the hourly diffs
83                         exec('wget --quiet --mirror -l 1 -P '.$sMirrorDir.' http://planet.openstreetmap.org/hourly');
84                         $sNextFile = $oDB->getOne('select TO_CHAR(lastimportdate,\'YYYYMMDDHH24\')||\'-\'||TO_CHAR(lastimportdate+\'1 hour\'::interval,\'YYYYMMDDHH24\')||\'.osc.gz\' from import_status');
85                         $sNextFile = $sMirrorDir.'planet.openstreetmap.org/hourly/'.$sNextFile;
86                         $sUpdateSQL = 'update import_status set lastimportdate = lastimportdate+\'1 hour\'::interval';
87                 }
88
89                 if ($aResult['import-daily'])
90                 {
91                         // Mirror the daily diffs
92                         exec('wget --quiet --mirror -l 1 -P '.$sMirrorDir.' http://planet.openstreetmap.org/daily');
93                         $sNextFile = $oDB->getOne('select TO_CHAR(lastimportdate,\'YYYYMMDD\')||\'-\'||TO_CHAR(lastimportdate+\'1 day\'::interval,\'YYYYMMDD\')||\'.osc.gz\' from import_status');
94                         $sNextFile = $sMirrorDir.'planet.openstreetmap.org/daily/'.$sNextFile;
95                         $sUpdateSQL = 'update import_status set lastimportdate = lastimportdate::date + 1';
96                 }
97                 
98                 if (isset($aResult['import-diff']))
99                 {
100                         // import diff directly (e.g. from osmosis --rri)
101                         $sNextFile = $aResult['import-diff'];
102                         if (!file_exists($sNextFile))
103                         {
104                                 echo "Cannot open $nextFile\n";
105                                 exit;
106                         }
107                         // Don't update the import status - we don't know what this file contains
108                         $sUpdateSQL = 'update import_status set lastimportdate = now() where false';
109                 }
110
111                 // Missing file is not an error - it might not be created yet
112                 if (($aResult['import-hourly'] || $aResult['import-daily'] || isset($aResult['import-diff'])) && file_exists($sNextFile))
113                 {
114                         // Import the file
115                         $sCMD = CONST_Osm2pgsql_Binary.' -klas -C 2000 -O gazetteer -d '.$aDSNInfo['database'].' '.$sNextFile;
116                         echo $sCMD."\n";
117                         exec($sCMD, $sJunk, $iErrorLevel);
118
119                         if ($iErrorLevel)
120                         {
121                                 echo "Error from osm2pgsql, $iErrorLevel\n";
122                                 exit;
123                         }
124         
125                         // Move the date onwards
126                         $oDB->query($sUpdateSQL);
127                 }
128                 else
129                 {
130                         $bContinue = false;
131                 }
132         }
133
134         $bModifyXML = false;
135         $sModifyXMLstr = '';
136         $bUseOSMApi = isset($aResult['import-from-main-api']) && $aResult['import-from-main-api'];
137         if (isset($aResult['import-file']) && $aResult['import-file'])
138         {
139                 $bModifyXML = true;
140         }
141         if (isset($aResult['import-node']) && $aResult['import-node'])
142         {
143                 $bModifyXML = true;
144                 if ($bUseOSMApi)
145                 {
146                         $sModifyXMLstr = file_get_contents('http://www.openstreetmap.org/api/0.6/node/'.$aResult['import-node']);
147                 }
148                 else
149                 {
150                         $sModifyXMLstr = file_get_contents('http://overpass.osm.rambler.ru/cgi/interpreter?data=node('.$aResult['import-node'].');out%20meta;');
151                 }
152         }
153         if (isset($aResult['import-way']) && $aResult['import-way'])
154         {
155                 $bModifyXML = true;
156                 if ($bUseOSMApi)
157                 {
158                         $sCmd = 'http://www.openstreetmap.org/api/0.6/way/'.$aResult['import-way'].'/full';
159                 }
160                 else
161                 {
162                         $sCmd = 'http://overpass.osm.rambler.ru/cgi/interpreter?data=(way('.$aResult['import-way'].');node(w););out%20meta;';
163                 }
164                 $sModifyXMLstr = file_get_contents($sCmd);
165         }
166         if (isset($aResult['import-relation']) && $aResult['import-relation'])
167         {
168                 $bModifyXML = true;
169                 if ($bUseOSMApi)
170                 {
171                         $sModifyXMLstr = file_get_contents('http://www.openstreetmap.org/api/0.6/relation/'.$aResult['import-relation'].'/full');
172                 }
173                 else
174                 {
175                         $sModifyXMLstr = file_get_contents('http://overpass.osm.rambler.ru/cgi/interpreter?data=((rel('.$aResult['import-relation'].');way(r);node(w));node(r));out%20meta;');
176                 }
177         }
178         if ($bModifyXML)
179         {
180                 // derive change from normal osm file with osmosis
181                 $sTemporaryFile = CONST_BasePath.'/data/osmosischange.osc';
182                 if (isset($aResult['import-file']) && $aResult['import-file'])
183                 {
184                         $sCMD = CONST_Osmosis_Binary.' --read-xml \''.$aResult['import-file'].'\' --read-empty --derive-change --write-xml-change '.$sTemporaryFile;
185                         echo $sCMD."\n";
186                         exec($sCMD, $sJunk, $iErrorLevel);
187                         if ($iErrorLevel)
188                         {
189                                 echo "Error converting osm to osc, osmosis returned: $iErrorLevel\n";
190                                 exit;
191                         }
192                 }
193                 else
194                 {
195                         $aSpec = array(
196                                 0 => array("pipe", "r"),  // stdin
197                                 1 => array("pipe", "w"),  // stdout
198                                 2 => array("pipe", "w") // stderr
199                         );
200                         $sCMD = CONST_Osmosis_Binary.' --read-xml - --read-empty --derive-change --write-xml-change '.$sTemporaryFile;
201                         echo $sCMD."\n";
202                         $hProc = proc_open($sCMD, $aSpec, $aPipes);
203                         if (!is_resource($hProc))
204                         {
205                                 echo "Error converting osm to osc, osmosis failed\n";
206                                 exit;
207                         }
208                         fwrite($aPipes[0], $sModifyXMLstr);
209                         fclose($aPipes[0]);
210                         $sOut = stream_get_contents($aPipes[1]);
211                         if ($aResult['verbose']) echo $sOut;
212                         fclose($aPipes[1]);
213                         $sErrors = stream_get_contents($aPipes[2]);
214                         if ($aResult['verbose']) echo $sErrors;
215                         fclose($aPipes[2]);
216                         if ($iError = proc_close($hProc))
217                         {
218                                 echo "Error converting osm to osc, osmosis returned: $iError\n";
219                                 echo $sOut;
220                                 echo $sErrors;
221                                 exit;
222                         }
223                 }
224
225                 // import generated change file
226                 $sCMD = CONST_Osm2pgsql_Binary.' -klas -C 2000 -O gazetteer -d '.$aDSNInfo['database'].' '.$sTemporaryFile;
227                 echo $sCMD."\n";
228                 exec($sCMD, $sJunk, $iErrorLevel);
229                 if ($iErrorLevel)
230                 {
231                         echo "osm2pgsql exited with error level $iErrorLevel\n";
232                         exit;
233                 }
234         }
235
236         if ($aResult['deduplicate'])
237         {
238                 $oDB =& getDB();
239                 $sSQL = 'select partition from country_name order by country_code';
240                 $aPartitions = $oDB->getCol($sSQL);
241                 if (PEAR::isError($aPartitions))
242                 {
243                         fail($aPartitions->getMessage());
244                 }
245                 $aPartitions[] = 0;
246
247                 $sSQL = "select word_token,count(*) from word where substr(word_token, 1, 1) = ' ' and class is null and type is null and country_code is null group by word_token having count(*) > 1 order by word_token";
248                 $aDuplicateTokens = $oDB->getAll($sSQL);
249                 foreach($aDuplicateTokens as $aToken)
250                 {
251                         if (trim($aToken['word_token']) == '' || trim($aToken['word_token']) == '-') continue;
252                         echo "Deduping ".$aToken['word_token']."\n";
253                         $sSQL = "select word_id,(select count(*) from search_name where nameaddress_vector @> ARRAY[word_id]) as num from word where word_token = '".$aToken['word_token']."' and class is null and type is null and country_code is null order by num desc";
254                         $aTokenSet = $oDB->getAll($sSQL);
255                         if (PEAR::isError($aTokenSet))
256                         {
257                                 var_dump($aTokenSet, $sSQL);
258                                 exit;
259                         }
260                         
261                         $aKeep = array_shift($aTokenSet);
262                         $iKeepID = $aKeep['word_id'];
263
264                         foreach($aTokenSet as $aRemove)
265                         {
266                                 $sSQL = "update search_name set";
267                                 $sSQL .= " name_vector = (name_vector - ".$aRemove['word_id'].")+".$iKeepID.",";
268                                 $sSQL .= " nameaddress_vector = (nameaddress_vector - ".$aRemove['word_id'].")+".$iKeepID;
269                                 $sSQL .= " where name_vector @> ARRAY[".$aRemove['word_id']."]";
270                                 $x = $oDB->query($sSQL);
271                                 if (PEAR::isError($x))
272                                 {
273                                         var_dump($x);
274                                         exit;
275                                 }
276
277                                 $sSQL = "update search_name set";
278                                 $sSQL .= " nameaddress_vector = (nameaddress_vector - ".$aRemove['word_id'].")+".$iKeepID;
279                                 $sSQL .= " where nameaddress_vector @> ARRAY[".$aRemove['word_id']."]";
280                                 $x = $oDB->query($sSQL);
281                                 if (PEAR::isError($x))
282                                 {
283                                         var_dump($x);
284                                         exit;
285                                 }
286
287                                 $sSQL = "update location_area_country set";
288                                 $sSQL .= " keywords = (keywords - ".$aRemove['word_id'].")+".$iKeepID;
289                                 $sSQL .= " where keywords @> ARRAY[".$aRemove['word_id']."]";
290                                 $x = $oDB->query($sSQL);
291                                 if (PEAR::isError($x))
292                                 {
293                                         var_dump($x);
294                                         exit;
295                                 }
296
297                                 foreach ($aPartitions as $sPartition)
298                                 {
299                                         $sSQL = "update search_name_".$sPartition." set";
300                                         $sSQL .= " name_vector = (name_vector - ".$aRemove['word_id'].")+".$iKeepID.",";
301                                         $sSQL .= " nameaddress_vector = (nameaddress_vector - ".$aRemove['word_id'].")+".$iKeepID;
302                                         $sSQL .= " where name_vector @> ARRAY[".$aRemove['word_id']."]";
303                                         $x = $oDB->query($sSQL);
304                                         if (PEAR::isError($x))
305                                         {
306                                                 var_dump($x);
307                                                 exit;
308                                         }
309
310                                         $sSQL = "update search_name_".$sPartition." set";
311                                         $sSQL .= " nameaddress_vector = (nameaddress_vector - ".$aRemove['word_id'].")+".$iKeepID;
312                                         $sSQL .= " where nameaddress_vector @> ARRAY[".$aRemove['word_id']."]";
313                                         $x = $oDB->query($sSQL);
314                                         if (PEAR::isError($x))
315                                         {
316                                                 var_dump($x);
317                                                 exit;
318                                         }
319
320                                         $sSQL = "update location_area_country set";
321                                         $sSQL .= " keywords = (keywords - ".$aRemove['word_id'].")+".$iKeepID;
322                                         $sSQL .= " where keywords @> ARRAY[".$aRemove['word_id']."]";
323                                         $x = $oDB->query($sSQL);
324                                         if (PEAR::isError($x))
325                                         {
326                                                 var_dump($x);
327                                                 exit;
328                                         }
329                                 }
330
331                                 $sSQL = "delete from word where word_id = ".$aRemove['word_id'];
332                                 $x = $oDB->query($sSQL);
333                                 if (PEAR::isError($x))
334                                 {
335                                         var_dump($x);
336                                         exit;
337                                 }
338                         }
339
340                 }
341         }
342
343         if ($aResult['index'])
344         {
345                 passthru(CONST_BasePath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -t '.$aResult['index-instances'].' -r '.$aResult['index-rank']);
346         }
347
348         if ($aResult['import-osmosis'] || $aResult['import-osmosis-all'])
349         {
350                 $sImportFile = CONST_BasePath.'/data/osmosischange.osc';
351                 $sOsmosisCMD = CONST_Osmosis_Binary;
352                 $sOsmosisConfigDirectory = CONST_BasePath.'/settings';
353                 $sCMDDownload = $sOsmosisCMD.' --read-replication-interval workingDirectory='.$sOsmosisConfigDirectory.' --simplify-change --write-xml-change '.$sImportFile;
354                 $sCMDImport = CONST_Osm2pgsql_Binary.' -klas -C 2000 -O gazetteer -d '.$aDSNInfo['database'].' '.$sImportFile;
355                 $sCMDIndex = $sBasePath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -t '.$aResult['index-instances'];
356                 if (!$aResult['no-npi']) {
357                         $sCMDIndex .= '-F ';
358                 }
359                 while(true)
360                 {
361                         $fStartTime = time();
362                         $iFileSize = 1001;
363
364                         // Logic behind this is that osm2pgsql locks the database quite a bit
365                         // So it is better to import lots of small files
366                         // But indexing works most efficiently on large amounts of data
367                         // So do lots of small imports and a BIG index
368
369 //                      while($aResult['import-osmosis-all'] && $iFileSize > 1000)
370 //                      {
371                                 if (!file_exists($sImportFile))
372                                 {
373                                         // Use osmosis to download the file
374                                         $fCMDStartTime = time();
375                                         echo $sCMDDownload."\n";
376                                         exec($sCMDDownload, $sJunk, $iErrorLevel);
377                                         while ($iErrorLevel == 1)
378                                         {
379                                                 echo "Error: $iErrorLevel\n";
380                                                 sleep(60);
381                                                 echo 'Re-trying: '.$sCMDDownload."\n";
382                                                 exec($sCMDDownload, $sJunk, $iErrorLevel);
383                                         }
384                                         $iFileSize = filesize($sImportFile);
385                                         $sBatchEnd = getosmosistimestamp($sOsmosisConfigDirectory);
386                                         echo "Completed for $sBatchEnd in ".round((time()-$fCMDStartTime)/60,2)." minutes\n";
387                                         $sSQL = "INSERT INTO import_osmosis_log values ('$sBatchEnd',$iFileSize,'".date('Y-m-d H:i:s',$fCMDStartTime)."','".date('Y-m-d H:i:s')."','osmosis')";
388                                         $oDB->query($sSQL);
389                                 }
390
391                                 $iFileSize = filesize($sImportFile);
392                                 $sBatchEnd = getosmosistimestamp($sOsmosisConfigDirectory);
393                 
394                                 // Import the file
395                                 $fCMDStartTime = time();
396                                 echo $sCMDImport."\n";
397                                 exec($sCMDImport, $sJunk, $iErrorLevel);
398                                 if ($iErrorLevel)
399                                 {
400                                         echo "Error: $iErrorLevel\n";
401                                         exit;
402                                 }
403                                 echo "Completed for $sBatchEnd in ".round((time()-$fCMDStartTime)/60,2)." minutes\n";
404                                 $sSQL = "INSERT INTO import_osmosis_log values ('$sBatchEnd',$iFileSize,'".date('Y-m-d H:i:s',$fCMDStartTime)."','".date('Y-m-d H:i:s')."','osm2pgsql')";
405                                 var_Dump($sSQL);
406                                 $oDB->query($sSQL);
407
408                                 // Archive for debug?
409                                 unlink($sImportFile);
410 //                      }
411
412                         $sBatchEnd = getosmosistimestamp($sOsmosisConfigDirectory);
413
414                         // Index file
415                         $sThisIndexCmd = $sCMDIndex;
416
417                         if (!$aResult['no-npi'])
418                         {
419                                 $fCMDStartTime = time();
420                                 $iFileID = $oDB->getOne('select nextval(\'file\')');
421                                 if (PEAR::isError($iFileID))
422                                 {
423                                         echo $iFileID->getMessage()."\n";
424                                         exit;
425                                 } 
426                                 $sFileDir = CONST_BasePath.'/export/diff/';
427                                 $sFileDir .= str_pad(floor($iFileID/1000000), 3, '0', STR_PAD_LEFT);
428                                 $sFileDir .= '/'.str_pad(floor($iFileID/1000) % 1000, 3, '0', STR_PAD_LEFT);
429
430                                 if (!is_dir($sFileDir)) mkdir($sFileDir, 0777, true);
431                                 $sThisIndexCmd .= $sFileDir;
432                                 $sThisIndexCmd .= '/'.str_pad($iFileID % 1000, 3, '0', STR_PAD_LEFT);
433                                 $sThisIndexCmd .= ".npi.out";
434
435                                 preg_match('#^([0-9]{4})-([0-9]{2})-([0-9]{2})#', $sBatchEnd, $aBatchMatch);
436                                 $sFileDir = CONST_BasePath.'/export/index/';
437                                 $sFileDir .= $aBatchMatch[1].'/'.$aBatchMatch[2];
438
439                                 if (!is_dir($sFileDir)) mkdir($sFileDir, 0777, true);
440                                 file_put_contents($sFileDir.'/'.$aBatchMatch[3].'.idx', "$sBatchEnd\t$iFileID\n", FILE_APPEND);
441                         }
442
443                         if (!$aResult['no-index'])
444                         {
445                                 echo "$sThisIndexCmd\n";
446                                 exec($sThisIndexCmd, $sJunk, $iErrorLevel);
447                                 if ($iErrorLevel)
448                                 {
449                                         echo "Error: $iErrorLevel\n";
450                                         exit;
451                                 }
452
453                                 if (!$aResult['no-npi'])
454                                 {
455                                         $sFileDir = CONST_BasePath.'/export/diff/';
456                                         $sFileDir .= str_pad(floor($iFileID/1000000), 3, '0', STR_PAD_LEFT);
457                                         $sFileDir .= '/'.str_pad(floor($iFileID/1000) % 1000, 3, '0', STR_PAD_LEFT);
458
459                                         $sThisIndexCmd = 'bzip2 -z9 '.$sFileDir.'/'.str_pad($iFileID % 1000, 3, '0', STR_PAD_LEFT).".npi.out";
460                                         echo "$sThisIndexCmd\n";
461                                         exec($sThisIndexCmd, $sJunk, $iErrorLevel);
462                                         if ($iErrorLevel)
463                                         {
464                                                 echo "Error: $iErrorLevel\n";
465                                                 exit;
466                                         }
467
468                                         rename($sFileDir.'/'.str_pad($iFileID % 1000, 3, '0', STR_PAD_LEFT).".npi.out.bz2",
469                                                 $sFileDir.'/'.str_pad($iFileID % 1000, 3, '0', STR_PAD_LEFT).".npi.bz2");
470                                 }
471                         }
472
473                         echo "Completed for $sBatchEnd in ".round((time()-$fCMDStartTime)/60,2)." minutes\n";
474                         $sSQL = "INSERT INTO import_osmosis_log values ('$sBatchEnd',$iFileSize,'".date('Y-m-d H:i:s',$fCMDStartTime)."','".date('Y-m-d H:i:s')."','index')";
475                         $oDB->query($sSQL);
476
477                         $sSQL = "update import_status set lastimportdate = '$sBatchEnd'";
478                         $oDB->query($sSQL);
479
480                         $fDuration = time() - $fStartTime;
481                         echo "Completed for $sBatchEnd in ".round($fDuration/60,2)."\n";
482                         if (!$aResult['import-osmosis-all']) exit;
483
484                         echo "Sleeping ".max(0,60-$fDuration)." seconds\n";
485                         sleep(max(0,60-$fDuration));
486                 }
487
488         }
489
490         if ($aResult['import-npi-all'])
491         {
492                 $iNPIID = $oDB->getOne('select max(npiid) from import_npi_log');
493                 if (PEAR::isError($iNPIID))
494                 {
495                         var_dump($iNPIID);
496                         exit;
497                 }
498                 $sConfigDirectory = CONST_BasePath.'/settings';
499                 $sCMDImportTemplate = $sBasePath.'/nominatim/nominatim -d gazetteer -P 5433 -I -T '.$sBasePath.'/nominatim/partitionedtags.def -F ';
500                 while(true)
501                 {
502                         $fStartTime = time();
503
504                         $iNPIID++;
505
506                         $sImportFile = CONST_BasePath.'/export/diff/';
507                         $sImportFile .= str_pad(floor($iNPIID/1000000), 3, '0', STR_PAD_LEFT);
508                         $sImportFile .= '/'.str_pad(floor($iNPIID/1000) % 1000, 3, '0', STR_PAD_LEFT);
509                         $sImportFile .= '/'.str_pad($iNPIID % 1000, 3, '0', STR_PAD_LEFT);
510                         $sImportFile .= ".npi";
511                         while(!file_exists($sImportFile) && !file_exists($sImportFile.'.bz2'))
512                         {
513                                 echo "sleep (waiting for $sImportFile)\n";
514                                 sleep(10);
515                         }
516                         if (file_exists($sImportFile.'.bz2')) $sImportFile .= '.bz2';
517
518                         $iFileSize = filesize($sImportFile);
519                 
520                         // Import the file
521                         $fCMDStartTime = time();
522                         $sCMDImport = $sCMDImportTemplate . $sImportFile;
523                         echo $sCMDImport."\n";
524                         exec($sCMDImport, $sJunk, $iErrorLevel);
525                         if ($iErrorLevel)
526                         {
527                                 echo "Error: $iErrorLevel\n";
528                                 exit;
529                         }
530                         $sBatchEnd = $iNPIID;
531                         echo "Completed for $sBatchEnd in ".round((time()-$fCMDStartTime)/60,2)." minutes\n";
532                         $sSQL = "INSERT INTO import_npi_log values ($iNPIID, null, $iFileSize,'".date('Y-m-d H:i:s',$fCMDStartTime)."','".date('Y-m-d H:i:s')."','import')";
533                         var_Dump($sSQL);
534                         $oDB->query($sSQL);
535                 }
536                 
537         }
538
539         function getosmosistimestamp($sOsmosisConfigDirectory)
540         {
541                 $sStateFile = file_get_contents($sOsmosisConfigDirectory.'/state.txt');
542                 preg_match('#timestamp=(.+)#', $sStateFile, $aResult);
543                 return str_replace('\:',':',$aResult[1]);
544         }