3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Feed\Reader\Feed;
15 use Zend\Feed\Reader\Collection;
16 use Zend\Feed\Reader\Exception;
20 class Rss extends AbstractFeed
25 * @param DOMDocument $dom
28 public function __construct(DOMDocument $dom, $type = null)
30 parent::__construct($dom, $type);
32 $manager = Reader\Reader::getExtensionManager();
34 $feed = $manager->get('DublinCore\Feed');
35 $feed->setDomDocument($dom);
36 $feed->setType($this->data['type']);
37 $feed->setXpath($this->xpath);
38 $this->extensions['DublinCore\Feed'] = $feed;
40 $feed = $manager->get('Atom\Feed');
41 $feed->setDomDocument($dom);
42 $feed->setType($this->data['type']);
43 $feed->setXpath($this->xpath);
44 $this->extensions['Atom\Feed'] = $feed;
46 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
47 && $this->getType() !== Reader\Reader::TYPE_RSS_090
49 $xpathPrefix = '/rss/channel';
51 $xpathPrefix = '/rdf:RDF/rss:channel';
53 foreach ($this->extensions as $extension) {
54 $extension->setXpathPrefix($xpathPrefix);
64 public function getAuthor($index = 0)
66 $authors = $this->getAuthors();
68 if (isset($authors[$index])) {
69 return $authors[$index];
76 * Get an array with feed authors
80 public function getAuthors()
82 if (array_key_exists('authors', $this->data)) {
83 return $this->data['authors'];
87 $authorsDc = $this->getExtension('DublinCore')->getAuthors();
88 if (! empty($authorsDc)) {
89 foreach ($authorsDc as $author) {
91 'name' => $author['name']
97 * Technically RSS doesn't specific author element use at the feed level
98 * but it's supported on a "just in case" basis.
100 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
101 && $this->getType() !== Reader\Reader::TYPE_RSS_090) {
102 $list = $this->xpath->query('//author');
104 $list = $this->xpath->query('//rss:author');
107 foreach ($list as $author) {
108 $string = trim($author->nodeValue);
110 // Pretty rough parsing - but it's a catchall
111 if (preg_match("/^.*@[^ ]*/", $string, $matches)) {
112 $data['email'] = trim($matches[0]);
113 if (preg_match("/\((.*)\)$/", $string, $matches)) {
114 $data['name'] = $matches[1];
121 if (count($authors) == 0) {
122 $authors = $this->getExtension('Atom')->getAuthors();
124 $authors = new Reader\Collection\Author(
125 Reader\Reader::arrayUnique($authors)
129 if (count($authors) == 0) {
133 $this->data['authors'] = $authors;
135 return $this->data['authors'];
139 * Get the copyright entry
141 * @return string|null
143 public function getCopyright()
145 if (array_key_exists('copyright', $this->data)) {
146 return $this->data['copyright'];
151 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
152 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
153 $copyright = $this->xpath->evaluate('string(/rss/channel/copyright)');
156 if (! $copyright && $this->getExtension('DublinCore') !== null) {
157 $copyright = $this->getExtension('DublinCore')->getCopyright();
160 if (empty($copyright)) {
161 $copyright = $this->getExtension('Atom')->getCopyright();
168 $this->data['copyright'] = $copyright;
170 return $this->data['copyright'];
174 * Get the feed creation date
176 * @return DateTime|null
178 public function getDateCreated()
180 return $this->getDateModified();
184 * Get the feed modification date
187 * @throws Exception\RuntimeException
189 public function getDateModified()
191 if (array_key_exists('datemodified', $this->data)) {
192 return $this->data['datemodified'];
197 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
198 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
199 $dateModified = $this->xpath->evaluate('string(/rss/channel/pubDate)');
200 if (! $dateModified) {
201 $dateModified = $this->xpath->evaluate('string(/rss/channel/lastBuildDate)');
204 $dateModifiedParsed = strtotime($dateModified);
205 if ($dateModifiedParsed) {
206 $date = new DateTime('@' . $dateModifiedParsed);
208 $dateStandards = [DateTime::RSS, DateTime::RFC822,
209 DateTime::RFC2822, null];
210 foreach ($dateStandards as $standard) {
212 $date = DateTime::createFromFormat($standard, $dateModified);
214 } catch (\Exception $e) {
215 if ($standard === null) {
216 throw new Exception\RuntimeException(
217 'Could not load date due to unrecognised'
218 .' format (should follow RFC 822 or 2822):'
231 $date = $this->getExtension('DublinCore')->getDate();
235 $date = $this->getExtension('Atom')->getDateModified();
242 $this->data['datemodified'] = $date;
244 return $this->data['datemodified'];
248 * Get the feed lastBuild date
250 * @throws Exception\RuntimeException
253 public function getLastBuildDate()
255 if (array_key_exists('lastBuildDate', $this->data)) {
256 return $this->data['lastBuildDate'];
261 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
262 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
263 $lastBuildDate = $this->xpath->evaluate('string(/rss/channel/lastBuildDate)');
264 if ($lastBuildDate) {
265 $lastBuildDateParsed = strtotime($lastBuildDate);
266 if ($lastBuildDateParsed) {
267 $date = new DateTime('@' . $lastBuildDateParsed);
269 $dateStandards = [DateTime::RSS, DateTime::RFC822,
270 DateTime::RFC2822, null];
271 foreach ($dateStandards as $standard) {
273 $date = DateTime::createFromFormat($standard, $lastBuildDateParsed);
275 } catch (\Exception $e) {
276 if ($standard === null) {
277 throw new Exception\RuntimeException(
278 'Could not load date due to unrecognised'
279 .' format (should follow RFC 822 or 2822):'
295 $this->data['lastBuildDate'] = $date;
297 return $this->data['lastBuildDate'];
301 * Get the feed description
303 * @return string|null
305 public function getDescription()
307 if (array_key_exists('description', $this->data)) {
308 return $this->data['description'];
311 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
312 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
313 $description = $this->xpath->evaluate('string(/rss/channel/description)');
315 $description = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/rss:description)');
318 if (! $description && $this->getExtension('DublinCore') !== null) {
319 $description = $this->getExtension('DublinCore')->getDescription();
322 if (empty($description)) {
323 $description = $this->getExtension('Atom')->getDescription();
326 if (! $description) {
330 $this->data['description'] = $description;
332 return $this->data['description'];
338 * @return string|null
340 public function getId()
342 if (array_key_exists('id', $this->data)) {
343 return $this->data['id'];
348 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
349 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
350 $id = $this->xpath->evaluate('string(/rss/channel/guid)');
353 if (! $id && $this->getExtension('DublinCore') !== null) {
354 $id = $this->getExtension('DublinCore')->getId();
358 $id = $this->getExtension('Atom')->getId();
362 if ($this->getLink()) {
363 $id = $this->getLink();
364 } elseif ($this->getTitle()) {
365 $id = $this->getTitle();
371 $this->data['id'] = $id;
373 return $this->data['id'];
377 * Get the feed image data
381 public function getImage()
383 if (array_key_exists('image', $this->data)) {
384 return $this->data['image'];
387 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
388 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
389 $list = $this->xpath->query('/rss/channel/image');
390 $prefix = '/rss/channel/image[1]';
392 $list = $this->xpath->query('/rdf:RDF/rss:channel/rss:image');
393 $prefix = '/rdf:RDF/rss:channel/rss:image[1]';
395 if ($list->length > 0) {
397 $value = $this->xpath->evaluate('string(' . $prefix . '/url)');
399 $image['uri'] = $value;
401 $value = $this->xpath->evaluate('string(' . $prefix . '/link)');
403 $image['link'] = $value;
405 $value = $this->xpath->evaluate('string(' . $prefix . '/title)');
407 $image['title'] = $value;
409 $value = $this->xpath->evaluate('string(' . $prefix . '/height)');
411 $image['height'] = $value;
413 $value = $this->xpath->evaluate('string(' . $prefix . '/width)');
415 $image['width'] = $value;
417 $value = $this->xpath->evaluate('string(' . $prefix . '/description)');
419 $image['description'] = $value;
425 $this->data['image'] = $image;
427 return $this->data['image'];
431 * Get the feed language
433 * @return string|null
435 public function getLanguage()
437 if (array_key_exists('language', $this->data)) {
438 return $this->data['language'];
443 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
444 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
445 $language = $this->xpath->evaluate('string(/rss/channel/language)');
448 if (! $language && $this->getExtension('DublinCore') !== null) {
449 $language = $this->getExtension('DublinCore')->getLanguage();
452 if (empty($language)) {
453 $language = $this->getExtension('Atom')->getLanguage();
457 $language = $this->xpath->evaluate('string(//@xml:lang[1])');
464 $this->data['language'] = $language;
466 return $this->data['language'];
470 * Get a link to the feed
472 * @return string|null
474 public function getLink()
476 if (array_key_exists('link', $this->data)) {
477 return $this->data['link'];
480 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
481 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
482 $link = $this->xpath->evaluate('string(/rss/channel/link)');
484 $link = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/rss:link)');
488 $link = $this->getExtension('Atom')->getLink();
495 $this->data['link'] = $link;
497 return $this->data['link'];
501 * Get a link to the feed XML
503 * @return string|null
505 public function getFeedLink()
507 if (array_key_exists('feedlink', $this->data)) {
508 return $this->data['feedlink'];
511 $link = $this->getExtension('Atom')->getFeedLink();
513 if ($link === null || empty($link)) {
514 $link = $this->getOriginalSourceUri();
517 $this->data['feedlink'] = $link;
519 return $this->data['feedlink'];
523 * Get the feed generator entry
525 * @return string|null
527 public function getGenerator()
529 if (array_key_exists('generator', $this->data)) {
530 return $this->data['generator'];
535 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
536 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
537 $generator = $this->xpath->evaluate('string(/rss/channel/generator)');
541 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
542 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
543 $generator = $this->xpath->evaluate('string(/rss/channel/atom:generator)');
545 $generator = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/atom:generator)');
549 if (empty($generator)) {
550 $generator = $this->getExtension('Atom')->getGenerator();
557 $this->data['generator'] = $generator;
559 return $this->data['generator'];
565 * @return string|null
567 public function getTitle()
569 if (array_key_exists('title', $this->data)) {
570 return $this->data['title'];
573 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
574 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
575 $title = $this->xpath->evaluate('string(/rss/channel/title)');
577 $title = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/rss:title)');
580 if (! $title && $this->getExtension('DublinCore') !== null) {
581 $title = $this->getExtension('DublinCore')->getTitle();
585 $title = $this->getExtension('Atom')->getTitle();
592 $this->data['title'] = $title;
594 return $this->data['title'];
598 * Get an array of any supported Pusubhubbub endpoints
602 public function getHubs()
604 if (array_key_exists('hubs', $this->data)) {
605 return $this->data['hubs'];
608 $hubs = $this->getExtension('Atom')->getHubs();
613 $hubs = array_unique($hubs);
616 $this->data['hubs'] = $hubs;
618 return $this->data['hubs'];
624 * @return Reader\Collection\Category
626 public function getCategories()
628 if (array_key_exists('categories', $this->data)) {
629 return $this->data['categories'];
632 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
633 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
634 $list = $this->xpath->query('/rss/channel//category');
636 $list = $this->xpath->query('/rdf:RDF/rss:channel//rss:category');
640 $categoryCollection = new Collection\Category;
641 foreach ($list as $category) {
642 $categoryCollection[] = [
643 'term' => $category->nodeValue,
644 'scheme' => $category->getAttribute('domain'),
645 'label' => $category->nodeValue,
649 $categoryCollection = $this->getExtension('DublinCore')->getCategories();
652 if (count($categoryCollection) == 0) {
653 $categoryCollection = $this->getExtension('Atom')->getCategories();
656 $this->data['categories'] = $categoryCollection;
658 return $this->data['categories'];
662 * Read all entries to the internal entries array
665 protected function indexEntries()
667 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) {
668 $entries = $this->xpath->evaluate('//item');
670 $entries = $this->xpath->evaluate('//rss:item');
673 foreach ($entries as $index => $entry) {
674 $this->entries[$index] = $entry;
679 * Register the default namespaces for the current feed format
682 protected function registerNamespaces()
684 switch ($this->data['type']) {
685 case Reader\Reader::TYPE_RSS_10:
686 $this->xpath->registerNamespace('rdf', Reader\Reader::NAMESPACE_RDF);
687 $this->xpath->registerNamespace('rss', Reader\Reader::NAMESPACE_RSS_10);
690 case Reader\Reader::TYPE_RSS_090:
691 $this->xpath->registerNamespace('rdf', Reader\Reader::NAMESPACE_RDF);
692 $this->xpath->registerNamespace('rss', Reader\Reader::NAMESPACE_RSS_090);