Source of file Rss.php
Size: 16,168 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Feed/Reader/Entry/Rss.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 | <?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\Entry; use DateTime; use DOMElement; use DOMXPath; use Zend\Feed\Reader; use Zend\Feed\Reader\Exception; class Rss extends AbstractEntry implements EntryInterface { /** * XPath query for RDF * * @var string */ protected $xpathQueryRdf = ''; /** * XPath query for RSS * * @var string */ protected $xpathQueryRss = ''; /** * Constructor * * @param DOMElement $entry * @param string $entryKey * @param string $type */ public function __construct(DOMElement $entry, $entryKey, $type = null) { parent::__construct($entry, $entryKey, $type); $this->xpathQueryRss = '//item[' . ($this->entryKey+1) . ']'; $this->xpathQueryRdf = '//rss:item[' . ($this->entryKey+1) . ']'; $manager = Reader\Reader::getExtensionManager(); $extensions = array( 'DublinCore\Entry', 'Content\Entry', 'Atom\Entry', 'WellFormedWeb\Entry', 'Slash\Entry', 'Thread\Entry', ); foreach ($extensions as $name) { $extension = $manager->get($name); $extension->setEntryElement($entry); $extension->setEntryKey($entryKey); $extension->setType($type); $this->extensions[$name] = $extension; } } /** * Get an author entry * * @param int $index * @return string */ 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'] ); } } if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $list = $this->xpath->query($this->xpathQueryRss . '//author'); } else { $list = $this->xpath->query($this->xpathQueryRdf . '//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 entry content * * @return string */ public function getContent() { if (array_key_exists('content', $this->data)) { return $this->data['content']; } $content = $this->getExtension('Content')->getContent(); if (!$content) { $content = $this->getDescription(); } if (empty($content)) { $content = $this->getExtension('Atom')->getContent(); } $this->data['content'] = $content; return $this->data['content']; } /** * Get the entry's date of creation * * @return string */ public function getDateCreated() { return $this->getDateModified(); } /** * Get the entry's date of modification * * @throws Exception\RuntimeException * @return string */ 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(' . $this->xpathQueryRss . '/pubDate)'); 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 = date_create_from_format($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 entry description * * @return string */ 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(' . $this->xpathQueryRss . '/description)'); } else { $description = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:description)'); } if (!$description) { $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 entry enclosure * @return string */ public function getEnclosure() { if (array_key_exists('enclosure', $this->data)) { return $this->data['enclosure']; } $enclosure = null; if ($this->getType() == Reader\Reader::TYPE_RSS_20) { $nodeList = $this->xpath->query($this->xpathQueryRss . '/enclosure'); if ($nodeList->length > 0) { $enclosure = new \stdClass(); $enclosure->url = $nodeList->item(0)->getAttribute('url'); $enclosure->length = $nodeList->item(0)->getAttribute('length'); $enclosure->type = $nodeList->item(0)->getAttribute('type'); } } if (!$enclosure) { $enclosure = $this->getExtension('Atom')->getEnclosure(); } $this->data['enclosure'] = $enclosure; return $this->data['enclosure']; } /** * Get the entry ID * * @return string */ 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(' . $this->xpathQueryRss . '/guid)'); } if (!$id) { $id = $this->getExtension('DublinCore')->getId(); } if (empty($id)) { $id = $this->getExtension('Atom')->getId(); } if (!$id) { if ($this->getPermalink()) { $id = $this->getPermalink(); } elseif ($this->getTitle()) { $id = $this->getTitle(); } else { $id = null; } } $this->data['id'] = $id; return $this->data['id']; } /** * Get a specific link * * @param int $index * @return string */ public function getLink($index = 0) { if (!array_key_exists('links', $this->data)) { $this->getLinks(); } if (isset($this->data['links'][$index])) { return $this->data['links'][$index]; } return null; } /** * Get all links * * @return array */ public function getLinks() { if (array_key_exists('links', $this->data)) { return $this->data['links']; } $links = array(); if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090) { $list = $this->xpath->query($this->xpathQueryRss . '//link'); } else { $list = $this->xpath->query($this->xpathQueryRdf . '//rss:link'); } if (!$list->length) { $links = $this->getExtension('Atom')->getLinks(); } else { foreach ($list as $link) { $links[] = $link->nodeValue; } } $this->data['links'] = $links; return $this->data['links']; } /** * 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($this->xpathQueryRss . '//category'); } else { $list = $this->xpath->query($this->xpathQueryRdf . '//rss:category'); } if ($list->length) { $categoryCollection = new Reader\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']; } /** * Get a permalink to the entry * * @return string */ public function getPermalink() { return $this->getLink(0); } /** * Get the entry title * * @return string */ 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(' . $this->xpathQueryRss . '/title)'); } else { $title = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:title)'); } if (!$title) { $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 the number of comments/replies for current entry * * @return string|null */ public function getCommentCount() { if (array_key_exists('commentcount', $this->data)) { return $this->data['commentcount']; } $commentcount = $this->getExtension('Slash')->getCommentCount(); if (!$commentcount) { $commentcount = $this->getExtension('Thread')->getCommentCount(); } if (!$commentcount) { $commentcount = $this->getExtension('Atom')->getCommentCount(); } if (!$commentcount) { $commentcount = null; } $this->data['commentcount'] = $commentcount; return $this->data['commentcount']; } /** * Returns a URI pointing to the HTML page where comments can be made on this entry * * @return string */ public function getCommentLink() { if (array_key_exists('commentlink', $this->data)) { return $this->data['commentlink']; } $commentlink = null; if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && $this->getType() !== Reader\Reader::TYPE_RSS_090 ) { $commentlink = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/comments)'); } if (!$commentlink) { $commentlink = $this->getExtension('Atom')->getCommentLink(); } if (!$commentlink) { $commentlink = null; } $this->data['commentlink'] = $commentlink; return $this->data['commentlink']; } /** * Returns a URI pointing to a feed of all comments for this entry * * @return string */ public function getCommentFeedLink() { if (array_key_exists('commentfeedlink', $this->data)) { return $this->data['commentfeedlink']; } $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink(); if (!$commentfeedlink) { $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss'); } if (!$commentfeedlink) { $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf'); } if (!$commentfeedlink) { $commentfeedlink = null; } $this->data['commentfeedlink'] = $commentfeedlink; return $this->data['commentfeedlink']; } /** * Set the XPath query (incl. on all Extensions) * * @param DOMXPath $xpath * @return void */ public function setXpath(DOMXPath $xpath) { parent::setXpath($xpath); foreach ($this->extensions as $extension) { $extension->setXpath($this->xpath); } } } |