home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Translation2 / Container / db.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  11.5 KB  |  359 lines

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