TeraWurflAdmin
[ class tree: TeraWurflAdmin ] [ index: TeraWurflAdmin ] [ all elements ]

Source for file updatedb.php

Documentation is available at updatedb.php

  1. <?php
  2. /**
  3.  * Tera_WURFL - PHP MySQL driven WURFL
  4.  * 
  5.  * Tera-WURFL was written by Steve Kamerman, and is based on the
  6.  * Java WURFL Evolution package by Luca Passani and WURFL PHP Tools by Andrea Trassati.
  7.  * This version uses a MySQL database to store the entire WURFL file, multiple patch
  8.  * files, and a persistent caching mechanism to provide extreme performance increases.
  9.  * 
  10.  * @package TeraWurflAdmin
  11.  * @author Steve Kamerman <stevekamerman AT gmail.com>
  12.  * @version Stable 2.1.3 $Date: 2010/09/18 15:43:21
  13.  * @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
  14.  */
  15. /**
  16.  * Include required files
  17.  */
  18. require_once realpath(dirname(__FILE__).'/../TeraWurfl.php');
  19. require_once realpath(dirname(__FILE__).'/../TeraWurflLoader.php');
  20. require_once realpath(dirname(__FILE__).'/../TeraWurflXMLParsers/TeraWurflXMLParser.php');
  21. require_once realpath(dirname(__FILE__).'/../TeraWurflXMLParsers/TeraWurflXMLParser_XMLReader.php');
  22. require_once realpath(dirname(__FILE__).'/../TeraWurflXMLParsers/TeraWurflXMLParser_SimpleXML.php');
  23.  
  24. @ini_set("display_errors","on");
  25. if(TeraWurflConfig::$OVERRIDE_MEMORY_LIMIT){
  26.     ini_set("memory_limit",TeraWurflConfig::$MEMORY_LIMIT);
  27. }
  28. /**
  29.  * Set the script time limit (default: 20 minutes)
  30.  */
  31. set_time_limit(60*20);
  32.  
  33. $source (isset($_GET['source']))$_GET['source']"local";
  34.  
  35. $base new TeraWurfl();
  36. if($base->db->connected !== true){
  37.     throw new Exception("Cannot connect to database: ".$base->db->errors[0]);
  38. }
  39.  
  40. if(isset($_GET['action']&& $_GET['action']=='rebuildCache'){
  41.     $base->db->rebuildCacheTable();
  42.     header("Location: index.php?msg=".urlencode("The cache has been successfully rebuilt ({$base->db->numQueries} queries).")."&severity=notice");
  43.     exit(0);
  44. }
  45. if(isset($_GET['action']&& $_GET['action']=='clearCache'){
  46.     $base->db->createCacheTable();
  47.     header("Location: index.php?msg=".urlencode("The cache has been successfully cleared ({$base->db->numQueries} queries).")."&severity=notice");
  48.     exit(0);
  49. }
  50.  
  51. $newfile TeraWurfl::absoluteDataDir().TeraWurflConfig::$WURFL_FILE.".zip";
  52. $wurflfile TeraWurfl::absoluteDataDir().TeraWurflConfig::$WURFL_FILE;
  53.  
  54. if($source == "remote" || $source == "remote_cvs"){
  55.     if($source == "remote"){
  56.         $dl_url TeraWurflConfig::$WURFL_DL_URL
  57.     }elseif($source == "remote_cvs"){
  58.         $dl_url TeraWurflConfig::$WURFL_CVS_URL;
  59.     }
  60.     echo "Downloading WURFL from $dl_url ...\n<br/>";
  61.     flush();
  62.     if(!file_exists($newfile&& !is_writable($base->rootdir.TeraWurflConfig::$DATADIR)){
  63.         $base->toLog("Cannot write to data directory (permission denied)",LOG_ERR);
  64.         Throw New Exception("Fatal Error: Cannot write to data directory (permission denied). (".$base->rootdir.TeraWurflConfig::$DATADIR.")<br/><br/><strong>Please make the data directory writable by the user or group that runs the webserver process, in Linux this command would do the trick if you're not too concerned about security: <pre>chmod -R 777 ".$base->rootdir.TeraWurflConfig::$DATADIR."</pre></strong>");
  65.         exit(1);
  66.     }
  67.     if(file_exists($newfile&& !is_writable($newfile)){
  68.         $base->toLog("Cannot overwrite WURFL file (permission denied)",LOG_ERR);
  69.         Throw New Exception("Fatal Error: Cannot overwrite WURFL file (permission denied). (".$base->rootdir.TeraWurflConfig::$DATADIR.")<br/><br/><strong>Please make the data directory writable by the user or group that runs the webserver process, in Linux this command would do the trick if you're not too concerned about security: <pre>chmod -R 777 ".$base->rootdir.TeraWurflConfig::$DATADIR."</pre></strong>");
  70.         exit(1);
  71.     }
  72.     // Download the new WURFL file and save it in the DATADIR as wurfl.zip
  73.     @ini_set('user_agent'"PHP/Tera-WURFL_$version");
  74.     $download_start microtime(true);
  75.     if(!$gzdata file_get_contents($dl_url)){
  76.         Throw New Exception("Error: Unable to download WURFL file from ".TeraWurflConfig::$WURFL_DL_URL);
  77.         exit(1);
  78.     }
  79. /*    $destination=fopen($newfile,"w"); 
  80.     $source=fopen($dl_url,"r"); 
  81.     while ($block=fread($source,256*1024)) fwrite($destination,$block);
  82.     fclose($source);
  83.     fclose($destination);
  84. */
  85.     $download_time microtime(true$download_start;
  86.     file_put_contents($newfile,$gzdata);
  87.     $gzsize WurflSupport::formatBytes(filesize($newfile));
  88.     // Try to use ZipArchive, included from 5.2.0
  89.     if(class_exists("ZipArchive")){
  90.         $zip new ZipArchive();
  91.         if ($zip->open(str_replace('\\','/',$newfile)) === TRUE{
  92.             $zip->extractTo(str_replace('\\','/',dirname($wurflfile)),array('wurfl.xml'));
  93.             $zip->close();
  94.         else {
  95.             Throw New Exception("Error: Unable to extract wurfl.xml from downloaded archive: $newfile");
  96.             exit(1);
  97.         }
  98.     }else{
  99.         system("gunzip $newfile");
  100.     }
  101.     $size WurflSupport::formatBytes(filesize($wurflfile))." [$gzsize compressed]";
  102.     $download_rate WurflSupport::formatBitrate(filesize($newfile)$download_time);
  103.     $ok true;
  104.     echo "done ($wurflfile$size)<br />Downloaded in $download_time sec @ $download_rate <br/><br/>";
  105.     usleep(50000);
  106.     flush();
  107. }
  108.  
  109. $loader new TeraWurflLoader($base);
  110. //$ok = $base->db->initializeDB();
  111. $ok $loader->load();
  112. if($ok){
  113.     echo "<strong>Database Update OK</strong><hr />";
  114.     echo "Total Time: ".$loader->totalLoadTime()."<br/>";
  115.     echo "Parse Time: ".$loader->parseTime()." (".$loader->getParserName().")<br/>";
  116.     echo "Validate Time: ".$loader->validateTime()."<br/>";
  117.     echo "Sort Time: ".$loader->sortTime()."<br/>";
  118.     echo "Patch Time: ".$loader->patchTime()."<br/>";
  119.     echo "Database Time: ".$loader->databaseTime()."<br/>";
  120.     echo "Cache Rebuild Time: ".$loader->cacheRebuildTime()."<br/>";
  121.     echo "Number of Queries: ".$base->db->numQueries."<br/>";
  122.     if(version_compare(PHP_VERSION,'5.2.0'=== 1){
  123.         echo "PHP Memory Usage: ".WurflSupport::formatBytes(memory_get_usage())."<br/>";
  124.     }
  125.     echo "--------------------------------<br/>";
  126.     echo "WURFL Version: ".$loader->version." (".$loader->last_updated.")<br />";
  127.     echo "WURFL Devices: ".$loader->mainDevices."<br/>";
  128.     echo "PATCH New Devices: ".$loader->patchAddedDevices."<br/>";
  129.     echo "PATCH Merged Devices: ".$loader->patchMergedDevices."<br/>";
  130. }else{
  131.     echo "ERROR LOADING DATA!<br/>";
  132.     echo "Errors: <br/>\n";
  133.     echo "<pre>".htmlspecialchars(var_export($loader->errors,true))."</pre>";
  134. }
  135.  
  136. echo "<hr/><a href=\"index.php\">Return to administration tool.</a>";

Documentation generated on Sun, 19 Sep 2010 00:16:07 +0000 by phpDocumentor 1.4.3