Source for file mdb2.php
Documentation is available at mdb2.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Contains the Translation2_Container_mdb2 class
* LICENSE: Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @category Internationalization
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @copyright 2004-2008 Lorenzo Alberton
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: mdb2.php,v 1.30 2008/05/03 09:17:59 quipo Exp $
* @link http://pear.php.net/package/Translation2
* require Translation2_Container class
require_once 'Translation2/Container.php';
* Storage driver for fetching data from a database
* This storage driver can use all databases which are supported
* by the PEAR::MDB2 abstraction layer to fetch data.
* @category Internationalization
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @copyright 2004-2008 Lorenzo Alberton
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: mdb2.php,v 1.30 2008/05/03 09:17:59 quipo Exp $
* @link http://pear.php.net/package/Translation2
* Initialize the container
* @param string &$db Connection data or MDB2 object
* @return boolean|PEAR_Errorobject if something went wrong
$this->_setDefaultOptions ();
if (PEAR ::isError ($err = $this->_connect ($db))) {
* Connect to database by using the given DSN string
* @param mixed &$db DSN string | array | MDB2 object
* @return boolean|PEAR_Erroron error
$this->db = & MDB2 ::connect ($db);
} elseif (is_object($db) && MDB2 ::isError ($db)) {
return PEAR ::raiseError ($db->getMessage (), $db->code );
return PEAR ::raiseError ('The given dsn was not valid in file '
. __FILE__ . ' at line ' . __LINE__ ,
if (PEAR ::isError ($this->db)) {
// {{{ _setDefaultOptions()
* Set some default options
function _setDefaultOptions ()
$this->options['langs_avail_table'] = 'langs';
$this->options['lang_id_col'] = 'id';
$this->options['lang_name_col'] = 'name';
$this->options['lang_meta_col'] = 'meta';
$this->options['lang_errmsg_col'] = 'error_text';
$this->options['lang_encoding_col'] = 'encoding';
$this->options['strings_default_table'] = 'i18n';
$this->options['strings_tables'] = array (); // 'lang_id' => 'table_name'
$this->options['string_id_col'] = 'id';
$this->options['string_page_id_col'] = 'page_id';
$this->options['string_page_id_col_length'] = 50;
$this->options['string_text_col'] = '%s'; // col_name if one table per lang is used,
// or a pattern (i.e. "tr_%s" => "tr_EN_US")
* Set charset used to read/store the translations
* @param string $charset character set (encoding)
* @return PEAR_Error on error
return $this->db->setCharset ($charset);
* Fetch the available langs if they're not cached yet.
* @return PEAR_Error on error
$query = sprintf('SELECT %s AS id, %s AS name, %s AS meta, %s AS error_text, %s AS encoding FROM %s',
$this->db->quoteIdentifier ($this->options['lang_id_col'], true ),
$this->db->quoteIdentifier ($this->options['lang_name_col'], true ),
$this->db->quoteIdentifier ($this->options['lang_meta_col'], true ),
$this->db->quoteIdentifier ($this->options['lang_errmsg_col'], true ),
$this->db->quoteIdentifier ($this->options['lang_encoding_col'], true ),
$this->db->quoteIdentifier ($this->options['langs_avail_table'], true )
$res = $this->db->queryAll ($query, null , MDB2_FETCHMODE_ASSOC );
if (PEAR ::isError ($res)) {
$this->langs[$row['id']] = $row;
* Returns an array of the strings in the selected page
* @param string $pageID page/group ID
* @param string $langID language ID
function getPage($pageID = null , $langID = null )
$langID = $this->_getLangID ($langID);
if (PEAR ::isError ($langID)) {
$lang_col = $this->_getLangCol ($langID);
$table = $this->_getLangTable ($langID);
$query = sprintf('SELECT %s, %s FROM %s WHERE %s ',
$this->db->quoteIdentifier ($this->options['string_id_col'], true ),
$this->db->quoteIdentifier ($lang_col, true ),
$this->db->quoteIdentifier ($table, true ),
$this->db->quoteIdentifier ($this->options['string_page_id_col'], true )
$query .= ' = ' . $this->db->quote ($pageID, 'text');
$res = $this->db->query ($query);
if (PEAR ::isError ($res)) {
while (list ($key, $value) = $res->fetchRow (MDB2_FETCHMODE_ORDERED )) {
* Get a single item from the container
* @param string $stringID string ID
* @param string $pageID page/group ID
* @param string $langID language ID
function getOne($stringID, $pageID = null , $langID = null )
$langID = $this->_getLangID ($langID);
if (PEAR ::isError ($langID)) {
$lang_col = $this->_getLangCol ($langID);
$table = $this->_getLangTable ($langID);
$query = sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
$this->db->quoteIdentifier ($lang_col, true ),
$this->db->quoteIdentifier ($table, true ),
$this->db->quoteIdentifier ($this->options['string_id_col'], true ),
$this->db->quote ($stringID, 'text'),
$this->db->quoteIdentifier ($this->options['string_page_id_col'], true )
$query .= ' = ' . $this->db->quote ($pageID, 'text');
return $this->db->queryOne ($query);
* Get the stringID for the given string
* @param string $string string
* @param string $pageID page/group ID
$lang_col = $this->_getLangCol ($this->currentLang['id']);
$table = $this->_getLangTable ($this->currentLang['id']);
$query = sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
$this->db->quoteIdentifier ($this->options['string_id_col'], true ),
$this->db->quoteIdentifier ($table, true ),
$this->db->quoteIdentifier ($lang_col, true ),
$this->db->quote ($string, 'text'),
$this->db->quoteIdentifier ($this->options['string_page_id_col'], true )
$query .= ' = ' . $this->db->quote ($pageID, 'text');
return $this->db->queryOne ($query);
* Get the table a language is stored in
* @param string $langID language ID
* @return string table $langID is stored in
function _getLangTable ($langID)
if (isset ($this->options['strings_tables'][$langID])) {
return $this->options['strings_tables'][$langID];
* Get the column a language's string is stored in
* @param string $langID language ID
* @return string column $langID is stored in
function _getLangCol ($langID)
if (!isset ($cols[$langID])) {
if (isset ($this->options['string_text_col']) &&
!empty ($this->options['string_text_col'])) {
$cols[$langID] = $langID;
Documentation generated on Fri, 14 Nov 2008 11:30:31 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|