| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4: */
|
|
|
3 |
// +----------------------------------------------------------------------+
|
|
|
4 |
// | PHP Version 4 |
|
|
|
5 |
// +----------------------------------------------------------------------+
|
|
|
6 |
// | Copyright (c) 1997-2003 The PHP Group |
|
|
|
7 |
// +----------------------------------------------------------------------+
|
|
|
8 |
// | This source file is subject to version 2.02 of the PHP license, |
|
|
|
9 |
// | that is bundled with this package in the file LICENSE, and is |
|
|
|
10 |
// | available at through the world-wide-web at |
|
|
|
11 |
// | http://www.php.net/license/2_02.txt. |
|
|
|
12 |
// | If you did not receive a copy of the PHP license and are unable to |
|
|
|
13 |
// | obtain it through the world-wide-web, please send a note to |
|
|
|
14 |
// | license@php.net so we can mail you a copy immediately. |
|
|
|
15 |
// +----------------------------------------------------------------------+
|
|
|
16 |
// | Authors: Wolfram Kriesing <wolfram@kriesing.de> |
|
|
|
17 |
// +----------------------------------------------------------------------+
|
|
|
18 |
//
|
|
|
19 |
// $Id: XML.php,v 1.11.2.1 2009/03/12 17:19:49 dufuz Exp $
|
|
|
20 |
|
|
|
21 |
require_once 'XML/Parser.php';
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* the XML interface for the tree class
|
|
|
25 |
*
|
|
|
26 |
* @package Tree
|
|
|
27 |
* @author
|
|
|
28 |
* @version
|
|
|
29 |
* @access public
|
|
|
30 |
*/
|
|
|
31 |
class Tree_Memory_XML extends XML_Parser
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @var array the first element has to be empty, so we can use
|
|
|
36 |
* the parentId=0 as "no parent"
|
|
|
37 |
*/
|
|
|
38 |
var $data = array(0 => null);
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @var integer $level
|
|
|
42 |
*/
|
|
|
43 |
var $level = 0;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @var array $parentIdOnLevel
|
|
|
47 |
*/
|
|
|
48 |
var $parentIdOnLevel = array();
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @var boolean set case folding for the XML_Parser to false
|
|
|
52 |
*/
|
|
|
53 |
var $folding = false; // turn off case folding
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @var boolean if true it converts all attributes and tag names etc
|
|
|
57 |
* to lower case this is default, since i dont see no way
|
|
|
58 |
* of case insensitive comparison in the tree class, since
|
|
|
59 |
* you can access the internal data directly or you get
|
|
|
60 |
* them returned I know this is not 100% proper OOP but that's
|
|
|
61 |
* how it is right now.
|
|
|
62 |
*/
|
|
|
63 |
var $_toLower = true;
|
|
|
64 |
|
|
|
65 |
// {{{ Tree_Memory_XML()
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
*
|
|
|
69 |
*
|
|
|
70 |
* @version 2002/01/17
|
|
|
71 |
* @access public
|
|
|
72 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
73 |
* @return boolean true on success
|
|
|
74 |
*/
|
|
|
75 |
function Tree_Memory_XML($dsn, $options)
|
|
|
76 |
{
|
|
|
77 |
$handle = $dsn;
|
|
|
78 |
|
|
|
79 |
$this->XML_Parser();
|
|
|
80 |
|
|
|
81 |
if (@is_resource($handle)) {
|
|
|
82 |
$this->setInput($handle);
|
|
|
83 |
} elseif ($handle != '') {
|
|
|
84 |
$this->setInputFile($handle);
|
|
|
85 |
} else {
|
|
|
86 |
return $this->raiseError('No filename passed.');
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
// }}}
|
|
|
91 |
// {{{ startHandler()
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
*
|
|
|
95 |
*
|
|
|
96 |
* @version 2002/01/17
|
|
|
97 |
* @access public
|
|
|
98 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
99 |
* @return boolean true on success
|
|
|
100 |
*/
|
|
|
101 |
function startHandler($parser, $element, $attribs)
|
|
|
102 |
{
|
|
|
103 |
$elementBeforeId = sizeof($this->data) - 1;
|
|
|
104 |
$curId = sizeof($this->data);
|
|
|
105 |
|
|
|
106 |
$this->data[$curId]['id'] = $curId;
|
|
|
107 |
$this->data[$curId]['name'] = $this->_toLower?
|
|
|
108 |
strtolower($element):$element;
|
|
|
109 |
$this->data[$curId]['level'] = $this->level;
|
|
|
110 |
$this->data[$curId]['attributes'] = $attribs;
|
|
|
111 |
if ($this->_toLower) {
|
|
|
112 |
$this->data[$curId]['attributes'] = array();
|
|
|
113 |
foreach($attribs as $key => $value) {
|
|
|
114 |
$this->data[$curId]['attributes'][strtolower($key)] = $value;
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
// is that a new child, or just a 'next' of a child?
|
|
|
119 |
if (isset($this->data[$elementBeforeId]['level']) &&
|
|
|
120 |
$this->level == $this->data[$elementBeforeId]['level']) {
|
|
|
121 |
$this->data[$curId]['parentId'] =
|
|
|
122 |
$this->data[$elementBeforeId]['parentId'];
|
|
|
123 |
} else {
|
|
|
124 |
// set stuff for the first child !!!
|
|
|
125 |
// the root has no parent
|
|
|
126 |
if ($this->level > 0) {
|
|
|
127 |
$parentId = $this->parentIdOnLevel[$this->level-1];
|
|
|
128 |
$this->data[$curId]['parentId'] = $parentId;
|
|
|
129 |
} else {
|
|
|
130 |
$this->data[$curId]['parentId'] = 0;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
$this->parentIdOnLevel[$this->level] = $curId;
|
|
|
134 |
$this->level++;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// }}}
|
|
|
138 |
// {{{ endHandler()
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
*
|
|
|
142 |
*
|
|
|
143 |
* @version 2002/01/17
|
|
|
144 |
* @access public
|
|
|
145 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
146 |
* @return boolean true on success
|
|
|
147 |
*/
|
|
|
148 |
function endHandler($parser, $element)
|
|
|
149 |
{
|
|
|
150 |
$this->level--;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// }}}
|
|
|
154 |
// {{{ cdataHandler()
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
*
|
|
|
158 |
*
|
|
|
159 |
* @version 2002/01/17
|
|
|
160 |
* @access public
|
|
|
161 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
162 |
* @return boolean true on success
|
|
|
163 |
*/
|
|
|
164 |
function cdataHandler($parser, $cdata)
|
|
|
165 |
{
|
|
|
166 |
# QUESTION: why is this method called multiple times for one element?
|
|
|
167 |
# is every space a cdata ???
|
|
|
168 |
# ANSWER: if you call xml_parse($parser, "foo ", false) and then
|
|
|
169 |
# xml_parse($parser, "bar", true), callbacks are done once
|
|
|
170 |
# for each xml_parse() call.
|
|
|
171 |
if (!isset($this->data[ sizeof($this->data)-1 ]['cdata'])) {
|
|
|
172 |
$this->data[ sizeof($this->data)-1 ]['cdata'] = '';
|
|
|
173 |
}
|
|
|
174 |
#print "cdata = '$cdata'\r\n";
|
|
|
175 |
$this->data[ sizeof($this->data)-1 ]['cdata'].= $cdata;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
// }}}
|
|
|
179 |
// {{{ defaultHandler()
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
*
|
|
|
183 |
*
|
|
|
184 |
* @version 2002/01/17
|
|
|
185 |
* @access public
|
|
|
186 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
187 |
* @return boolean true on success
|
|
|
188 |
*/
|
|
|
189 |
function defaultHandler($parser, $cdata)
|
|
|
190 |
{
|
|
|
191 |
// $this->data[ sizeof($this->data)-1 ]['cdata'] = $cdata;
|
|
|
192 |
// not in use yet :-( is that ok??
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
// }}}
|
|
|
196 |
// {{{ setup()
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* read the data from the xml file and prepare them so the tree
|
|
|
200 |
* class can work with it, the preparation is mainly done in startHandler
|
|
|
201 |
*
|
|
|
202 |
* @version 2002/01/17
|
|
|
203 |
* @access public
|
|
|
204 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
205 |
* @return boolean true on success
|
|
|
206 |
*/
|
|
|
207 |
function setup()
|
|
|
208 |
{
|
|
|
209 |
$this->parse();
|
|
|
210 |
|
|
|
211 |
return $this->data;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
// }}}
|
|
|
215 |
// {{{ setupByRawData()
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* read the data from an xml string and prepare them so the tree
|
|
|
219 |
* class can work with it, the preparation is mainly done in startHandler
|
|
|
220 |
*
|
|
|
221 |
* @version 2002/02/05
|
|
|
222 |
* @access public
|
|
|
223 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
224 |
* @return boolean true on success
|
|
|
225 |
*/
|
|
|
226 |
function setupByRawData($xmlString)
|
|
|
227 |
{
|
|
|
228 |
$this->parseString($xmlString, true);
|
|
|
229 |
|
|
|
230 |
return $this->data;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
// }}}
|
|
|
234 |
// {{{ add()
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* TO BE IMPLEMNTED
|
|
|
238 |
* adds _one_ new element in the tree under the given parent
|
|
|
239 |
* the values' keys given have to match the db-columns, because the
|
|
|
240 |
* value gets inserted in the db directly
|
|
|
241 |
* to add an entire node containing children and so on see 'addNode()'
|
|
|
242 |
*
|
|
|
243 |
* @see addNode()
|
|
|
244 |
* @version 2001/10/09
|
|
|
245 |
* @access public
|
|
|
246 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
247 |
* @param array this array contains the values that shall be
|
|
|
248 |
* inserted in the db-table
|
|
|
249 |
* @return mixed either boolean false on failure or the id
|
|
|
250 |
* of the inserted row
|
|
|
251 |
*/
|
|
|
252 |
/* function add($newValues)
|
|
|
253 |
{
|
|
|
254 |
// add the data in the internal structure $this->data
|
|
|
255 |
$this->data[sizeof($this->data)] = $newValues;
|
|
|
256 |
|
|
|
257 |
# i am thinking if it might be a good solution to walk the data-array
|
|
|
258 |
# and write each line singlely until the one to add comes, write it and
|
|
|
259 |
# keep on writing the data-array
|
|
|
260 |
# but that means writing the entire file every time any method that
|
|
|
261 |
# changes the xml-file's structure the entire file is written,
|
|
|
262 |
# can that not be done somehow better ???
|
|
|
263 |
|
|
|
264 |
# // and regenerate the xml file
|
|
|
265 |
# $this->_writeFile();
|
|
|
266 |
|
|
|
267 |
}
|
|
|
268 |
*/
|
|
|
269 |
|
|
|
270 |
// }}}
|
|
|
271 |
// {{{ remove()
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* TO BE IMPLEMNTED
|
|
|
275 |
* removes the given node
|
|
|
276 |
*
|
|
|
277 |
* @version 2001/10/09
|
|
|
278 |
* @access public
|
|
|
279 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
280 |
* @param mixed $id the id of the node to be removed
|
|
|
281 |
* @return boolean true on success
|
|
|
282 |
*/
|
|
|
283 |
/* function remove($id)
|
|
|
284 |
{
|
|
|
285 |
// remove the data from this->data
|
|
|
286 |
unset($this->data[$id]);
|
|
|
287 |
|
|
|
288 |
# see comment in "add"-method
|
|
|
289 |
}
|
|
|
290 |
*/
|
|
|
291 |
|
|
|
292 |
// }}}
|
|
|
293 |
// {{{ move()
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* TO BE IMPLEMNTED
|
|
|
297 |
* move an entry under a given parent or behind a given entry
|
|
|
298 |
*
|
|
|
299 |
* @version 2001/10/10
|
|
|
300 |
* @access public
|
|
|
301 |
* @author Wolfram Kriesing <wolfram@kriesing.de>
|
|
|
302 |
* @param integer if prevId is given the element with the id idToMove shall be moved _behind_ element with id=prevId
|
|
|
303 |
* before would be easier, but then no element could be inserted at the end :-/
|
|
|
304 |
* @return boolean true for success
|
|
|
305 |
*/
|
|
|
306 |
/* function move($idToMove, $newParentId, $prevId=0)
|
|
|
307 |
{
|
|
|
308 |
$this->data[$idToMove]['parentId'] = $newParentId;
|
|
|
309 |
$this->data[$idToMove]['prevId'] = $prevId;
|
|
|
310 |
|
|
|
311 |
# see comment in "add"-method
|
|
|
312 |
}
|
|
|
313 |
*/
|
|
|
314 |
|
|
|
315 |
}
|