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