Chris@0: xpathQueryRss = '//item[' . ($this->entryKey + 1) . ']'; Chris@12: $this->xpathQueryRdf = '//rss:item[' . ($this->entryKey + 1) . ']'; Chris@0: Chris@0: $manager = Reader\Reader::getExtensionManager(); Chris@0: $extensions = [ Chris@0: 'DublinCore\Entry', Chris@0: 'Content\Entry', Chris@0: 'Atom\Entry', Chris@0: 'WellFormedWeb\Entry', Chris@0: 'Slash\Entry', Chris@0: 'Thread\Entry', Chris@0: ]; Chris@0: foreach ($extensions as $name) { Chris@0: $extension = $manager->get($name); Chris@0: $extension->setEntryElement($entry); Chris@0: $extension->setEntryKey($entryKey); Chris@0: $extension->setType($type); Chris@0: $this->extensions[$name] = $extension; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@18: * @inheritdoc Chris@0: */ Chris@0: public function getAuthor($index = 0) Chris@0: { Chris@0: $authors = $this->getAuthors(); Chris@0: Chris@0: if (isset($authors[$index])) { Chris@0: return $authors[$index]; Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get an array with feed authors Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getAuthors() Chris@0: { Chris@0: if (array_key_exists('authors', $this->data)) { Chris@0: return $this->data['authors']; Chris@0: } Chris@0: Chris@0: $authors = []; Chris@0: $authorsDc = $this->getExtension('DublinCore')->getAuthors(); Chris@12: if (! empty($authorsDc)) { Chris@0: foreach ($authorsDc as $author) { Chris@0: $authors[] = [ Chris@0: 'name' => $author['name'] Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 Chris@0: && $this->getType() !== Reader\Reader::TYPE_RSS_090) { Chris@0: $list = $this->xpath->query($this->xpathQueryRss . '//author'); Chris@0: } else { Chris@0: $list = $this->xpath->query($this->xpathQueryRdf . '//rss:author'); Chris@0: } Chris@0: if ($list->length) { Chris@0: foreach ($list as $author) { Chris@0: $string = trim($author->nodeValue); Chris@0: $data = []; Chris@0: // Pretty rough parsing - but it's a catchall Chris@0: if (preg_match("/^.*@[^ ]*/", $string, $matches)) { Chris@0: $data['email'] = trim($matches[0]); Chris@0: if (preg_match("/\((.*)\)$/", $string, $matches)) { Chris@0: $data['name'] = $matches[1]; Chris@0: } Chris@0: $authors[] = $data; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (count($authors) == 0) { Chris@0: $authors = $this->getExtension('Atom')->getAuthors(); Chris@0: } else { Chris@0: $authors = new Reader\Collection\Author( Chris@0: Reader\Reader::arrayUnique($authors) Chris@0: ); Chris@0: } Chris@0: Chris@0: if (count($authors) == 0) { Chris@0: $authors = null; Chris@0: } Chris@0: Chris@0: $this->data['authors'] = $authors; Chris@0: Chris@0: return $this->data['authors']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry content Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getContent() Chris@0: { Chris@0: if (array_key_exists('content', $this->data)) { Chris@0: return $this->data['content']; Chris@0: } Chris@0: Chris@0: $content = $this->getExtension('Content')->getContent(); Chris@0: Chris@12: if (! $content) { Chris@0: $content = $this->getDescription(); Chris@0: } Chris@0: Chris@0: if (empty($content)) { Chris@0: $content = $this->getExtension('Atom')->getContent(); Chris@0: } Chris@0: Chris@0: $this->data['content'] = $content; Chris@0: Chris@0: return $this->data['content']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry's date of creation Chris@0: * Chris@0: * @return \DateTime Chris@0: */ Chris@0: public function getDateCreated() Chris@0: { Chris@0: return $this->getDateModified(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry's date of modification Chris@0: * Chris@0: * @throws Exception\RuntimeException Chris@0: * @return \DateTime Chris@0: */ Chris@0: public function getDateModified() Chris@0: { Chris@0: if (array_key_exists('datemodified', $this->data)) { Chris@0: return $this->data['datemodified']; Chris@0: } Chris@0: Chris@0: $date = null; Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 Chris@0: && $this->getType() !== Reader\Reader::TYPE_RSS_090 Chris@0: ) { Chris@0: $dateModified = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/pubDate)'); Chris@0: if ($dateModified) { Chris@0: $dateModifiedParsed = strtotime($dateModified); Chris@0: if ($dateModifiedParsed) { Chris@0: $date = new DateTime('@' . $dateModifiedParsed); Chris@0: } else { Chris@0: $dateStandards = [DateTime::RSS, DateTime::RFC822, Chris@0: DateTime::RFC2822, null]; Chris@0: foreach ($dateStandards as $standard) { Chris@0: try { Chris@0: $date = date_create_from_format($standard, $dateModified); Chris@0: break; Chris@0: } catch (\Exception $e) { Chris@0: if ($standard === null) { Chris@0: throw new Exception\RuntimeException( Chris@0: 'Could not load date due to unrecognised' Chris@0: .' format (should follow RFC 822 or 2822):' Chris@0: . $e->getMessage(), Chris@12: 0, Chris@12: $e Chris@0: ); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@12: if (! $date) { Chris@0: $date = $this->getExtension('DublinCore')->getDate(); Chris@0: } Chris@0: Chris@12: if (! $date) { Chris@0: $date = $this->getExtension('Atom')->getDateModified(); Chris@0: } Chris@0: Chris@12: if (! $date) { Chris@0: $date = null; Chris@0: } Chris@0: Chris@0: $this->data['datemodified'] = $date; Chris@0: Chris@0: return $this->data['datemodified']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry description Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getDescription() Chris@0: { Chris@0: if (array_key_exists('description', $this->data)) { Chris@0: return $this->data['description']; Chris@0: } Chris@0: Chris@0: $description = null; Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 Chris@0: && $this->getType() !== Reader\Reader::TYPE_RSS_090 Chris@0: ) { Chris@0: $description = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/description)'); Chris@0: } else { Chris@0: $description = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:description)'); Chris@0: } Chris@0: Chris@12: if (! $description) { Chris@0: $description = $this->getExtension('DublinCore')->getDescription(); Chris@0: } Chris@0: Chris@0: if (empty($description)) { Chris@0: $description = $this->getExtension('Atom')->getDescription(); Chris@0: } Chris@0: Chris@12: if (! $description) { Chris@0: $description = null; Chris@0: } Chris@0: Chris@0: $this->data['description'] = $description; Chris@0: Chris@0: return $this->data['description']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry enclosure Chris@0: * @return string Chris@0: */ Chris@0: public function getEnclosure() Chris@0: { Chris@0: if (array_key_exists('enclosure', $this->data)) { Chris@0: return $this->data['enclosure']; Chris@0: } Chris@0: Chris@0: $enclosure = null; Chris@0: Chris@0: if ($this->getType() == Reader\Reader::TYPE_RSS_20) { Chris@0: $nodeList = $this->xpath->query($this->xpathQueryRss . '/enclosure'); Chris@0: Chris@0: if ($nodeList->length > 0) { Chris@0: $enclosure = new \stdClass(); Chris@0: $enclosure->url = $nodeList->item(0)->getAttribute('url'); Chris@0: $enclosure->length = $nodeList->item(0)->getAttribute('length'); Chris@0: $enclosure->type = $nodeList->item(0)->getAttribute('type'); Chris@0: } Chris@0: } Chris@0: Chris@12: if (! $enclosure) { Chris@0: $enclosure = $this->getExtension('Atom')->getEnclosure(); Chris@0: } Chris@0: Chris@0: $this->data['enclosure'] = $enclosure; Chris@0: Chris@0: return $this->data['enclosure']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry ID Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getId() Chris@0: { Chris@0: if (array_key_exists('id', $this->data)) { Chris@0: return $this->data['id']; Chris@0: } Chris@0: Chris@0: $id = null; Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 Chris@0: && $this->getType() !== Reader\Reader::TYPE_RSS_090 Chris@0: ) { Chris@0: $id = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/guid)'); Chris@0: } Chris@0: Chris@12: if (! $id) { Chris@0: $id = $this->getExtension('DublinCore')->getId(); Chris@0: } Chris@0: Chris@0: if (empty($id)) { Chris@0: $id = $this->getExtension('Atom')->getId(); Chris@0: } Chris@0: Chris@12: if (! $id) { Chris@0: if ($this->getPermalink()) { Chris@0: $id = $this->getPermalink(); Chris@0: } elseif ($this->getTitle()) { Chris@0: $id = $this->getTitle(); Chris@0: } else { Chris@0: $id = null; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->data['id'] = $id; Chris@0: Chris@0: return $this->data['id']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a specific link Chris@0: * Chris@0: * @param int $index Chris@0: * @return string Chris@0: */ Chris@0: public function getLink($index = 0) Chris@0: { Chris@12: if (! array_key_exists('links', $this->data)) { Chris@0: $this->getLinks(); Chris@0: } Chris@0: Chris@0: if (isset($this->data['links'][$index])) { Chris@0: return $this->data['links'][$index]; Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get all links Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getLinks() Chris@0: { Chris@0: if (array_key_exists('links', $this->data)) { Chris@0: return $this->data['links']; Chris@0: } Chris@0: Chris@0: $links = []; Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && Chris@0: $this->getType() !== Reader\Reader::TYPE_RSS_090) { Chris@0: $list = $this->xpath->query($this->xpathQueryRss . '//link'); Chris@0: } else { Chris@0: $list = $this->xpath->query($this->xpathQueryRdf . '//rss:link'); Chris@0: } Chris@0: Chris@12: if (! $list->length) { Chris@0: $links = $this->getExtension('Atom')->getLinks(); Chris@0: } else { Chris@0: foreach ($list as $link) { Chris@0: $links[] = $link->nodeValue; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->data['links'] = $links; Chris@0: Chris@0: return $this->data['links']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get all categories Chris@0: * Chris@0: * @return Reader\Collection\Category Chris@0: */ Chris@0: public function getCategories() Chris@0: { Chris@0: if (array_key_exists('categories', $this->data)) { Chris@0: return $this->data['categories']; Chris@0: } Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 && Chris@0: $this->getType() !== Reader\Reader::TYPE_RSS_090) { Chris@0: $list = $this->xpath->query($this->xpathQueryRss . '//category'); Chris@0: } else { Chris@0: $list = $this->xpath->query($this->xpathQueryRdf . '//rss:category'); Chris@0: } Chris@0: Chris@0: if ($list->length) { Chris@0: $categoryCollection = new Reader\Collection\Category; Chris@0: foreach ($list as $category) { Chris@0: $categoryCollection[] = [ Chris@0: 'term' => $category->nodeValue, Chris@0: 'scheme' => $category->getAttribute('domain'), Chris@0: 'label' => $category->nodeValue, Chris@0: ]; Chris@0: } Chris@0: } else { Chris@0: $categoryCollection = $this->getExtension('DublinCore')->getCategories(); Chris@0: } Chris@0: Chris@0: if (count($categoryCollection) == 0) { Chris@0: $categoryCollection = $this->getExtension('Atom')->getCategories(); Chris@0: } Chris@0: Chris@0: $this->data['categories'] = $categoryCollection; Chris@0: Chris@0: return $this->data['categories']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a permalink to the entry Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getPermalink() Chris@0: { Chris@0: return $this->getLink(0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the entry title Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getTitle() Chris@0: { Chris@0: if (array_key_exists('title', $this->data)) { Chris@0: return $this->data['title']; Chris@0: } Chris@0: Chris@0: $title = null; Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 Chris@0: && $this->getType() !== Reader\Reader::TYPE_RSS_090 Chris@0: ) { Chris@0: $title = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/title)'); Chris@0: } else { Chris@0: $title = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:title)'); Chris@0: } Chris@0: Chris@12: if (! $title) { Chris@0: $title = $this->getExtension('DublinCore')->getTitle(); Chris@0: } Chris@0: Chris@12: if (! $title) { Chris@0: $title = $this->getExtension('Atom')->getTitle(); Chris@0: } Chris@0: Chris@12: if (! $title) { Chris@0: $title = null; Chris@0: } Chris@0: Chris@0: $this->data['title'] = $title; Chris@0: Chris@0: return $this->data['title']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the number of comments/replies for current entry Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: public function getCommentCount() Chris@0: { Chris@0: if (array_key_exists('commentcount', $this->data)) { Chris@0: return $this->data['commentcount']; Chris@0: } Chris@0: Chris@0: $commentcount = $this->getExtension('Slash')->getCommentCount(); Chris@0: Chris@12: if (! $commentcount) { Chris@0: $commentcount = $this->getExtension('Thread')->getCommentCount(); Chris@0: } Chris@0: Chris@12: if (! $commentcount) { Chris@0: $commentcount = $this->getExtension('Atom')->getCommentCount(); Chris@0: } Chris@0: Chris@12: if (! $commentcount) { Chris@0: $commentcount = null; Chris@0: } Chris@0: Chris@0: $this->data['commentcount'] = $commentcount; Chris@0: Chris@0: return $this->data['commentcount']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a URI pointing to the HTML page where comments can be made on this entry Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getCommentLink() Chris@0: { Chris@0: if (array_key_exists('commentlink', $this->data)) { Chris@0: return $this->data['commentlink']; Chris@0: } Chris@0: Chris@0: $commentlink = null; Chris@0: Chris@0: if ($this->getType() !== Reader\Reader::TYPE_RSS_10 Chris@0: && $this->getType() !== Reader\Reader::TYPE_RSS_090 Chris@0: ) { Chris@0: $commentlink = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/comments)'); Chris@0: } Chris@0: Chris@12: if (! $commentlink) { Chris@0: $commentlink = $this->getExtension('Atom')->getCommentLink(); Chris@0: } Chris@0: Chris@12: if (! $commentlink) { Chris@0: $commentlink = null; Chris@0: } Chris@0: Chris@0: $this->data['commentlink'] = $commentlink; Chris@0: Chris@0: return $this->data['commentlink']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a URI pointing to a feed of all comments for this entry Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getCommentFeedLink() Chris@0: { Chris@0: if (array_key_exists('commentfeedlink', $this->data)) { Chris@0: return $this->data['commentfeedlink']; Chris@0: } Chris@0: Chris@0: $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink(); Chris@0: Chris@12: if (! $commentfeedlink) { Chris@0: $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss'); Chris@0: } Chris@0: Chris@12: if (! $commentfeedlink) { Chris@0: $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf'); Chris@0: } Chris@0: Chris@12: if (! $commentfeedlink) { Chris@0: $commentfeedlink = null; Chris@0: } Chris@0: Chris@0: $this->data['commentfeedlink'] = $commentfeedlink; Chris@0: Chris@0: return $this->data['commentfeedlink']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the XPath query (incl. on all Extensions) Chris@0: * Chris@0: * @param DOMXPath $xpath Chris@0: * @return void Chris@0: */ Chris@0: public function setXpath(DOMXPath $xpath) Chris@0: { Chris@0: parent::setXpath($xpath); Chris@0: foreach ($this->extensions as $extension) { Chris@0: $extension->setXpath($this->xpath); Chris@0: } Chris@0: } Chris@0: }