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\Extension\Atom;
17 use Zend\Feed\Reader\Collection;
18 use Zend\Feed\Reader\Extension;
21 class Entry extends Extension\AbstractEntry
24 * Get the specified author
29 public function getAuthor($index = 0)
31 $authors = $this->getAuthors();
33 if (isset($authors[$index])) {
34 return $authors[$index];
41 * Get an array with feed authors
43 * @return Collection\Author
45 public function getAuthors()
47 if (array_key_exists('authors', $this->data)) {
48 return $this->data['authors'];
52 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:author');
54 if (! $list->length) {
56 * TODO: Limit query to feed level els only!
58 $list = $this->getXpath()->query('//atom:author');
62 foreach ($list as $author) {
63 $author = $this->getAuthorFromElement($author);
64 if (! empty($author)) {
70 if (count($authors) == 0) {
71 $authors = new Collection\Author();
73 $authors = new Collection\Author(
74 Reader\Reader::arrayUnique($authors)
78 $this->data['authors'] = $authors;
79 return $this->data['authors'];
83 * Get the entry content
87 public function getContent()
89 if (array_key_exists('content', $this->data)) {
90 return $this->data['content'];
95 $el = $this->getXpath()->query($this->getXpathPrefix() . '/atom:content');
96 if ($el->length > 0) {
98 $type = $el->getAttribute('type');
105 $content = $el->nodeValue;
108 $this->getXpath()->registerNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
109 $xhtml = $this->getXpath()->query(
110 $this->getXpathPrefix() . '/atom:content/xhtml:div'
112 $d = new DOMDocument('1.0', $this->getEncoding());
113 $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
114 $xhtmls = $d->importNode($xhtml, $deep);
115 $d->appendChild($xhtmls);
116 $content = $this->collectXhtml(
118 $d->lookupPrefix('http://www.w3.org/1999/xhtml')
125 $content = $this->getDescription();
128 $this->data['content'] = trim($content);
130 return $this->data['content'];
134 * Parse out XHTML to remove the namespacing
140 protected function collectXhtml($xhtml, $prefix)
142 if (! empty($prefix)) {
143 $prefix = $prefix . ':';
146 "/<\?xml[^<]*>[^<]*<" . $prefix . "div[^<]*/",
147 "/<\/" . $prefix . "div>\s*$/"
149 $xhtml = preg_replace($matches, '', $xhtml);
150 if (! empty($prefix)) {
151 $xhtml = preg_replace("/(<[\/]?)" . $prefix . "([a-zA-Z]+)/", '$1$2', $xhtml);
157 * Get the entry creation date
161 public function getDateCreated()
163 if (array_key_exists('datecreated', $this->data)) {
164 return $this->data['datecreated'];
169 if ($this->getAtomType() === Reader\Reader::TYPE_ATOM_03) {
170 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
172 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
176 $date = new DateTime($dateCreated);
179 $this->data['datecreated'] = $date;
181 return $this->data['datecreated'];
185 * Get the entry modification date
189 public function getDateModified()
191 if (array_key_exists('datemodified', $this->data)) {
192 return $this->data['datemodified'];
197 if ($this->getAtomType() === Reader\Reader::TYPE_ATOM_03) {
198 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
200 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
204 $date = new DateTime($dateModified);
207 $this->data['datemodified'] = $date;
209 return $this->data['datemodified'];
213 * Get the entry description
217 public function getDescription()
219 if (array_key_exists('description', $this->data)) {
220 return $this->data['description'];
223 $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:summary)');
225 if (! $description) {
229 $this->data['description'] = $description;
231 return $this->data['description'];
235 * Get the entry enclosure
239 public function getEnclosure()
241 if (array_key_exists('enclosure', $this->data)) {
242 return $this->data['enclosure'];
247 $nodeList = $this->getXpath()->query($this->getXpathPrefix() . '/atom:link[@rel="enclosure"]');
249 if ($nodeList->length > 0) {
250 $enclosure = new stdClass();
251 $enclosure->url = $nodeList->item(0)->getAttribute('href');
252 $enclosure->length = $nodeList->item(0)->getAttribute('length');
253 $enclosure->type = $nodeList->item(0)->getAttribute('type');
256 $this->data['enclosure'] = $enclosure;
258 return $this->data['enclosure'];
266 public function getId()
268 if (array_key_exists('id', $this->data)) {
269 return $this->data['id'];
272 $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
275 if ($this->getPermalink()) {
276 $id = $this->getPermalink();
277 } elseif ($this->getTitle()) {
278 $id = $this->getTitle();
284 $this->data['id'] = $id;
286 return $this->data['id'];
290 * Get the base URI of the feed (if set).
292 * @return string|null
294 public function getBaseUrl()
296 if (array_key_exists('baseUrl', $this->data)) {
297 return $this->data['baseUrl'];
300 $baseUrl = $this->getXpath()->evaluate(
302 . $this->getXpathPrefix()
308 $baseUrl = $this->getXpath()->evaluate('string(//@xml:base[1])');
315 $this->data['baseUrl'] = $baseUrl;
317 return $this->data['baseUrl'];
321 * Get a specific link
326 public function getLink($index = 0)
328 if (! array_key_exists('links', $this->data)) {
332 if (isset($this->data['links'][$index])) {
333 return $this->data['links'][$index];
344 public function getLinks()
346 if (array_key_exists('links', $this->data)) {
347 return $this->data['links'];
352 $list = $this->getXpath()->query(
353 $this->getXpathPrefix() . '//atom:link[@rel="alternate"]/@href' . '|' .
354 $this->getXpathPrefix() . '//atom:link[not(@rel)]/@href'
358 foreach ($list as $link) {
359 $links[] = $this->absolutiseUri($link->value);
363 $this->data['links'] = $links;
365 return $this->data['links'];
369 * Get a permalink to the entry
373 public function getPermalink()
375 return $this->getLink(0);
379 * Get the entry title
383 public function getTitle()
385 if (array_key_exists('title', $this->data)) {
386 return $this->data['title'];
389 $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
395 $this->data['title'] = $title;
397 return $this->data['title'];
401 * Get the number of comments/replies for current entry
405 public function getCommentCount()
407 if (array_key_exists('commentcount', $this->data)) {
408 return $this->data['commentcount'];
413 $this->getXpath()->registerNamespace('thread10', 'http://purl.org/syndication/thread/1.0');
414 $list = $this->getXpath()->query(
415 $this->getXpathPrefix() . '//atom:link[@rel="replies"]/@thread10:count'
419 $count = $list->item(0)->value;
422 $this->data['commentcount'] = $count;
424 return $this->data['commentcount'];
428 * Returns a URI pointing to the HTML page where comments can be made on this entry
432 public function getCommentLink()
434 if (array_key_exists('commentlink', $this->data)) {
435 return $this->data['commentlink'];
440 $list = $this->getXpath()->query(
441 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="text/html"]/@href'
445 $link = $list->item(0)->value;
446 $link = $this->absolutiseUri($link);
449 $this->data['commentlink'] = $link;
451 return $this->data['commentlink'];
455 * Returns a URI pointing to a feed of all comments for this entry
457 * @param string $type
460 public function getCommentFeedLink($type = 'atom')
462 if (array_key_exists('commentfeedlink', $this->data)) {
463 return $this->data['commentfeedlink'];
468 $list = $this->getXpath()->query(
469 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="application/' . $type.'+xml"]/@href'
473 $link = $list->item(0)->value;
474 $link = $this->absolutiseUri($link);
477 $this->data['commentfeedlink'] = $link;
479 return $this->data['commentfeedlink'];
485 * @return Collection\Category
487 public function getCategories()
489 if (array_key_exists('categories', $this->data)) {
490 return $this->data['categories'];
493 if ($this->getAtomType() == Reader\Reader::TYPE_ATOM_10) {
494 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:category');
497 * Since Atom 0.3 did not support categories, it would have used the
498 * Dublin Core extension. However there is a small possibility Atom 0.3
499 * may have been retrofitted to use Atom 1.0 instead.
501 $this->getXpath()->registerNamespace('atom10', Reader\Reader::NAMESPACE_ATOM_10);
502 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom10:category');
506 $categoryCollection = new Collection\Category;
507 foreach ($list as $category) {
508 $categoryCollection[] = [
509 'term' => $category->getAttribute('term'),
510 'scheme' => $category->getAttribute('scheme'),
511 'label' => $category->getAttribute('label')
515 return new Collection\Category;
518 $this->data['categories'] = $categoryCollection;
520 return $this->data['categories'];
524 * Get source feed metadata from the entry
526 * @return Reader\Feed\Atom\Source|null
528 public function getSource()
530 if (array_key_exists('source', $this->data)) {
531 return $this->data['source'];
535 // TODO: Investigate why _getAtomType() fails here. Is it even needed?
536 if ($this->getType() == Reader\Reader::TYPE_ATOM_10) {
537 $list = $this->getXpath()->query($this->getXpathPrefix() . '/atom:source[1]');
539 $element = $list->item(0);
540 $source = new Reader\Feed\Atom\Source($element, $this->getXpathPrefix());
544 $this->data['source'] = $source;
545 return $this->data['source'];
549 * Attempt to absolutise the URI, i.e. if a relative URI apply the
550 * xml:base value as a prefix to turn into an absolute URI.
555 protected function absolutiseUri($link)
557 if (! Uri::factory($link)->isAbsolute()) {
558 if ($this->getBaseUrl() !== null) {
559 $link = $this->getBaseUrl() . $link;
560 if (! Uri::factory($link)->isValid()) {
569 * Get an author entry
571 * @param DOMElement $element
574 protected function getAuthorFromElement(DOMElement $element)
578 $emailNode = $element->getElementsByTagName('email');
579 $nameNode = $element->getElementsByTagName('name');
580 $uriNode = $element->getElementsByTagName('uri');
582 if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
583 $author['email'] = $emailNode->item(0)->nodeValue;
586 if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
587 $author['name'] = $nameNode->item(0)->nodeValue;
590 if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
591 $author['uri'] = $uriNode->item(0)->nodeValue;
594 if (empty($author)) {
601 * Register the default namespaces for the current feed format
603 protected function registerNamespaces()
605 switch ($this->getAtomType()) {
606 case Reader\Reader::TYPE_ATOM_03:
607 $this->getXpath()->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_03);
610 $this->getXpath()->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_10);
616 * Detect the presence of any Atom namespaces in use
620 protected function getAtomType()
622 $dom = $this->getDomDocument();
623 $prefixAtom03 = $dom->lookupPrefix(Reader\Reader::NAMESPACE_ATOM_03);
624 $prefixAtom10 = $dom->lookupPrefix(Reader\Reader::NAMESPACE_ATOM_10);
625 if ($dom->isDefaultNamespace(Reader\Reader::NAMESPACE_ATOM_03)
626 || ! empty($prefixAtom03)) {
627 return Reader\Reader::TYPE_ATOM_03;
629 if ($dom->isDefaultNamespace(Reader\Reader::NAMESPACE_ATOM_10)
630 || ! empty($prefixAtom10)) {
631 return Reader\Reader::TYPE_ATOM_10;