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

Source for file db.php

Documentation is available at db.php

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

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