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

Source for file mdb.php

Documentation is available at mdb.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Container_mdb class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Internationalization
  31.  * @package   Translation2
  32.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  33.  * @copyright 2004-2008 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: mdb.php,v 1.30 2008/05/03 09:17:59 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * require Translation2_Container class
  41.  */
  42. require_once 'Translation2/Container.php';
  43.  
  44. /**
  45.  * Storage driver for fetching data from a database
  46.  *
  47.  * This storage driver can use all databases which are supported
  48.  * by the PEAR::MDB abstraction layer to fetch data.
  49.  *
  50.  * @category  Internationalization
  51.  * @package   Translation2
  52.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  53.  * @copyright 2004-2008 Lorenzo Alberton
  54.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  55.  * @version   CVS: $Id: mdb.php,v 1.30 2008/05/03 09:17:59 quipo Exp $
  56.  * @link      http://pear.php.net/package/Translation2
  57.  */
  58. {
  59.  
  60.     // {{{ class vars
  61.  
  62.     /**
  63.      * MDB object
  64.      * @var object 
  65.      */
  66.     var $db = null;
  67.  
  68.     /**
  69.      * query counter
  70.      * @var integer 
  71.      * @access private
  72.      */
  73.     var $_queries = 0;
  74.  
  75.     // }}}
  76.     // {{{ init
  77.  
  78.     /**
  79.      * Initialize the container
  80.      *
  81.      * @param string &$db Connection data or MDB object
  82.      *
  83.      * @return boolean|PEAR_Errorobject if something went wrong
  84.      */
  85.     function init(&$db)
  86.     {
  87.         $this->_setDefaultOptions();
  88.         if (PEAR::isError($err $this->_connect($db))) {
  89.             return $err;
  90.         }
  91.         return true;
  92.     }
  93.  
  94.     // }}}
  95.     // {{{ _connect()
  96.  
  97.     /**
  98.      * Connect to database by using the given DSN string
  99.      *
  100.      * @param mixed &$db DSN string | array | mdb object
  101.      *
  102.      * @return boolean|PEAR_Erroron error
  103.      * @access private
  104.      */
  105.     function _connect(&$db)
  106.     {
  107.         if (is_object($db&& is_a($db'MDB_Common')) {
  108.             $this->db = &$db;
  109.         elseif (is_string($db|| is_array($db)) {
  110.             include_once 'MDB.php';
  111.             $this->db =MDB::connect($db);
  112.         elseif (is_object($db&& MDB::isError($db)) {
  113.             return PEAR::raiseError($db->getMessage()$db->code);
  114.         else {
  115.             return PEAR::raiseError('The given dsn was not valid in file '
  116.                                     . __FILE__ . ' at line ' . __LINE__,
  117.                                     TRANSLATION2_ERROR_CANNOT_CONNECT,
  118.                                     PEAR_ERROR_RETURN);
  119.         }
  120.  
  121.         if (PEAR::isError($this->db)) {
  122.             return $this->db;
  123.         }
  124.         return true;
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ _setDefaultOptions()
  129.  
  130.     /**
  131.      * Set some default options
  132.      *
  133.      * @access private
  134.      * @return void 
  135.      */
  136.     function _setDefaultOptions()
  137.     {
  138.         $this->options['langs_avail_table''langs';
  139.         $this->options['lang_id_col']       'id';
  140.         $this->options['lang_name_col']     'name';
  141.         $this->options['lang_meta_col']     'meta';
  142.         $this->options['lang_errmsg_col']   'error_text';
  143.         $this->options['lang_encoding_col''encoding';
  144.  
  145.         $this->options['strings_default_table''i18n';
  146.         $this->options['strings_tables']        = array()// 'lang_id' => 'table_name'
  147.         $this->options['string_id_col']         'id';
  148.         $this->options['string_page_id_col']    'page_id';
  149.         $this->options['string_page_id_col_length'= 50;
  150.         $this->options['string_text_col']       '%s'// col_name if one table per lang is used,
  151.                                                         // or a pattern (i.e. "tr_%s" => "tr_EN_US")
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ fetchLangs()
  156.  
  157.     /**
  158.      * Fetch the available langs if they're not cached yet.
  159.      *
  160.      * @return PEAR_Error on error
  161.      */
  162.     function fetchLangs()
  163.     {
  164.         $query sprintf('SELECT %s AS id, %s AS name, %s AS meta, %s AS error_text, %s AS encoding FROM %s',
  165.             $this->db->quoteIdentifier($this->options['lang_id_col']),
  166.             $this->db->quoteIdentifier($this->options['lang_name_col']),
  167.             $this->db->quoteIdentifier($this->options['lang_meta_col']),
  168.             $this->db->quoteIdentifier($this->options['lang_errmsg_col']),
  169.             $this->db->quoteIdentifier($this->options['lang_encoding_col']),
  170.             $this->db->quoteIdentifier($this->options['langs_avail_table'])
  171.         );
  172.  
  173.         ++$this->_queries;
  174.         $res $this->db->getAll($querynullarray()nullMDB_FETCHMODE_ASSOC);
  175.         if (PEAR::isError($res)) {
  176.             return $res;
  177.         }
  178.         foreach ($res as $row{
  179.             $row array_change_key_case($rowCASE_LOWER);
  180.             $this->langs[$row['id']] $row;
  181.         }
  182.     }
  183.  
  184.     // }}}
  185.     // {{{ getPage()
  186.  
  187.     /**
  188.      * Returns an array of the strings in the selected page
  189.      *
  190.      * @param string $pageID page/group ID
  191.      * @param string $langID language ID
  192.      *
  193.      * @return array|PEAR_Erroron error
  194.      */
  195.     function getPage($pageID = null$langID = null)
  196.     {
  197.         $langID $this->_getLangID($langID);
  198.         if (PEAR::isError($langID)) {
  199.             return $langID;
  200.         }
  201.         $lang_col $this->_getLangCol($langID);
  202.         $table    $this->_getLangTable($langID);
  203.  
  204.         $query sprintf('SELECT %s, %s FROM %s WHERE %s ',
  205.              $this->db->quoteIdentifier($this->options['string_id_col']),
  206.              $this->db->quoteIdentifier($lang_col),
  207.              $this->db->quoteIdentifier($table),
  208.              $this->db->quoteIdentifier($this->options['string_page_id_col'])
  209.         );
  210.  
  211.         if (is_null($pageID)) {
  212.             $query .= 'IS NULL';
  213.         else {
  214.             $query .= ' = ' $this->db->getTextValue($pageID);
  215.         }
  216.  
  217.         ++$this->_queries;
  218.         $res $this->db->getAssoc($query);
  219.         return $res;
  220.     }
  221.  
  222.     // }}}
  223.     // {{{ getOne()
  224.  
  225.     /**
  226.      * Get a single item from the container
  227.      *
  228.      * @param string $stringID string ID
  229.      * @param string $pageID   page/group ID
  230.      * @param string $langID   language ID
  231.      *
  232.      * @return string 
  233.      */
  234.     function getOne($stringID$pageID = null$langID = null)
  235.     {
  236.         $langID $this->_getLangID($langID);
  237.         if (PEAR::isError($langID)) {
  238.             return $langID;
  239.         }
  240.         $lang_col $this->_getLangCol($langID);
  241.         $table    $this->_getLangTable($langID);
  242.  
  243.         $query sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
  244.              $this->db->quoteIdentifier($lang_col),
  245.              $this->db->quoteIdentifier($table),
  246.              $this->db->quoteIdentifier($this->options['string_id_col']),
  247.              $this->db->getTextValue($stringID),
  248.              $this->db->quoteIdentifier($this->options['string_page_id_col'])
  249.         );
  250.  
  251.         if (is_null($pageID)) {
  252.             $query .= ' IS NULL';
  253.         else {
  254.             $query .= ' = ' $this->db->getTextValue($pageID);
  255.         }
  256.  
  257.         ++$this->_queries;
  258.         return $this->db->getOne($query);
  259.     }
  260.  
  261.     // }}}
  262.     // {{{ getStringID()
  263.  
  264.     /**
  265.      * Get the stringID for the given string
  266.      *
  267.      * @param string $string string
  268.      * @param string $pageID page/group ID
  269.      *
  270.      * @return string 
  271.      */
  272.     function getStringID($string$pageID = null)
  273.     {
  274.         $lang_col $this->_getLangCol($this->currentLang['id']);
  275.         $table $this->_getLangTable($this->currentLang['id']);
  276.         $query sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
  277.              $this->db->quoteIdentifier($this->options['string_id_col']),
  278.              $this->db->quoteIdentifier($table),
  279.              $this->db->quoteIdentifier($lang_col),
  280.              $this->db->getTextValue($string),
  281.              $this->db->quoteIdentifier($this->options['string_page_id_col'])
  282.         );
  283.         if (is_null($pageID)) {
  284.             $query .= ' IS NULL';
  285.         else {
  286.             $query .= ' = ' $this->db->getTextValue($pageID);
  287.         }
  288.         ++$this->_queries;
  289.         return $this->db->getOne($query);
  290.     }
  291.  
  292.     // }}}
  293.     // {{{ _getLangTable()
  294.  
  295.     /**
  296.      * Get the table a language is stored in
  297.      *
  298.      * @param string $langID language ID
  299.      *
  300.      * @return string table $langID is stored in
  301.      * @access private
  302.      */
  303.     function _getLangTable($langID)
  304.     {
  305.         if (isset($this->options['strings_tables'][$langID])) {
  306.             return $this->options['strings_tables'][$langID];
  307.         }
  308.         return str_replace('%s'$langID$this->options['strings_default_table']);
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ _getLangCol()
  313.  
  314.     /**
  315.      * Get the column a language's string is stored in
  316.      *
  317.      * @param string $langID language ID
  318.      *
  319.      * @return string column $langID is stored in
  320.      * @access private
  321.      */
  322.     function _getLangCol($langID)
  323.     {
  324.         static $cols;
  325.         if (!isset($cols[$langID])) {
  326.             if (isset($this->options['string_text_col']&&
  327.                 !empty($this->options['string_text_col'])) {
  328.                 $cols[$langIDstr_replace('%s'$langID$this->options['string_text_col']);
  329.             else {
  330.                 $cols[$langID$langID;
  331.             }
  332.         }
  333.         return $cols[$langID];
  334.     }
  335.  
  336.     // }}}
  337. }
  338. ?>

Documentation generated on Tue, 06 May 2008 06:00:32 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.