comparison vendor/zendframework/zend-feed/src/Reader/Extension/Atom/Entry.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 5311817fb629
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
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
8 */
9
10 namespace Zend\Feed\Reader\Extension\Atom;
11
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;
20
21 class Entry extends Extension\AbstractEntry
22 {
23 /**
24 * Get the specified author
25 *
26 * @param int $index
27 * @return string|null
28 */
29 public function getAuthor($index = 0)
30 {
31 $authors = $this->getAuthors();
32
33 if (isset($authors[$index])) {
34 return $authors[$index];
35 }
36
37 return;
38 }
39
40 /**
41 * Get an array with feed authors
42 *
43 * @return Collection\Author
44 */
45 public function getAuthors()
46 {
47 if (array_key_exists('authors', $this->data)) {
48 return $this->data['authors'];
49 }
50
51 $authors = [];
52 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:author');
53
54 if (!$list->length) {
55 /**
56 * TODO: Limit query to feed level els only!
57 */
58 $list = $this->getXpath()->query('//atom:author');
59 }
60
61 if ($list->length) {
62 foreach ($list as $author) {
63 $author = $this->getAuthorFromElement($author);
64 if (!empty($author)) {
65 $authors[] = $author;
66 }
67 }
68 }
69
70 if (count($authors) == 0) {
71 $authors = new Collection\Author();
72 } else {
73 $authors = new Collection\Author(
74 Reader\Reader::arrayUnique($authors)
75 );
76 }
77
78 $this->data['authors'] = $authors;
79 return $this->data['authors'];
80 }
81
82 /**
83 * Get the entry content
84 *
85 * @return string
86 */
87 public function getContent()
88 {
89 if (array_key_exists('content', $this->data)) {
90 return $this->data['content'];
91 }
92
93 $content = null;
94
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 $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
114 $xhtmls = $d->importNode($xhtml, $deep);
115 $d->appendChild($xhtmls);
116 $content = $this->collectXhtml(
117 $d->saveXML(),
118 $d->lookupPrefix('http://www.w3.org/1999/xhtml')
119 );
120 break;
121 }
122 }
123
124 if (!$content) {
125 $content = $this->getDescription();
126 }
127
128 $this->data['content'] = trim($content);
129
130 return $this->data['content'];
131 }
132
133 /**
134 * Parse out XHTML to remove the namespacing
135 *
136 * @param $xhtml
137 * @param $prefix
138 * @return mixed
139 */
140 protected function collectXhtml($xhtml, $prefix)
141 {
142 if (!empty($prefix)) {
143 $prefix = $prefix . ':';
144 }
145 $matches = [
146 "/<\?xml[^<]*>[^<]*<" . $prefix . "div[^<]*/",
147 "/<\/" . $prefix . "div>\s*$/"
148 ];
149 $xhtml = preg_replace($matches, '', $xhtml);
150 if (!empty($prefix)) {
151 $xhtml = preg_replace("/(<[\/]?)" . $prefix . "([a-zA-Z]+)/", '$1$2', $xhtml);
152 }
153 return $xhtml;
154 }
155
156 /**
157 * Get the entry creation date
158 *
159 * @return string
160 */
161 public function getDateCreated()
162 {
163 if (array_key_exists('datecreated', $this->data)) {
164 return $this->data['datecreated'];
165 }
166
167 $date = null;
168
169 if ($this->getAtomType() === Reader\Reader::TYPE_ATOM_03) {
170 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
171 } else {
172 $dateCreated = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
173 }
174
175 if ($dateCreated) {
176 $date = new DateTime($dateCreated);
177 }
178
179 $this->data['datecreated'] = $date;
180
181 return $this->data['datecreated'];
182 }
183
184 /**
185 * Get the entry modification date
186 *
187 * @return string
188 */
189 public function getDateModified()
190 {
191 if (array_key_exists('datemodified', $this->data)) {
192 return $this->data['datemodified'];
193 }
194
195 $date = null;
196
197 if ($this->getAtomType() === Reader\Reader::TYPE_ATOM_03) {
198 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
199 } else {
200 $dateModified = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
201 }
202
203 if ($dateModified) {
204 $date = new DateTime($dateModified);
205 }
206
207 $this->data['datemodified'] = $date;
208
209 return $this->data['datemodified'];
210 }
211
212 /**
213 * Get the entry description
214 *
215 * @return string
216 */
217 public function getDescription()
218 {
219 if (array_key_exists('description', $this->data)) {
220 return $this->data['description'];
221 }
222
223 $description = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:summary)');
224
225 if (!$description) {
226 $description = null;
227 }
228
229 $this->data['description'] = $description;
230
231 return $this->data['description'];
232 }
233
234 /**
235 * Get the entry enclosure
236 *
237 * @return string
238 */
239 public function getEnclosure()
240 {
241 if (array_key_exists('enclosure', $this->data)) {
242 return $this->data['enclosure'];
243 }
244
245 $enclosure = null;
246
247 $nodeList = $this->getXpath()->query($this->getXpathPrefix() . '/atom:link[@rel="enclosure"]');
248
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');
254 }
255
256 $this->data['enclosure'] = $enclosure;
257
258 return $this->data['enclosure'];
259 }
260
261 /**
262 * Get the entry ID
263 *
264 * @return string
265 */
266 public function getId()
267 {
268 if (array_key_exists('id', $this->data)) {
269 return $this->data['id'];
270 }
271
272 $id = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
273
274 if (!$id) {
275 if ($this->getPermalink()) {
276 $id = $this->getPermalink();
277 } elseif ($this->getTitle()) {
278 $id = $this->getTitle();
279 } else {
280 $id = null;
281 }
282 }
283
284 $this->data['id'] = $id;
285
286 return $this->data['id'];
287 }
288
289 /**
290 * Get the base URI of the feed (if set).
291 *
292 * @return string|null
293 */
294 public function getBaseUrl()
295 {
296 if (array_key_exists('baseUrl', $this->data)) {
297 return $this->data['baseUrl'];
298 }
299
300 $baseUrl = $this->getXpath()->evaluate(
301 'string('
302 . $this->getXpathPrefix()
303 . '/@xml:base[1]'
304 . ')'
305 );
306
307 if (!$baseUrl) {
308 $baseUrl = $this->getXpath()->evaluate('string(//@xml:base[1])');
309 }
310
311 if (!$baseUrl) {
312 $baseUrl = null;
313 }
314
315 $this->data['baseUrl'] = $baseUrl;
316
317 return $this->data['baseUrl'];
318 }
319
320 /**
321 * Get a specific link
322 *
323 * @param int $index
324 * @return string
325 */
326 public function getLink($index = 0)
327 {
328 if (!array_key_exists('links', $this->data)) {
329 $this->getLinks();
330 }
331
332 if (isset($this->data['links'][$index])) {
333 return $this->data['links'][$index];
334 }
335
336 return;
337 }
338
339 /**
340 * Get all links
341 *
342 * @return array
343 */
344 public function getLinks()
345 {
346 if (array_key_exists('links', $this->data)) {
347 return $this->data['links'];
348 }
349
350 $links = [];
351
352 $list = $this->getXpath()->query(
353 $this->getXpathPrefix() . '//atom:link[@rel="alternate"]/@href' . '|' .
354 $this->getXpathPrefix() . '//atom:link[not(@rel)]/@href'
355 );
356
357 if ($list->length) {
358 foreach ($list as $link) {
359 $links[] = $this->absolutiseUri($link->value);
360 }
361 }
362
363 $this->data['links'] = $links;
364
365 return $this->data['links'];
366 }
367
368 /**
369 * Get a permalink to the entry
370 *
371 * @return string
372 */
373 public function getPermalink()
374 {
375 return $this->getLink(0);
376 }
377
378 /**
379 * Get the entry title
380 *
381 * @return string
382 */
383 public function getTitle()
384 {
385 if (array_key_exists('title', $this->data)) {
386 return $this->data['title'];
387 }
388
389 $title = $this->getXpath()->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
390
391 if (!$title) {
392 $title = null;
393 }
394
395 $this->data['title'] = $title;
396
397 return $this->data['title'];
398 }
399
400 /**
401 * Get the number of comments/replies for current entry
402 *
403 * @return int
404 */
405 public function getCommentCount()
406 {
407 if (array_key_exists('commentcount', $this->data)) {
408 return $this->data['commentcount'];
409 }
410
411 $count = null;
412
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'
416 );
417
418 if ($list->length) {
419 $count = $list->item(0)->value;
420 }
421
422 $this->data['commentcount'] = $count;
423
424 return $this->data['commentcount'];
425 }
426
427 /**
428 * Returns a URI pointing to the HTML page where comments can be made on this entry
429 *
430 * @return string
431 */
432 public function getCommentLink()
433 {
434 if (array_key_exists('commentlink', $this->data)) {
435 return $this->data['commentlink'];
436 }
437
438 $link = null;
439
440 $list = $this->getXpath()->query(
441 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="text/html"]/@href'
442 );
443
444 if ($list->length) {
445 $link = $list->item(0)->value;
446 $link = $this->absolutiseUri($link);
447 }
448
449 $this->data['commentlink'] = $link;
450
451 return $this->data['commentlink'];
452 }
453
454 /**
455 * Returns a URI pointing to a feed of all comments for this entry
456 *
457 * @param string $type
458 * @return string
459 */
460 public function getCommentFeedLink($type = 'atom')
461 {
462 if (array_key_exists('commentfeedlink', $this->data)) {
463 return $this->data['commentfeedlink'];
464 }
465
466 $link = null;
467
468 $list = $this->getXpath()->query(
469 $this->getXpathPrefix() . '//atom:link[@rel="replies" and @type="application/' . $type.'+xml"]/@href'
470 );
471
472 if ($list->length) {
473 $link = $list->item(0)->value;
474 $link = $this->absolutiseUri($link);
475 }
476
477 $this->data['commentfeedlink'] = $link;
478
479 return $this->data['commentfeedlink'];
480 }
481
482 /**
483 * Get all categories
484 *
485 * @return Collection\Category
486 */
487 public function getCategories()
488 {
489 if (array_key_exists('categories', $this->data)) {
490 return $this->data['categories'];
491 }
492
493 if ($this->getAtomType() == Reader\Reader::TYPE_ATOM_10) {
494 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom:category');
495 } else {
496 /**
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.
500 */
501 $this->getXpath()->registerNamespace('atom10', Reader\Reader::NAMESPACE_ATOM_10);
502 $list = $this->getXpath()->query($this->getXpathPrefix() . '//atom10:category');
503 }
504
505 if ($list->length) {
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')
512 ];
513 }
514 } else {
515 return new Collection\Category;
516 }
517
518 $this->data['categories'] = $categoryCollection;
519
520 return $this->data['categories'];
521 }
522
523 /**
524 * Get source feed metadata from the entry
525 *
526 * @return Reader\Feed\Atom\Source|null
527 */
528 public function getSource()
529 {
530 if (array_key_exists('source', $this->data)) {
531 return $this->data['source'];
532 }
533
534 $source = null;
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]');
538 if ($list->length) {
539 $element = $list->item(0);
540 $source = new Reader\Feed\Atom\Source($element, $this->getXpathPrefix());
541 }
542 }
543
544 $this->data['source'] = $source;
545 return $this->data['source'];
546 }
547
548 /**
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.
551 *
552 * @param $link
553 * @return string
554 */
555 protected function absolutiseUri($link)
556 {
557 if (!Uri::factory($link)->isAbsolute()) {
558 if ($this->getBaseUrl() !== null) {
559 $link = $this->getBaseUrl() . $link;
560 if (!Uri::factory($link)->isValid()) {
561 $link = null;
562 }
563 }
564 }
565 return $link;
566 }
567
568 /**
569 * Get an author entry
570 *
571 * @param DOMElement $element
572 * @return string
573 */
574 protected function getAuthorFromElement(DOMElement $element)
575 {
576 $author = [];
577
578 $emailNode = $element->getElementsByTagName('email');
579 $nameNode = $element->getElementsByTagName('name');
580 $uriNode = $element->getElementsByTagName('uri');
581
582 if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
583 $author['email'] = $emailNode->item(0)->nodeValue;
584 }
585
586 if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
587 $author['name'] = $nameNode->item(0)->nodeValue;
588 }
589
590 if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
591 $author['uri'] = $uriNode->item(0)->nodeValue;
592 }
593
594 if (empty($author)) {
595 return;
596 }
597 return $author;
598 }
599
600 /**
601 * Register the default namespaces for the current feed format
602 */
603 protected function registerNamespaces()
604 {
605 switch ($this->getAtomType()) {
606 case Reader\Reader::TYPE_ATOM_03:
607 $this->getXpath()->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_03);
608 break;
609 default:
610 $this->getXpath()->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_10);
611 break;
612 }
613 }
614
615 /**
616 * Detect the presence of any Atom namespaces in use
617 *
618 * @return string
619 */
620 protected function getAtomType()
621 {
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;
628 }
629 if ($dom->isDefaultNamespace(Reader\Reader::NAMESPACE_ATOM_10)
630 || !empty($prefixAtom10)) {
631 return Reader\Reader::TYPE_ATOM_10;
632 }
633 }
634 }