cvs: pear /XML_sql2xml package.xml sql2xml.php sql2xml_ext.php
| From: | Christian Stocker | Date: | Thu, 16 Aug 2001 15:01:38 +0000 |
| Subject: | cvs: pear /XML_sql2xml package.xml sql2xml.php sql2xml_ext.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-556@lists.php.net to get a copy of this message | ||
chregu Thu Aug 16 11:01:38 2001 EDT
Modified files:
/pear/XML_sql2xml package.xml sql2xml.php sql2xml_ext.php
Log:
- added possibility of other encoding schemes than iso-8859-1 (needs ext/iconv)
- got rid of some warning messages
Index: pear/XML_sql2xml/package.xml diff -u pear/XML_sql2xml/package.xml:1.2 pear/XML_sql2xml/package.xml:1.3 --- pear/XML_sql2xml/package.xml:1.2 Thu Aug 16 04:14:34 2001 +++ pear/XML_sql2xml/package.xml Thu Aug 16 11:01:37 2001 @@ -17,6 +17,7 @@ - Adding of XML-files/strings is possible - works with 4.0.6 and below - setOption can delete values set before. + - Added possibility of other input encoding schemes than iso-8859-1 </Notes> </Release> <FileList> Index: pear/XML_sql2xml/sql2xml.php diff -u pear/XML_sql2xml/sql2xml.php:1.2 pear/XML_sql2xml/sql2xml.php:1.3 --- pear/XML_sql2xml/sql2xml.php:1.2 Thu Aug 16 04:14:34 2001 +++ pear/XML_sql2xml/sql2xml.php Thu Aug 16 11:01:37 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ +// $Id: sql2xml.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ /** * This class takes a PEAR::DB-Result Object, a sql-query-string or an array @@ -46,7 +46,7 @@ * http://php.chregu.tv/sql2xml * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: sql2xml.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ +* @version $Id: sql2xml.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ * @package XML */ class XML_sql2xml { @@ -156,8 +156,18 @@ */ var $user_tableInfo = array(); + /** + * the encoding type, the input from the db has + */ + var $encoding_from = "ISO-8859-1"; /** + * the encoding type, the output in the xml should have + * (note that domxml at the moment only support UTF-8, or at least it looks like) + */ + var $encoding_to = "UTF-8"; + + /** * Constructor * The Constructor can take a Pear::DB "data source name" (eg. * "mysql://user:passwd@localhost/dbname") and will then connect @@ -203,6 +213,10 @@ } $this->xmldoc = domxml_new_xmldoc('1.0'); + + //oehm, seems not to work, unfortunately.... does anybody know a solution? + $this->xmldoc->encoding = $this->encoding_to; + if ($root) { $this->xmlroot = $this->xmldoc->add_root($root); } @@ -402,6 +416,7 @@ } else { + $FirstFetchDone = False; $fetchmode = DB_FETCHMODE_ORDERED; } @@ -420,7 +435,7 @@ } - if (is_null($tableInfo["parent_table"][$value["table"]])) + if (!isset($tableInfo["parent_table"]) || is_null($tableInfo["parent_table"][$value["table"]])) { $tableInfo["parent_key"][$value["table"]] = $key; $tableInfo["parent_table"][$value["table"]] = $parenttable; @@ -439,12 +454,16 @@ { $tableInfo = $this->array_merge_clobber($tableInfo,$this->user_tableInfo); } - $parent[root] = $this->insertNewResult(&$tableInfo); - - while ($FirstFetchDone || $res = $result->FetchRow($fetchmode)) + $parent['root'] = $this->insertNewResult(&$tableInfo); + + //initialize $resold to get rid of warning messages; + $resold[0] = Null; + + while ($FirstFetchDone == True || $res = $result->FetchRow($fetchmode)) { //FirstFetchDone is only for emulating tableInfo, as long as not all dbs support tableInfo. can go away later $FirstFetchDone = False; + while (list($key, $val) = each($res)) { if ($resold[$tableInfo["parent_key"][$tableInfo[$key]["table"]]] != $res[$tableInfo["parent_key"][$tableInfo[$key]["table"]]] || !$this->nested) @@ -647,7 +666,31 @@ */ function xml_encode ($text) { - $text = utf8_encode(ereg_replace("&","&",$text)); + if (function_exists("iconv") && isset($this->encoding_from)) + { + ini_set("track_errors",1); + $text = iconv($this->encoding_from,$this->encoding_to,ereg_replace("&","&",$text)); + + if (! isset($text) ) + { + if (isset($php_errormsg)) + { + $errormsg = "error: $php_errormsg"; + } + else + { + $errormsg = "undefined iconv error, turn on track_errors in php.ini to get more details"; + } + return PEAR::raiseError($errormsg,Null,PEAR_ERROR_DIE); + } + else { + return $text; + } + } + else + { + $text = utf8_encode(ereg_replace("&","&",$text)); + } return $text; } @@ -705,6 +748,17 @@ $this->xmlroot = $this->xmldoc->root(); } - + /** + * sets the encoding for the db2xml transformation + * @param string encoding to transform from + * @param string encoding to transform to + * @acces public + */ + function setEncoding ($encoding_from = "ISO-8859-1", $encoding_to ="UTF-8") + { + $this->encoding_from = $encoding_from; + $this->encoding_to = $encoding_to; + } + } ?> Index: pear/XML_sql2xml/sql2xml_ext.php diff -u pear/XML_sql2xml/sql2xml_ext.php:1.2 pear/XML_sql2xml/sql2xml_ext.php:1.3 --- pear/XML_sql2xml/sql2xml_ext.php:1.2 Thu Aug 16 04:14:34 2001 +++ pear/XML_sql2xml/sql2xml_ext.php Thu Aug 16 11:01:37 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml_ext.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ +// $Id: sql2xml_ext.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ require_once ("XML/sql2xml.php"); @@ -38,7 +38,7 @@ * for the time being * * @author Christian Stocker <chregu@nomad.ch> - * @version $Id: sql2xml_ext.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ + * @version $Id: sql2xml_ext.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ */ class XML_sql2xml_ext extends XML_sql2xml { @@ -49,9 +49,12 @@ // DefaultValues for user_options $user_options = array ( - xml_seperator =>"_", - element_id => "ID", - print_empty_ids => False, + 'xml_seperator' =>"_", + 'element_id' => "ID", + 'print_empty_ids' => False, + 'selected_id' => array(), + 'field_translate' => array(), + 'attributes' => array() ); $this->setOptions(array("user_options"=>$user_options)); @@ -63,7 +66,7 @@ if (!$tableInfo[$key]["table"]) { $tableInfo[$key]["table"] = $this->tagNameResult; } - if ($this->user_options[element_id] && !$res[$tableInfo["id"][$tableInfo[$key]["table"]]] && !$this->user_options[print_empty_ids]) + if ($this->user_options['element_id'] && !$res[$tableInfo["id"][$tableInfo[$key]["table"]]] && !$this->user_options['print_empty_ids']) { return Null; } @@ -80,9 +83,9 @@ /* make attribute selected if ID = selected_id OR tableName.ID = selected_id. for the second case you can give an array for multiple selected entries */ - if ($res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options[selected_id] - || $tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options[selected_id] - || (is_array($this->user_options[selected_id]) && in_array($tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]],$this->user_options[selected_id]))) + if ($res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options['selected_id'] + || $tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options['selected_id'] + || (is_array($this->user_options['selected_id']) && in_array($tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]],$this->user_options['selected_id']))) { $this->SetAttribute($new_row,"selected", "selected"); @@ -102,9 +105,9 @@ function insertNewResult (&$tableInfo) { - if ($this->user_options["result_root"]) + if (isset($this->user_options["result_root"])) $result_root = $this->user_options["result_root"]; - elseif ($tableInfo[0]["table"]) + elseif (isset($tableInfo[0]["table"])) $result_root = $tableInfo[0]["table"]; else $result_root = "resultset"; @@ -125,19 +128,28 @@ } elseif ($this->user_options["xml_seperator"]) { - //the preg should be only done once.... + // initialize some variables to get rid of warning messages + $beforetags = ""; + $before[-1] = Null; + //the preg should be only done once... $i = 0; - preg_match_all("/([^" . $this->user_options["xml_seperator"] . "]+)" . $this->user_options[xml_seperator] . "*/", $tableInfo[$key]["name"], $regs); + preg_match_all("/([^" . $this->user_options["xml_seperator"] . "]+)" . $this->user_options['xml_seperator'] . "*/", $tableInfo[$key]["name"], $regs); - - - $subrow[$regs[1][-1]] = $parent; + if (isset($regs[1][-1])) + { + $subrow[$regs[1][-1]] = $parent; + } + else + { + $subrow[Null] = $parent; + } // here we separate db fields to subtags. + for ($i = 0; $i < (count($regs[1]) - 1); $i++) { $beforetags .=$regs[1][$i]."_"; $before[$i] = $beforetags; - if ( ! $subrow[$before[$i]] ) { + if ( ! isset($subrow[$before[$i]]) ) { $subrow[$before[$i]] = $subrow[$before[$i - 1]]->new_child($regs[1][$i], NULL); } } @@ -152,11 +164,12 @@ } function addTableinfo($key, $value, &$tableInfo) { - if (!$tableInfo[id][$value["table"]] && $value["name"] == $this->user_options["element_id"] ) + + if (!isset($tableInfo['id'][$value["table"]]) && $value["name"] == $this->user_options["element_id"] ) { - $tableInfo[id][$value["table"]]= $key; + $tableInfo['id'][$value["table"]]= $key; } - if ($this->user_options["field_translate"][$value["name"]]) { + if (isset($this->user_options["field_translate"][$value["name"]])) { $tableInfo[$key]["name"] = $this->user_options["field_translate"][$value["name"]]; } }
Index: pear/XML_sql2xml/package.xml diff -u pear/XML_sql2xml/package.xml:1.2 pear/XML_sql2xml/package.xml:1.3 --- pear/XML_sql2xml/package.xml:1.2 Thu Aug 16 04:14:34 2001 +++ pear/XML_sql2xml/package.xml Thu Aug 16 11:01:37 2001 @@ -17,6 +17,7 @@ - Adding of XML-files/strings is possible - works with 4.0.6 and below - setOption can delete values set before. + - Added possibility of other input encoding schemes than iso-8859-1 </Notes> </Release> <FileList> Index: pear/XML_sql2xml/sql2xml.php diff -u pear/XML_sql2xml/sql2xml.php:1.2 pear/XML_sql2xml/sql2xml.php:1.3 --- pear/XML_sql2xml/sql2xml.php:1.2 Thu Aug 16 04:14:34 2001 +++ pear/XML_sql2xml/sql2xml.php Thu Aug 16 11:01:37 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ +// $Id: sql2xml.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ /** * This class takes a PEAR::DB-Result Object, a sql-query-string or an array @@ -46,7 +46,7 @@ * http://php.chregu.tv/sql2xml * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: sql2xml.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ +* @version $Id: sql2xml.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ * @package XML */ class XML_sql2xml { @@ -156,8 +156,18 @@ */ var $user_tableInfo = array(); + /** + * the encoding type, the input from the db has + */ + var $encoding_from = "ISO-8859-1"; /** + * the encoding type, the output in the xml should have + * (note that domxml at the moment only support UTF-8, or at least it looks like) + */ + var $encoding_to = "UTF-8"; + + /** * Constructor * The Constructor can take a Pear::DB "data source name" (eg. * "mysql://user:passwd@localhost/dbname") and will then connect @@ -203,6 +213,10 @@ } $this->xmldoc = domxml_new_xmldoc('1.0'); + + //oehm, seems not to work, unfortunately.... does anybody know a solution? + $this->xmldoc->encoding = $this->encoding_to; + if ($root) { $this->xmlroot = $this->xmldoc->add_root($root); } @@ -402,6 +416,7 @@ } else { + $FirstFetchDone = False; $fetchmode = DB_FETCHMODE_ORDERED; } @@ -420,7 +435,7 @@ } - if (is_null($tableInfo["parent_table"][$value["table"]])) + if (!isset($tableInfo["parent_table"]) || is_null($tableInfo["parent_table"][$value["table"]])) { $tableInfo["parent_key"][$value["table"]] = $key; $tableInfo["parent_table"][$value["table"]] = $parenttable; @@ -439,12 +454,16 @@ { $tableInfo = $this->array_merge_clobber($tableInfo,$this->user_tableInfo); } - $parent[root] = $this->insertNewResult(&$tableInfo); - - while ($FirstFetchDone || $res = $result->FetchRow($fetchmode)) + $parent['root'] = $this->insertNewResult(&$tableInfo); + + //initialize $resold to get rid of warning messages; + $resold[0] = Null; + + while ($FirstFetchDone == True || $res = $result->FetchRow($fetchmode)) { //FirstFetchDone is only for emulating tableInfo, as long as not all dbs support tableInfo. can go away later $FirstFetchDone = False; + while (list($key, $val) = each($res)) { if ($resold[$tableInfo["parent_key"][$tableInfo[$key]["table"]]] != $res[$tableInfo["parent_key"][$tableInfo[$key]["table"]]] || !$this->nested) @@ -647,7 +666,31 @@ */ function xml_encode ($text) { - $text = utf8_encode(ereg_replace("&","&",$text)); + if (function_exists("iconv") && isset($this->encoding_from)) + { + ini_set("track_errors",1); + $text = iconv($this->encoding_from,$this->encoding_to,ereg_replace("&","&",$text)); + + if (! isset($text) ) + { + if (isset($php_errormsg)) + { + $errormsg = "error: $php_errormsg"; + } + else + { + $errormsg = "undefined iconv error, turn on track_errors in php.ini to get more details"; + } + return PEAR::raiseError($errormsg,Null,PEAR_ERROR_DIE); + } + else { + return $text; + } + } + else + { + $text = utf8_encode(ereg_replace("&","&",$text)); + } return $text; } @@ -705,6 +748,17 @@ $this->xmlroot = $this->xmldoc->root(); } - + /** + * sets the encoding for the db2xml transformation + * @param string encoding to transform from + * @param string encoding to transform to + * @acces public + */ + function setEncoding ($encoding_from = "ISO-8859-1", $encoding_to ="UTF-8") + { + $this->encoding_from = $encoding_from; + $this->encoding_to = $encoding_to; + } + } ?> Index: pear/XML_sql2xml/sql2xml_ext.php diff -u pear/XML_sql2xml/sql2xml_ext.php:1.2 pear/XML_sql2xml/sql2xml_ext.php:1.3 --- pear/XML_sql2xml/sql2xml_ext.php:1.2 Thu Aug 16 04:14:34 2001 +++ pear/XML_sql2xml/sql2xml_ext.php Thu Aug 16 11:01:37 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml_ext.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ +// $Id: sql2xml_ext.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ require_once ("XML/sql2xml.php"); @@ -38,7 +38,7 @@ * for the time being * * @author Christian Stocker <chregu@nomad.ch> - * @version $Id: sql2xml_ext.php,v 1.2 2001/08/16 08:14:34 chregu Exp $ + * @version $Id: sql2xml_ext.php,v 1.3 2001/08/16 15:01:37 chregu Exp $ */ class XML_sql2xml_ext extends XML_sql2xml { @@ -49,9 +49,12 @@ // DefaultValues for user_options $user_options = array ( - xml_seperator =>"_", - element_id => "ID", - print_empty_ids => False, + 'xml_seperator' =>"_", + 'element_id' => "ID", + 'print_empty_ids' => False, + 'selected_id' => array(), + 'field_translate' => array(), + 'attributes' => array() ); $this->setOptions(array("user_options"=>$user_options)); @@ -63,7 +66,7 @@ if (!$tableInfo[$key]["table"]) { $tableInfo[$key]["table"] = $this->tagNameResult; } - if ($this->user_options[element_id] && !$res[$tableInfo["id"][$tableInfo[$key]["table"]]] && !$this->user_options[print_empty_ids]) + if ($this->user_options['element_id'] && !$res[$tableInfo["id"][$tableInfo[$key]["table"]]] && !$this->user_options['print_empty_ids']) { return Null; } @@ -80,9 +83,9 @@ /* make attribute selected if ID = selected_id OR tableName.ID = selected_id. for the second case you can give an array for multiple selected entries */ - if ($res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options[selected_id] - || $tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options[selected_id] - || (is_array($this->user_options[selected_id]) && in_array($tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]],$this->user_options[selected_id]))) + if ($res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options['selected_id'] + || $tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options['selected_id'] + || (is_array($this->user_options['selected_id']) && in_array($tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]],$this->user_options['selected_id']))) { $this->SetAttribute($new_row,"selected", "selected"); @@ -102,9 +105,9 @@ function insertNewResult (&$tableInfo) { - if ($this->user_options["result_root"]) + if (isset($this->user_options["result_root"])) $result_root = $this->user_options["result_root"]; - elseif ($tableInfo[0]["table"]) + elseif (isset($tableInfo[0]["table"])) $result_root = $tableInfo[0]["table"]; else $result_root = "resultset"; @@ -125,19 +128,28 @@ } elseif ($this->user_options["xml_seperator"]) { - //the preg should be only done once.... + // initialize some variables to get rid of warning messages + $beforetags = ""; + $before[-1] = Null; + //the preg should be only done once... $i = 0; - preg_match_all("/([^" . $this->user_options["xml_seperator"] . "]+)" . $this->user_options[xml_seperator] . "*/", $tableInfo[$key]["name"], $regs); + preg_match_all("/([^" . $this->user_options["xml_seperator"] . "]+)" . $this->user_options['xml_seperator'] . "*/", $tableInfo[$key]["name"], $regs); - - - $subrow[$regs[1][-1]] = $parent; + if (isset($regs[1][-1])) + { + $subrow[$regs[1][-1]] = $parent; + } + else + { + $subrow[Null] = $parent; + } // here we separate db fields to subtags. + for ($i = 0; $i < (count($regs[1]) - 1); $i++) { $beforetags .=$regs[1][$i]."_"; $before[$i] = $beforetags; - if ( ! $subrow[$before[$i]] ) { + if ( ! isset($subrow[$before[$i]]) ) { $subrow[$before[$i]] = $subrow[$before[$i - 1]]->new_child($regs[1][$i], NULL); } } @@ -152,11 +164,12 @@ } function addTableinfo($key, $value, &$tableInfo) { - if (!$tableInfo[id][$value["table"]] && $value["name"] == $this->user_options["element_id"] ) + + if (!isset($tableInfo['id'][$value["table"]]) && $value["name"] == $this->user_options["element_id"] ) { - $tableInfo[id][$value["table"]]= $key; + $tableInfo['id'][$value["table"]]= $key; } - if ($this->user_options["field_translate"][$value["name"]]) { + if (isset($this->user_options["field_translate"][$value["name"]])) { $tableInfo[$key]["name"] = $this->user_options["field_translate"][$value["name"]]; } }