Source for file TeraWurflLoader.php
Documentation is available at TeraWurflLoader.php
* Tera_WURFL - PHP MySQL driven WURFL
* Tera-WURFL was written by Steve Kamerman, and is based on the
* Java WURFL Evolution package by Luca Passani and WURFL PHP Tools by Andrea Trassati.
* This version uses a MySQL database to store the entire WURFL file, multiple patch
* files, and a persistent caching mechanism to provide extreme performance increases.
* @author Steve Kamerman <stevekamerman AT gmail.com>
* @version Stable 2.1.3 $Date: 2010/09/18 15:43:21
* @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
* Loads the WURFL file from a local file or remote URL into the Tera-WURFL database.
public static $WURFL_LOCAL = "local";
public static $WURFL_REMOTE = "remote";
public static $WURFL_REMOTE_CVS = "remote_cvs";
public static $WURFL_PATCH = "patch";
* @var int Performance tracking variable
$this->table = TeraWurflConfig::$TABLE_PREFIX;
$this->parser = TeraWurflXMLParser::getInstance();
* Loads the WURFL and patch files into the database
$this->wurfl->toLog("Loading WURFL",LOG_INFO);
$this->wurfl->toLog("The main WURFL file could not be opened: ". $this->file,LOG_ERROR);
$this->errors[]= "The main WURFL file could not be opened: ". $this->file;
// Parse XML data into $this->devices array
$this->wurfl->toLog("Loading Patches",LOG_INFO);
$this->wurfl->toLog("Validating WURFL Data",LOG_INFO);
$this->wurfl->toLog("Sorting WURFL Data",LOG_INFO);
if(!$this->sort()) return false;
$this->wurfl->toLog("Loading data into DB",LOG_INFO);
$this->wurfl->toLog("Rebuilding cache",LOG_INFO);
$this->wurfl->db->rebuildCacheTable();
$this->wurfl->db->createCacheTable();
$this->wurfl->db->updateSetting(TeraWurfl::$SETTING_PATCHES_LOADED,TeraWurflConfig::$PATCH_FILE);
$this->wurfl->db->updateSetting(TeraWurfl::$SETTING_WURFL_VERSION,$this->version);
$this->wurfl->db->updateSetting(TeraWurfl::$SETTING_LOADED_DATE,time());
* Validates the data from the WURFL file or Patch file
public function validate(){
$before_errors = count($this->errors);
foreach($this->devices as $id => &$device){
// Must have a valid wurfl ID
$this->wurfl->toLog("Skipping WURFL entry (invalid ID):\n". var_export($device,true),LOG_WARNING);
$this->errors[] = "Skipping WURFL entry (invalid ID):\n". var_export($device,true);
// Must have a valid User Agent unless it's "generic"
if(strlen($device['user_agent'])==0){
$this->wurfl->toLog("Skipping WURFL entry (invalid User Agent):\n". var_export($device,true),LOG_WARNING);
$this->errors[] = "Skipping WURFL entry (invalid User Agent):\n". var_export($device,true);
// Must have a valid fall_back
if(!$this->validID($device['fall_back'])){
$this->wurfl->toLog("Invalid Fallback '". $device['fall_back']. "':\n". var_export($device,true),LOG_WARNING);
$this->errors[] = "Invalid Fallback '". $device['fall_back']. "':\n". var_export($device,true);
return ($before_errors == count($this->errors));
* Sorts the validated data from $this->devices into their respective UserAgentMatcher tables ($this->tables)
* based on the UserAgentMatcher that matches the device's user agent
foreach($this->devices as $id => &$device){
// This will return something like "Nokia", "Motorola", or "CatchAll"
$matcher = UserAgentFactory::userAgentType($this->wurfl,$device['user_agent']);
$uatable = $this->table. '_'. $matcher;
if(!isset ($this->tables[$uatable]))$this->tables[$uatable]= array();
$this->tables[$uatable][$device['id']]= $device;
// Destroy the devices array
* Loads the WURFL devices into the database.
* @return Bool Completed without error
public function loadIntoDB(){
* Loads the patch files from TeraWurflConfig::PATCH_FILE
public function loadPatches(){
if(!TeraWurflConfig::$PATCH_ENABLE) return true;
// Explode the patchfile string into an array of patch files (normally just one file)
$patches = explode(';',TeraWurflConfig::$PATCH_FILE);
foreach($patches as $patch){
$patch_devices = array();
$this->wurfl->toLog("Loading patch: ". $patch,LOG_WARNING);
$patch_parser = TeraWurflXMLParser::getInstance();
$patch_parser->open(TeraWurfl::absoluteDataDir(). $patch, TeraWurflXMLParser::$TYPE_PATCH);
$patch_parser->process($patch_devices);
foreach($patch_devices as $id => &$device){
// Merge this device on top of the existing device
TeraWurfl::mergeCapabilities($this->devices[$id],$device);
// Add this new device to the table
public function getParserName(){
return get_class(TeraWurflXMLParser::getInstance());
* Get performance information
* @return int Duration in seconds
public function totalLoadTime(){
public function parseTime(){
public function patchTime(){
public function validateTime(){
public function sortTime(){
public function databaseTime(){
public function cacheRebuildTime(){
* Is WURFL Device ID Valid?
protected function validID($id){
if(strlen($id)==0) return false;
return array_key_exists($id,$this->devices);
|