3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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');
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 $xhtmls = $d->importNode($xhtml, true);
114 $d->appendChild($xhtmls);
115 $content = $this->collectXhtml(
117 $d->lookupPrefix('http://www.w3.org/1999/xhtml')
124 $content = $this->getDescription();
127 $this->data
['content'] = trim($content);
129 return $this->data
['content'];
133 * Parse out XHTML to remove the namespacing
139 protected function collectXhtml($xhtml, $prefix)
141 if (!empty($prefix)) $prefix = $prefix . ':';
143 "/<\?xml[^<]*>[^<]*<" . $prefix . "div[^<]*/",
144 "/<\/" . $prefix . "div>\s*$/"
146 $xhtml = preg_replace($matches, '', $xhtml);
147 if (!empty($prefix)) {
148 $xhtml = preg_replace("/(<[\/]?)" . $prefix . "([a-zA-Z]+)/", '$1$2', $xhtml);
154 * Get the entry creation date
158 public function getDateCreated()
160 if (array_key_exists('datecreated', $this->data
)) {
161 return $this->data
['datecreated'];
166 if ($this->getAtomType() === Reader\Reader
::TYPE_ATOM_03
) {
167 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
169 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
173 $date = new DateTime($dateCreated);
176 $this->data
['datecreated'] = $date;
178 return $this->data
['datecreated'];
182 * Get the entry modification date
186 public function getDateModified()
188 if (array_key_exists('datemodified', $this->data
)) {
189 return $this->data
['datemodified'];
194 if ($this->getAtomType() === Reader\Reader
::TYPE_ATOM_03
) {
195 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
197 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
201 $date = new DateTime($dateModified);
204 $this->data
['datemodified'] = $date;
206 return $this->data
['datemodified'];
210 * Get the entry description
214 public function getDescription()
216 if (array_key_exists('description', $this->data
)) {
217 return $this->data
['description'];
220 $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:summary)');
226 $this->data
['description'] = $description;
228 return $this->data
['description'];
232 * Get the entry enclosure
236 public function getEnclosure()
238 if (array_key_exists('enclosure', $this->data
)) {
239 return $this->data
['enclosure'];
244 $nodeList = $this->getXpath()->query($this->getXpathPrefix() . '/atom:link[@rel="enclosure"]');
246 if ($nodeList->length
> 0) {
247 $enclosure = new stdClass();
248 $enclosure->url
= $nodeList->item(0)->getAttribute('href');
249 $enclosure->length
= $nodeList->item(0)->getAttribute('length');
250 $enclosure->type
= $nodeList->item(0)->getAttribute('type');
253 $this->data
['enclosure'] = $enclosure;
255 return $this->data
['enclosure'];
263 public function getId()
265 if (array_key_exists('id', $this->data
)) {
266 return $this->data
['id'];
269 $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
272 if ($this->getPermalink()) {
273 $id = $this->getPermalink();
274 } elseif ($this->getTitle()) {
275 $id = $this->getTitle();
281 $this->data
['id'] = $id;
283 return $this->data
['id'];
287 * Get the base URI of the feed (if set).
289 * @return string|null
291 public function getBaseUrl()
293 if (array_key_exists('baseUrl', $this->data
)) {
294 return $this->data
['baseUrl'];
297 $baseUrl = $this->getXpath()->evaluate('string('
298 . $this->getXpathPrefix() . '/@xml:base[1]'
302 $baseUrl = $this->getXpath()->evaluate('string(//@xml:base[1])');
309 $this->data
['baseUrl'] = $baseUrl;
311 return $this->data
['baseUrl'];
315 * Get a specific link
320 public function getLink($index = 0)
322 if (!array_key_exists('links', $this->data
)) {
326 if (isset($this->data
['links'][$index])) {
327 return $this->data
['links'][$index];
338 public function getLinks()
340 if (array_key_exists('links', $this->data
)) {
341 return $this->data
['links'];
346 $list = $this->getXpath()->query(
347 $this->getXpathPrefix() . '//atom:link[@rel="alternate"]/@href' . '|' .
348 $this->getXpathPrefix() . '//atom:link[not(@rel)]/@href'
352 foreach ($list as $link) {
353 $links[] = $this->absolutiseUri($link->value
);
357 $this->data
['links'] = $links;
359 return $this->data
['links'];
363 * Get a permalink to the entry
367 public function getPermalink()
369 return $this->getLink(0);
373 * Get the entry title
377 public function getTitle()
379 if (array_key_exists('title', $this->data
)) {
380 return $this->data
['title'];
383 $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
389 $this->data
['title'] = $title;
391 return $this->data
['title'];
395 * Get the number of comments/replies for current entry
399 public function getCommentCount()
401 if (array_key_exists('commentcount', $this->data
)) {
402 return $this->data
['commentcount'];
407 $this->getXpath()->registerNamespace('thread10', 'http://purl.org/syndication/thread/1.0');
408 $list = $this->getXpath()->query(
409 $this->getXpathPrefix() . '//atom:link[@rel="replies"]/@thread10:count'
413 $count = $list->item(0)->value
;
416 $this->data
['commentcount'] = $count;
418 return $this->data
['commentcount'];
422 * Returns a URI pointing to the HTML page where comments can be made on this entry
426 public function getCommentLink()
428 if (array_key_exists('commentlink', $this->data
)) {
429 return $this->data
['commentlink'];
434 $list = $this->getXpath()->query(
435 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="text/html"]/@href'
439 $link = $list->item(0)->value
;
440 $link = $this->absolutiseUri($link);
443 $this->data
['commentlink'] = $link;
445 return $this->data
['commentlink'];
449 * Returns a URI pointing to a feed of all comments for this entry
451 * @param string $type
454 public function getCommentFeedLink($type = 'atom')
456 if (array_key_exists('commentfeedlink', $this->data
)) {
457 return $this->data
['commentfeedlink'];
462 $list = $this->getXpath()->query(
463 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="application/' . $type.'+xml"]/@href'
467 $link = $list->item(0)->value
;
468 $link = $this->absolutiseUri($link);
471 $this->data
['commentfeedlink'] = $link;
473 return $this->data
['commentfeedlink'];
479 * @return Collection\Category
481 public function getCategories()
483 if (array_key_exists('categories', $this->data
)) {
484 return $this->data
['categories'];
487 if ($this->getAtomType() == Reader\Reader
::TYPE_ATOM_10
) {
488 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:category');
491 * Since Atom 0.3 did not support categories, it would have used the
492 * Dublin Core extension. However there is a small possibility Atom 0.3
493 * may have been retrofitted to use Atom 1.0 instead.
495 $this->getXpath()->registerNamespace('atom10', Reader\Reader
::NAMESPACE_ATOM_10
);
496 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom10:category');
500 $categoryCollection = new Collection\Category
;
501 foreach ($list as $category) {
502 $categoryCollection[] = array(
503 'term' => $category->getAttribute('term'),
504 'scheme' => $category->getAttribute('scheme'),
505 'label' => $category->getAttribute('label')
509 return new Collection\Category
;
512 $this->data
['categories'] = $categoryCollection;
514 return $this->data
['categories'];
518 * Get source feed metadata from the entry
520 * @return Reader\Feed\Atom\Source|null
522 public function getSource()
524 if (array_key_exists('source', $this->data
)) {
525 return $this->data
['source'];
529 // TODO: Investigate why _getAtomType() fails here. Is it even needed?
530 if ($this->getType() == Reader\Reader
::TYPE_ATOM_10
) {
531 $list = $this->getXpath()->query($this->getXpathPrefix() . '/atom:source[1]');
533 $element = $list->item(0);
534 $source = new Reader\Feed\Atom\
Source($element, $this->getXpathPrefix());
538 $this->data
['source'] = $source;
539 return $this->data
['source'];
543 * Attempt to absolutise the URI, i.e. if a relative URI apply the
544 * xml:base value as a prefix to turn into an absolute URI.
549 protected function absolutiseUri($link)
551 if (!Uri
::factory($link)->isAbsolute()) {
552 if ($this->getBaseUrl() !== null) {
553 $link = $this->getBaseUrl() . $link;
554 if (!Uri
::factory($link)->isValid()) {
563 * Get an author entry
565 * @param DOMElement $element
568 protected function getAuthorFromElement(DOMElement
$element)
572 $emailNode = $element->getElementsByTagName('email');
573 $nameNode = $element->getElementsByTagName('name');
574 $uriNode = $element->getElementsByTagName('uri');
576 if ($emailNode->length
&& strlen($emailNode->item(0)->nodeValue
) > 0) {
577 $author['email'] = $emailNode->item(0)->nodeValue
;
580 if ($nameNode->length
&& strlen($nameNode->item(0)->nodeValue
) > 0) {
581 $author['name'] = $nameNode->item(0)->nodeValue
;
584 if ($uriNode->length
&& strlen($uriNode->item(0)->nodeValue
) > 0) {
585 $author['uri'] = $uriNode->item(0)->nodeValue
;
588 if (empty($author)) {
595 * Register the default namespaces for the current feed format
597 protected function registerNamespaces()
599 switch ($this->getAtomType()) {
600 case Reader\Reader
::TYPE_ATOM_03
:
601 $this->getXpath()->registerNamespace('atom', Reader\Reader
::NAMESPACE_ATOM_03
);
604 $this->getXpath()->registerNamespace('atom', Reader\Reader
::NAMESPACE_ATOM_10
);
610 * Detect the presence of any Atom namespaces in use
614 protected function getAtomType()
616 $dom = $this->getDomDocument();
617 $prefixAtom03 = $dom->lookupPrefix(Reader\Reader
::NAMESPACE_ATOM_03
);
618 $prefixAtom10 = $dom->lookupPrefix(Reader\Reader
::NAMESPACE_ATOM_10
);
619 if ($dom->isDefaultNamespace(Reader\Reader
::NAMESPACE_ATOM_03
)
620 ||
!empty($prefixAtom03)) {
621 return Reader\Reader
::TYPE_ATOM_03
;
623 if ($dom->isDefaultNamespace(Reader\Reader
::NAMESPACE_ATOM_10
)
624 ||
!empty($prefixAtom10)) {
625 return Reader\Reader
::TYPE_ATOM_10
;