Source of file Rss.php
Size: 20,468 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Feed/Reader/Feed/Rss.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Feed\Reader\Feed; use DateTime; use DOMDocument; use Zend\Feed\Reader; use Zend\Feed\Reader\Collection; use Zend\Feed\Reader\Exception; /** */ class Rss extends AbstractFeed { /** * Constructor * * @param DOMDocument $dom * @param string $type */ public function __construct(DOMDocument $dom, $type = null) { parent::__construct($dom, $type); $manager = Reader\Reader::getExtensionManager(); $feed = $manager->get('DublinCore\Feed'); $feed->setDomDocument($dom); $feed->setType($this->data['type']); $feed->setXpath($this->xpath); $this->extensions['DublinCore\Feed'] = $feed; $feed = $manager->get('Atom\Feed'); $feed->setDomDocument($dom); $feed->setType($this->data['type']); $feed->setXpath($this->xpath); $this->extensions['Atom\Feed'] = $feed; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090 ) { $xpathPrefix = '/rss/channel'; } else { $xpathPrefix = '/rdf:RDF/rss:channel'; } foreach ($this->extensions as $extension) { $extension->setXpathPrefix($xpathPrefix); } } /** * Get a single author * * @param int $index * @return string|null */ public function getAuthor($index = 0) { $authors = $this->getAuthors(); if (isset($authors[$index])) { return $authors[$index]; } return null; } /** * Get an array with feed authors * * @return array */ public function getAuthors() { if (array_key_exists('authors', $this->data)) { return $this->data['authors']; } $authors = array(); $authorsDc = $this->getExtension('DublinCore')->getAuthors(); if (!empty($authorsDc)) { foreach ($authorsDc as $author) { $authors[] = array( 'name' => $author['name'] ); } } /** * Technically RSS doesn't specific author element use at the feed level * but it's supported on a "just in case" basis. */ if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $list = $this->xpath->query('//author'); } else { $list = $this->xpath->query('//rss:author'); } if ($list->length) { foreach ($list as $author) { $string = trim($author->nodeValue); $email = null; $name = null; $data = array(); // Pretty rough parsing - but it's a catchall if (preg_match("/^.*@[^ ]*/", $string, $matches)) { $data['email'] = trim($matches[0]); if (preg_match("/\((.*)\)$/", $string, $matches)) { $data['name'] = $matches[1]; } $authors[] = $data; } } } if (count($authors) == 0) { $authors = $this->getExtension('Atom')->getAuthors(); } else { $authors = new Reader\Collection\Author( Reader\Reader::arrayUnique($authors) ); } if (count($authors) == 0) { $authors = null; } $this->data['authors'] = $authors; return $this->data['authors']; } /** * Get the copyright entry * * @return string|null */ public function getCopyright() { if (array_key_exists('copyright', $this->data)) { return $this->data['copyright']; } $copyright = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $copyright = $this->xpath->evaluate('string(/rss/channel/copyright)'); } if (!$copyright && $this->getExtension('DublinCore') !== null) { $copyright = $this->getExtension('DublinCore')->getCopyright(); } if (empty($copyright)) { $copyright = $this->getExtension('Atom')->getCopyright(); } if (!$copyright) { $copyright = null; } $this->data['copyright'] = $copyright; return $this->data['copyright']; } /** * Get the feed creation date * * @return string|null */ public function getDateCreated() { return $this->getDateModified(); } /** * Get the feed modification date * * @return DateTime * @throws Exception\RuntimeException */ public function getDateModified() { if (array_key_exists('datemodified', $this->data)) { return $this->data['datemodified']; } $dateModified = null; $date = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $dateModified = $this->xpath->evaluate('string(/rss/channel/pubDate)'); if (!$dateModified) { $dateModified = $this->xpath->evaluate('string(/rss/channel/lastBuildDate)'); } if ($dateModified) { $dateModifiedParsed = strtotime($dateModified); if ($dateModifiedParsed) { $date = new DateTime('@' . $dateModifiedParsed); } else { $dateStandards = array(DateTime::RSS, DateTime::RFC822, DateTime::RFC2822, null); foreach ($dateStandards as $standard) { try { $date = DateTime::createFromFormat($standard, $dateModified); break; } catch (\Exception $e) { if ($standard == null) { throw new Exception\RuntimeException( 'Could not load date due to unrecognised' .' format (should follow RFC 822 or 2822):' . $e->getMessage(), 0, $e ); } } } } } } if (!$date) { $date = $this->getExtension('DublinCore')->getDate(); } if (!$date) { $date = $this->getExtension('Atom')->getDateModified(); } if (!$date) { $date = null; } $this->data['datemodified'] = $date; return $this->data['datemodified']; } /** * Get the feed lastBuild date * * @throws Exception\RuntimeException * @return DateTime */ public function getLastBuildDate() { if (array_key_exists('lastBuildDate', $this->data)) { return $this->data['lastBuildDate']; } $lastBuildDate = null; $date = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $lastBuildDate = $this->xpath->evaluate('string(/rss/channel/lastBuildDate)'); if ($lastBuildDate) { $lastBuildDateParsed = strtotime($lastBuildDate); if ($lastBuildDateParsed) { $date = new DateTime('@' . $lastBuildDateParsed); } else { $dateStandards = array(DateTime::RSS, DateTime::RFC822, DateTime::RFC2822, null); foreach ($dateStandards as $standard) { try { $date = DateTime::createFromFormat($standard, $lastBuildDateParsed); break; } catch (\Exception $e) { if ($standard == null) { throw new Exception\RuntimeException( 'Could not load date due to unrecognised' .' format (should follow RFC 822 or 2822):' . $e->getMessage(), 0, $e ); } } } } } } if (!$date) { $date = null; } $this->data['lastBuildDate'] = $date; return $this->data['lastBuildDate']; } /** * Get the feed description * * @return string|null */ public function getDescription() { if (array_key_exists('description', $this->data)) { return $this->data['description']; } $description = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $description = $this->xpath->evaluate('string(/rss/channel/description)'); } else { $description = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/rss:description)'); } if (!$description && $this->getExtension('DublinCore') !== null) { $description = $this->getExtension('DublinCore')->getDescription(); } if (empty($description)) { $description = $this->getExtension('Atom')->getDescription(); } if (!$description) { $description = null; } $this->data['description'] = $description; return $this->data['description']; } /** * Get the feed ID * * @return string|null */ public function getId() { if (array_key_exists('id', $this->data)) { return $this->data['id']; } $id = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $id = $this->xpath->evaluate('string(/rss/channel/guid)'); } if (!$id && $this->getExtension('DublinCore') !== null) { $id = $this->getExtension('DublinCore')->getId(); } if (empty($id)) { $id = $this->getExtension('Atom')->getId(); } if (!$id) { if ($this->getLink()) { $id = $this->getLink(); } elseif ($this->getTitle()) { $id = $this->getTitle(); } else { $id = null; } } $this->data['id'] = $id; return $this->data['id']; } /** * Get the feed image data * * @return array|null */ public function getImage() { if (array_key_exists('image', $this->data)) { return $this->data['image']; } if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $list = $this->xpath->query('/rss/channel/image'); $prefix = '/rss/channel/image[1]'; } else { $list = $this->xpath->query('/rdf:RDF/rss:channel/rss:image'); $prefix = '/rdf:RDF/rss:channel/rss:image[1]'; } if ($list->length > 0) { $image = array(); $value = $this->xpath->evaluate('string(' . $prefix . '/url)'); if ($value) { $image['uri'] = $value; } $value = $this->xpath->evaluate('string(' . $prefix . '/link)'); if ($value) { $image['link'] = $value; } $value = $this->xpath->evaluate('string(' . $prefix . '/title)'); if ($value) { $image['title'] = $value; } $value = $this->xpath->evaluate('string(' . $prefix . '/height)'); if ($value) { $image['height'] = $value; } $value = $this->xpath->evaluate('string(' . $prefix . '/width)'); if ($value) { $image['width'] = $value; } $value = $this->xpath->evaluate('string(' . $prefix . '/description)'); if ($value) { $image['description'] = $value; } } else { $image = null; } $this->data['image'] = $image; return $this->data['image']; } /** * Get the feed language * * @return string|null */ public function getLanguage() { if (array_key_exists('language', $this->data)) { return $this->data['language']; } $language = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $language = $this->xpath->evaluate('string(/rss/channel/language)'); } if (!$language && $this->getExtension('DublinCore') !== null) { $language = $this->getExtension('DublinCore')->getLanguage(); } if (empty($language)) { $language = $this->getExtension('Atom')->getLanguage(); } if (!$language) { $language = $this->xpath->evaluate('string(//@xml:lang[1])'); } if (!$language) { $language = null; } $this->data['language'] = $language; return $this->data['language']; } /** * Get a link to the feed * * @return string|null */ public function getLink() { if (array_key_exists('link', $this->data)) { return $this->data['link']; } $link = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $link = $this->xpath->evaluate('string(/rss/channel/link)'); } else { $link = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/rss:link)'); } if (empty($link)) { $link = $this->getExtension('Atom')->getLink(); } if (!$link) { $link = null; } $this->data['link'] = $link; return $this->data['link']; } /** * Get a link to the feed XML * * @return string|null */ public function getFeedLink() { if (array_key_exists('feedlink', $this->data)) { return $this->data['feedlink']; } $link = null; $link = $this->getExtension('Atom')->getFeedLink(); if ($link === null || empty($link)) { $link = $this->getOriginalSourceUri(); } $this->data['feedlink'] = $link; return $this->data['feedlink']; } /** * Get the feed generator entry * * @return string|null */ public function getGenerator() { if (array_key_exists('generator', $this->data)) { return $this->data['generator']; } $generator = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $generator = $this->xpath->evaluate('string(/rss/channel/generator)'); } if (!$generator) { if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $generator = $this->xpath->evaluate('string(/rss/channel/atom:generator)'); } else { $generator = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/atom:generator)'); } } if (empty($generator)) { $generator = $this->getExtension('Atom')->getGenerator(); } if (!$generator) { $generator = null; } $this->data['generator'] = $generator; return $this->data['generator']; } /** * Get the feed title * * @return string|null */ public function getTitle() { if (array_key_exists('title', $this->data)) { return $this->data['title']; } $title = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $title = $this->xpath->evaluate('string(/rss/channel/title)'); } else { $title = $this->xpath->evaluate('string(/rdf:RDF/rss:channel/rss:title)'); } if (!$title && $this->getExtension('DublinCore') !== null) { $title = $this->getExtension('DublinCore')->getTitle(); } if (!$title) { $title = $this->getExtension('Atom')->getTitle(); } if (!$title) { $title = null; } $this->data['title'] = $title; return $this->data['title']; } /** * Get an array of any supported Pusubhubbub endpoints * * @return array|null */ public function getHubs() { if (array_key_exists('hubs', $this->data)) { return $this->data['hubs']; } $hubs = $this->getExtension('Atom')->getHubs(); if (empty($hubs)) { $hubs = null; } else { $hubs = array_unique($hubs); } $this->data['hubs'] = $hubs; return $this->data['hubs']; } /** * Get all categories * * @return Reader\Collection\Category */ public function getCategories() { if (array_key_exists('categories', $this->data)) { return $this->data['categories']; } if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $list = $this->xpath->query('/rss/channel//category'); } else { $list = $this->xpath->query('/rdf:RDF/rss:channel//rss:category'); } if ($list->length) { $categoryCollection = new Collection\Category; foreach ($list as $category) { $categoryCollection[] = array( 'term' => $category->nodeValue, 'scheme' => $category->getAttribute('domain'), 'label' => $category->nodeValue, ); } } else { $categoryCollection = $this->getExtension('DublinCore')->getCategories(); } if (count($categoryCollection) == 0) { $categoryCollection = $this->getExtension('Atom')->getCategories(); } $this->data['categories'] = $categoryCollection; return $this->data['categories']; } /** * Read all entries to the internal entries array * */ protected function indexEntries() { $entries = array(); if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $entries = $this->xpath->evaluate('//item'); } else { $entries = $this->xpath->evaluate('//rss:item'); } foreach ($entries as $index => $entry) { $this->entries[$index] = $entry; } } /** * Register the default namespaces for the current feed format * */ protected function registerNamespaces() { switch ($this->data['type']) { case Reader\Reader::TYPE_RSS_10: $this->xpath->registerNamespace('rdf', Reader\Reader::NAMESPACE_RDF); $this->xpath->registerNamespace('rss', Reader\Reader::NAMESPACE_RSS_10); break; case Reader\Reader::TYPE_RSS_090: $this->xpath->registerNamespace('rdf', Reader\Reader::NAMESPACE_RDF); $this->xpath->registerNamespace('rss', Reader\Reader::NAMESPACE_RSS_090); break; } } } |