Source for file xml.php
Documentation is available at xml.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Contains the Translation2_Admin_Container_xml 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>
* @author Olivier Guilyardi <olivier@samalyse.com>
* @copyright 2004-2007 Lorenzo Alberton, Olivier Guilyardi
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @version CVS: $Id: xml.php,v 1.18 2007/11/10 00:02:50 quipo Exp $
* @link http://pear.php.net/package/Translation2
* require Translation2_Container_xml class
require_once 'Translation2/Container/xml.php';
require_once 'XML/Util.php';
* Storage driver for storing/fetching data to/from a XML file
* @category Internationalization
* @author Lorenzo Alberton <l.alberton@quipo.it>
* @author Olivier Guilyardi <olivier@samalyse.com>
* @copyright 2004-2007 Lorenzo Alberton, Olivier Guilyardi
* @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
* @link http://pear.php.net/package/Translation2
* Whether _saveData() is already registered at shutdown or not
var $_isScheduledSaving = false;
* Does nothing (here for compatibility with the container interface)
* @param array $langData language data
* @param array $options language options
* @return true|PEAR_Error
function addLang($langData, $options = array ())
* Creates a new entry in the <languages> section
* @param array $langData array('lang_id' => 'en',
* 'meta' => 'some meta info',
* 'error_text' => 'not available',
* 'encoding' => 'iso-8859-1',
* @return true|PEAR_Error
'encoding' => 'iso-8859-1',
foreach ($validInput as $key => $val) {
if (isset ($langData[$key])) $validInput[$key] = $langData[$key];
$this->_data['languages'][$langData['lang_id']] = $validInput;
return $this->_scheduleSaving ();
* Update the lang info in the langsAvail table
* @param array $langData array [@see addLangToList()]
* @return true|PEAR_Error
$allFields = array ( //'lang_id',
'name', 'meta', 'error_text', 'encoding',
foreach ($allFields as $field) {
if (isset ($this->_data['languages'][$langData['lang_id']][$field])) {
$this->_data['languages'][$langData['lang_id']][$field] = $langData[$field];
$success = $this->_scheduleSaving ();
* Add a new entry in the strings table.
* @param string $stringID string ID
* @param string $pageID page/group ID
* @param array $stringArray Associative array with string translations.
* Sample format: array('en' => 'sample', 'it' => 'esempio')
* @return true|PEAR_Error
function add($stringID, $pageID, $stringArray)
$pageID = is_null($pageID) ? '#NULL' : $pageID;
$pageID = empty ($pageID) ? '#EMPTY' : $pageID;
$this->_data['pages'][$pageID] = array ();
$this->_data['pages'][$pageID][$stringID] = array ();
foreach ($langs as $lang) {
$this->_data['pages'][$pageID][$stringID][$lang] = $stringArray[$lang];
return $this->_scheduleSaving ();
* Update an existing entry in the strings table.
* @param string $stringID string ID
* @param string $pageID page/group ID
* @param array $stringArray Associative array with string translations.
* Sample format: array('en' => 'sample', 'it' => 'esempio')
* @return true|PEAR_Error
function update($stringID, $pageID, $stringArray)
return $this->add($stringID, $pageID, $stringArray);
* Remove an entry from the strings table.
* @param string $stringID string ID
* @param string $pageID page/group ID
* @return true|PEAR_Error
function remove($stringID, $pageID)
$pageID = is_null($pageID) ? '#NULL' : $pageID;
$pageID = empty ($pageID) ? '#EMPTY' : $pageID;
unset ($this->_data['pages'][$pageID][$stringID]);
if (!count($this->_data['pages'][$pageID])) {
unset ($this->_data['pages'][$pageID]);
return $this->_scheduleSaving ();
* Remove all the entries for the given lang from the strings table.
* @param string $langID language ID
* @param boolean $force (ignored)
* @return true|PEAR_Error
unset ($this->_data['languages'][$langID]);
foreach (array_keys($this->_data['pages']) as $pageID) {
foreach (array_keys($this->_data['pages'][$pageID]) as $stringID) {
unset ($this->_data['pages'][$pageID][$stringID][$langID]);
return $this->_scheduleSaving ();
* Remove all the strings in the given page/group
* @param string $pageID page/group ID
* @return true|PEAR_Error
$pageID = is_null($pageID) ? '#NULL' : $pageID;
$pageID = empty ($pageID) ? '#EMPTY' : $pageID;
unset ($this->_data['pages'][$pageID]);
return $this->_scheduleSaving ();
* Get a list of all the pageIDs.
if ($k !== false && !is_null($k)) {
if ($k !== false && !is_null($k)) {
* This methods registers _saveData() as a PEAR shutdown function. This
* is to avoid saving multiple times if the programmer makes several
* @return true|PEAR_Error
* @see Translation2_Admin_Container_xml::_saveData()
function _scheduleSaving ()
if ($this->options['save_on_shutdown']) {
if (!$this->_isScheduledSaving) {
// save the changes on shutdown
$this->_isScheduledSaving = true;
return $this->_saveData ();
* Serialize and save the updated tranlation data to the XML file
* @return boolean | PEAR_Error
* @see Translation2_Admin_Container_xml::_scheduleSaving()
if ($this->options['save_on_shutdown']) {
$this->_convertEncodings ('to_xml', $data);
$this->_convertLangEncodings ('to_xml', $data);
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" .
foreach ($data['languages'] as $lang => $spec) {
$xml .= " <lang id=\"$lang\">\n" .
($name ? ' ' . XML_Util ::replaceEntities ($name) . ' ' : '') .
($meta ? ' ' . XML_Util ::replaceEntities ($meta) . ' ' : "") .
? ' ' . XML_Util ::replaceEntities ($error_text) . ' '
" <encoding>" . ($encoding ? " $encoding " : "") .
$xml .= " </languages>\n" .
foreach ($data['pages'] as $page => $strings) {
$xml .= " <page key=\"" . XML_Util ::replaceEntities ($page) .
foreach ($strings as $str_id => $translations) {
$xml .= " <string key=\"" .
XML_Util ::replaceEntities ($str_id) . "\">\n";
foreach ($translations as $lang => $str) {
$xml .= " <tr lang=\"$lang\"> " .
XML_Util ::replaceEntities ($str) . " </tr>\n";
if (!$f = fopen ($this->_filename, 'w')) {
'Unable to open the XML file ("%s") for writing',
Documentation generated on Tue, 06 May 2008 06:00:46 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|