-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRss.php
More file actions
330 lines (300 loc) · 10.1 KB
/
Copy pathRss.php
File metadata and controls
330 lines (300 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* Rss class
*
* Features:
* - Only depends on libxml2 library (no SimpleXML)
* - Xml validity is performed by libxml2
* - Parse rdf:RDF, rss, atom feeds
* - Let you customize what information you want with simple syntax
* - Created for rss/atom feed but useful for common xml format
*
* How to use:
* - http://tontof.net/kriss/php5/rss
*/
class Rss
{
const UNKNOWN = 0;
const RSS = 1;
const ATOM = 2;
public static $feedFormat = array(
'title' => array('>title'),
'description' => array('>description', '>subtitle'),
'htmlUrl' => array('>link', '>link[rel=self][href]', '>link[href]', '>id')
);
public static $itemFormat = array(
'author' => array('>author>name', '>author', '>dc:creator', 'feed>author>name', '>dc:author', '>creator'),
'content' => array('>content:encoded', '>content', '>description', '>summary', '>subtitle'),
'description' => array('>description', '>summary', '>subtitle', '>content', '>content:encoded'),
'via' => array('>guid', '>id'),
'link' => array('>feedburner:origLink', '>link[rel=alternate][href]', '>link[href]', '>link', '>guid', '>id'),
'time' => array('>pubDate', '>updated', '>lastBuildDate', '>published', '>dc:date', '>date', '>created', '>modified'),
'title' => array('>title')
);
/**
* Check for a list of attributes if current node is valid
*
* @param DOMNode $node to check if valid
* @param array $attrs to test if in $node
*
* @return boolean true if $node is valid for $attrs, false otherwise
*/
public static function isValidNodeAttrs($node, $attrs)
{
foreach ($attrs as $attr) {
$val = '';
if (strpos($attr, '=') !== false) {
list($attr, $val) = explode('=', $attr);
}
if (!$node->hasAttribute($attr)
|| (!empty($val) && $node->getAttribute($attr) !== $val)) {
return false;
}
}
return true;
}
/**
* Check if tagName from $nodes are correct depending on $name
*
* @param DOMNodeList $nodes to check
* @param string $name to compare with tagName
*
* @return array of nodes with correct $name
*/
public static function filterNodeListByName($nodes, $name)
{
$res = array();
for ($i = 0; $i < $nodes->length; $i++) {
if ($nodes->item($i)->tagName === $name) {
$res[] = $nodes->item($i);
}
}
return $res;
}
/**
* Return array of descendant DOMNode of $node with tagName equals to $name
*
* @param DOMNode $node to starts with
* @param string $name of descendant
*
* @return array of descendant DOMNode with tagName equals to $name
*/
public static function getNodesName($node, $name)
{
if (strpos($name, ':') !== false) {
list(, $localname) = explode(':', $name);
$nodes = $node->getElementsByTagNameNS('*', $localname);
} else {
$nodes = $node->getElementsByTagName($name);
}
return self::filterNodeListByName($nodes, $name);
}
/**
* Return content of $node depending on defined $selectors
*
* @param DOMNode $node to starts with
* @param array $selectors defined using node>node[attr=val][attr]
*
* @return string of the desired selection or empty string if not found
*/
public static function getElement($node, $selectors)
{
$res = '';
$selector = array_shift($selectors);
$attributes = explode('[', str_replace(']','',$selector));
$name = array_shift($attributes);
if (substr($name, -1) == "*") {
$name = substr($name, 0, -1);
$res = array();
}
$nodes = self::getNodesName($node, $name);
foreach ($nodes as $currentNode) {
if ($currentNode->parentNode->isSameNode($node)
&& self::isValidNodeAttrs($currentNode, $attributes)) {
if (empty($selectors)) {
$attr = end($attributes);
if (empty($attr) || strpos($attr, '=') !== false) {
if (is_array($res)) {
$res[] = $currentNode->textContent;
} else {
$res = $currentNode->textContent;
}
} else {
if (is_array($res)) {
$res[] = $currentNode->getAttribute($attr);
} else {
$res = $currentNode->getAttribute($attr);
}
}
} else {
return self::getElement($currentNode, $selectors);
}
}
if (!is_array($res) && !empty($res)) {
break;
}
}
return $res;
}
/**
* Format $element depending on $formats
*
* @param DOMDocument $dom of document
* @param DOMNode $element to starts with
* @param array $formats to use to extract information
*
* @return array of extracted information
*/
public static function formatElement($dom, $element, $formats)
{
$newElement = array();
foreach ($formats as $format => $list) {
$newElement[$format] = '';
for ($i = 0, $len = count($list);
$i < $len && empty($newElement[$format]);
$i++) {
$selectors = explode('>', $list[$i]);
$selector = array_shift($selectors);
if (empty($selector)) {
$newElement[$format] = self::getElement($element, $selectors);
} else if (strpos($selector, '[') === 0) {
$attributes = explode('[', str_replace(']','',$selector));
if (self::isValidNodeAttrs($element, $attributes)) {
$newElement[$format] = self::getElement($element, $selectors);
}
} else {
$attributes = explode('[', str_replace(']','',$selector));
$name = array_shift($attributes);
$nodes = self::getNodesName($dom, $name);
foreach ($nodes as $node) {
if (self::isValidNodeAttrs($node, $attributes)) {
$newElement[$format] = self::getElement($node, $selectors);
}
if (!empty($newElement[$format])) {
break;
}
}
}
}
}
return $newElement;
}
/**
* Return array of feed from a DOMDocument
*
* @param DOMDocument $dom
*
* @return array of feed info extracted from $dom
*/
public static function getFeed($dom)
{
$feed = new DOMNodelist;
$type = self::getType($dom);
if ($type === self::RSS) {
$feed = $dom->getElementsByTagName('channel')->item(0);
} elseif ($type === self::ATOM) {
$feed = $dom->getElementsByTagName('feed')->item(0);
}
return self::formatElement($dom, $feed, self::$feedFormat);
}
/**
* Return array of items from a DOMDocument
*
* @param DOMDocument $dom
* @param integer $nb of items to select
*
* @return array of items extracted from the $dom
*/
public static function getItems($dom, $nb = -1)
{
$items = new DOMNodelist;
$type = self::getType($dom);
if ($type === self::RSS) {
$items = $dom->getElementsByTagName('item');
} elseif ($type === self::ATOM) {
$items = $dom->getElementsByTagName('entry');
}
$newItems = array();
$max = ($nb === -1 ? $items->length : min($nb, $items->length));
for ($i = 0; $i < $max; $i++) {
$newItems[] = self::formatElement($dom, $items->item($i), self::$itemFormat);
}
return $newItems;
}
/**
* Return type of a DOMDocument
*
* @param DOMDocument $dom
*
* @return const corresponding to the type of $dom
*/
public static function getType($dom)
{
$type = self::UNKNOWN;
$feed = $dom->getElementsByTagName('channel');
if ($feed->item(0)) {
$type = self::RSS;
} else {
$feed = $dom->getElementsByTagName('feed');
if ($feed->item(0)) {
$type = self::ATOM;
}
}
return $type;
}
/**
* Load a XML string into DOMDocument
*
* @param string $data
*
* @return array with a DOMDocument and a string error
*/
public static function loadDom($data)
{
libxml_clear_errors();
set_error_handler(array('Rss', 'silenceErrors'));
$dom = new DOMDocument();
$dom->loadXML($data);
restore_error_handler();
return array(
'dom' => $dom,
'error' => self::getError(libxml_get_last_error())
);
}
/**
* Explicit libxml2 error
*
* @param LibXMLError $error
*
* @return string of the error
*/
public static function getError($error)
{
$return = '';
if ($error !== false) {
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return = "Warning XML $error->code: ";
break;
case LIBXML_ERR_ERROR:
$return = "Error XML $error->code: ";
break;
case LIBXML_ERR_FATAL:
$return = "Fatal Error XML $error->code: ";
break;
}
$return .= $return.trim($error->message);
}
return $return;
}
/**
* From Simplie Pie
*
* @param integer $num of errno
* @param string $str of errstr
*/
public static function silenceErrors($num, $str)
{
// No-op
}
}