cvs: pear /XML_sql2xml package.xml sql2xml.php sql2xml_ext.php
| From: | Christian Stocker | Date: | Tue, 28 Aug 2001 17:22:52 +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-684@lists.php.net to get a copy of this message | ||
chregu Tue Aug 28 13:22:52 2001 EDT
Modified files:
/pear/XML_sql2xml package.xml sql2xml.php sql2xml_ext.php
Log:
- xpath support for sql-queries and xml-files/strings
- package.xml converted to lowercase
Index: pear/XML_sql2xml/package.xml diff -u pear/XML_sql2xml/package.xml:1.5 pear/XML_sql2xml/package.xml:1.6 --- pear/XML_sql2xml/package.xml:1.5 Fri Aug 17 11:27:00 2001 +++ pear/XML_sql2xml/package.xml Tue Aug 28 13:22:52 2001 @@ -1,18 +1,18 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> -<Package> - <Name>XML_sql2xml</Name> - <Summary>This class takes a PEAR::DB-Result Object, a sql-query-string, an array and/or an xml-string/file and returns a xml-representation of it.</Summary> - <Maintainer> - <Initials>cs</Initials> - <Name>Christian Stocker</Name> - <Email>chregu@php.net</Email> - </Maintainer> - <Release> - <Version>0.3</Version> - <Date>2001-08-17</Date> - <Notes> +<package> + <name>XML_sql2xml</name> + <summary>This class takes a PEAR::DB-Result Object, a sql-query-string, an array and/or an xml-string/file and returns a xml-representation of it.</summary> + <maintainer> + <initials>cs</initials> + <name>Christian Stocker</name> + <email>chregu@php.net</email> + </maintainer> + <release> + <version>0.3</version> + <date>2001-08-17</date> + <notes> New Features: - works with PHP 4.0.7 and below - Adding of XML-files/strings is possible @@ -23,13 +23,14 @@ - user_option selected_id can take the arguments first and all BugFixes - sql selects are recognized with capitals as well - - if there was a < in the database it gave an xml-error, now "< " is "escaped" - </Notes> - </Release> - <FileList> - <Dir Name="XML" BaseInstallDir="XML" > - <File Role="php">sql2xml.php</File> - <File Role="php">sql2xml_ext.php</File> - </Dir> - </FileList> -</Package> + - if there was a [lesserthan] in the database it gave an xml-error, now "[lesserthan] " is "escaped" + - xpath support for sql-queries and xml-files to be inserted + </notes> + </release> + <filelist> + <dir name="XML" baseinstalldir="XML"> + <file role="php">sql2xml.php</file> + <file role="php">sql2xml_ext.php</file> + </dir> + </filelist> +</package> Index: pear/XML_sql2xml/sql2xml.php diff -u pear/XML_sql2xml/sql2xml.php:1.5 pear/XML_sql2xml/sql2xml.php:1.6 --- pear/XML_sql2xml/sql2xml.php:1.5 Fri Aug 17 11:25:07 2001 +++ pear/XML_sql2xml/sql2xml.php Tue Aug 28 13:22:52 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml.php,v 1.5 2001/08/17 15:25:07 chregu Exp $ +// $Id: sql2xml.php,v 1.6 2001/08/28 17:22:52 chregu Exp $ /** * This class takes a PEAR::DB-Result Object, a sql-query-string or an array @@ -48,7 +48,7 @@ * http://php.chregu.tv/sql2xml * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: sql2xml.php,v 1.5 2001/08/17 15:25:07 chregu Exp $ +* @version $Id: sql2xml.php,v 1.6 2001/08/28 17:22:52 chregu Exp $ * @package XML */ class XML_sql2xml { @@ -92,7 +92,7 @@ /** * Options to be used in extended Classes (for example in sql2xml_ext). - * They are passed with SetOptions as an array (arrary("user_optpaions" = array()); + * They are passed with SetOptions as an array (arrary("user_options" = array()); * and can then be accessed with $this->user_options["bla"] from your * extended classes for additional features. * This array is not use in this base class, it's only for passing easy parameters @@ -161,16 +161,16 @@ /** * the encoding type, the input from the db has */ - var $encoding_from = "ISO-8859-1"; + var $encoding_from = "ISO-8859-1"; /** - * the encoding type, the output in the xml should have + * 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"; var $tagname = "tagname"; - + /** * Constructor * The Constructor can take a Pear::DB "data source name" (eg. @@ -220,10 +220,10 @@ //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); - //PHP 4.0.6 had $root->name as tagname, check for that here... + //PHP 4.0.6 had $root->name as tagname, check for that here... if (!isset($this->xmlroot->{$this->tagname})) { $this->tagname = "name"; @@ -239,16 +239,17 @@ * input and adds this to $this->xmldoc * * @param $resultset string sql-string, or object db_result, or array + * @param $param mixed additional parameters for the following functions * @access public - * @see addResult(), addSql(), addArray() + * @see addResult(), addSql(), addArray(), addXmlFile() */ - function add ($resultset) + function add ($resultset,$params=Null) { // if string, then it's a query, a xml-file or a xml-string... if (is_string($resultset)) { if (preg_match("/\.xml$/",$resultset)) { - $this->AddXmlFile($resultset); + $this->AddXmlFile($resultset,$params); } elseif (preg_match("/.*select.*from.*/i" , $resultset)) { $this->AddSql($resultset); @@ -273,16 +274,17 @@ * as a normal resultset (mostly just below <root>) * * @param string filename + * @param mixed xpath either a string with the xpath expression or an array with "xpath"=>xpath expression and "root"=tag/subtag/etc, which are the tags to be inserted before the result * @access public * @see doXmlString2Xml() */ - function addXmlFile($file) + function addXmlFile($file,$xpath = Null) { $fd = fopen( $file, "r" ); $content = fread( $fd, filesize( $file ) ); fclose( $fd ); - $this->doXmlString2Xml($content); + $this->doXmlString2Xml($content,$xpath); } /** @@ -290,12 +292,13 @@ * as a normal resultset (mostly just below <root>) * * @param string xml + * @param mixed xpath either a string with the xpath expression or an array with "xpath"=>xpath expression and "root"=tag/subtag/etc, which are the tags to be inserted before the result * @access public * @see doXmlString2Xml() */ - function addXmlString($string) + function addXmlString($string,$xpath = Null) { - $this->doXmlString2Xml($string); + $this->doXmlString2Xml($string,$xpath); } /** @@ -320,10 +323,20 @@ */ function addSql($sql) { + /* if there are {} expressions in the sql query, we assume it's an xpath expression to + * be evaluated. + */ + if (preg_match_all ("/\{([^\}]+)\}/i",$sql,$matches)) + { + foreach ($matches[1] as $match) + { + $sql = preg_replace("#\{".preg_quote($match)."\}# ", $this->getXpathValue($match),$sql); + } + } $result = $this->db->query($sql); - //very strange + //very strange if (PEAR::isError($result->result)) { print "You have an SQL-Error:<br>".$result->result->userinfo; print "<br>"; @@ -455,7 +468,7 @@ //if you need more tableInfo for later use you can write a function addTableInfo.. $this->addTableInfo($key, $value, &$tableInfo); } - + // end initialize // if user made some own tableInfo data, merge them here. @@ -466,21 +479,21 @@ $parent['root'] = $this->insertNewResult(&$tableInfo); //initialize $resold to get rid of warning messages; - $resold[0] = "ThisValueIsImpossibleForTheFirstFieldInTheFirstRow"; + $resold[0] = "ThisValueIsImpossibleForTheFirstFieldInTheFirstRow"; 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) { if ($tableInfo["parent_key"][$tableInfo[$key]["table"]] == $key ) - { + { if ($this->nested || $key == 0) { @@ -501,7 +514,7 @@ } } - if ( $parent[$tableInfo[$key]["table"]] != Null) + if ( $parent[$tableInfo[$key]["table"]] != Null) { $this->insertNewElement($parent[$tableInfo[$key]["table"]], $res, $key, &$tableInfo, &$subrow); } @@ -583,20 +596,20 @@ { foreach ($options as $option => $value) { - if (isset($this->{$option})) + if (isset($this->{$option})) { - if (is_array($value) && ! $delete) + if (is_array($value) && ! $delete) { foreach ($value as $suboption => $subvalue) { $this->{$option}["$suboption"] = $subvalue; } } - else + else { - $this->$option = $value; + $this->$option = $value; } - } + } } } } @@ -617,7 +630,7 @@ else { $this->xmlroot = $this->xmldoc->add_root($this->tagNameResult); - //PHP 4.0.6 had $root->name as tagname, check for that here... + //PHP 4.0.6 had $root->name as tagname, check for that here... if (!isset($this->xmlroot->{$this->tagname})) { $this->tagname = "name"; @@ -678,7 +691,7 @@ // here come some helper functions... /** - * make utf8 out of the input data and escape & with & and "< " with "< " + * make utf8 out of the input data and escape & with & and "< " with "< " * (we assume that when there's no space after < it's a tag, which we need in the xml) * I'm not sure, if this is the standard way, but it works for me. * @@ -701,16 +714,16 @@ 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 + else { - $text = utf8_encode(ereg_replace("&","&",$text)); + $text = utf8_encode(ereg_replace("&","&",ereg_replace("< ","< ",$text))); } return $text; } @@ -751,31 +764,87 @@ /** * Adds a xml string to $this->xmldoc. * It's inserted on the same level as a "normal" resultset, means just as a children of <root> - * + * if a xpath expression is supplied, it takes that for selecting only part of the xml-file + * + * the clean code works only with php 4.0.7 + * for php4.0.6 : * I found no cleaner method than the below one. it's maybe nasty (xmlObject->string->xmlObject), * but it works. If someone knows how to add whole DomNodes to another one, let me know... * * @param string xml string + * @param mixed xpath either a string with the xpath expression or an array with "xpath"=>xpath expression and "root"=tag/subtag/etc, which are the tags to be inserted before the result * @acces private */ - function doXmlString2Xml ($string) + function doXmlString2Xml ($string,$xpath = Null) { - $MainXmlString = $this->xmldoc->dumpmem(); - - $string = preg_replace("/<\?xml.*\?>/","",$string); - - $MainXmlString = preg_replace("/<".$this->xmlroot->{$this->tagname}."\/>/","<".$this->xmlroot->{$this->tagname}."></".$this->xmlroot->{$this->tagname}.">",$MainXmlString); - $MainXmlString = preg_replace("/<\/".$this->xmlroot->{$this->tagname}.">/",$string."</".$this->xmlroot->{$this->tagname}.">",$MainXmlString); - - $this->xmldoc = xmldoc($MainXmlString); - $this->xmlroot = $this->xmldoc->root(); + + //check if we have a recent domxml. otherwise use the workaround... + $version = explode(".",phpversion()); + if (! ($version[0] <= 4 and $version[1] <= 0 and $version[2] < 7) ){ + + if (is_array($xpath)) + { + if (isset($xpath["root"])) + { + $root = $xpath["root"]; + } + $xpath = $xpath["xpath"]; + } + + $tmpxml = xmldoc($string); + $subroot = $this->xmlroot; + if (isset($root)) + { + $roots = explode("/",$root); + foreach ($roots as $rootelement) + { + if ( strlen($rootelement) > 0 ) + { + print "$rootelement<br>"; + $subroot = $subroot->new_child($rootelement,""); + } + } + } + + + //$this->xmlroot->addchild does some strange things when added nodes from xpath.... so this comment helps out + $newchild = $subroot->add_child($this->xmldoc->create_comment("the purpose of this comment was a workaround in sql2php.php line ".__LINE__)); + + + // if no xpath is given, just take the whole file + if ( (is_null($xpath))) + { + $newchild->append_child($tmpxml->root()); + } + else + { + $xctx = $tmpxml->xpath_new_context(); + $xnode = xpath_eval($xctx,$xpath); + debug::print_rp($xnode); + foreach ($xnode->nodeset as $node) + { + $newchild->append_child($node); + } + } + } + else { + $MainXmlString = $this->xmldoc->dumpmem(); + $string = preg_replace("/<\?xml.*\?>/","",$string); + + $MainXmlString = preg_replace("/<".$this->xmlroot->{$this->tagname}."\/>/","<".$this->xmlroot->{$this->tagname}."></".$this->xmlroot->{$this->tagname}.">",$MainXmlString); + $MainXmlString = preg_replace("/<\/".$this->xmlroot->{$this->tagname}.">/",$string."</".$this->xmlroot->{$this->tagname}.">",$MainXmlString); + + $this->xmldoc = xmldoc($MainXmlString); + $this->xmlroot = $this->xmldoc->root(); + + } } /** * sets the encoding for the db2xml transformation * @param string encoding to transform from - * @param string encoding to transform to + * @param string encoding to transform to * @acces public */ function setEncoding ($encoding_from = "ISO-8859-1", $encoding_to ="UTF-8") @@ -783,6 +852,58 @@ $this->encoding_from = $encoding_from; $this->encoding_to = $encoding_to; } - + + function SetParentTables($parentTables) + { + foreach ($parentTables as $table => $parent) + { + $table_info["parent_table"][$table]=$parent; + } + $this->SetOptions(array("user_tableInfo"=>$table_info)); + } + + // returns the content of the first match of the xpath expression + function getXpathValue ($expr) + { + + $xpth = $this->xmldoc->xpath_new_context(); + $xnode = xpath_eval($xpth,$expr); + + if (isset ($xnode->nodeset[0])) + { + $firstnode = $xnode->nodeset[0]; + + $children = $firstnode->children(); + $value = $children[0]->content; + return $value; + } + + else + { + return Null; + } + } + + // get the values as an array from the childtags from the first match of the xpath expression + function getXpathChildValues ($expr) + { + $xpth = $this->xmldoc->xpath_new_context(); + $xnode = xpath_eval($xpth,$expr); + + if (isset ($xnode->nodeset[0])) + { + foreach ($xnode->nodeset[0]->children() as $child) + { + $children = $child->children(); + $value[$child->{$this->tagname}] = $children[0]->content; + } + return $value; + } + else + { + return Null; + } + } + } ?> Index: pear/XML_sql2xml/sql2xml_ext.php diff -u pear/XML_sql2xml/sql2xml_ext.php:1.4 pear/XML_sql2xml/sql2xml_ext.php:1.5 --- pear/XML_sql2xml/sql2xml_ext.php:1.4 Thu Aug 16 13:12:44 2001 +++ pear/XML_sql2xml/sql2xml_ext.php Tue Aug 28 13:22:52 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml_ext.php,v 1.4 2001/08/16 17:12:44 chregu Exp $ +// $Id: sql2xml_ext.php,v 1.5 2001/08/28 17:22:52 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.4 2001/08/16 17:12:44 chregu Exp $ + * @version $Id: sql2xml_ext.php,v 1.5 2001/08/28 17:22:52 chregu Exp $ */ class XML_sql2xml_ext extends XML_sql2xml { @@ -94,7 +94,7 @@ 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->user_options['selected_id'] == "all" + || $this->user_options['selected_id'] == "all" || ($this->user_options['selected_id'] == "first") && !isset($this->table_selected[$tableInfo[$key]["table"]]) ) { @@ -128,7 +128,6 @@ else $xmlroot= $this->xmldoc->add_root($result_root); $this->SetAttribute($xmlroot,"type","resultset"); - return $xmlroot; } @@ -143,7 +142,7 @@ // initialize some variables to get rid of warning messages $beforetags = ""; $before[-1] = Null; - //the preg should be only done once... + //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); @@ -151,7 +150,7 @@ { $subrow[$regs[1][-1]] = $parent; } - else + else { $subrow[Null] = $parent; } @@ -176,7 +175,7 @@ } function addTableinfo($key, $value, &$tableInfo) { - + if (!isset($tableInfo['id'][$value["table"]]) && $value["name"] == $this->user_options["element_id"] ) { $tableInfo['id'][$value["table"]]= $key; @@ -194,6 +193,16 @@ } else { return $node->setattr($name,$value); + } + } + + + function SetResultRootTag ($resultroot) + { + if (isset($resultroot)) + { + $options = array("user_options" => array("result_root"=>$resultroot)); + $this->setoptions($options); } }
Index: pear/XML_sql2xml/package.xml diff -u pear/XML_sql2xml/package.xml:1.5 pear/XML_sql2xml/package.xml:1.6 --- pear/XML_sql2xml/package.xml:1.5 Fri Aug 17 11:27:00 2001 +++ pear/XML_sql2xml/package.xml Tue Aug 28 13:22:52 2001 @@ -1,18 +1,18 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> -<Package> - <Name>XML_sql2xml</Name> - <Summary>This class takes a PEAR::DB-Result Object, a sql-query-string, an array and/or an xml-string/file and returns a xml-representation of it.</Summary> - <Maintainer> - <Initials>cs</Initials> - <Name>Christian Stocker</Name> - <Email>chregu@php.net</Email> - </Maintainer> - <Release> - <Version>0.3</Version> - <Date>2001-08-17</Date> - <Notes> +<package> + <name>XML_sql2xml</name> + <summary>This class takes a PEAR::DB-Result Object, a sql-query-string, an array and/or an xml-string/file and returns a xml-representation of it.</summary> + <maintainer> + <initials>cs</initials> + <name>Christian Stocker</name> + <email>chregu@php.net</email> + </maintainer> + <release> + <version>0.3</version> + <date>2001-08-17</date> + <notes> New Features: - works with PHP 4.0.7 and below - Adding of XML-files/strings is possible @@ -23,13 +23,14 @@ - user_option selected_id can take the arguments first and all BugFixes - sql selects are recognized with capitals as well - - if there was a < in the database it gave an xml-error, now "< " is "escaped" - </Notes> - </Release> - <FileList> - <Dir Name="XML" BaseInstallDir="XML" > - <File Role="php">sql2xml.php</File> - <File Role="php">sql2xml_ext.php</File> - </Dir> - </FileList> -</Package> + - if there was a [lesserthan] in the database it gave an xml-error, now "[lesserthan] " is "escaped" + - xpath support for sql-queries and xml-files to be inserted + </notes> + </release> + <filelist> + <dir name="XML" baseinstalldir="XML"> + <file role="php">sql2xml.php</file> + <file role="php">sql2xml_ext.php</file> + </dir> + </filelist> +</package> Index: pear/XML_sql2xml/sql2xml.php diff -u pear/XML_sql2xml/sql2xml.php:1.5 pear/XML_sql2xml/sql2xml.php:1.6 --- pear/XML_sql2xml/sql2xml.php:1.5 Fri Aug 17 11:25:07 2001 +++ pear/XML_sql2xml/sql2xml.php Tue Aug 28 13:22:52 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml.php,v 1.5 2001/08/17 15:25:07 chregu Exp $ +// $Id: sql2xml.php,v 1.6 2001/08/28 17:22:52 chregu Exp $ /** * This class takes a PEAR::DB-Result Object, a sql-query-string or an array @@ -48,7 +48,7 @@ * http://php.chregu.tv/sql2xml * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: sql2xml.php,v 1.5 2001/08/17 15:25:07 chregu Exp $ +* @version $Id: sql2xml.php,v 1.6 2001/08/28 17:22:52 chregu Exp $ * @package XML */ class XML_sql2xml { @@ -92,7 +92,7 @@ /** * Options to be used in extended Classes (for example in sql2xml_ext). - * They are passed with SetOptions as an array (arrary("user_optpaions" = array()); + * They are passed with SetOptions as an array (arrary("user_options" = array()); * and can then be accessed with $this->user_options["bla"] from your * extended classes for additional features. * This array is not use in this base class, it's only for passing easy parameters @@ -161,16 +161,16 @@ /** * the encoding type, the input from the db has */ - var $encoding_from = "ISO-8859-1"; + var $encoding_from = "ISO-8859-1"; /** - * the encoding type, the output in the xml should have + * 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"; var $tagname = "tagname"; - + /** * Constructor * The Constructor can take a Pear::DB "data source name" (eg. @@ -220,10 +220,10 @@ //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); - //PHP 4.0.6 had $root->name as tagname, check for that here... + //PHP 4.0.6 had $root->name as tagname, check for that here... if (!isset($this->xmlroot->{$this->tagname})) { $this->tagname = "name"; @@ -239,16 +239,17 @@ * input and adds this to $this->xmldoc * * @param $resultset string sql-string, or object db_result, or array + * @param $param mixed additional parameters for the following functions * @access public - * @see addResult(), addSql(), addArray() + * @see addResult(), addSql(), addArray(), addXmlFile() */ - function add ($resultset) + function add ($resultset,$params=Null) { // if string, then it's a query, a xml-file or a xml-string... if (is_string($resultset)) { if (preg_match("/\.xml$/",$resultset)) { - $this->AddXmlFile($resultset); + $this->AddXmlFile($resultset,$params); } elseif (preg_match("/.*select.*from.*/i" , $resultset)) { $this->AddSql($resultset); @@ -273,16 +274,17 @@ * as a normal resultset (mostly just below <root>) * * @param string filename + * @param mixed xpath either a string with the xpath expression or an array with "xpath"=>xpath expression and "root"=tag/subtag/etc, which are the tags to be inserted before the result * @access public * @see doXmlString2Xml() */ - function addXmlFile($file) + function addXmlFile($file,$xpath = Null) { $fd = fopen( $file, "r" ); $content = fread( $fd, filesize( $file ) ); fclose( $fd ); - $this->doXmlString2Xml($content); + $this->doXmlString2Xml($content,$xpath); } /** @@ -290,12 +292,13 @@ * as a normal resultset (mostly just below <root>) * * @param string xml + * @param mixed xpath either a string with the xpath expression or an array with "xpath"=>xpath expression and "root"=tag/subtag/etc, which are the tags to be inserted before the result * @access public * @see doXmlString2Xml() */ - function addXmlString($string) + function addXmlString($string,$xpath = Null) { - $this->doXmlString2Xml($string); + $this->doXmlString2Xml($string,$xpath); } /** @@ -320,10 +323,20 @@ */ function addSql($sql) { + /* if there are {} expressions in the sql query, we assume it's an xpath expression to + * be evaluated. + */ + if (preg_match_all ("/\{([^\}]+)\}/i",$sql,$matches)) + { + foreach ($matches[1] as $match) + { + $sql = preg_replace("#\{".preg_quote($match)."\}# ", $this->getXpathValue($match),$sql); + } + } $result = $this->db->query($sql); - //very strange + //very strange if (PEAR::isError($result->result)) { print "You have an SQL-Error:<br>".$result->result->userinfo; print "<br>"; @@ -455,7 +468,7 @@ //if you need more tableInfo for later use you can write a function addTableInfo.. $this->addTableInfo($key, $value, &$tableInfo); } - + // end initialize // if user made some own tableInfo data, merge them here. @@ -466,21 +479,21 @@ $parent['root'] = $this->insertNewResult(&$tableInfo); //initialize $resold to get rid of warning messages; - $resold[0] = "ThisValueIsImpossibleForTheFirstFieldInTheFirstRow"; + $resold[0] = "ThisValueIsImpossibleForTheFirstFieldInTheFirstRow"; 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) { if ($tableInfo["parent_key"][$tableInfo[$key]["table"]] == $key ) - { + { if ($this->nested || $key == 0) { @@ -501,7 +514,7 @@ } } - if ( $parent[$tableInfo[$key]["table"]] != Null) + if ( $parent[$tableInfo[$key]["table"]] != Null) { $this->insertNewElement($parent[$tableInfo[$key]["table"]], $res, $key, &$tableInfo, &$subrow); } @@ -583,20 +596,20 @@ { foreach ($options as $option => $value) { - if (isset($this->{$option})) + if (isset($this->{$option})) { - if (is_array($value) && ! $delete) + if (is_array($value) && ! $delete) { foreach ($value as $suboption => $subvalue) { $this->{$option}["$suboption"] = $subvalue; } } - else + else { - $this->$option = $value; + $this->$option = $value; } - } + } } } } @@ -617,7 +630,7 @@ else { $this->xmlroot = $this->xmldoc->add_root($this->tagNameResult); - //PHP 4.0.6 had $root->name as tagname, check for that here... + //PHP 4.0.6 had $root->name as tagname, check for that here... if (!isset($this->xmlroot->{$this->tagname})) { $this->tagname = "name"; @@ -678,7 +691,7 @@ // here come some helper functions... /** - * make utf8 out of the input data and escape & with & and "< " with "< " + * make utf8 out of the input data and escape & with & and "< " with "< " * (we assume that when there's no space after < it's a tag, which we need in the xml) * I'm not sure, if this is the standard way, but it works for me. * @@ -701,16 +714,16 @@ 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 + else { - $text = utf8_encode(ereg_replace("&","&",$text)); + $text = utf8_encode(ereg_replace("&","&",ereg_replace("< ","< ",$text))); } return $text; } @@ -751,31 +764,87 @@ /** * Adds a xml string to $this->xmldoc. * It's inserted on the same level as a "normal" resultset, means just as a children of <root> - * + * if a xpath expression is supplied, it takes that for selecting only part of the xml-file + * + * the clean code works only with php 4.0.7 + * for php4.0.6 : * I found no cleaner method than the below one. it's maybe nasty (xmlObject->string->xmlObject), * but it works. If someone knows how to add whole DomNodes to another one, let me know... * * @param string xml string + * @param mixed xpath either a string with the xpath expression or an array with "xpath"=>xpath expression and "root"=tag/subtag/etc, which are the tags to be inserted before the result * @acces private */ - function doXmlString2Xml ($string) + function doXmlString2Xml ($string,$xpath = Null) { - $MainXmlString = $this->xmldoc->dumpmem(); - - $string = preg_replace("/<\?xml.*\?>/","",$string); - - $MainXmlString = preg_replace("/<".$this->xmlroot->{$this->tagname}."\/>/","<".$this->xmlroot->{$this->tagname}."></".$this->xmlroot->{$this->tagname}.">",$MainXmlString); - $MainXmlString = preg_replace("/<\/".$this->xmlroot->{$this->tagname}.">/",$string."</".$this->xmlroot->{$this->tagname}.">",$MainXmlString); - - $this->xmldoc = xmldoc($MainXmlString); - $this->xmlroot = $this->xmldoc->root(); + + //check if we have a recent domxml. otherwise use the workaround... + $version = explode(".",phpversion()); + if (! ($version[0] <= 4 and $version[1] <= 0 and $version[2] < 7) ){ + + if (is_array($xpath)) + { + if (isset($xpath["root"])) + { + $root = $xpath["root"]; + } + $xpath = $xpath["xpath"]; + } + + $tmpxml = xmldoc($string); + $subroot = $this->xmlroot; + if (isset($root)) + { + $roots = explode("/",$root); + foreach ($roots as $rootelement) + { + if ( strlen($rootelement) > 0 ) + { + print "$rootelement<br>"; + $subroot = $subroot->new_child($rootelement,""); + } + } + } + + + //$this->xmlroot->addchild does some strange things when added nodes from xpath.... so this comment helps out + $newchild = $subroot->add_child($this->xmldoc->create_comment("the purpose of this comment was a workaround in sql2php.php line ".__LINE__)); + + + // if no xpath is given, just take the whole file + if ( (is_null($xpath))) + { + $newchild->append_child($tmpxml->root()); + } + else + { + $xctx = $tmpxml->xpath_new_context(); + $xnode = xpath_eval($xctx,$xpath); + debug::print_rp($xnode); + foreach ($xnode->nodeset as $node) + { + $newchild->append_child($node); + } + } + } + else { + $MainXmlString = $this->xmldoc->dumpmem(); + $string = preg_replace("/<\?xml.*\?>/","",$string); + + $MainXmlString = preg_replace("/<".$this->xmlroot->{$this->tagname}."\/>/","<".$this->xmlroot->{$this->tagname}."></".$this->xmlroot->{$this->tagname}.">",$MainXmlString); + $MainXmlString = preg_replace("/<\/".$this->xmlroot->{$this->tagname}.">/",$string."</".$this->xmlroot->{$this->tagname}.">",$MainXmlString); + + $this->xmldoc = xmldoc($MainXmlString); + $this->xmlroot = $this->xmldoc->root(); + + } } /** * sets the encoding for the db2xml transformation * @param string encoding to transform from - * @param string encoding to transform to + * @param string encoding to transform to * @acces public */ function setEncoding ($encoding_from = "ISO-8859-1", $encoding_to ="UTF-8") @@ -783,6 +852,58 @@ $this->encoding_from = $encoding_from; $this->encoding_to = $encoding_to; } - + + function SetParentTables($parentTables) + { + foreach ($parentTables as $table => $parent) + { + $table_info["parent_table"][$table]=$parent; + } + $this->SetOptions(array("user_tableInfo"=>$table_info)); + } + + // returns the content of the first match of the xpath expression + function getXpathValue ($expr) + { + + $xpth = $this->xmldoc->xpath_new_context(); + $xnode = xpath_eval($xpth,$expr); + + if (isset ($xnode->nodeset[0])) + { + $firstnode = $xnode->nodeset[0]; + + $children = $firstnode->children(); + $value = $children[0]->content; + return $value; + } + + else + { + return Null; + } + } + + // get the values as an array from the childtags from the first match of the xpath expression + function getXpathChildValues ($expr) + { + $xpth = $this->xmldoc->xpath_new_context(); + $xnode = xpath_eval($xpth,$expr); + + if (isset ($xnode->nodeset[0])) + { + foreach ($xnode->nodeset[0]->children() as $child) + { + $children = $child->children(); + $value[$child->{$this->tagname}] = $children[0]->content; + } + return $value; + } + else + { + return Null; + } + } + } ?> Index: pear/XML_sql2xml/sql2xml_ext.php diff -u pear/XML_sql2xml/sql2xml_ext.php:1.4 pear/XML_sql2xml/sql2xml_ext.php:1.5 --- pear/XML_sql2xml/sql2xml_ext.php:1.4 Thu Aug 16 13:12:44 2001 +++ pear/XML_sql2xml/sql2xml_ext.php Tue Aug 28 13:22:52 2001 @@ -15,7 +15,7 @@ // | Authors: Christian Stocker <chregu@phant.ch> | // +----------------------------------------------------------------------+ // -// $Id: sql2xml_ext.php,v 1.4 2001/08/16 17:12:44 chregu Exp $ +// $Id: sql2xml_ext.php,v 1.5 2001/08/28 17:22:52 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.4 2001/08/16 17:12:44 chregu Exp $ + * @version $Id: sql2xml_ext.php,v 1.5 2001/08/28 17:22:52 chregu Exp $ */ class XML_sql2xml_ext extends XML_sql2xml { @@ -94,7 +94,7 @@ 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->user_options['selected_id'] == "all" + || $this->user_options['selected_id'] == "all" || ($this->user_options['selected_id'] == "first") && !isset($this->table_selected[$tableInfo[$key]["table"]]) ) { @@ -128,7 +128,6 @@ else $xmlroot= $this->xmldoc->add_root($result_root); $this->SetAttribute($xmlroot,"type","resultset"); - return $xmlroot; } @@ -143,7 +142,7 @@ // initialize some variables to get rid of warning messages $beforetags = ""; $before[-1] = Null; - //the preg should be only done once... + //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); @@ -151,7 +150,7 @@ { $subrow[$regs[1][-1]] = $parent; } - else + else { $subrow[Null] = $parent; } @@ -176,7 +175,7 @@ } function addTableinfo($key, $value, &$tableInfo) { - + if (!isset($tableInfo['id'][$value["table"]]) && $value["name"] == $this->user_options["element_id"] ) { $tableInfo['id'][$value["table"]]= $key; @@ -194,6 +193,16 @@ } else { return $node->setattr($name,$value); + } + } + + + function SetResultRootTag ($resultroot) + { + if (isset($resultroot)) + { + $options = array("user_options" => array("result_root"=>$resultroot)); + $this->setoptions($options); } }