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

Source for file TeraWurflXMLParser_XMLReader_LowMem.php

Documentation is available at TeraWurflXMLParser_XMLReader_LowMem.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 TeraWurflXMLParser
  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.  * Loads the wurfl.xml file using the stream-based XMLReader class.
  17.  * This version is a HACK, made only for the purpose of extremely low
  18.  * memory usage, unlike the other XMLParsers, which are designed for
  19.  * speed of updating the database and sanity checking.
  20.  * @package TeraWurflXMLParser
  21.  */
  22.     
  23.     private $dbcon;
  24.     private $pendingInsert array();
  25.     
  26.     public function __construct(){
  27.         if(class_exists('XMLReader')){
  28.             $this->parser_type = self::$PARSER_XMLREADER;
  29.         }else{
  30.             throw new Exception("Cannot load XMLReader");
  31.         }
  32.         $this->dbcon new mysqli(TeraWurflConfig::$DB_HOST,TeraWurflConfig::$DB_USER,TeraWurflConfig::$DB_PASS,TeraWurflConfig::$DB_SCHEMA);
  33.         $this->xml = new XMLReader();
  34.     }
  35.     
  36.     public function open($filename,$file_type){
  37.         $this->file_type = $file_type;
  38.         $this->xml->open($filename);
  39.         //TODO: add error handling
  40.     }
  41.     public function process(Array &$destination){
  42.         while($this->xml->read()){
  43.             switch ($this->xml->nodeType){
  44.                 case XMLReader::ELEMENT:
  45.                     if($this->xml->name == "device"){
  46.                         $this->parseDevice();
  47.                         continue;
  48.                     }
  49.                     if($this->file_type == self::$TYPE_WURFL){
  50.                         if($this->xml->name == "ver" || $this->xml->name == "last_updated"){
  51.                             if($this->xml->name == "ver"$this->wurflVersion = $this->getValue();
  52.                             if($this->xml->name == "last_updated"$this->wurflLastUpdated = $this->getValue();
  53.                         }
  54.                     }
  55.                     break;
  56.                 case XMLReader::END_ELEMENT:
  57.                 default:
  58.                     break;
  59.             
  60.         }
  61.     }
  62.     protected function getValue(){
  63.         $this->xml->read();
  64.         return $this->xml->value;
  65.     }
  66.     protected function parseDevice(){
  67.         if(count($this->pendingInsert)>0){
  68.             foreach($this->pendingInsert as $device){
  69.                 $matcher UserAgentFactory::userAgentType($this->wurfl,$device['user_agent']);
  70.                 $uatable TeraWurflConfig::$TABLE_PREFIX.'_'.$matcher;
  71.                 
  72.             }
  73.             $this->pendingInsert array();
  74.         }
  75.         $this->pendingInsert[$this->xml->getAttribute('id')array();
  76.         $device =$this->pendingInsert[$this->xml->getAttribute('id')];
  77.         $device=array(
  78.             'id' => $this->xml->getAttribute('id'),
  79.             'user_agent' => UserAgentUtils::cleanUserAgent($this->xml->getAttribute('user_agent')),
  80.             'fall_back' => $this->xml->getAttribute('fall_back'),
  81.         );
  82.         if($this->xml->getAttribute('actual_device_root')) $device['actual_device_root'($this->xml->getAttribute('actual_device_root')=="true")?1:0;
  83.         $groupdevice '';
  84.         $groupname '';
  85.         $filtering (TeraWurflConfig::$CAPABILITY_FILTER)true:false;
  86.         $includegroup false;
  87.         while($this->xml->read()){
  88.             if($this->xml->nodeType != XMLReader::ELEMENTcontinue;
  89.             // recurse back into this function for the rest of the devices
  90.             switch($this->xml->name){
  91.                 case "device":
  92.                     $this->parseDevice();
  93.                     break;
  94.                 case "group":
  95.                     $groupname $this->xml->getAttribute('id');
  96.                     if($filtering && $this->enabled($this->xml->getAttribute('id'))){
  97.                         $includegroup true;
  98.                     }else{
  99.                         $includegroup false;
  100.                         continue;
  101.                     }
  102.                     $device[$groupnamearray();
  103.                     break;
  104.                 case "capability":
  105.                     if(!$filtering || ($filtering && $includegroup)){
  106.                         // the groupdevice array must already exist
  107.                         $device[$groupname][$this->xml->getAttribute('name')self::cleanValue($this->xml->getAttribute('value'));
  108.                         continue;
  109.                     }
  110.                     if($filtering && !$includegroup && $this->enabled($this->xml->getAttribute('name'))){
  111.                         // the groupdevice array might already exists
  112.                         if(!array_key_exists($groupname,$device)) $device[$groupnamearray();
  113.                         $device[$groupname][$this->xml->getAttribute('name')self::cleanValue($this->xml->getAttribute('value'));
  114.                         continue;
  115.                     }
  116.                     break;
  117.             }
  118.         }
  119.     }
  120. }

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