annotate vendor/zendframework/zend-feed/src/Reader/Entry/Rss.php @ 19:fa3358dc1485 tip

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