|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Feed_Reader |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Rss.php 22300 2010-05-26 10:13:34Z padraic $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Feed_Reader |
|
24 */ |
|
25 require_once 'Zend/Feed/Reader.php'; |
|
26 |
|
27 /** |
|
28 * @see Zend_Feed_Reader_EntryInterface |
|
29 */ |
|
30 require_once 'Zend/Feed/Reader/EntryInterface.php'; |
|
31 |
|
32 /** |
|
33 * @see Zend_Feed_Reader_EntryAbstract |
|
34 */ |
|
35 require_once 'Zend/Feed/Reader/EntryAbstract.php'; |
|
36 |
|
37 /** |
|
38 * @see Zend_Feed_Reader_Extension_DublinCore_Entry |
|
39 */ |
|
40 require_once 'Zend/Feed/Reader/Extension/DublinCore/Entry.php'; |
|
41 |
|
42 /** |
|
43 * @see Zend_Feed_Reader_Extension_Content_Entry |
|
44 */ |
|
45 require_once 'Zend/Feed/Reader/Extension/Content/Entry.php'; |
|
46 |
|
47 /** |
|
48 * @see Zend_Feed_Reader_Extension_Atom_Entry |
|
49 */ |
|
50 require_once 'Zend/Feed/Reader/Extension/Atom/Entry.php'; |
|
51 |
|
52 /** |
|
53 * @see Zend_Feed_Reader_Extension_WellformedWeb_Entry |
|
54 */ |
|
55 require_once 'Zend/Feed/Reader/Extension/WellFormedWeb/Entry.php'; |
|
56 |
|
57 /** |
|
58 * @see Zend_Feed_Reader_Extension_Slash_Entry |
|
59 */ |
|
60 require_once 'Zend/Feed/Reader/Extension/Slash/Entry.php'; |
|
61 |
|
62 /** |
|
63 * @see Zend_Feed_Reader_Extension_Thread_Entry |
|
64 */ |
|
65 require_once 'Zend/Feed/Reader/Extension/Thread/Entry.php'; |
|
66 |
|
67 /** |
|
68 * @see Zend_Date |
|
69 */ |
|
70 require_once 'Zend/Date.php'; |
|
71 |
|
72 /** |
|
73 * @see Zend_Feed_Reader_Collection_Category |
|
74 */ |
|
75 require_once 'Zend/Feed/Reader/Collection/Category.php'; |
|
76 |
|
77 /** |
|
78 * @category Zend |
|
79 * @package Zend_Feed_Reader |
|
80 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
81 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
82 */ |
|
83 class Zend_Feed_Reader_Entry_Rss extends Zend_Feed_Reader_EntryAbstract implements Zend_Feed_Reader_EntryInterface |
|
84 { |
|
85 |
|
86 /** |
|
87 * XPath query for RDF |
|
88 * |
|
89 * @var string |
|
90 */ |
|
91 protected $_xpathQueryRdf = ''; |
|
92 |
|
93 /** |
|
94 * XPath query for RSS |
|
95 * |
|
96 * @var string |
|
97 */ |
|
98 protected $_xpathQueryRss = ''; |
|
99 |
|
100 /** |
|
101 * Constructor |
|
102 * |
|
103 * @param Zend_Feed_Entry_Abstract $entry |
|
104 * @param string $entryKey |
|
105 * @param string $type |
|
106 * @return void |
|
107 */ |
|
108 public function __construct(DOMElement $entry, $entryKey, $type = null) |
|
109 { |
|
110 parent::__construct($entry, $entryKey, $type); |
|
111 $this->_xpathQueryRss = '//item[' . ($this->_entryKey+1) . ']'; |
|
112 $this->_xpathQueryRdf = '//rss:item[' . ($this->_entryKey+1) . ']'; |
|
113 |
|
114 $pluginLoader = Zend_Feed_Reader::getPluginLoader(); |
|
115 |
|
116 $dublinCoreClass = $pluginLoader->getClassName('DublinCore_Entry'); |
|
117 $this->_extensions['DublinCore_Entry'] = new $dublinCoreClass($entry, $entryKey, $type); |
|
118 |
|
119 $contentClass = $pluginLoader->getClassName('Content_Entry'); |
|
120 $this->_extensions['Content_Entry'] = new $contentClass($entry, $entryKey, $type); |
|
121 |
|
122 $atomClass = $pluginLoader->getClassName('Atom_Entry'); |
|
123 $this->_extensions['Atom_Entry'] = new $atomClass($entry, $entryKey, $type); |
|
124 |
|
125 $wfwClass = $pluginLoader->getClassName('WellFormedWeb_Entry'); |
|
126 $this->_extensions['WellFormedWeb_Entry'] = new $wfwClass($entry, $entryKey, $type); |
|
127 |
|
128 $slashClass = $pluginLoader->getClassName('Slash_Entry'); |
|
129 $this->_extensions['Slash_Entry'] = new $slashClass($entry, $entryKey, $type); |
|
130 |
|
131 $threadClass = $pluginLoader->getClassName('Thread_Entry'); |
|
132 $this->_extensions['Thread_Entry'] = new $threadClass($entry, $entryKey, $type); |
|
133 } |
|
134 |
|
135 /** |
|
136 * Get an author entry |
|
137 * |
|
138 * @param DOMElement $element |
|
139 * @return string |
|
140 */ |
|
141 public function getAuthor($index = 0) |
|
142 { |
|
143 $authors = $this->getAuthors(); |
|
144 |
|
145 if (isset($authors[$index])) { |
|
146 return $authors[$index]; |
|
147 } |
|
148 |
|
149 return null; |
|
150 } |
|
151 |
|
152 /** |
|
153 * Get an array with feed authors |
|
154 * |
|
155 * @return array |
|
156 */ |
|
157 public function getAuthors() |
|
158 { |
|
159 if (array_key_exists('authors', $this->_data)) { |
|
160 return $this->_data['authors']; |
|
161 } |
|
162 |
|
163 $authors = array(); |
|
164 $authors_dc = $this->getExtension('DublinCore')->getAuthors(); |
|
165 if (!empty($authors_dc)) { |
|
166 foreach ($authors_dc as $author) { |
|
167 $authors[] = array( |
|
168 'name' => $author['name'] |
|
169 ); |
|
170 } |
|
171 } |
|
172 |
|
173 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 |
|
174 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) { |
|
175 $list = $this->_xpath->query($this->_xpathQueryRss . '//author'); |
|
176 } else { |
|
177 $list = $this->_xpath->query($this->_xpathQueryRdf . '//rss:author'); |
|
178 } |
|
179 if ($list->length) { |
|
180 foreach ($list as $author) { |
|
181 $string = trim($author->nodeValue); |
|
182 $email = null; |
|
183 $name = null; |
|
184 $data = array(); |
|
185 // Pretty rough parsing - but it's a catchall |
|
186 if (preg_match("/^.*@[^ ]*/", $string, $matches)) { |
|
187 $data['email'] = trim($matches[0]); |
|
188 if (preg_match("/\((.*)\)$/", $string, $matches)) { |
|
189 $data['name'] = $matches[1]; |
|
190 } |
|
191 $authors[] = $data; |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 if (count($authors) == 0) { |
|
197 $authors = $this->getExtension('Atom')->getAuthors(); |
|
198 } else { |
|
199 $authors = new Zend_Feed_Reader_Collection_Author( |
|
200 Zend_Feed_Reader::arrayUnique($authors) |
|
201 ); |
|
202 } |
|
203 |
|
204 if (count($authors) == 0) { |
|
205 $authors = null; |
|
206 } |
|
207 |
|
208 $this->_data['authors'] = $authors; |
|
209 |
|
210 return $this->_data['authors']; |
|
211 } |
|
212 |
|
213 /** |
|
214 * Get the entry content |
|
215 * |
|
216 * @return string |
|
217 */ |
|
218 public function getContent() |
|
219 { |
|
220 if (array_key_exists('content', $this->_data)) { |
|
221 return $this->_data['content']; |
|
222 } |
|
223 |
|
224 $content = $this->getExtension('Content')->getContent(); |
|
225 |
|
226 if (!$content) { |
|
227 $content = $this->getDescription(); |
|
228 } |
|
229 |
|
230 if (empty($content)) { |
|
231 $content = $this->getExtension('Atom')->getContent(); |
|
232 } |
|
233 |
|
234 $this->_data['content'] = $content; |
|
235 |
|
236 return $this->_data['content']; |
|
237 } |
|
238 |
|
239 /** |
|
240 * Get the entry's date of creation |
|
241 * |
|
242 * @return string |
|
243 */ |
|
244 public function getDateCreated() |
|
245 { |
|
246 return $this->getDateModified(); |
|
247 } |
|
248 |
|
249 /** |
|
250 * Get the entry's date of modification |
|
251 * |
|
252 * @return string |
|
253 */ |
|
254 public function getDateModified() |
|
255 { |
|
256 if (array_key_exists('datemodified', $this->_data)) { |
|
257 return $this->_data['datemodified']; |
|
258 } |
|
259 |
|
260 $dateModified = null; |
|
261 $date = null; |
|
262 |
|
263 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 |
|
264 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090 |
|
265 ) { |
|
266 $dateModified = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/pubDate)'); |
|
267 if ($dateModified) { |
|
268 $dateModifiedParsed = strtotime($dateModified); |
|
269 if ($dateModifiedParsed) { |
|
270 $date = new Zend_Date($dateModifiedParsed); |
|
271 } else { |
|
272 $dateStandards = array(Zend_Date::RSS, Zend_Date::RFC_822, |
|
273 Zend_Date::RFC_2822, Zend_Date::DATES); |
|
274 $date = new Zend_Date; |
|
275 foreach ($dateStandards as $standard) { |
|
276 try { |
|
277 $date->set($dateModified, $standard); |
|
278 break; |
|
279 } catch (Zend_Date_Exception $e) { |
|
280 if ($standard == Zend_Date::DATES) { |
|
281 require_once 'Zend/Feed/Exception.php'; |
|
282 throw new Zend_Feed_Exception( |
|
283 'Could not load date due to unrecognised' |
|
284 .' format (should follow RFC 822 or 2822):' |
|
285 . $e->getMessage(), |
|
286 0, $e |
|
287 ); |
|
288 } |
|
289 } |
|
290 } |
|
291 } |
|
292 } |
|
293 } |
|
294 |
|
295 if (!$date) { |
|
296 $date = $this->getExtension('DublinCore')->getDate(); |
|
297 } |
|
298 |
|
299 if (!$date) { |
|
300 $date = $this->getExtension('Atom')->getDateModified(); |
|
301 } |
|
302 |
|
303 if (!$date) { |
|
304 $date = null; |
|
305 } |
|
306 |
|
307 $this->_data['datemodified'] = $date; |
|
308 |
|
309 return $this->_data['datemodified']; |
|
310 } |
|
311 |
|
312 /** |
|
313 * Get the entry description |
|
314 * |
|
315 * @return string |
|
316 */ |
|
317 public function getDescription() |
|
318 { |
|
319 if (array_key_exists('description', $this->_data)) { |
|
320 return $this->_data['description']; |
|
321 } |
|
322 |
|
323 $description = null; |
|
324 |
|
325 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 |
|
326 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090 |
|
327 ) { |
|
328 $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/description)'); |
|
329 } else { |
|
330 $description = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:description)'); |
|
331 } |
|
332 |
|
333 if (!$description) { |
|
334 $description = $this->getExtension('DublinCore')->getDescription(); |
|
335 } |
|
336 |
|
337 if (empty($description)) { |
|
338 $description = $this->getExtension('Atom')->getDescription(); |
|
339 } |
|
340 |
|
341 if (!$description) { |
|
342 $description = null; |
|
343 } |
|
344 |
|
345 $this->_data['description'] = $description; |
|
346 |
|
347 return $this->_data['description']; |
|
348 } |
|
349 |
|
350 /** |
|
351 * Get the entry enclosure |
|
352 * @return string |
|
353 */ |
|
354 public function getEnclosure() |
|
355 { |
|
356 if (array_key_exists('enclosure', $this->_data)) { |
|
357 return $this->_data['enclosure']; |
|
358 } |
|
359 |
|
360 $enclosure = null; |
|
361 |
|
362 if ($this->getType() == Zend_Feed_Reader::TYPE_RSS_20) { |
|
363 $nodeList = $this->_xpath->query($this->_xpathQueryRss . '/enclosure'); |
|
364 |
|
365 if ($nodeList->length > 0) { |
|
366 $enclosure = new stdClass(); |
|
367 $enclosure->url = $nodeList->item(0)->getAttribute('url'); |
|
368 $enclosure->length = $nodeList->item(0)->getAttribute('length'); |
|
369 $enclosure->type = $nodeList->item(0)->getAttribute('type'); |
|
370 } |
|
371 } |
|
372 |
|
373 if (!$enclosure) { |
|
374 $enclosure = $this->getExtension('Atom')->getEnclosure(); |
|
375 } |
|
376 |
|
377 $this->_data['enclosure'] = $enclosure; |
|
378 |
|
379 return $this->_data['enclosure']; |
|
380 } |
|
381 |
|
382 /** |
|
383 * Get the entry ID |
|
384 * |
|
385 * @return string |
|
386 */ |
|
387 public function getId() |
|
388 { |
|
389 if (array_key_exists('id', $this->_data)) { |
|
390 return $this->_data['id']; |
|
391 } |
|
392 |
|
393 $id = null; |
|
394 |
|
395 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 |
|
396 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090 |
|
397 ) { |
|
398 $id = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/guid)'); |
|
399 } |
|
400 |
|
401 if (!$id) { |
|
402 $id = $this->getExtension('DublinCore')->getId(); |
|
403 } |
|
404 |
|
405 if (empty($id)) { |
|
406 $id = $this->getExtension('Atom')->getId(); |
|
407 } |
|
408 |
|
409 if (!$id) { |
|
410 if ($this->getPermalink()) { |
|
411 $id = $this->getPermalink(); |
|
412 } elseif ($this->getTitle()) { |
|
413 $id = $this->getTitle(); |
|
414 } else { |
|
415 $id = null; |
|
416 } |
|
417 } |
|
418 |
|
419 $this->_data['id'] = $id; |
|
420 |
|
421 return $this->_data['id']; |
|
422 } |
|
423 |
|
424 /** |
|
425 * Get a specific link |
|
426 * |
|
427 * @param int $index |
|
428 * @return string |
|
429 */ |
|
430 public function getLink($index = 0) |
|
431 { |
|
432 if (!array_key_exists('links', $this->_data)) { |
|
433 $this->getLinks(); |
|
434 } |
|
435 |
|
436 if (isset($this->_data['links'][$index])) { |
|
437 return $this->_data['links'][$index]; |
|
438 } |
|
439 |
|
440 return null; |
|
441 } |
|
442 |
|
443 /** |
|
444 * Get all links |
|
445 * |
|
446 * @return array |
|
447 */ |
|
448 public function getLinks() |
|
449 { |
|
450 if (array_key_exists('links', $this->_data)) { |
|
451 return $this->_data['links']; |
|
452 } |
|
453 |
|
454 $links = array(); |
|
455 |
|
456 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && |
|
457 $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) { |
|
458 $list = $this->_xpath->query($this->_xpathQueryRss.'//link'); |
|
459 } else { |
|
460 $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:link'); |
|
461 } |
|
462 |
|
463 if (!$list->length) { |
|
464 $links = $this->getExtension('Atom')->getLinks(); |
|
465 } else { |
|
466 foreach ($list as $link) { |
|
467 $links[] = $link->nodeValue; |
|
468 } |
|
469 } |
|
470 |
|
471 $this->_data['links'] = $links; |
|
472 |
|
473 return $this->_data['links']; |
|
474 } |
|
475 |
|
476 /** |
|
477 * Get all categories |
|
478 * |
|
479 * @return Zend_Feed_Reader_Collection_Category |
|
480 */ |
|
481 public function getCategories() |
|
482 { |
|
483 if (array_key_exists('categories', $this->_data)) { |
|
484 return $this->_data['categories']; |
|
485 } |
|
486 |
|
487 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 && |
|
488 $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) { |
|
489 $list = $this->_xpath->query($this->_xpathQueryRss.'//category'); |
|
490 } else { |
|
491 $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:category'); |
|
492 } |
|
493 |
|
494 if ($list->length) { |
|
495 $categoryCollection = new Zend_Feed_Reader_Collection_Category; |
|
496 foreach ($list as $category) { |
|
497 $categoryCollection[] = array( |
|
498 'term' => $category->nodeValue, |
|
499 'scheme' => $category->getAttribute('domain'), |
|
500 'label' => $category->nodeValue, |
|
501 ); |
|
502 } |
|
503 } else { |
|
504 $categoryCollection = $this->getExtension('DublinCore')->getCategories(); |
|
505 } |
|
506 |
|
507 if (count($categoryCollection) == 0) { |
|
508 $categoryCollection = $this->getExtension('Atom')->getCategories(); |
|
509 } |
|
510 |
|
511 $this->_data['categories'] = $categoryCollection; |
|
512 |
|
513 return $this->_data['categories']; |
|
514 } |
|
515 |
|
516 /** |
|
517 * Get a permalink to the entry |
|
518 * |
|
519 * @return string |
|
520 */ |
|
521 public function getPermalink() |
|
522 { |
|
523 return $this->getLink(0); |
|
524 } |
|
525 |
|
526 /** |
|
527 * Get the entry title |
|
528 * |
|
529 * @return string |
|
530 */ |
|
531 public function getTitle() |
|
532 { |
|
533 if (array_key_exists('title', $this->_data)) { |
|
534 return $this->_data['title']; |
|
535 } |
|
536 |
|
537 $title = null; |
|
538 |
|
539 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 |
|
540 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090 |
|
541 ) { |
|
542 $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/title)'); |
|
543 } else { |
|
544 $title = $this->_xpath->evaluate('string('.$this->_xpathQueryRdf.'/rss:title)'); |
|
545 } |
|
546 |
|
547 if (!$title) { |
|
548 $title = $this->getExtension('DublinCore')->getTitle(); |
|
549 } |
|
550 |
|
551 if (!$title) { |
|
552 $title = $this->getExtension('Atom')->getTitle(); |
|
553 } |
|
554 |
|
555 if (!$title) { |
|
556 $title = null; |
|
557 } |
|
558 |
|
559 $this->_data['title'] = $title; |
|
560 |
|
561 return $this->_data['title']; |
|
562 } |
|
563 |
|
564 /** |
|
565 * Get the number of comments/replies for current entry |
|
566 * |
|
567 * @return string|null |
|
568 */ |
|
569 public function getCommentCount() |
|
570 { |
|
571 if (array_key_exists('commentcount', $this->_data)) { |
|
572 return $this->_data['commentcount']; |
|
573 } |
|
574 |
|
575 $commentcount = $this->getExtension('Slash')->getCommentCount(); |
|
576 |
|
577 if (!$commentcount) { |
|
578 $commentcount = $this->getExtension('Thread')->getCommentCount(); |
|
579 } |
|
580 |
|
581 if (!$commentcount) { |
|
582 $commentcount = $this->getExtension('Atom')->getCommentCount(); |
|
583 } |
|
584 |
|
585 if (!$commentcount) { |
|
586 $commentcount = null; |
|
587 } |
|
588 |
|
589 $this->_data['commentcount'] = $commentcount; |
|
590 |
|
591 return $this->_data['commentcount']; |
|
592 } |
|
593 |
|
594 /** |
|
595 * Returns a URI pointing to the HTML page where comments can be made on this entry |
|
596 * |
|
597 * @return string |
|
598 */ |
|
599 public function getCommentLink() |
|
600 { |
|
601 if (array_key_exists('commentlink', $this->_data)) { |
|
602 return $this->_data['commentlink']; |
|
603 } |
|
604 |
|
605 $commentlink = null; |
|
606 |
|
607 if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 |
|
608 && $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090 |
|
609 ) { |
|
610 $commentlink = $this->_xpath->evaluate('string('.$this->_xpathQueryRss.'/comments)'); |
|
611 } |
|
612 |
|
613 if (!$commentlink) { |
|
614 $commentlink = $this->getExtension('Atom')->getCommentLink(); |
|
615 } |
|
616 |
|
617 if (!$commentlink) { |
|
618 $commentlink = null; |
|
619 } |
|
620 |
|
621 $this->_data['commentlink'] = $commentlink; |
|
622 |
|
623 return $this->_data['commentlink']; |
|
624 } |
|
625 |
|
626 /** |
|
627 * Returns a URI pointing to a feed of all comments for this entry |
|
628 * |
|
629 * @return string |
|
630 */ |
|
631 public function getCommentFeedLink() |
|
632 { |
|
633 if (array_key_exists('commentfeedlink', $this->_data)) { |
|
634 return $this->_data['commentfeedlink']; |
|
635 } |
|
636 |
|
637 $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink(); |
|
638 |
|
639 if (!$commentfeedlink) { |
|
640 $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss'); |
|
641 } |
|
642 |
|
643 if (!$commentfeedlink) { |
|
644 $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf'); |
|
645 } |
|
646 |
|
647 if (!$commentfeedlink) { |
|
648 $commentfeedlink = null; |
|
649 } |
|
650 |
|
651 $this->_data['commentfeedlink'] = $commentfeedlink; |
|
652 |
|
653 return $this->_data['commentfeedlink']; |
|
654 } |
|
655 |
|
656 /** |
|
657 * Set the XPath query (incl. on all Extensions) |
|
658 * |
|
659 * @param DOMXPath $xpath |
|
660 */ |
|
661 public function setXpath(DOMXPath $xpath) |
|
662 { |
|
663 parent::setXpath($xpath); |
|
664 foreach ($this->_extensions as $extension) { |
|
665 $extension->setXpath($this->_xpath); |
|
666 } |
|
667 } |
|
668 } |