Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Feed / Reader / Extension / Atom / Entry.php
blobd68577cdf72d8810264a3477bf6ee7f2b9fce3be
1 <?php
2 /**
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
8 */
10 namespace Zend\Feed\Reader\Extension\Atom;
12 use DateTime;
13 use DOMDocument;
14 use DOMElement;
15 use stdClass;
16 use Zend\Feed\Reader;
17 use Zend\Feed\Reader\Collection;
18 use Zend\Feed\Reader\Extension;
19 use Zend\Feed\Uri;
21 class Entry extends Extension\AbstractEntry
23 /**
24 * Get the specified author
26 * @param int $index
27 * @return string|null
29 public function getAuthor($index = 0)
31 $authors = $this->getAuthors();
33 if (isset($authors[$index])) {
34 return $authors[$index];
37 return null;
40 /**
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'];
51 $authors = array();
52 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:author');
54 if (!$list->length) {
55 /**
56 * TODO: Limit query to feed level els only!
58 $list = $this->getXpath()->query('//atom:author');
61 if ($list->length) {
62 foreach ($list as $author) {
63 $author = $this->getAuthorFromElement($author);
64 if (!empty($author)) {
65 $authors[] = $author;
70 if (count($authors) == 0) {
71 $authors = new Collection\Author();
72 } else {
73 $authors = new Collection\Author(
74 Reader\Reader::arrayUnique($authors)
78 $this->data['authors'] = $authors;
79 return $this->data['authors'];
82 /**
83 * Get the entry content
85 * @return string
87 public function getContent()
89 if (array_key_exists('content', $this->data)) {
90 return $this->data['content'];
93 $content = null;
95 $el = $this->getXpath()->query($this->getXpathPrefix() . '/atom:content');
96 if ($el->length > 0) {
97 $el = $el->item(0);
98 $type = $el->getAttribute('type');
99 switch ($type) {
100 case '':
101 case 'text':
102 case 'text/plain':
103 case 'html':
104 case 'text/html':
105 $content = $el->nodeValue;
106 break;
107 case 'xhtml':
108 $this->getXpath()->registerNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
109 $xhtml = $this->getXpath()->query(
110 $this->getXpathPrefix() . '/atom:content/xhtml:div'
111 )->item(0);
112 $d = new DOMDocument('1.0', $this->getEncoding());
113 $xhtmls = $d->importNode($xhtml, true);
114 $d->appendChild($xhtmls);
115 $content = $this->collectXhtml(
116 $d->saveXML(),
117 $d->lookupPrefix('http://www.w3.org/1999/xhtml')
119 break;
123 if (!$content) {
124 $content = $this->getDescription();
127 $this->data['content'] = trim($content);
129 return $this->data['content'];
133 * Parse out XHTML to remove the namespacing
135 * @param $xhtml
136 * @param $prefix
137 * @return mixed
139 protected function collectXhtml($xhtml, $prefix)
141 if (!empty($prefix)) $prefix = $prefix . ':';
142 $matches = array(
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);
150 return $xhtml;
154 * Get the entry creation date
156 * @return string
158 public function getDateCreated()
160 if (array_key_exists('datecreated', $this->data)) {
161 return $this->data['datecreated'];
164 $date = null;
166 if ($this->getAtomType() === Reader\Reader::TYPE_ATOM_03) {
167 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
168 } else {
169 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
172 if ($dateCreated) {
173 $date = new DateTime($dateCreated);
176 $this->data['datecreated'] = $date;
178 return $this->data['datecreated'];
182 * Get the entry modification date
184 * @return string
186 public function getDateModified()
188 if (array_key_exists('datemodified', $this->data)) {
189 return $this->data['datemodified'];
192 $date = null;
194 if ($this->getAtomType() === Reader\Reader::TYPE_ATOM_03) {
195 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
196 } else {
197 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
200 if ($dateModified) {
201 $date = new DateTime($dateModified);
204 $this->data['datemodified'] = $date;
206 return $this->data['datemodified'];
210 * Get the entry description
212 * @return string
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)');
222 if (!$description) {
223 $description = null;
226 $this->data['description'] = $description;
228 return $this->data['description'];
232 * Get the entry enclosure
234 * @return string
236 public function getEnclosure()
238 if (array_key_exists('enclosure', $this->data)) {
239 return $this->data['enclosure'];
242 $enclosure = null;
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'];
259 * Get the entry ID
261 * @return string
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)');
271 if (!$id) {
272 if ($this->getPermalink()) {
273 $id = $this->getPermalink();
274 } elseif ($this->getTitle()) {
275 $id = $this->getTitle();
276 } else {
277 $id = null;
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]'
299 . ')');
301 if (!$baseUrl) {
302 $baseUrl = $this->getXpath()->evaluate('string(//@xml:base[1])');
305 if (!$baseUrl) {
306 $baseUrl = null;
309 $this->data['baseUrl'] = $baseUrl;
311 return $this->data['baseUrl'];
315 * Get a specific link
317 * @param int $index
318 * @return string
320 public function getLink($index = 0)
322 if (!array_key_exists('links', $this->data)) {
323 $this->getLinks();
326 if (isset($this->data['links'][$index])) {
327 return $this->data['links'][$index];
330 return null;
334 * Get all links
336 * @return array
338 public function getLinks()
340 if (array_key_exists('links', $this->data)) {
341 return $this->data['links'];
344 $links = array();
346 $list = $this->getXpath()->query(
347 $this->getXpathPrefix() . '//atom:link[@rel="alternate"]/@href' . '|' .
348 $this->getXpathPrefix() . '//atom:link[not(@rel)]/@href'
351 if ($list->length) {
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
365 * @return string
367 public function getPermalink()
369 return $this->getLink(0);
373 * Get the entry title
375 * @return string
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)');
385 if (!$title) {
386 $title = null;
389 $this->data['title'] = $title;
391 return $this->data['title'];
395 * Get the number of comments/replies for current entry
397 * @return int
399 public function getCommentCount()
401 if (array_key_exists('commentcount', $this->data)) {
402 return $this->data['commentcount'];
405 $count = null;
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'
412 if ($list->length) {
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
424 * @return string
426 public function getCommentLink()
428 if (array_key_exists('commentlink', $this->data)) {
429 return $this->data['commentlink'];
432 $link = null;
434 $list = $this->getXpath()->query(
435 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="text/html"]/@href'
438 if ($list->length) {
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
452 * @return string
454 public function getCommentFeedLink($type = 'atom')
456 if (array_key_exists('commentfeedlink', $this->data)) {
457 return $this->data['commentfeedlink'];
460 $link = null;
462 $list = $this->getXpath()->query(
463 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="application/' . $type.'+xml"]/@href'
466 if ($list->length) {
467 $link = $list->item(0)->value;
468 $link = $this->absolutiseUri($link);
471 $this->data['commentfeedlink'] = $link;
473 return $this->data['commentfeedlink'];
477 * Get all categories
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');
489 } else {
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');
499 if ($list->length) {
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')
508 } else {
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'];
528 $source = null;
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]');
532 if ($list->length) {
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.
546 * @param $link
547 * @return string
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()) {
555 $link = null;
559 return $link;
563 * Get an author entry
565 * @param DOMElement $element
566 * @return string
568 protected function getAuthorFromElement(DOMElement $element)
570 $author = array();
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)) {
589 return null;
591 return $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);
602 break;
603 default:
604 $this->getXpath()->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_10);
605 break;
610 * Detect the presence of any Atom namespaces in use
612 * @return string
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;