cvs: pear /Translation2 package.xml /Translation2/Admin/Container xml.php /Translation2/Container xml.php

From: Date: Mon, 21 Feb 2005 16:49:16 +0000
Subject: cvs: pear /Translation2 package.xml /Translation2/Admin/Container xml.php /Translation2/Container xml.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-29619@lists.php.net to get a copy of this message
quipo Mon Feb 21 11:49:16 2005 EDT Modified files: /pear/Translation2/Admin/Container xml.php /pear/Translation2/Container xml.php /pear/Translation2 package.xml Log: bug #3498, part 2: optimize saving when save_on_shutdown is set to false Many thanks to Olivier Guilyardi for the patch

http://cvs.php.net/diff.php/pear/Translation2/Admin/Container/xml.php?r1=1.9&r2=1.10&ty=u Index: pear/Translation2/Admin/Container/xml.php diff -u pear/Translation2/Admin/Container/xml.php:1.9 pear/Translation2/Admin/Container/xml.php:1.10 --- pear/Translation2/Admin/Container/xml.php:1.9 Thu Feb 17 08:23:51 2005 +++ pear/Translation2/Admin/Container/xml.php Mon Feb 21 11:49:15 2005 @@ -16,11 +16,11 @@ // | Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: xml.php,v 1.9 2005/02/17 13:23:51 quipo Exp $ +// $Id: xml.php,v 1.10 2005/02/21 16:49:15 quipo Exp $ // /** * @package Translation2 - * @version $Id: xml.php,v 1.9 2005/02/17 13:23:51 quipo Exp $ + * @version $Id: xml.php,v 1.10 2005/02/21 16:49:15 quipo Exp $ */ /** @@ -83,11 +83,11 @@ 'error_text' => '', '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(); } @@ -147,7 +147,7 @@ foreach ($langs as $lang) { $this->_data['pages'][$pageID][$stringID][$lang] = $stringArray[$lang]; } - + return $this->_scheduleSaving(); } @@ -203,7 +203,7 @@ */ function removeLang($langID, $force = true) { - // remove lang metadata + // remove lang metadata unset($this->_data['languages'][$langID]); // remove the entries @@ -241,14 +241,14 @@ // }}} // {{{ _scheduleSaving() - + /** * Prepare data saving * * This methods registers _saveData() as a PEAR shutdown function. This - * is to avoid saving multiple times if the programmer makes several + * is to avoid saving multiple times if the programmer makes several * changes. - * + * * @return true|PEAR_Error * @access private * @see Translation2_Admin_Container_xml::_saveData() @@ -263,18 +263,18 @@ } return true; } - + // save the changes now - if (PEAR::isError($e = $this->_saveData())) { - return $e; - } - // refresh memory cache - return $this->_loadFile(); + return $this->_saveData(); + + + + } // }}} // {{{ _saveData() - + /** * Serialize and save the updated tranlation data to the XML file * @@ -284,43 +284,49 @@ */ function _saveData() { - $this->_convertEncodings('to_xml'); - $this->_convertLangEncodings('to_xml'); - + if ($this->options['save_on_shutdown']) { + $data =& $this->_data; + } else { + $data = $this->_data; + } + + $this->_convertEncodings('to_xml', $data); + $this->_convertLangEncodings('to_xml', $data); + // Serializing - + $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n" . "<!DOCTYPE translation2 [\n" . TRANSLATION2_DTD . "]>\n\n" . "<translation2>\n" . " <languages>\n"; - foreach ($this->_data['languages'] as $lang => $spec) { + foreach ($data['languages'] as $lang => $spec) { extract ($spec); $xml .= " <lang id=\"$lang\">\n" . - " <name>" . - ($name ? ' ' . XML_Util::replaceEntities($name) . ' ' : '') . + " <name>" . + ($name ? ' ' . XML_Util::replaceEntities($name) . ' ' : '') . "</name>\n" . - " <meta>" . - ($meta ? ' ' . XML_Util::replaceEntities($meta) . ' ' : "") . + " <meta>" . + ($meta ? ' ' . XML_Util::replaceEntities($meta) . ' ' : "") . "</meta>\n" . - " <error_text>" . - ($error_text - ? ' ' . XML_Util::replaceEntities($error_text) . ' ' - : "") . + " <error_text>" . + ($error_text + ? ' ' . XML_Util::replaceEntities($error_text) . ' ' + : "") . "</error_text>\n" . - " <encoding>" . ($encoding ? " $encoding " : "") . - "</encoding>\n" . + " <encoding>" . ($encoding ? " $encoding " : "") . + "</encoding>\n" . " </lang>\n"; } $xml .= " </languages>\n" . " <pages>\n"; - foreach ($this->_data['pages'] as $page => $strings) { - $xml .= " <page key=\"" . XML_Util::replaceEntities($page) . + foreach ($data['pages'] as $page => $strings) { + $xml .= " <page key=\"" . XML_Util::replaceEntities($page) . "\">\n"; foreach ($strings as $str_id => $translations) { - $xml .= " <string key=\"" . + $xml .= " <string key=\"" . XML_Util::replaceEntities($str_id) . "\">\n"; foreach ($translations as $lang => $str) { $xml .= " <tr lang=\"$lang\"> " . @@ -334,6 +340,8 @@ $xml .= " </pages>\n" . "</translation2>\n"; + unset ($data); + // Saving if (!$f = fopen ($this->_filename, 'w')) { http://cvs.php.net/diff.php/pear/Translation2/Container/xml.php?r1=1.6&r2=1.7&ty=u Index: pear/Translation2/Container/xml.php diff -u pear/Translation2/Container/xml.php:1.6 pear/Translation2/Container/xml.php:1.7 --- pear/Translation2/Container/xml.php:1.6 Thu Feb 10 03:09:42 2005 +++ pear/Translation2/Container/xml.php Mon Feb 21 11:49:16 2005 @@ -16,11 +16,11 @@ // | Lorenzo Alberton <l dot alberton at quipo dot it> | // +----------------------------------------------------------------------+ // -// $Id: xml.php,v 1.6 2005/02/10 08:09:42 quipo Exp $ +// $Id: xml.php,v 1.7 2005/02/21 16:49:16 quipo Exp $ // /** * @package Translation2 - * @version $Id $ + * @version $Id: xml.php,v 1.7 2005/02/21 16:49:16 quipo Exp $ */ /** @@ -52,10 +52,10 @@ "<!ATTLIST tr lang IDREF #REQUIRED>\n" ); /** - * Storage driver for fetching data from an xml file + * Storage driver for fetching data from an xml file * * Example file : - * + * * <?xml version="1.0" encoding="iso-8859-1"?> * <translation2> * <languages> @@ -87,7 +87,7 @@ // {{{ class vars /** - * Unserialized XML data + * Unserialized XML data * @var object */ var $_data = null; @@ -97,12 +97,12 @@ * @var string */ var $_filename; - + // }}} // {{{ init /** - * Initialize the container + * Initialize the container * * @param string $filename Path to the XML file * @return boolean|PEAR_Error object if something went wrong @@ -119,7 +119,7 @@ // }}} // {{{ _loadFile() - + /** * Load an XML file into memory, and eventually decode the strings from UTF-8 * @@ -173,40 +173,41 @@ } // convert lang metadata from UTF-8 - if (PEAR::isError($e = $this->_convertLangEncodings('from_xml'))) { + if (PEAR::isError($e = $this->_convertLangEncodings('from_xml', $this->_data))) { return $e; } // convert encodings of the translated strings from xml (somehow heavy) - return $this->_convertEncodings('from_xml'); + return $this->_convertEncodings('from_xml', $this->_data); } - + // }}} // {{{ _convertEncodings() - /** + /** * Convert strings to/from XML unique charset (UTF-8) * * @param string ['from_xml' | 'to_xml'] + * @param array $data Data buffer to operate on * @return boolean|PEAR_Error */ - function _convertEncodings($direction) + function _convertEncodings($direction, &$data) { if ($direction == 'from_xml') { $source_encoding = 'UTF-8'; } else { $target_encoding = 'UTF-8'; } - - foreach ($this->_data['pages'] as $page_id => $page_content) { + + foreach ($data['pages'] as $page_id => $page_content) { foreach ($page_content as $str_id => $translations) { foreach ($translations as $lang => $str) { if ($direction == 'from_xml') { $target_encoding = - strtoupper($this->_data['languages'][$lang]['encoding']); + strtoupper($data['languages'][$lang]['encoding']); } else { $source_encoding = - strtoupper($this->_data['languages'][$lang]['encoding']); + strtoupper($data['languages'][$lang]['encoding']); } if ($target_encoding != $source_encoding) { $res = iconv ($source_encoding, $target_encoding, $str); @@ -220,14 +221,14 @@ PEAR_ERROR_RETURN, E_USER_WARNING); } - $this->_data['pages'][$page_id][$str_id][$lang] = $res; + $data['pages'][$page_id][$str_id][$lang] = $res; } } } } return true; } - + // }}} // {{{ _convertLangEncodings() @@ -235,9 +236,10 @@ * Convert lang data to/from XML unique charset (UTF-8) * * @param string $direction ['from_xml' | 'to_xml'] + * @param array $data Data buffer to operate on * @return boolean|PEAR_Error */ - function _convertLangEncodings($direction) + function _convertLangEncodings($direction, &$data) { static $fields = array('name', 'meta', 'error_text'); @@ -246,8 +248,8 @@ } else { $target_encoding = 'UTF-8'; } - - foreach ($this->_data['languages'] as $lang_id => $lang) { + + foreach ($data['languages'] as $lang_id => $lang) { if ($direction == 'from_xml') { $target_encoding = strtoupper($lang['encoding']); } else { @@ -267,7 +269,7 @@ PEAR_ERROR_RETURN, E_USER_WARNING); } - $this->_data['languages'][$lang_id][$field] = $res; + $data['languages'][$lang_id][$field] = $res; } } } @@ -298,7 +300,7 @@ /** * Turn empty strings returned by XML_Unserializer into empty arrays * - * Note: this method is public because called statically by the t2xmlchk.php + * Note : this method is public because called statically by the t2xmlchk.php * script. It is not meant to be called by user-space code. * * @access public @@ -392,11 +394,11 @@ $result = array(); foreach ($this->_data['pages'][$pageID] as $str_id => $translations) { - $result[$str_id] = isset($translations[$langID]) - ? $translations[$langID] + $result[$str_id] = isset($translations[$langID]) + ? $translations[$langID] : null; } - + return $result; } @@ -414,7 +416,7 @@ function getOne($stringID, $pageID = null, $langID = null) { $langID = $this->_getLangID($langID); - $pageID = (is_null($pageID)) ? '#NULL' : $pageID; + $pageID = (is_null($pageID)) ? '#NULL' : $pageID; return isset($this->_data['pages'][$pageID][$stringID][$langID]) ? $this->_data['pages'][$pageID][$stringID][$langID] @@ -433,8 +435,8 @@ */ function getStringID($string, $pageID = null) { - $pageID = (is_null($pageID)) ? '#NULL' : $pageID; - + $pageID = (is_null($pageID)) ? '#NULL' : $pageID; + foreach ($this->_data['pages'][$pageID] as $str_id => $translations) { if (array_search($string,$translations) !== false) { return $str_id; @@ -443,7 +445,7 @@ return ''; } - + // }}} } ?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Translation2/package.xml?r1=1.67&r2=1.68&ty=u Index: pear/Translation2/package.xml diff -u pear/Translation2/package.xml:1.67 pear/Translation2/package.xml:1.68 --- pear/Translation2/package.xml:1.67 Thu Feb 17 08:23:51 2005 +++ pear/Translation2/package.xml Mon Feb 21 11:49:16 2005 @@ -45,15 +45,16 @@ </maintainers> <release> <version>2.0.0beta7</version> - <date>2005-02-17</date> + <date>2005-02-21</date> <license>PHP License</license> <state>beta</state> <notes> -- Some fixes to the XML container, thanks to Olivier Guilyardi: +- Some fixes to the XML container, many thanks to Olivier Guilyardi: * fixed bug #3408: empty data sets were not correctly handled; * fixed bug #3420: get a shared file lock instead of an exclusive one; * fixed bug #3498: saveData() is not registered multiple times as shutdown - function anymore. + function anymore; optimized saving when save_on_shutdown is set to false. + </notes> <deps> <dep type="pkg" rel="has" optional="yes">Cache_Lite</dep> @@ -153,15 +154,15 @@ <changelog> <release> <version>2.0.0beta7</version> - <date>2005-02-17</date> + <date>2005-02-21</date> <license>PHP License</license> <state>beta</state> <notes> -- Some fixes to the XML container, thanks to Olivier Guilyardi: +- Some fixes to the XML container, many thanks to Olivier Guilyardi: * fixed bug #3408: empty data sets were not correctly handled; * fixed bug #3420: get a shared file lock instead of an exclusive one; * fixed bug #3498: saveData() is not registered multiple times as shutdown - function anymore. + function anymore; optimized saving when save_on_shutdown is set to false. </notes> </release> <release>
« previous php.pear.cvs (#29619) next »