/config/site/update_indexes.php

https://github.com/mikeschneider/Osmand · PHP · 139 lines · 115 code · 15 blank · 9 comment · 26 complexity · 8146dd8121a520709868510ceea0653e MD5 · raw file

  1. <?php
  2. function updateGoogleCodeIndexes($update=false) {
  3. $localFileName='indexes.xml';
  4. // check each 30 minutes
  5. if(!$update && file_exists($localFileName) && time() - filemtime($localFileName) < 60 * 30) {
  6. return;
  7. }
  8. $dom = new DomDocument();
  9. $output = new DOMDocument();
  10. $output->formatOutput = true;
  11. $outputIndexes = $output->createElement( "osmand_regions" );
  12. $outputIndexes->setAttribute('mapversion','1');
  13. $output->appendChild( $outputIndexes );
  14. $st = 0;
  15. $num = 200;
  16. $count = 0;
  17. $mapNodes = array();
  18. /// 1. dlownload indexes from googlecode
  19. while($st != -1){
  20. $dom->loadHTMLFile("http://code.google.com/p/osmand/downloads/list?num=".$num."&start=".$st."&colspec=Filename+Summary+Uploaded+Size");
  21. $count ++;
  22. $xpath = new DOMXpath($dom);
  23. $xpathI = new DOMXpath($dom);
  24. $res = $xpath->query('//td[contains(@class,"col_0")]');
  25. if($res && $res->length > 0) {
  26. foreach($res as $node) {
  27. $indexName = trim($node->nodeValue);
  28. $s = $xpathI->query('td[contains(@class,"col_1")]/a[1]', $node->parentNode);
  29. if(!$s || $s->length == 0) {
  30. continue;
  31. }
  32. $description = $s->item(0)->nodeValue;
  33. $i = strpos($description,"{");
  34. if(!$i) {
  35. continue;
  36. }
  37. $i1 = strpos($description,":", $i);
  38. $i2 = stripos($description,"mb", $i1);
  39. if(!$i2) {
  40. $i2 = strpos($description,"}", $i1);
  41. }
  42. $date = trim(substr($description, $i + 1, $i1 - $i -1));
  43. $size = trim(substr($description, $i1 + 1, $i2 - $i1 -1));
  44. $description = trim(substr($description, 0, $i));
  45. if(strpos($indexName,"voice.zip") || strpos($indexName,"_1.poi.zip") ||
  46. strpos($indexName,"_1.poi.odb") || strpos($indexName,"_1.obf")) {
  47. $ipart = strpos($indexName,"zip-");
  48. $part = 1;
  49. $base = $indexName;
  50. if($ipart) {
  51. $part = (int)substr($indexName, $ipart+4);
  52. $base = substr($indexName, 0, $ipart+3);
  53. if(isset($mapNodes[$base])) {
  54. $out = $mapNodes[$base];
  55. } else {
  56. $out = $output->createElement( "multiregion" );
  57. $out -> setAttribute("parts", $part);
  58. $mapNodes[$base] = $out;
  59. $out -> setAttribute("date", $date);
  60. $out -> setAttribute("size", $size);
  61. $out -> setAttribute("name", $base);
  62. $out -> setAttribute("description", $description);
  63. $outputIndexes->appendChild($out);
  64. }
  65. if( (int) $out -> getAttribute("parts") < $part){
  66. $out -> setAttribute("parts", $part);
  67. }
  68. } else {
  69. $out = $output->createElement( "region" );
  70. $out -> setAttribute("date", $date);
  71. $out -> setAttribute("size", $size);
  72. $out -> setAttribute("name", $indexName);
  73. $out -> setAttribute("description", $description);
  74. $outputIndexes->appendChild($out);
  75. $mapNodes[$indexName] = $out;
  76. }
  77. }
  78. }
  79. $st += $num;
  80. } else {
  81. $st = -1;
  82. }
  83. }
  84. /// 2. append local indexes
  85. $local = new DomDocument();
  86. // Open a known directory, and proceed to read its contents
  87. $dir='indexes/';
  88. if (is_dir($dir)) {
  89. if ($dh = opendir($dir)) {
  90. $zip = new ZipArchive();
  91. while (($file = readdir($dh)) !== false) {
  92. $filename = $dir . $file ; //"./test112.zip";
  93. //print("processing file:" . $filename . "\n");
  94. if ($zip->open($filename,ZIPARCHIVE::CHECKCONS)!==TRUE) {
  95. //echo exit("cannot open <$filename>\n");
  96. //print($filename . " cannot open as zip\n");
  97. continue;
  98. }
  99. $indexName=$file;
  100. if (isset($mapNodes[$indexName])) {
  101. //print($indexName . " is listed already, skipping\n");
  102. continue;
  103. }
  104. $description = $zip->getCommentIndex(0);
  105. $stat = $zip->statIndex( 0 );
  106. $date= date('d.m.Y',$stat['mtime']);
  107. $size= number_format((filesize($filename) / (1024.0*1024.0)), 1, '.', '');
  108. $zip->close();
  109. $out = $output->createElement( "region" );
  110. $out -> setAttribute("date", $date);
  111. $out -> setAttribute("size", $size);
  112. $out -> setAttribute("name", $indexName);
  113. $out -> setAttribute("description", $description);
  114. $outputIndexes->appendChild($out);
  115. //$mapNodes[$indexName] = $out;
  116. }
  117. closedir($dh);
  118. }
  119. } else {
  120. print($dir . " not a directory!\n");
  121. }
  122. $output->save($localFileName);
  123. }
  124. ?>