OsmAnd/config/site/update_indexes.php

194 lines
5.8 KiB
PHP
Raw Normal View History

<?php
2012-08-30 21:22:29 +02:00
2012-11-14 00:19:21 +01:00
function attachRoadIndexItem($node, $indexName) {
$filename = 'road-indexes/'.$indexName;
if (file_exists($filename)) {
if ($zip->open($filename,ZIPARCHIVE::CHECKCONS)!==TRUE) {
// echo exit("cannot open <$filename>\n");
// print($filename . " cannot open as zip\n");
continue;
}
$description = $zip->getCommentIndex(0);
$stat = $zip->statIndex( 0 );
$date= date('d.m.Y',$stat['mtime']);
$size= number_format((filesize($filename) / (1024.0*1024.0)), 1, '.', '');
$zip->close();
$out -> setAttribute("road_file", "yes");
$out -> setAttribute("road_date", $date);
$out -> setAttribute("road_size", $size);
}
}
2012-01-08 13:05:05 +01:00
function updateGoogleCodeIndexes($update=false) {
2012-08-30 21:22:29 +02:00
$local_file = false;
if( basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
$update = true;
$local_file = true;
}
2012-01-08 13:05:05 +01:00
$localFileName='indexes.xml';
// check each 30 minutes
if(!$update && file_exists($localFileName) && time() - filemtime($localFileName) < 60 * 30) {
return;
}
2012-08-30 21:22:29 +02:00
if($local_file) {
echo '<h1>File update : </h1> <br>';
}
2012-01-08 13:05:05 +01:00
$dom = new DomDocument();
$output = new DOMDocument();
$output->formatOutput = true;
2012-01-08 13:05:05 +01:00
$outputIndexes = $output->createElement( "osmand_regions" );
$outputIndexes->setAttribute('mapversion','1');
$output->appendChild( $outputIndexes );
2012-01-08 13:05:05 +01:00
$st = 0;
$num = 200;
$count = 0;
$mapNodes = array();
/// 1. dlownload indexes from googlecode
while($st != -1){
$dom->loadHTMLFile("http://code.google.com/p/osmand/downloads/list?num=".$num."&start=".$st."&colspec=Filename+Summary+Uploaded+Size");
$count ++;
$xpath = new DOMXpath($dom);
$xpathI = new DOMXpath($dom);
$res = $xpath->query('//td[contains(@class,"col_0")]');
2012-01-08 13:05:05 +01:00
if($res && $res->length > 0) {
foreach($res as $node) {
$indexName = trim($node->nodeValue);
$s = $xpathI->query('td[contains(@class,"col_1")]/a[1]', $node->parentNode);
if(!$s || $s->length == 0) {
continue;
}
$description = $s->item(0)->nodeValue;
$i = strpos($description,"{");
if(!$i) {
continue;
}
$i1 = strpos($description,":", $i);
$i2 = stripos($description,"mb", $i1);
if(!$i2) {
$i2 = strpos($description,"}", $i1);
}
$date = trim(substr($description, $i + 1, $i1 - $i -1));
$size = trim(substr($description, $i1 + 1, $i2 - $i1 -1));
$description = trim(substr($description, 0, $i));
2012-08-30 21:22:29 +02:00
if($local_file) {
echo $indexName.' '.$date.' '.$size.' <br>';
}
2012-05-19 15:34:47 +02:00
if(strpos($indexName,"voice.zip") || strpos($indexName,".obf")) {
2012-01-08 13:05:05 +01:00
$ipart = strpos($indexName,"zip-");
$part = 1;
$base = $indexName;
if($ipart) {
$part = (int)substr($indexName, $ipart+4);
$base = substr($indexName, 0, $ipart+3);
if(isset($mapNodes[$base])) {
$out = $mapNodes[$base];
} else {
$out = $output->createElement( "multiregion" );
$out -> setAttribute("parts", $part);
$mapNodes[$base] = $out;
$out -> setAttribute("date", $date);
$out -> setAttribute("size", $size);
$out -> setAttribute("name", $base);
$out -> setAttribute("description", $description);
$outputIndexes->appendChild($out);
}
if( (int) $out -> getAttribute("parts") < $part){
$out -> setAttribute("parts", $part);
}
} else {
$out = $output->createElement( "region" );
$out -> setAttribute("date", $date);
$out -> setAttribute("size", $size);
$out -> setAttribute("name", $indexName);
$out -> setAttribute("description", $description);
$outputIndexes->appendChild($out);
$mapNodes[$indexName] = $out;
}
}
}
$st += $num;
} else {
2012-01-08 13:05:05 +01:00
$st = -1;
}
2012-01-08 13:05:05 +01:00
}
/// 2. append local indexes
$local = new DomDocument();
// Open a known directory, and proceed to read its contents
$dir='indexes/';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$zip = new ZipArchive();
while (($file = readdir($dh)) !== false) {
$filename = $dir . $file ; //"./test112.zip";
//print("processing file:" . $filename . "\n");
if ($zip->open($filename,ZIPARCHIVE::CHECKCONS)!==TRUE) {
// echo exit("cannot open <$filename>\n");
// print($filename . " cannot open as zip\n");
continue;
}
$indexName=$file;
$description = $zip->getCommentIndex(0);
$stat = $zip->statIndex( 0 );
$date= date('d.m.Y',$stat['mtime']);
$size= number_format((filesize($filename) / (1024.0*1024.0)), 1, '.', '');
$zip->close();
2012-08-30 21:22:29 +02:00
if($local_file) {
2012-11-13 23:23:53 +01:00
echo 'Local : '.$indexName.' '.$date.' '.$size.'<br>';
}
2012-01-08 13:05:05 +01:00
if (isset($mapNodes[$indexName])) {
$exdate = DateTime::createFromFormat('d.m.Y', $mapNodes[$indexName]->getAttribute("date"));
$localdate = DateTime::createFromFormat('d.m.Y', $date);
2012-11-14 00:19:21 +01:00
attachRoadIndexItem($mapNodes[$indexName], $indexName);
if($localdate->getTimestamp() <= $exdate->getTimestamp()) {
2012-01-08 13:05:05 +01:00
continue;
2012-11-13 23:23:53 +01:00
}
2012-01-08 13:51:30 +01:00
if($out -> getAttribute("parts")) {
2012-01-08 14:58:53 +01:00
$outputIndexes->removeChild($out);
2012-01-08 13:51:30 +01:00
$out = $output->createElement( "region" );
$outputIndexes->appendChild($out);
}
2012-01-08 13:05:05 +01:00
} else {
$out = $output->createElement( "region" );
$outputIndexes->appendChild($out);
}
2012-11-14 00:19:21 +01:00
attachRoadIndexItem($out, $indexName);
2012-01-08 13:05:05 +01:00
$out -> setAttribute("date", $date);
$out -> setAttribute("local", "true");
$out -> setAttribute("size", $size);
$out -> setAttribute("name", $indexName);
2012-11-13 23:23:53 +01:00
2012-11-13 23:11:29 +01:00
2012-01-08 13:05:05 +01:00
$out -> setAttribute("description", $description);
//$mapNodes[$indexName] = $out;
}
closedir($dh);
}
2012-01-08 13:05:05 +01:00
} else {
print($dir . " not a directory!\n");
}
2012-08-30 21:22:29 +02:00
if($local_file) {
// header('Content-Type: text/xml');
// echo $output->asXML();
}
2012-01-08 13:05:05 +01:00
$output->save($localFileName);
}
2012-08-30 21:22:29 +02:00
updateGoogleCodeIndexes(false);
2012-08-31 00:55:17 +02:00
?>