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

Source for file mdb2.php

Documentation is available at mdb2.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Container_mdb2 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: mdb2.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::MDB2 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: mdb2.php,v 1.30 2008/05/03 09:17:59 quipo Exp $
  56.  * @link      http://pear.php.net/package/Translation2
  57.  */
  58. {
  59.     // {{{ class vars
  60.  
  61.     /**
  62.      * MDB2 object
  63.      * @var object 
  64.      */
  65.     var $db = null;
  66.  
  67.     /**
  68.      * query counter
  69.      * @var integer 
  70.      * @access private
  71.      */
  72.     var $_queries = 0;
  73.  
  74.     // }}}
  75.     // {{{ init
  76.  
  77.     /**
  78.      * Initialize the container
  79.      *
  80.      * @param string &$db Connection data or MDB2 object
  81.      *
  82.      * @return boolean|PEAR_Errorobject if something went wrong
  83.      */
  84.     function init(&$db)
  85.     {
  86.         $this->_setDefaultOptions();
  87.         if (PEAR::isError($err $this->_connect($db))) {
  88.             return $err;
  89.         }
  90.         return true;
  91.     }
  92.  
  93.     // }}}
  94.     // {{{ _connect()
  95.  
  96.     /**
  97.      * Connect to database by using the given DSN string
  98.      *
  99.      * @param mixed &$db DSN string | array | MDB2 object
  100.      *
  101.      * @return boolean|PEAR_Erroron error
  102.      * @access private
  103.      */
  104.     function _connect(&$db)
  105.     {
  106.         if (is_object($db&& is_a($db'MDB2_Driver_Common')) {
  107.             $this->db = &$db;
  108.         elseif (is_string($db|| is_array($db)) {
  109.             include_once 'MDB2.php';
  110.             $this->db =MDB2::connect($db);
  111.         elseif (is_object($db&& MDB2::isError($db)) {
  112.             return PEAR::raiseError($db->getMessage()$db->code);
  113.         else {
  114.             return PEAR::raiseError('The given dsn was not valid in file '
  115.                                     . __FILE__ . ' at line ' . __LINE__,
  116.                                     TRANSLATION2_ERROR_CANNOT_CONNECT,
  117.                                     PEAR_ERROR_RETURN);
  118.         }
  119.  
  120.         if (PEAR::isError($this->db)) {
  121.             return $this->db;
  122.         }
  123.         return true;
  124.     }
  125.  
  126.     // }}}
  127.     // {{{ _setDefaultOptions()
  128.  
  129.     /**
  130.      * Set some default options
  131.      *
  132.      * @return void 
  133.      * @access private
  134.      */
  135.     function _setDefaultOptions()
  136.     {
  137.         $this->options['langs_avail_table''langs';
  138.         $this->options['lang_id_col']       'id';
  139.         $this->options['lang_name_col']     'name';
  140.         $this->options['lang_meta_col']     'meta';
  141.         $this->options['lang_errmsg_col']   'error_text';
  142.         $this->options['lang_encoding_col''encoding';
  143.  
  144.         $this->options['strings_default_table''i18n';
  145.         $this->options['strings_tables']        = array()// 'lang_id' => 'table_name'
  146.         $this->options['string_id_col']         'id';
  147.         $this->options['string_page_id_col']    'page_id';
  148.         $this->options['string_page_id_col_length'= 50;
  149.         $this->options['string_text_col']       '%s'// col_name if one table per lang is used,
  150.                                                         // or a pattern (i.e. "tr_%s" => "tr_EN_US")
  151.     }
  152.  
  153.     // }}}
  154.     // {{{ setCharset()
  155.  
  156.     /**
  157.      * Set charset used to read/store the translations
  158.      *
  159.      * @param string $charset character set (encoding)
  160.      *
  161.      * @return PEAR_Error on error
  162.      */
  163.     function setCharset($charset)
  164.     {
  165.         return $this->db->setCharset($charset);
  166.     }
  167.  
  168.     // }}}
  169.     // {{{ fetchLangs()
  170.  
  171.     /**
  172.      * Fetch the available langs if they're not cached yet.
  173.      *
  174.      * @return PEAR_Error on error
  175.      */
  176.     function fetchLangs()
  177.     {
  178.         $query sprintf('SELECT %s AS id, %s AS name, %s AS meta, %s AS error_text, %s AS encoding FROM %s',
  179.             $this->db->quoteIdentifier($this->options['lang_id_col']true),
  180.             $this->db->quoteIdentifier($this->options['lang_name_col']true),
  181.             $this->db->quoteIdentifier($this->options['lang_meta_col']true),
  182.             $this->db->quoteIdentifier($this->options['lang_errmsg_col']true),
  183.             $this->db->quoteIdentifier($this->options['lang_encoding_col']true),
  184.             $this->db->quoteIdentifier($this->options['langs_avail_table']true)
  185.         );
  186.  
  187.         ++$this->_queries;
  188.         $res $this->db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  189.         if (PEAR::isError($res)) {
  190.             return $res;
  191.         }
  192.         foreach ($res as $row{
  193.             $row array_change_key_case($rowCASE_LOWER);
  194.             $this->langs[$row['id']] $row;
  195.         }
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ getPage()
  200.  
  201.     /**
  202.      * Returns an array of the strings in the selected page
  203.      *
  204.      * @param string $pageID page/group ID
  205.      * @param string $langID language ID
  206.      *
  207.      * @return array 
  208.      */
  209.     function getPage($pageID = null$langID = null)
  210.     {
  211.         $langID   $this->_getLangID($langID);
  212.         if (PEAR::isError($langID)) {
  213.             return $langID;
  214.         }
  215.         $lang_col $this->_getLangCol($langID);
  216.         $table    $this->_getLangTable($langID);
  217.         
  218.         $query sprintf('SELECT %s, %s FROM %s WHERE %s ',
  219.              $this->db->quoteIdentifier($this->options['string_id_col']true),
  220.              $this->db->quoteIdentifier($lang_coltrue),
  221.              $this->db->quoteIdentifier($tabletrue),
  222.              $this->db->quoteIdentifier($this->options['string_page_id_col']true)
  223.         );
  224.  
  225.         if (is_null($pageID)) {
  226.             $query .= 'IS NULL';
  227.         else {
  228.             $query .= ' = ' $this->db->quote($pageID'text');
  229.         }
  230.  
  231.         ++$this->_queries;
  232.         $res $this->db->query($query);
  233.         if (PEAR::isError($res)) {
  234.             return $res;
  235.         }
  236.  
  237.         $strings = array();
  238.         while (list($key$value$res->fetchRow(MDB2_FETCHMODE_ORDERED)) {
  239.             $strings[$key$value;
  240.         }
  241.         $res->free();
  242.         return $strings;
  243.     }
  244.  
  245.     // }}}
  246.     // {{{ getOne()
  247.  
  248.     /**
  249.      * Get a single item from the container
  250.      *
  251.      * @param string $stringID string ID
  252.      * @param string $pageID   page/group ID
  253.      * @param string $langID   language ID
  254.      *
  255.      * @return string 
  256.      */
  257.     function getOne($stringID$pageID = null$langID = null)
  258.     {
  259.         $langID   $this->_getLangID($langID);
  260.         if (PEAR::isError($langID)) {
  261.             return $langID;
  262.         }
  263.         $lang_col $this->_getLangCol($langID);
  264.         $table    $this->_getLangTable($langID);
  265.  
  266.         $query sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
  267.              $this->db->quoteIdentifier($lang_coltrue),
  268.              $this->db->quoteIdentifier($tabletrue),
  269.              $this->db->quoteIdentifier($this->options['string_id_col']true),
  270.              $this->db->quote($stringID'text'),
  271.              $this->db->quoteIdentifier($this->options['string_page_id_col']true)
  272.         );
  273.  
  274.         if (is_null($pageID)) {
  275.             $query .= ' IS NULL';
  276.         else {
  277.             $query .= ' = ' $this->db->quote($pageID'text');
  278.         }
  279.         
  280.         ++$this->_queries;
  281.         return $this->db->queryOne($query);
  282.     }
  283.  
  284.     // }}}
  285.     // {{{ getStringID()
  286.  
  287.     /**
  288.      * Get the stringID for the given string
  289.      *
  290.      * @param string $string string
  291.      * @param string $pageID page/group ID
  292.      *
  293.      * @return string 
  294.      */
  295.     function getStringID($string$pageID = null)
  296.     {
  297.         $lang_col $this->_getLangCol($this->currentLang['id']);
  298.         $table $this->_getLangTable($this->currentLang['id']);
  299.         $query sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
  300.              $this->db->quoteIdentifier($this->options['string_id_col']true),
  301.              $this->db->quoteIdentifier($tabletrue),
  302.              $this->db->quoteIdentifier($lang_coltrue),
  303.              $this->db->quote($string'text'),
  304.              $this->db->quoteIdentifier($this->options['string_page_id_col']true)
  305.         );
  306.         if (is_null($pageID)) {
  307.             $query .= ' IS NULL';
  308.         else {
  309.             $query .= ' = ' $this->db->quote($pageID'text');
  310.         }
  311.         ++$this->_queries;
  312.         return $this->db->queryOne($query);
  313.     }
  314.  
  315.     // }}}
  316.     // {{{ _getLangTable()
  317.  
  318.     /**
  319.      * Get the table a language is stored in
  320.      *
  321.      * @param string $langID language ID
  322.      *
  323.      * @return string table $langID is stored in
  324.      * @access private
  325.      */
  326.     function _getLangTable($langID)
  327.     {
  328.         if (isset($this->options['strings_tables'][$langID])) {
  329.             return $this->options['strings_tables'][$langID];
  330.         }
  331.         return str_replace('%s'$langID$this->options['strings_default_table']);
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ _getLangCol()
  336.  
  337.     /**
  338.      * Get the column a language's string is stored in
  339.      *
  340.      * @param string $langID language ID
  341.      *
  342.      * @return string column $langID is stored in
  343.      * @access private
  344.      */
  345.     function _getLangCol($langID)
  346.     {
  347.         static $cols;
  348.         if (!isset($cols[$langID])) {
  349.             if (isset($this->options['string_text_col']&&
  350.                 !empty($this->options['string_text_col'])) {
  351.                 $cols[$langIDstr_replace('%s'$langID$this->options['string_text_col']);
  352.             else {
  353.                 $cols[$langID$langID;
  354.             }
  355.         }
  356.         return $cols[$langID];
  357.     }
  358.  
  359.     // }}}
  360. }
  361. ?>

Documentation generated on Fri, 14 Nov 2008 11:30:31 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.