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

Source for file TeraWurflDatabase.php

Documentation is available at TeraWurflDatabase.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 TeraWurflDatabase
  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.  * Includes the necessary database connector as defined in TeraWurflConfig::$DB_CONNECTOR
  17.  */
  18. require_once realpath(dirname(__FILE__).'/TeraWurflDatabase_'.TeraWurflConfig::$DB_CONNECTOR.'.php');
  19. /**
  20.  * Abstract class to provide a skeleton for the Tera-WURFL database connectors.
  21.  * @abstract
  22.  * @package TeraWurflDatabase
  23.  *
  24.  */
  25. abstract class TeraWurflDatabase{
  26.     
  27.     // Properties
  28.     
  29.     /**
  30.      * Errors
  31.      * @var array 
  32.      */
  33.     public $errors;
  34.     /**
  35.      * Database connector implements the RIS search function directly
  36.      * @var bool 
  37.      */
  38.     public $db_implements_ris = false;
  39.     /**
  40.      * Database connector implements the LD search function directly
  41.      * @var bool 
  42.      */
  43.     public $db_implements_ld = false;
  44.     /**
  45.      * Database connector implements the building of a fallback tree directly
  46.      * @var bool 
  47.      */
  48.     public $db_implements_fallback = false;
  49.     /**
  50.      * Number of queries to database
  51.      * @var int 
  52.      */
  53.     public $numQueries = 0;
  54.     /**
  55.      * Full table name to use for the search functions
  56.      * @var string 
  57.      */
  58.     public $tablename;
  59.     /**
  60.      * True if connection to database is active
  61.      * @var bool 
  62.      */
  63.     public $connected = false;
  64.     /**
  65.      * Raw database connection
  66.      * @var database_object 
  67.      */
  68.     protected $dbcon;
  69.     /**
  70.      * Database table name extension for temporary tables
  71.      * @var string 
  72.      */
  73.     public static $DB_TEMP_EXT "_TEMP";
  74.     
  75.  
  76.     public function __construct(){
  77.         $this->errors = array();
  78.     }
  79.     
  80.     // Device Table Functions
  81.     
  82.     /**
  83.      * Returns the capabilities array from a given WURFL Device ID
  84.      * @param $wurflID WURFL ID
  85.      * @return array Device capabilities
  86.      */
  87.     abstract public function getDeviceFromID($wurflID);
  88.     /**
  89.      * Returns the WURFL ID for the Actual Device Root in the given device's fall back tree.  This can be null if it does not exist.
  90.      * @param $wurflID WURFL ID
  91.      * @return string WURFL ID
  92.      */
  93.     abstract public function getActualDeviceAncestor($wurflID);
  94.     /**
  95.      * Returns an associative array of all the data from the given table in the form [WURFL ID] => [User Agent]
  96.      * @param $tablename 
  97.      * @return array 
  98.      */
  99.     abstract public function getFullDeviceList($tablename);
  100.     /**
  101.      * Returns the WURFL ID from a raw User Agent if an exact match is found
  102.      * @param $userAgent 
  103.      * @return string WURFL ID
  104.      */
  105.     abstract public function getDeviceFromUA($userAgent);
  106.     /**
  107.      * Find the matching Device ID for a given User Agent using RIS (Reduction in String)
  108.      * @param string $ua User Agent
  109.      * @param int $tolerance How short the strings are allowed to get before a match is abandoned
  110.      * @param UserAgentMatcher $matcher The UserAgentMatcherInstance that is matching the User Agent
  111.      * @return string WURFL ID
  112.      */
  113.     public function getDeviceFromUA_RIS($userAgent,$tolerance,UserAgentMatcher &$matcher){}
  114.     /**
  115.      * Find the matching Device ID for a given User Agent using LD (Leveshtein Distance)
  116.      * @param string $ua User Agent
  117.      * @param int $tolerance Tolerance that is still considered a match
  118.      * @param UserAgentMatcher $matcher The UserAgentMatcherInstance that is matching the User Agent
  119.      * @return string WURFL ID
  120.      */
  121.     public function getDeviceFromUA_LD($userAgent,$tolerance,UserAgentMatcher &$matcher){}
  122.     /**
  123.      * Find the matching Device ID for a given User Agent using LD (Leveshtein Distance)
  124.      * @param string WURFL ID
  125.      * @return string WURFL ID
  126.      */
  127.     public function getDeviceFallBackTree($wurflID){}
  128.     /**
  129.      * Loads the pre-processed WURFL tables into the database
  130.      * @param string Device tables
  131.      * @return array Array of devices in fallback tree
  132.      */
  133.     abstract public function loadDevices(&$tables);
  134.     /**
  135.      * Creates a table capable of holding devices (WURFL ID, User Agent and Capabilities)
  136.      * @param $tablename Name of the table
  137.      * @return bool Success
  138.      */
  139.     abstract public function createGenericDeviceTable($tablename);
  140.     
  141.     // Cache Table Functions
  142.     
  143.     // should return (bool)false or the device array
  144.     /**
  145.      * Return capabilities array for the given User Agent, or null if not found
  146.      * @param $userAgent 
  147.      * @return array Capabilities
  148.      */
  149.     abstract public function getDeviceFromCache($userAgent);
  150.     /**
  151.      * Save the given User Agent and Device capabilities array to the database
  152.      * @param $userAgent User Agent
  153.      * @param $device Device capabilities array
  154.      * @return bool Success
  155.      */
  156.     abstract public function saveDeviceInCache($userAgent,$device);
  157.     /**
  158.      * Creates the cache table
  159.      * @return bool Success
  160.      */
  161.     abstract public function createCacheTable();
  162.     /**
  163.      * Rebuilds the cache table by redetecting the cached devices against the loaded WURFL
  164.      * @return bool Success
  165.      */
  166.     abstract public function rebuildCacheTable();
  167.     
  168.     // Supporting DB Functions
  169.     /**
  170.      * Creates the index table
  171.      * @return bool success
  172.      */
  173.     abstract public function createIndexTable();
  174.     /**
  175.      * Creates the settings table
  176.      * @return bool success
  177.      */
  178.     abstract public function createSettingsTable();
  179.     /**
  180.      * Truncate or drop+create the given table
  181.      * @param $tablename 
  182.      * @return bool Success
  183.      */
  184.     abstract public function clearTable($tablename);
  185.     /**
  186.      * Establishes a database connection and stores connection in $this->dbcon
  187.      * @return bool Success
  188.      */
  189.     abstract public function connect();
  190.     
  191.     // Settings functions
  192.     /**
  193.      * Adds/updates a key=>value pair in the settings table
  194.      * @param string setting name (key)
  195.      * @param string setting value
  196.      * @return void 
  197.      */
  198.     abstract public function updateSetting($key,$value);
  199.     /**
  200.      * Get setting from settings table by a given key
  201.      * @param string setting name (key)
  202.      * @return string value or NULL if not found
  203.      */
  204.     abstract public function getSetting($key)
  205.     
  206.     // drop+create supporting functions / procedures / views /etc...
  207.     /**
  208.      * Creates supporting stored procedures
  209.      * @return bool Success
  210.      */
  211.     public function createProcedures(){}
  212.     /**
  213.      * Prepares raw text for use in queries (adding quotes and escaping characters if necessary)
  214.      * @param $raw_text 
  215.      * @return string SQL-Safe text
  216.      */
  217.     abstract public function SQLPrep($raw_text);
  218.     /**
  219.      * Returns an array of all the tables in the database
  220.      * @return array 
  221.      */
  222.     abstract public function getTableList();
  223.     /**
  224.      * Returns an array of the User Agent Matcher tables in the database
  225.      * @return array 
  226.      */
  227.     abstract public function getMatcherTableList();
  228.     /**
  229.      * Returns an associative array of statistics from given table
  230.      * @param $table 
  231.      * @return array 
  232.      */
  233.     abstract public function getTableStats($table);
  234.     /**
  235.      * Returns and array of the cached User Agents
  236.      * @return array 
  237.      */
  238.     abstract public function getCachedUserAgents();
  239.     /**
  240.      * Creates and prepares the database
  241.      * @return void 
  242.      */
  243.     public function initializeDB(){
  244.         $this->createDeviceTable();
  245.         $this->createPatchTable();
  246.         $this->createCacheTable();
  247.         $this->createIndexTable();
  248.         $this->createSettingsTable();
  249.         $this->createProcedures();
  250.     }
  251.     /**
  252.      * Checks if the database configuration is correct and that all required permissions
  253.      * are properly configured
  254.      * @return array list of errors
  255.      */
  256.     public function verifyConfig(){
  257.         return array();
  258.     }
  259.     /**
  260.      * Returns the version string of the database server
  261.      * @return string 
  262.      */
  263.     abstract public function getServerVersion();
  264.     /**
  265.      * Returns the most recent error message
  266.      * @return string Error message
  267.      */
  268.     public function getLastError(){
  269.         return $this->errors[count($this->errors)-1];
  270.     }
  271.     
  272. }

Documentation generated on Sun, 19 Sep 2010 00:15:57 +0000 by phpDocumentor 1.4.3