Skip to content

Commit df2ce97

Browse files
author
Cédric de Saint Léger
committed
Fixed a bug in recursive get(), sitemap driver + various fixes
1 parent a3e41dc commit df2ce97

File tree

7 files changed

+166
-78
lines changed

7 files changed

+166
-78
lines changed

classes/xml/core.php

+33-41
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
* XML_Core class.
99
*/
1010

11-
class XML_Core
12-
{
11+
class XML_Core {
1312
/**
1413
* @var string XML document version
1514
*/
@@ -77,8 +76,8 @@ public static function factory($driver = NULL, $root_node = NULL, $element = NUL
7776
return new XML($element, $root_node);
7877
}
7978
}
80-
81-
79+
80+
8281
/**
8382
* Class constructor. You should use the factory instead.
8483
* @param string $element [optional] What to construct from. Could be some xml string, a file name, or a DOMNode
@@ -136,10 +135,10 @@ public function __construct($element = NULL, $root_node = NULL)
136135
// Create the root node
137136
$root_node = $this->dom_doc->createElement($this->root_node);
138137
}
139-
138+
140139
// Append the root node to the object DOMDocument, and set the resulting DOMNode as it's node
141140
$this->dom_node = $this->dom_doc->appendChild($root_node);
142-
141+
143142
// Add other attributes
144143
$this->add_attributes($this->dom_node);
145144
}
@@ -159,31 +158,24 @@ public function __construct($element = NULL, $root_node = NULL)
159158
* @return XML instance for the node that's been added.
160159
*/
161160
public function add_node($name, $value = NULL, $attributes = array())
162-
{
161+
{
163162
// Trim the name
164163
$name = trim($name);
165-
164+
166165
// Create the element
167166
$node = $this->create_element($name);
168167

169168
// Add the attributes
170169
$this->add_attributes($node, $attributes);
171-
170+
172171
// Add the value if provided
173172
if ($value !== NULL)
174-
{
175-
$value = strval($this->filter($name, $value, &$node));
176-
177-
if (str_replace(array('<', '>', '&'), "", $value) === $value)
178-
{
179-
// Value is valid CDATA, let's add it as a new text node
180-
$value = $this->dom_doc->createTextNode($value);
181-
}
182-
else
183-
{
184-
// We shall create a CDATA section to wrap the text provided
185-
$value = $this->dom_doc->createCDATASection($value);
186-
}
173+
{
174+
$value = strval($this->filter($name, $value, $node));
175+
176+
// Value is valid CDATA, let's add it as a new text node
177+
$value = $this->dom_doc->createTextNode($value);
178+
187179
$node->appendChild($value);
188180
}
189181

@@ -207,7 +199,7 @@ public function __get($value)
207199
if ( ! isset($this->$value))
208200
{
209201
$node = current($this->get($value));
210-
202+
211203
if ($node instanceof XML)
212204
{
213205
// Return the whole XML document
@@ -231,7 +223,7 @@ public function __get($value)
231223
public function get($value, $as_array = FALSE)
232224
{
233225
$return = array();
234-
226+
235227
$value = $this->meta()->alias($value);
236228

237229
foreach ($this->dom_node->getElementsByTagName($value) as $item)
@@ -264,9 +256,9 @@ public function get($value, $as_array = FALSE)
264256
public function xpath($query, $as_array = TRUE)
265257
{
266258
$return = array();
267-
259+
268260
$xpath = new DOMXPath($this->dom_doc);
269-
261+
270262
foreach ($xpath->query($query) as $item)
271263
{
272264
if ($as_array)
@@ -291,9 +283,9 @@ public function xpath($query, $as_array = TRUE)
291283
/**
292284
* Exports the document as a multi-dimensional array.
293285
* Handles element with the same name.
294-
*
286+
*
295287
* Root node is ignored, as it is known and available in the driver.
296-
* Example :
288+
* Example :
297289
* <node_name attr_name="val">
298290
* <child_node_name>
299291
* value1
@@ -302,17 +294,17 @@ public function xpath($query, $as_array = TRUE)
302294
* value2
303295
* </child_node_name>
304296
* </node_name>
305-
*
297+
*
306298
* Here's the resulting array structure :
307299
* array ("node_name" => array(
308-
* // array of nodes called "node_name"
300+
* // array of nodes called "node_name"
309301
* 0 => array(
310302
* // Attributes of that node
311303
* "xml_attributes" => array(
312304
* "attr_name" => "val",
313305
* )
314306
* // node contents
315-
* "child_node_name" => array(
307+
* "child_node_name" => array(
316308
* // array of nodes called "child_node_name"
317309
* 0 => value1,
318310
* 1 => value2,
@@ -323,9 +315,9 @@ public function xpath($query, $as_array = TRUE)
323315
public function as_array()
324316
{
325317
$dom_element = $this->dom_node;
326-
318+
327319
$return = array();
328-
320+
329321
// This function is run on a whole XML document and this is the root node.
330322
// That root node shall be ignored in the array as it driven by the driver and handles document namespaces.
331323
foreach($dom_element->childNodes as $dom_child)
@@ -341,7 +333,7 @@ public function as_array()
341333
}
342334
}
343335
}
344-
336+
345337
return $return;
346338
}
347339

@@ -374,7 +366,7 @@ private function _as_array(DOMNode $dom_node)
374366
if ($dom_child->nodeType === XML_ELEMENT_NODE)
375367
{
376368
$child = $this->_as_array($dom_child);
377-
369+
378370
foreach ($child as $key=>$val)
379371
{
380372
$object_element[$node_name][$key][]=$val;
@@ -386,7 +378,7 @@ private function _as_array(DOMNode $dom_node)
386378
// Get attributes
387379
if ($dom_node->hasAttributes())
388380
{
389-
$object_element[$dom_node->nodeName]['xml_attributes'] = array();
381+
$object_element[$node_name] = array('xml_attributes' => array());
390382
foreach($dom_node->attributes as $att_name => $dom_attribute)
391383
{
392384
// Get the desired name for this attribute
@@ -451,13 +443,13 @@ protected function _from_array($mixed, DOMElement $dom_element)
451443
// Create a new element with the key as the element name.
452444
// Create the element corresponding to the key
453445
$node = $this->create_element($index);
454-
446+
455447
// Add the driver attributes
456448
$this->add_attributes($node);
457449

458450
// Append it
459451
$dom_element->appendChild($node);
460-
452+
461453
// Treat the array by recursion
462454
$this->_from_array($mixed_element, $node);
463455
}
@@ -466,7 +458,7 @@ protected function _from_array($mixed, DOMElement $dom_element)
466458
elseif ($mixed)
467459
{
468460
// This is a string value that shall be appended as such
469-
$mixed = $this->filter($dom_element->tagName, $mixed, &$dom_element);
461+
$mixed = $this->filter($dom_element->tagName, $mixed, $dom_element);
470462
$dom_element->appendChild($this->dom_doc->createTextNode($mixed));
471463
}
472464
}
@@ -499,7 +491,7 @@ public function import($xml)
499491
// Import the node, and all its children, to the document
500492
$node = $this->dom_doc->importNode($xml->dom_node, TRUE);
501493
$this->dom_node->appendChild($node);
502-
494+
503495
return $this;
504496
}
505497

@@ -605,7 +597,7 @@ private function add_attributes(DOMNode $node, $attributes = array())
605597
protected function filter($name, $value, &$node)
606598
{
607599
$name = $this->meta()->alias($name);
608-
600+
609601
if ($this->meta()->get("filter", $name))
610602
{
611603
return call_user_func(array($this, $this->meta()->get("filter", $name)), $value, $node);

classes/xml/driver/atom.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* Atom driver
99
*/
1010

11-
class XML_Driver_Atom extends XML
12-
{
11+
class XML_Driver_Atom extends XML {
12+
1313
public $root_node = 'feed';
1414

1515

classes/xml/driver/rss2.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* RSS2 driver
99
*/
1010

11-
class XML_Driver_Rss2 extends XML
12-
{
11+
class XML_Driver_Rss2 extends XML {
12+
1313
public $root_node = 'rss';
1414

1515
protected static function initialize(XML_Meta $meta)

classes/xml/driver/sitemap.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php defined('SYSPATH') or die('No direct script access.');
2+
/**
3+
* Document : sitemap.php
4+
* Created on : 1 mai 2009, 13:03:03
5+
* @author Cedric de Saint Leger <[email protected]>
6+
*
7+
* Description:
8+
* Sitemap driver
9+
* http://www.sitemaps.org/protocol.html
10+
*/
11+
12+
class XML_Driver_Sitemap extends XML {
13+
14+
public $root_node = 'urlset';
15+
16+
17+
protected static function initialize(XML_Meta $meta)
18+
{
19+
$meta ->nodes (
20+
array(
21+
"urlset" => array("namespace" => "http://www.sitemaps.org/schemas/sitemap/0.9"),
22+
"loc" => array("filter" => "normalize_uri"),
23+
"lastmod" => array("filter" => "normalize_date"),
24+
25+
// Sitemap extensions
26+
// Videos
27+
// http://support.google.com/webmasters/bin/answer.py?hl=fr&answer=80472
28+
"video:video" => array("namespace" => "http://www.google.com/schemas/sitemap-video/1.1"),
29+
"video:thumbnail_loc" => array("filter" => "normalize_uri"),
30+
"video:content_loc" => array("filter" => "normalize_uri"),
31+
"video:player_loc" => array("filter" => "normalize_uri"),
32+
"video:expiration_date" => array("filter" => "normalize_datetime"),
33+
34+
// Images
35+
// http://support.google.com/webmasters/bin/answer.py?hl=fr&answer=178636&topic=20986&ctx=topic
36+
"image:image" => array("namespace" => "http://www.google.com/schemas/sitemap-image/1.1"),
37+
"image:loc" => array("filter" => "normalize_uri"),
38+
39+
// News
40+
// http://support.google.com/webmasters/bin/answer.py?hl=en&answer=74288
41+
"news:news" => array("namespace" => "http://www.google.com/schemas/sitemap-news/0.9"),
42+
"news:publication_date" => array("filter" => "normalize_date"),
43+
44+
// Mobile
45+
// http://support.google.com/webmasters/bin/answer.py?hl=fr&answer=34648
46+
"mobile:mobile" => array("namespace" => "http://www.google.com/schemas/sitemap-mobile/1.0"),
47+
48+
// Code Search
49+
// http://support.google.com/webmasters/bin/answer.py?hl=fr&answer=75225
50+
"codesearch:codesearch" => array("namespace" => "http://www.google.com/codesearch/schemas/sitemap/1.0"),
51+
)
52+
);
53+
}
54+
55+
56+
public function add_url($url, $options = array(), $extensions = array())
57+
{
58+
$url_node = $this->add_node("url");
59+
$url_node->add_node("loc", $url);
60+
foreach ($options as $key => $val)
61+
{
62+
$url_node->add_node($key, $val);
63+
}
64+
foreach ($extensions as $extension => $params)
65+
{
66+
$extension = $url_node->add_node("$extension:$extension");
67+
foreach ($params as $key => $val)
68+
{
69+
$extension->add_node($key, $val);
70+
}
71+
}
72+
return $this;
73+
}
74+
75+
76+
public function normalize_datetime($value)
77+
{
78+
if ( ! is_numeric($value))
79+
{
80+
$value = strtotime($value);
81+
}
82+
83+
// Convert timestamps to W3C format datetime
84+
return date(DATE_W3C, $value);
85+
}
86+
87+
88+
public function normalize_date($value)
89+
{
90+
if ( ! is_numeric($value))
91+
{
92+
$value = strtotime($value);
93+
}
94+
95+
// Convert timestamps to RFC 3339 formatted dates
96+
return date("Y-m-d", $value);
97+
}
98+
}

classes/xml/driver/xrds.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* XRDS driver. For Service Discovery.
99
*/
1010

11-
class XML_Driver_XRDS extends XML
12-
{
11+
class XML_Driver_XRDS extends XML {
12+
1313
public $root_node = 'xrds:XRDS';
14-
14+
1515
protected static function initialize(XML_Meta $meta)
1616
{
1717
$meta ->content_type("application/xrds+xml")
@@ -24,8 +24,8 @@ protected static function initialize(XML_Meta $meta)
2424
)
2525
);
2626
}
27-
28-
27+
28+
2929
public function add_service($type, $uri, $priority = NULL)
3030
{
3131
if (! is_null($priority))
@@ -36,20 +36,20 @@ public function add_service($type, $uri, $priority = NULL)
3636
{
3737
$priority = array();
3838
}
39-
39+
4040
$service_node = $this->add_node("Service", NULL, $priority);
4141

4242
if (! is_array($type))
4343
{
4444
$type = array($type);
4545
}
46-
46+
4747
foreach ($type as $t)
4848
{
4949
$service_node->add_node("Type", $t);
5050
}
5151
$service_node->add_node("URI", $uri);
52-
52+
5353
return $service_node;
5454
}
5555
}

classes/xml/meta.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88
* XML_Meta class. Use this to override XML_Meta_Core
99
*/
1010

11-
class XML_Meta extends XML_Meta_Core
12-
{}
11+
class XML_Meta extends XML_Meta_Core {}

0 commit comments

Comments
 (0)