Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 235 lines (204 sloc) 9.18 kB
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
1 <?php
2 // +----------------------------------------------------------------------+
e17d2f6 Update headers.
Sebastian Bergmann authored
3 // | PHP Version 4 |
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
4 // +----------------------------------------------------------------------+
e17d2f6 Update headers.
Sebastian Bergmann authored
5 // | Copyright (c) 1997-2002 The PHP Group |
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/2_02.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+
720f92b @chregu - it's possible to add an xml-file
chregu authored
15 // | Authors: Christian Stocker <chregu@phant.ch> |
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
16 // +----------------------------------------------------------------------+
17 //
18 // $Id$
19
20 require_once ("XML/sql2xml.php");
21
22 /**
720f92b @chregu - it's possible to add an xml-file
chregu authored
23 * This class shows with an example, how the base sql2xml-class
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
24 * could be extended.
25 *
26 * Usage example
27 *
28 * include_once("XML/sql2xml_ext.php");
29 * $options= array( user_options => array (xml_seperator =>"_",
30 * element_id => "id"),
31 * );
720f92b @chregu - it's possible to add an xml-file
chregu authored
32 * $sql2xml = new xml_sql2xml_ext("mysql://root@localhost/xmltest");
33 * $sql2xml->SetOptions($options);
34 * $xmlstring = $sql2xml->getxml("select * from bands");
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
35
36 * more examples and outputs on
720f92b @chregu - it's possible to add an xml-file
chregu authored
37 * http://php.chregu.tv/sql2xml/
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
38 * for the time being
39 *
40 * @author Christian Stocker <chregu@nomad.ch>
41 * @version $Id$
42 */
43 class XML_sql2xml_ext extends XML_sql2xml {
44
7860cfe @chregu some phpdoc fixes
chregu authored
45 /**
46 * Constructor
47 * The Constructor can take a Pear::DB "data source name" (eg.
48 * "mysql://user:passwd@localhost/dbname") and will then connect
49 * to the DB, or a PEAR::DB object link, if you already connected
50 * the db before.
51 " If you provide nothing as $dsn, you only can later add stuff with
52 * a pear::db-resultset or as an array. providing sql-strings will
53 * not work.
54 * the $root param is used, if you want to provide another name for your
55 * root-tag than "root". if you give an empty string (""), there will be no
56 * root element created here, but only when you add a resultset/array/sql-string.
57 * And the first tag of this result is used as the root tag.
58 *
59 * @param string with PEAR::DB "data source name" or object DB object
60 * @param string of the name of the xml-doc root element.
61 * @access public
62 * @see XML_sql2xml::XML_sql2xml()
63 */
720f92b @chregu - it's possible to add an xml-file
chregu authored
64 function XML_sql2xml_ext ($dsn=Null,$root = "root")
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
65 {
720f92b @chregu - it's possible to add an xml-file
chregu authored
66 $this->XML_sql2xml($dsn,$root);
67 // DefaultValues for user_options
68
69 $user_options = array (
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
70 'xml_seperator' =>"_",
71 'element_id' => "ID",
37ce3f1 @chregu for sql2xml.php
chregu authored
72 'print_empty_ids' => True,
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
73 'selected_id' => array(),
74 'field_translate' => array(),
37ce3f1 @chregu for sql2xml.php
chregu authored
75 'attributes' => array(),
76 'TableNameForRowTags' => True
720f92b @chregu - it's possible to add an xml-file
chregu authored
77 );
78
79 $this->setOptions(array("user_options"=>$user_options));
80
81 }
c7f7ef6 @chregu missing /* caused an error
chregu authored
82 /*
7860cfe @chregu some phpdoc fixes
chregu authored
83 * @param $dsn string with PEAR::DB "data source name" or object DB object
84 * @param $root string of the name of the xml-doc root element.
85 * @access public
86 * @see XML_sql2xml::XML_sql2xml()
87 */
88
720f92b @chregu - it's possible to add an xml-file
chregu authored
89 function insertNewRow ($parent_row, $res, $key, &$tableInfo)
90 {
37ce3f1 @chregu for sql2xml.php
chregu authored
91
92 if (!$tableInfo[$key]["table"] ) {
720f92b @chregu - it's possible to add an xml-file
chregu authored
93 $tableInfo[$key]["table"] = $this->tagNameResult;
94 }
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
95 if ($this->user_options['element_id'] && !$res[$tableInfo["id"][$tableInfo[$key]["table"]]] && !$this->user_options['print_empty_ids'])
720f92b @chregu - it's possible to add an xml-file
chregu authored
96 {
97 return Null;
98 }
37ce3f1 @chregu for sql2xml.php
chregu authored
99 if ( !$this->user_options['TableNameForRowTags'])
100 {
101 $new_row= $parent_row->new_child($this->tagNameRow,Null);
102 }
103 else
104 {
105 $new_row= $parent_row->new_child($tableInfo[$key]["table"],Null);
106 }
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
107 /* make an unique ID attribute in the row element with tablename.id if there's an id
108 otherwise just make an unique id with the php-function, just that there's a unique id for this row.
109 CAUTION: This ID changes every time ;) (if no id from db-table)
110 */
720f92b @chregu - it's possible to add an xml-file
chregu authored
111 $this->SetAttribute($new_row,"type","row");
112
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
113 if ($res[$tableInfo["id"][$tableInfo[$key]["table"]]])
114 {
720f92b @chregu - it's possible to add an xml-file
chregu authored
115 /* make attribute selected if ID = selected_id OR tableName.ID = selected_id. for the second case
116 you can give an array for multiple selected entries */
117
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
118 if ($res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options['selected_id']
119 || $tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]] == $this->user_options['selected_id']
37ce3f1 @chregu for sql2xml.php
chregu authored
120 || (is_array($this->user_options['selected_id']) && in_array($tableInfo[$key]["table"].$res[$tableInfo["id"][$tableInfo[$key]["table"]]],$this->user_options['selected_id']))
44a6576 @chregu - xpath support for sql-queries and xml-files/strings
chregu authored
121 || $this->user_options['selected_id'] == "all"
37ce3f1 @chregu for sql2xml.php
chregu authored
122 || ($this->user_options['selected_id'] == "first") && !isset($this->table_selected[$tableInfo[$key]["table"]])
123 )
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
124 {
720f92b @chregu - it's possible to add an xml-file
chregu authored
125 $this->SetAttribute($new_row,"selected", "selected");
37ce3f1 @chregu for sql2xml.php
chregu authored
126 $this->table_selected[$tableInfo[$key]["table"]] = True;
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
127 }
720f92b @chregu - it's possible to add an xml-file
chregu authored
128 $this->SetAttribute($new_row,"ID", utf8_encode($tableInfo[$key]["table"] . $res[$tableInfo["id"][$tableInfo[$key]["table"]]]));
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
129 }
130 else
131 {
132 $this->IDcounter[$tableInfo[$key]["table"]]++;
720f92b @chregu - it's possible to add an xml-file
chregu authored
133 $this->SetAttribute($new_row,"ID", $tableInfo[$key]["table"].$this->IDcounter[$tableInfo[$key]["table"]]);
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
134
135 }
136
137 return $new_row;
138 }
139
140
720f92b @chregu - it's possible to add an xml-file
chregu authored
141 function insertNewResult (&$tableInfo) {
142
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
143 if (isset($this->user_options["result_root"]))
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
144 $result_root = $this->user_options["result_root"];
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
145 elseif (isset($tableInfo[0]["table"]))
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
146 $result_root = $tableInfo[0]["table"];
720f92b @chregu - it's possible to add an xml-file
chregu authored
147 else
148 $result_root = "resultset";
149
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
150 if ($this->xmlroot)
151 $xmlroot=$this->xmlroot->new_child($result_root,Null);
152 else
153 $xmlroot= $this->xmldoc->add_root($result_root);
720f92b @chregu - it's possible to add an xml-file
chregu authored
154 $this->SetAttribute($xmlroot,"type","resultset");
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
155 return $xmlroot;
156 }
157
720f92b @chregu - it's possible to add an xml-file
chregu authored
158
159 function insertNewElement ($parent, $res, $key, &$tableInfo, &$subrow) {
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
160
161 if (is_array($this->user_options["attributes"]) && in_array($tableInfo[$key]["name"],$this->user_options["attributes"])) {
720f92b @chregu - it's possible to add an xml-file
chregu authored
162 $subrow=$this->SetAttribute($parent,$tableInfo[$key]["name"],$this->xml_encode($res[$key]));
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
163 }
164 elseif ($this->user_options["xml_seperator"])
165 {
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
166 // initialize some variables to get rid of warning messages
167 $beforetags = "";
168 $before[-1] = Null;
44a6576 @chregu - xpath support for sql-queries and xml-files/strings
chregu authored
169 //the preg should be only done once...
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
170 $i = 0;
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
171 preg_match_all("/([^" . $this->user_options["xml_seperator"] . "]+)" . $this->user_options['xml_seperator'] . "*/", $tableInfo[$key]["name"], $regs);
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
172
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
173 if (isset($regs[1][-1]))
174 {
175 $subrow[$regs[1][-1]] = $parent;
176 }
44a6576 @chregu - xpath support for sql-queries and xml-files/strings
chregu authored
177 else
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
178 {
179 $subrow[Null] = $parent;
180 }
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
181 // here we separate db fields to subtags.
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
182
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
183 for ($i = 0; $i < (count($regs[1]) - 1); $i++)
184 {
720f92b @chregu - it's possible to add an xml-file
chregu authored
185 $beforetags .=$regs[1][$i]."_";
186 $before[$i] = $beforetags;
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
187 if ( ! isset($subrow[$before[$i]]) ) {
720f92b @chregu - it's possible to add an xml-file
chregu authored
188 $subrow[$before[$i]] = $subrow[$before[$i - 1]]->new_child($regs[1][$i], NULL);
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
189 }
190 }
720f92b @chregu - it's possible to add an xml-file
chregu authored
191 $subrows = $subrow[$before[$i - 1]]->new_child($regs[1][$i], $this->xml_encode($res[$key]));
192
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
193 }
194 else
195 {
196 $subrow=$parent->new_child($tableInfo[$key]["name"], $this->xml_encode($res[$key]));
197 }
198
199 }
200
201 function addTableinfo($key, $value, &$tableInfo) {
44a6576 @chregu - xpath support for sql-queries and xml-files/strings
chregu authored
202
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
203 if (!isset($tableInfo['id'][$value["table"]]) && $value["name"] == $this->user_options["element_id"] )
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
204 {
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
205 $tableInfo['id'][$value["table"]]= $key;
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
206 }
f1934eb @chregu - added possibility of other encoding schemes than iso-8859-1 (needs …
chregu authored
207 if (isset($this->user_options["field_translate"][$value["name"]])) {
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
208 $tableInfo[$key]["name"] = $this->user_options["field_translate"][$value["name"]];
209 }
210 }
720f92b @chregu - it's possible to add an xml-file
chregu authored
211
212 // A wrapper for set setattr/set_attribute, since the function changed in php 4.0.6...
213 function SetAttribute ($node,$name,$value) {
214 if (method_exists($node,"Set_attribute"))
215 {
216 return $node->Set_Attribute($name,$value);
217 }
218 else {
219 return $node->setattr($name,$value);
220 }
221 }
222
44a6576 @chregu - xpath support for sql-queries and xml-files/strings
chregu authored
223
224 function SetResultRootTag ($resultroot)
225 {
226 if (isset($resultroot))
227 {
228 $options = array("user_options" => array("result_root"=>$resultroot));
229 $this->setoptions($options);
230 }
231 }
232
db8d791 @chregu Moved from /php4/pear/Experimental to here
chregu authored
233 }
234 ?>
Something went wrong with that request. Please try again.