annotate vendor/zendframework/zend-feed/src/Reader/Entry/Rss.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Zend Framework (http://framework.zend.com/)
Chris@0 4 *
Chris@0 5 * @link http://github.com/zendframework/zf2 for the canonical source repository
Chris@0 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 7 * @license http://framework.zend.com/license/new-bsd New BSD License
Chris@0 8 */
Chris@0 9
Chris@0 10 namespace Zend\Feed\Reader\Entry;
Chris@0 11
Chris@0 12 use DateTime;
Chris@0 13 use DOMElement;
Chris@0 14 use DOMXPath;
Chris@0 15 use Zend\Feed\Reader;
Chris@0 16 use Zend\Feed\Reader\Exception;
Chris@0 17
Chris@0 18 class Rss extends AbstractEntry implements EntryInterface
Chris@0 19 {
Chris@0 20 /**
Chris@0 21 * XPath query for RDF
Chris@0 22 *
Chris@0 23 * @var string
Chris@0 24 */
Chris@0 25 protected $xpathQueryRdf = '';
Chris@0 26
Chris@0 27 /**
Chris@0 28 * XPath query for RSS
Chris@0 29 *
Chris@0 30 * @var string
Chris@0 31 */
Chris@0 32 protected $xpathQueryRss = '';
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Constructor
Chris@0 36 *
Chris@0 37 * @param DOMElement $entry
Chris@0 38 * @param string $entryKey
Chris@0 39 * @param string $type
Chris@0 40 */
Chris@0 41 public function __construct(DOMElement $entry, $entryKey, $type = null)
Chris@0 42 {
Chris@0 43 parent::__construct($entry, $entryKey, $type);
Chris@12 44 $this->xpathQueryRss = '//item[' . ($this->entryKey + 1) . ']';
Chris@12 45 $this->xpathQueryRdf = '//rss:item[' . ($this->entryKey + 1) . ']';
Chris@0 46
Chris@0 47 $manager = Reader\Reader::getExtensionManager();
Chris@0 48 $extensions = [
Chris@0 49 'DublinCore\Entry',
Chris@0 50 'Content\Entry',
Chris@0 51 'Atom\Entry',
Chris@0 52 'WellFormedWeb\Entry',
Chris@0 53 'Slash\Entry',
Chris@0 54 'Thread\Entry',
Chris@0 55 ];
Chris@0 56 foreach ($extensions as $name) {
Chris@0 57 $extension = $manager->get($name);
Chris@0 58 $extension->setEntryElement($entry);
Chris@0 59 $extension->setEntryKey($entryKey);
Chris@0 60 $extension->setType($type);
Chris@0 61 $this->extensions[$name] = $extension;
Chris@0 62 }
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Get an author entry
Chris@0 67 *
Chris@0 68 * @param int $index
Chris@0 69 * @return string
Chris@0 70 */
Chris@0 71 public function getAuthor($index = 0)
Chris@0 72 {
Chris@0 73 $authors = $this->getAuthors();
Chris@0 74
Chris@0 75 if (isset($authors[$index])) {
Chris@0 76 return $authors[$index];
Chris@0 77 }
Chris@0 78
Chris@0 79 return;
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Get an array with feed authors
Chris@0 84 *
Chris@0 85 * @return array
Chris@0 86 */
Chris@0 87 public function getAuthors()
Chris@0 88 {
Chris@0 89 if (array_key_exists('authors', $this->data)) {
Chris@0 90 return $this->data['authors'];
Chris@0 91 }
Chris@0 92
Chris@0 93 $authors = [];
Chris@0 94 $authorsDc = $this->getExtension('DublinCore')->getAuthors();
Chris@12 95 if (! empty($authorsDc)) {
Chris@0 96 foreach ($authorsDc as $author) {
Chris@0 97 $authors[] = [
Chris@0 98 'name' => $author['name']
Chris@0 99 ];
Chris@0 100 }
Chris@0 101 }
Chris@0 102
Chris@0 103 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 104 && $this->getType() !== Reader\Reader::TYPE_RSS_090) {
Chris@0 105 $list = $this->xpath->query($this->xpathQueryRss . '//author');
Chris@0 106 } else {
Chris@0 107 $list = $this->xpath->query($this->xpathQueryRdf . '//rss:author');
Chris@0 108 }
Chris@0 109 if ($list->length) {
Chris@0 110 foreach ($list as $author) {
Chris@0 111 $string = trim($author->nodeValue);
Chris@0 112 $data = [];
Chris@0 113 // Pretty rough parsing - but it's a catchall
Chris@0 114 if (preg_match("/^.*@[^ ]*/", $string, $matches)) {
Chris@0 115 $data['email'] = trim($matches[0]);
Chris@0 116 if (preg_match("/\((.*)\)$/", $string, $matches)) {
Chris@0 117 $data['name'] = $matches[1];
Chris@0 118 }
Chris@0 119 $authors[] = $data;
Chris@0 120 }
Chris@0 121 }
Chris@0 122 }
Chris@0 123
Chris@0 124 if (count($authors) == 0) {
Chris@0 125 $authors = $this->getExtension('Atom')->getAuthors();
Chris@0 126 } else {
Chris@0 127 $authors = new Reader\Collection\Author(
Chris@0 128 Reader\Reader::arrayUnique($authors)
Chris@0 129 );
Chris@0 130 }
Chris@0 131
Chris@0 132 if (count($authors) == 0) {
Chris@0 133 $authors = null;
Chris@0 134 }
Chris@0 135
Chris@0 136 $this->data['authors'] = $authors;
Chris@0 137
Chris@0 138 return $this->data['authors'];
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Get the entry content
Chris@0 143 *
Chris@0 144 * @return string
Chris@0 145 */
Chris@0 146 public function getContent()
Chris@0 147 {
Chris@0 148 if (array_key_exists('content', $this->data)) {
Chris@0 149 return $this->data['content'];
Chris@0 150 }
Chris@0 151
Chris@0 152 $content = $this->getExtension('Content')->getContent();
Chris@0 153
Chris@12 154 if (! $content) {
Chris@0 155 $content = $this->getDescription();
Chris@0 156 }
Chris@0 157
Chris@0 158 if (empty($content)) {
Chris@0 159 $content = $this->getExtension('Atom')->getContent();
Chris@0 160 }
Chris@0 161
Chris@0 162 $this->data['content'] = $content;
Chris@0 163
Chris@0 164 return $this->data['content'];
Chris@0 165 }
Chris@0 166
Chris@0 167 /**
Chris@0 168 * Get the entry's date of creation
Chris@0 169 *
Chris@0 170 * @return \DateTime
Chris@0 171 */
Chris@0 172 public function getDateCreated()
Chris@0 173 {
Chris@0 174 return $this->getDateModified();
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * Get the entry's date of modification
Chris@0 179 *
Chris@0 180 * @throws Exception\RuntimeException
Chris@0 181 * @return \DateTime
Chris@0 182 */
Chris@0 183 public function getDateModified()
Chris@0 184 {
Chris@0 185 if (array_key_exists('datemodified', $this->data)) {
Chris@0 186 return $this->data['datemodified'];
Chris@0 187 }
Chris@0 188
Chris@0 189 $date = null;
Chris@0 190
Chris@0 191 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 192 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 193 ) {
Chris@0 194 $dateModified = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/pubDate)');
Chris@0 195 if ($dateModified) {
Chris@0 196 $dateModifiedParsed = strtotime($dateModified);
Chris@0 197 if ($dateModifiedParsed) {
Chris@0 198 $date = new DateTime('@' . $dateModifiedParsed);
Chris@0 199 } else {
Chris@0 200 $dateStandards = [DateTime::RSS, DateTime::RFC822,
Chris@0 201 DateTime::RFC2822, null];
Chris@0 202 foreach ($dateStandards as $standard) {
Chris@0 203 try {
Chris@0 204 $date = date_create_from_format($standard, $dateModified);
Chris@0 205 break;
Chris@0 206 } catch (\Exception $e) {
Chris@0 207 if ($standard === null) {
Chris@0 208 throw new Exception\RuntimeException(
Chris@0 209 'Could not load date due to unrecognised'
Chris@0 210 .' format (should follow RFC 822 or 2822):'
Chris@0 211 . $e->getMessage(),
Chris@12 212 0,
Chris@12 213 $e
Chris@0 214 );
Chris@0 215 }
Chris@0 216 }
Chris@0 217 }
Chris@0 218 }
Chris@0 219 }
Chris@0 220 }
Chris@0 221
Chris@12 222 if (! $date) {
Chris@0 223 $date = $this->getExtension('DublinCore')->getDate();
Chris@0 224 }
Chris@0 225
Chris@12 226 if (! $date) {
Chris@0 227 $date = $this->getExtension('Atom')->getDateModified();
Chris@0 228 }
Chris@0 229
Chris@12 230 if (! $date) {
Chris@0 231 $date = null;
Chris@0 232 }
Chris@0 233
Chris@0 234 $this->data['datemodified'] = $date;
Chris@0 235
Chris@0 236 return $this->data['datemodified'];
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * Get the entry description
Chris@0 241 *
Chris@0 242 * @return string
Chris@0 243 */
Chris@0 244 public function getDescription()
Chris@0 245 {
Chris@0 246 if (array_key_exists('description', $this->data)) {
Chris@0 247 return $this->data['description'];
Chris@0 248 }
Chris@0 249
Chris@0 250 $description = null;
Chris@0 251
Chris@0 252 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 253 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 254 ) {
Chris@0 255 $description = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/description)');
Chris@0 256 } else {
Chris@0 257 $description = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:description)');
Chris@0 258 }
Chris@0 259
Chris@12 260 if (! $description) {
Chris@0 261 $description = $this->getExtension('DublinCore')->getDescription();
Chris@0 262 }
Chris@0 263
Chris@0 264 if (empty($description)) {
Chris@0 265 $description = $this->getExtension('Atom')->getDescription();
Chris@0 266 }
Chris@0 267
Chris@12 268 if (! $description) {
Chris@0 269 $description = null;
Chris@0 270 }
Chris@0 271
Chris@0 272 $this->data['description'] = $description;
Chris@0 273
Chris@0 274 return $this->data['description'];
Chris@0 275 }
Chris@0 276
Chris@0 277 /**
Chris@0 278 * Get the entry enclosure
Chris@0 279 * @return string
Chris@0 280 */
Chris@0 281 public function getEnclosure()
Chris@0 282 {
Chris@0 283 if (array_key_exists('enclosure', $this->data)) {
Chris@0 284 return $this->data['enclosure'];
Chris@0 285 }
Chris@0 286
Chris@0 287 $enclosure = null;
Chris@0 288
Chris@0 289 if ($this->getType() == Reader\Reader::TYPE_RSS_20) {
Chris@0 290 $nodeList = $this->xpath->query($this->xpathQueryRss . '/enclosure');
Chris@0 291
Chris@0 292 if ($nodeList->length > 0) {
Chris@0 293 $enclosure = new \stdClass();
Chris@0 294 $enclosure->url = $nodeList->item(0)->getAttribute('url');
Chris@0 295 $enclosure->length = $nodeList->item(0)->getAttribute('length');
Chris@0 296 $enclosure->type = $nodeList->item(0)->getAttribute('type');
Chris@0 297 }
Chris@0 298 }
Chris@0 299
Chris@12 300 if (! $enclosure) {
Chris@0 301 $enclosure = $this->getExtension('Atom')->getEnclosure();
Chris@0 302 }
Chris@0 303
Chris@0 304 $this->data['enclosure'] = $enclosure;
Chris@0 305
Chris@0 306 return $this->data['enclosure'];
Chris@0 307 }
Chris@0 308
Chris@0 309 /**
Chris@0 310 * Get the entry ID
Chris@0 311 *
Chris@0 312 * @return string
Chris@0 313 */
Chris@0 314 public function getId()
Chris@0 315 {
Chris@0 316 if (array_key_exists('id', $this->data)) {
Chris@0 317 return $this->data['id'];
Chris@0 318 }
Chris@0 319
Chris@0 320 $id = null;
Chris@0 321
Chris@0 322 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 323 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 324 ) {
Chris@0 325 $id = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/guid)');
Chris@0 326 }
Chris@0 327
Chris@12 328 if (! $id) {
Chris@0 329 $id = $this->getExtension('DublinCore')->getId();
Chris@0 330 }
Chris@0 331
Chris@0 332 if (empty($id)) {
Chris@0 333 $id = $this->getExtension('Atom')->getId();
Chris@0 334 }
Chris@0 335
Chris@12 336 if (! $id) {
Chris@0 337 if ($this->getPermalink()) {
Chris@0 338 $id = $this->getPermalink();
Chris@0 339 } elseif ($this->getTitle()) {
Chris@0 340 $id = $this->getTitle();
Chris@0 341 } else {
Chris@0 342 $id = null;
Chris@0 343 }
Chris@0 344 }
Chris@0 345
Chris@0 346 $this->data['id'] = $id;
Chris@0 347
Chris@0 348 return $this->data['id'];
Chris@0 349 }
Chris@0 350
Chris@0 351 /**
Chris@0 352 * Get a specific link
Chris@0 353 *
Chris@0 354 * @param int $index
Chris@0 355 * @return string
Chris@0 356 */
Chris@0 357 public function getLink($index = 0)
Chris@0 358 {
Chris@12 359 if (! array_key_exists('links', $this->data)) {
Chris@0 360 $this->getLinks();
Chris@0 361 }
Chris@0 362
Chris@0 363 if (isset($this->data['links'][$index])) {
Chris@0 364 return $this->data['links'][$index];
Chris@0 365 }
Chris@0 366
Chris@0 367 return;
Chris@0 368 }
Chris@0 369
Chris@0 370 /**
Chris@0 371 * Get all links
Chris@0 372 *
Chris@0 373 * @return array
Chris@0 374 */
Chris@0 375 public function getLinks()
Chris@0 376 {
Chris@0 377 if (array_key_exists('links', $this->data)) {
Chris@0 378 return $this->data['links'];
Chris@0 379 }
Chris@0 380
Chris@0 381 $links = [];
Chris@0 382
Chris@0 383 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
Chris@0 384 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
Chris@0 385 $list = $this->xpath->query($this->xpathQueryRss . '//link');
Chris@0 386 } else {
Chris@0 387 $list = $this->xpath->query($this->xpathQueryRdf . '//rss:link');
Chris@0 388 }
Chris@0 389
Chris@12 390 if (! $list->length) {
Chris@0 391 $links = $this->getExtension('Atom')->getLinks();
Chris@0 392 } else {
Chris@0 393 foreach ($list as $link) {
Chris@0 394 $links[] = $link->nodeValue;
Chris@0 395 }
Chris@0 396 }
Chris@0 397
Chris@0 398 $this->data['links'] = $links;
Chris@0 399
Chris@0 400 return $this->data['links'];
Chris@0 401 }
Chris@0 402
Chris@0 403 /**
Chris@0 404 * Get all categories
Chris@0 405 *
Chris@0 406 * @return Reader\Collection\Category
Chris@0 407 */
Chris@0 408 public function getCategories()
Chris@0 409 {
Chris@0 410 if (array_key_exists('categories', $this->data)) {
Chris@0 411 return $this->data['categories'];
Chris@0 412 }
Chris@0 413
Chris@0 414 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
Chris@0 415 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
Chris@0 416 $list = $this->xpath->query($this->xpathQueryRss . '//category');
Chris@0 417 } else {
Chris@0 418 $list = $this->xpath->query($this->xpathQueryRdf . '//rss:category');
Chris@0 419 }
Chris@0 420
Chris@0 421 if ($list->length) {
Chris@0 422 $categoryCollection = new Reader\Collection\Category;
Chris@0 423 foreach ($list as $category) {
Chris@0 424 $categoryCollection[] = [
Chris@0 425 'term' => $category->nodeValue,
Chris@0 426 'scheme' => $category->getAttribute('domain'),
Chris@0 427 'label' => $category->nodeValue,
Chris@0 428 ];
Chris@0 429 }
Chris@0 430 } else {
Chris@0 431 $categoryCollection = $this->getExtension('DublinCore')->getCategories();
Chris@0 432 }
Chris@0 433
Chris@0 434 if (count($categoryCollection) == 0) {
Chris@0 435 $categoryCollection = $this->getExtension('Atom')->getCategories();
Chris@0 436 }
Chris@0 437
Chris@0 438 $this->data['categories'] = $categoryCollection;
Chris@0 439
Chris@0 440 return $this->data['categories'];
Chris@0 441 }
Chris@0 442
Chris@0 443 /**
Chris@0 444 * Get a permalink to the entry
Chris@0 445 *
Chris@0 446 * @return string
Chris@0 447 */
Chris@0 448 public function getPermalink()
Chris@0 449 {
Chris@0 450 return $this->getLink(0);
Chris@0 451 }
Chris@0 452
Chris@0 453 /**
Chris@0 454 * Get the entry title
Chris@0 455 *
Chris@0 456 * @return string
Chris@0 457 */
Chris@0 458 public function getTitle()
Chris@0 459 {
Chris@0 460 if (array_key_exists('title', $this->data)) {
Chris@0 461 return $this->data['title'];
Chris@0 462 }
Chris@0 463
Chris@0 464 $title = null;
Chris@0 465
Chris@0 466 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 467 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 468 ) {
Chris@0 469 $title = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/title)');
Chris@0 470 } else {
Chris@0 471 $title = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:title)');
Chris@0 472 }
Chris@0 473
Chris@12 474 if (! $title) {
Chris@0 475 $title = $this->getExtension('DublinCore')->getTitle();
Chris@0 476 }
Chris@0 477
Chris@12 478 if (! $title) {
Chris@0 479 $title = $this->getExtension('Atom')->getTitle();
Chris@0 480 }
Chris@0 481
Chris@12 482 if (! $title) {
Chris@0 483 $title = null;
Chris@0 484 }
Chris@0 485
Chris@0 486 $this->data['title'] = $title;
Chris@0 487
Chris@0 488 return $this->data['title'];
Chris@0 489 }
Chris@0 490
Chris@0 491 /**
Chris@0 492 * Get the number of comments/replies for current entry
Chris@0 493 *
Chris@0 494 * @return string|null
Chris@0 495 */
Chris@0 496 public function getCommentCount()
Chris@0 497 {
Chris@0 498 if (array_key_exists('commentcount', $this->data)) {
Chris@0 499 return $this->data['commentcount'];
Chris@0 500 }
Chris@0 501
Chris@0 502 $commentcount = $this->getExtension('Slash')->getCommentCount();
Chris@0 503
Chris@12 504 if (! $commentcount) {
Chris@0 505 $commentcount = $this->getExtension('Thread')->getCommentCount();
Chris@0 506 }
Chris@0 507
Chris@12 508 if (! $commentcount) {
Chris@0 509 $commentcount = $this->getExtension('Atom')->getCommentCount();
Chris@0 510 }
Chris@0 511
Chris@12 512 if (! $commentcount) {
Chris@0 513 $commentcount = null;
Chris@0 514 }
Chris@0 515
Chris@0 516 $this->data['commentcount'] = $commentcount;
Chris@0 517
Chris@0 518 return $this->data['commentcount'];
Chris@0 519 }
Chris@0 520
Chris@0 521 /**
Chris@0 522 * Returns a URI pointing to the HTML page where comments can be made on this entry
Chris@0 523 *
Chris@0 524 * @return string
Chris@0 525 */
Chris@0 526 public function getCommentLink()
Chris@0 527 {
Chris@0 528 if (array_key_exists('commentlink', $this->data)) {
Chris@0 529 return $this->data['commentlink'];
Chris@0 530 }
Chris@0 531
Chris@0 532 $commentlink = null;
Chris@0 533
Chris@0 534 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 535 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 536 ) {
Chris@0 537 $commentlink = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/comments)');
Chris@0 538 }
Chris@0 539
Chris@12 540 if (! $commentlink) {
Chris@0 541 $commentlink = $this->getExtension('Atom')->getCommentLink();
Chris@0 542 }
Chris@0 543
Chris@12 544 if (! $commentlink) {
Chris@0 545 $commentlink = null;
Chris@0 546 }
Chris@0 547
Chris@0 548 $this->data['commentlink'] = $commentlink;
Chris@0 549
Chris@0 550 return $this->data['commentlink'];
Chris@0 551 }
Chris@0 552
Chris@0 553 /**
Chris@0 554 * Returns a URI pointing to a feed of all comments for this entry
Chris@0 555 *
Chris@0 556 * @return string
Chris@0 557 */
Chris@0 558 public function getCommentFeedLink()
Chris@0 559 {
Chris@0 560 if (array_key_exists('commentfeedlink', $this->data)) {
Chris@0 561 return $this->data['commentfeedlink'];
Chris@0 562 }
Chris@0 563
Chris@0 564 $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink();
Chris@0 565
Chris@12 566 if (! $commentfeedlink) {
Chris@0 567 $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss');
Chris@0 568 }
Chris@0 569
Chris@12 570 if (! $commentfeedlink) {
Chris@0 571 $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf');
Chris@0 572 }
Chris@0 573
Chris@12 574 if (! $commentfeedlink) {
Chris@0 575 $commentfeedlink = null;
Chris@0 576 }
Chris@0 577
Chris@0 578 $this->data['commentfeedlink'] = $commentfeedlink;
Chris@0 579
Chris@0 580 return $this->data['commentfeedlink'];
Chris@0 581 }
Chris@0 582
Chris@0 583 /**
Chris@0 584 * Set the XPath query (incl. on all Extensions)
Chris@0 585 *
Chris@0 586 * @param DOMXPath $xpath
Chris@0 587 * @return void
Chris@0 588 */
Chris@0 589 public function setXpath(DOMXPath $xpath)
Chris@0 590 {
Chris@0 591 parent::setXpath($xpath);
Chris@0 592 foreach ($this->extensions as $extension) {
Chris@0 593 $extension->setXpath($this->xpath);
Chris@0 594 }
Chris@0 595 }
Chris@0 596 }