annotate vendor/zendframework/zend-feed/src/Reader/Entry/Rss.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children 7a779792577d
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@0 44 $this->xpathQueryRss = '//item[' . ($this->entryKey+1) . ']';
Chris@0 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@0 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@0 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@0 212 0, $e
Chris@0 213 );
Chris@0 214 }
Chris@0 215 }
Chris@0 216 }
Chris@0 217 }
Chris@0 218 }
Chris@0 219 }
Chris@0 220
Chris@0 221 if (!$date) {
Chris@0 222 $date = $this->getExtension('DublinCore')->getDate();
Chris@0 223 }
Chris@0 224
Chris@0 225 if (!$date) {
Chris@0 226 $date = $this->getExtension('Atom')->getDateModified();
Chris@0 227 }
Chris@0 228
Chris@0 229 if (!$date) {
Chris@0 230 $date = null;
Chris@0 231 }
Chris@0 232
Chris@0 233 $this->data['datemodified'] = $date;
Chris@0 234
Chris@0 235 return $this->data['datemodified'];
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Get the entry description
Chris@0 240 *
Chris@0 241 * @return string
Chris@0 242 */
Chris@0 243 public function getDescription()
Chris@0 244 {
Chris@0 245 if (array_key_exists('description', $this->data)) {
Chris@0 246 return $this->data['description'];
Chris@0 247 }
Chris@0 248
Chris@0 249 $description = null;
Chris@0 250
Chris@0 251 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 252 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 253 ) {
Chris@0 254 $description = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/description)');
Chris@0 255 } else {
Chris@0 256 $description = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:description)');
Chris@0 257 }
Chris@0 258
Chris@0 259 if (!$description) {
Chris@0 260 $description = $this->getExtension('DublinCore')->getDescription();
Chris@0 261 }
Chris@0 262
Chris@0 263 if (empty($description)) {
Chris@0 264 $description = $this->getExtension('Atom')->getDescription();
Chris@0 265 }
Chris@0 266
Chris@0 267 if (!$description) {
Chris@0 268 $description = null;
Chris@0 269 }
Chris@0 270
Chris@0 271 $this->data['description'] = $description;
Chris@0 272
Chris@0 273 return $this->data['description'];
Chris@0 274 }
Chris@0 275
Chris@0 276 /**
Chris@0 277 * Get the entry enclosure
Chris@0 278 * @return string
Chris@0 279 */
Chris@0 280 public function getEnclosure()
Chris@0 281 {
Chris@0 282 if (array_key_exists('enclosure', $this->data)) {
Chris@0 283 return $this->data['enclosure'];
Chris@0 284 }
Chris@0 285
Chris@0 286 $enclosure = null;
Chris@0 287
Chris@0 288 if ($this->getType() == Reader\Reader::TYPE_RSS_20) {
Chris@0 289 $nodeList = $this->xpath->query($this->xpathQueryRss . '/enclosure');
Chris@0 290
Chris@0 291 if ($nodeList->length > 0) {
Chris@0 292 $enclosure = new \stdClass();
Chris@0 293 $enclosure->url = $nodeList->item(0)->getAttribute('url');
Chris@0 294 $enclosure->length = $nodeList->item(0)->getAttribute('length');
Chris@0 295 $enclosure->type = $nodeList->item(0)->getAttribute('type');
Chris@0 296 }
Chris@0 297 }
Chris@0 298
Chris@0 299 if (!$enclosure) {
Chris@0 300 $enclosure = $this->getExtension('Atom')->getEnclosure();
Chris@0 301 }
Chris@0 302
Chris@0 303 $this->data['enclosure'] = $enclosure;
Chris@0 304
Chris@0 305 return $this->data['enclosure'];
Chris@0 306 }
Chris@0 307
Chris@0 308 /**
Chris@0 309 * Get the entry ID
Chris@0 310 *
Chris@0 311 * @return string
Chris@0 312 */
Chris@0 313 public function getId()
Chris@0 314 {
Chris@0 315 if (array_key_exists('id', $this->data)) {
Chris@0 316 return $this->data['id'];
Chris@0 317 }
Chris@0 318
Chris@0 319 $id = null;
Chris@0 320
Chris@0 321 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 322 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 323 ) {
Chris@0 324 $id = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/guid)');
Chris@0 325 }
Chris@0 326
Chris@0 327 if (!$id) {
Chris@0 328 $id = $this->getExtension('DublinCore')->getId();
Chris@0 329 }
Chris@0 330
Chris@0 331 if (empty($id)) {
Chris@0 332 $id = $this->getExtension('Atom')->getId();
Chris@0 333 }
Chris@0 334
Chris@0 335 if (!$id) {
Chris@0 336 if ($this->getPermalink()) {
Chris@0 337 $id = $this->getPermalink();
Chris@0 338 } elseif ($this->getTitle()) {
Chris@0 339 $id = $this->getTitle();
Chris@0 340 } else {
Chris@0 341 $id = null;
Chris@0 342 }
Chris@0 343 }
Chris@0 344
Chris@0 345 $this->data['id'] = $id;
Chris@0 346
Chris@0 347 return $this->data['id'];
Chris@0 348 }
Chris@0 349
Chris@0 350 /**
Chris@0 351 * Get a specific link
Chris@0 352 *
Chris@0 353 * @param int $index
Chris@0 354 * @return string
Chris@0 355 */
Chris@0 356 public function getLink($index = 0)
Chris@0 357 {
Chris@0 358 if (!array_key_exists('links', $this->data)) {
Chris@0 359 $this->getLinks();
Chris@0 360 }
Chris@0 361
Chris@0 362 if (isset($this->data['links'][$index])) {
Chris@0 363 return $this->data['links'][$index];
Chris@0 364 }
Chris@0 365
Chris@0 366 return;
Chris@0 367 }
Chris@0 368
Chris@0 369 /**
Chris@0 370 * Get all links
Chris@0 371 *
Chris@0 372 * @return array
Chris@0 373 */
Chris@0 374 public function getLinks()
Chris@0 375 {
Chris@0 376 if (array_key_exists('links', $this->data)) {
Chris@0 377 return $this->data['links'];
Chris@0 378 }
Chris@0 379
Chris@0 380 $links = [];
Chris@0 381
Chris@0 382 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
Chris@0 383 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
Chris@0 384 $list = $this->xpath->query($this->xpathQueryRss . '//link');
Chris@0 385 } else {
Chris@0 386 $list = $this->xpath->query($this->xpathQueryRdf . '//rss:link');
Chris@0 387 }
Chris@0 388
Chris@0 389 if (!$list->length) {
Chris@0 390 $links = $this->getExtension('Atom')->getLinks();
Chris@0 391 } else {
Chris@0 392 foreach ($list as $link) {
Chris@0 393 $links[] = $link->nodeValue;
Chris@0 394 }
Chris@0 395 }
Chris@0 396
Chris@0 397 $this->data['links'] = $links;
Chris@0 398
Chris@0 399 return $this->data['links'];
Chris@0 400 }
Chris@0 401
Chris@0 402 /**
Chris@0 403 * Get all categories
Chris@0 404 *
Chris@0 405 * @return Reader\Collection\Category
Chris@0 406 */
Chris@0 407 public function getCategories()
Chris@0 408 {
Chris@0 409 if (array_key_exists('categories', $this->data)) {
Chris@0 410 return $this->data['categories'];
Chris@0 411 }
Chris@0 412
Chris@0 413 if ($this->getType() !== Reader\Reader::TYPE_RSS_10 &&
Chris@0 414 $this->getType() !== Reader\Reader::TYPE_RSS_090) {
Chris@0 415 $list = $this->xpath->query($this->xpathQueryRss . '//category');
Chris@0 416 } else {
Chris@0 417 $list = $this->xpath->query($this->xpathQueryRdf . '//rss:category');
Chris@0 418 }
Chris@0 419
Chris@0 420 if ($list->length) {
Chris@0 421 $categoryCollection = new Reader\Collection\Category;
Chris@0 422 foreach ($list as $category) {
Chris@0 423 $categoryCollection[] = [
Chris@0 424 'term' => $category->nodeValue,
Chris@0 425 'scheme' => $category->getAttribute('domain'),
Chris@0 426 'label' => $category->nodeValue,
Chris@0 427 ];
Chris@0 428 }
Chris@0 429 } else {
Chris@0 430 $categoryCollection = $this->getExtension('DublinCore')->getCategories();
Chris@0 431 }
Chris@0 432
Chris@0 433 if (count($categoryCollection) == 0) {
Chris@0 434 $categoryCollection = $this->getExtension('Atom')->getCategories();
Chris@0 435 }
Chris@0 436
Chris@0 437 $this->data['categories'] = $categoryCollection;
Chris@0 438
Chris@0 439 return $this->data['categories'];
Chris@0 440 }
Chris@0 441
Chris@0 442 /**
Chris@0 443 * Get a permalink to the entry
Chris@0 444 *
Chris@0 445 * @return string
Chris@0 446 */
Chris@0 447 public function getPermalink()
Chris@0 448 {
Chris@0 449 return $this->getLink(0);
Chris@0 450 }
Chris@0 451
Chris@0 452 /**
Chris@0 453 * Get the entry title
Chris@0 454 *
Chris@0 455 * @return string
Chris@0 456 */
Chris@0 457 public function getTitle()
Chris@0 458 {
Chris@0 459 if (array_key_exists('title', $this->data)) {
Chris@0 460 return $this->data['title'];
Chris@0 461 }
Chris@0 462
Chris@0 463 $title = null;
Chris@0 464
Chris@0 465 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 466 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 467 ) {
Chris@0 468 $title = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/title)');
Chris@0 469 } else {
Chris@0 470 $title = $this->xpath->evaluate('string(' . $this->xpathQueryRdf . '/rss:title)');
Chris@0 471 }
Chris@0 472
Chris@0 473 if (!$title) {
Chris@0 474 $title = $this->getExtension('DublinCore')->getTitle();
Chris@0 475 }
Chris@0 476
Chris@0 477 if (!$title) {
Chris@0 478 $title = $this->getExtension('Atom')->getTitle();
Chris@0 479 }
Chris@0 480
Chris@0 481 if (!$title) {
Chris@0 482 $title = null;
Chris@0 483 }
Chris@0 484
Chris@0 485 $this->data['title'] = $title;
Chris@0 486
Chris@0 487 return $this->data['title'];
Chris@0 488 }
Chris@0 489
Chris@0 490 /**
Chris@0 491 * Get the number of comments/replies for current entry
Chris@0 492 *
Chris@0 493 * @return string|null
Chris@0 494 */
Chris@0 495 public function getCommentCount()
Chris@0 496 {
Chris@0 497 if (array_key_exists('commentcount', $this->data)) {
Chris@0 498 return $this->data['commentcount'];
Chris@0 499 }
Chris@0 500
Chris@0 501 $commentcount = $this->getExtension('Slash')->getCommentCount();
Chris@0 502
Chris@0 503 if (!$commentcount) {
Chris@0 504 $commentcount = $this->getExtension('Thread')->getCommentCount();
Chris@0 505 }
Chris@0 506
Chris@0 507 if (!$commentcount) {
Chris@0 508 $commentcount = $this->getExtension('Atom')->getCommentCount();
Chris@0 509 }
Chris@0 510
Chris@0 511 if (!$commentcount) {
Chris@0 512 $commentcount = null;
Chris@0 513 }
Chris@0 514
Chris@0 515 $this->data['commentcount'] = $commentcount;
Chris@0 516
Chris@0 517 return $this->data['commentcount'];
Chris@0 518 }
Chris@0 519
Chris@0 520 /**
Chris@0 521 * Returns a URI pointing to the HTML page where comments can be made on this entry
Chris@0 522 *
Chris@0 523 * @return string
Chris@0 524 */
Chris@0 525 public function getCommentLink()
Chris@0 526 {
Chris@0 527 if (array_key_exists('commentlink', $this->data)) {
Chris@0 528 return $this->data['commentlink'];
Chris@0 529 }
Chris@0 530
Chris@0 531 $commentlink = null;
Chris@0 532
Chris@0 533 if ($this->getType() !== Reader\Reader::TYPE_RSS_10
Chris@0 534 && $this->getType() !== Reader\Reader::TYPE_RSS_090
Chris@0 535 ) {
Chris@0 536 $commentlink = $this->xpath->evaluate('string(' . $this->xpathQueryRss . '/comments)');
Chris@0 537 }
Chris@0 538
Chris@0 539 if (!$commentlink) {
Chris@0 540 $commentlink = $this->getExtension('Atom')->getCommentLink();
Chris@0 541 }
Chris@0 542
Chris@0 543 if (!$commentlink) {
Chris@0 544 $commentlink = null;
Chris@0 545 }
Chris@0 546
Chris@0 547 $this->data['commentlink'] = $commentlink;
Chris@0 548
Chris@0 549 return $this->data['commentlink'];
Chris@0 550 }
Chris@0 551
Chris@0 552 /**
Chris@0 553 * Returns a URI pointing to a feed of all comments for this entry
Chris@0 554 *
Chris@0 555 * @return string
Chris@0 556 */
Chris@0 557 public function getCommentFeedLink()
Chris@0 558 {
Chris@0 559 if (array_key_exists('commentfeedlink', $this->data)) {
Chris@0 560 return $this->data['commentfeedlink'];
Chris@0 561 }
Chris@0 562
Chris@0 563 $commentfeedlink = $this->getExtension('WellFormedWeb')->getCommentFeedLink();
Chris@0 564
Chris@0 565 if (!$commentfeedlink) {
Chris@0 566 $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rss');
Chris@0 567 }
Chris@0 568
Chris@0 569 if (!$commentfeedlink) {
Chris@0 570 $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink('rdf');
Chris@0 571 }
Chris@0 572
Chris@0 573 if (!$commentfeedlink) {
Chris@0 574 $commentfeedlink = null;
Chris@0 575 }
Chris@0 576
Chris@0 577 $this->data['commentfeedlink'] = $commentfeedlink;
Chris@0 578
Chris@0 579 return $this->data['commentfeedlink'];
Chris@0 580 }
Chris@0 581
Chris@0 582 /**
Chris@0 583 * Set the XPath query (incl. on all Extensions)
Chris@0 584 *
Chris@0 585 * @param DOMXPath $xpath
Chris@0 586 * @return void
Chris@0 587 */
Chris@0 588 public function setXpath(DOMXPath $xpath)
Chris@0 589 {
Chris@0 590 parent::setXpath($xpath);
Chris@0 591 foreach ($this->extensions as $extension) {
Chris@0 592 $extension->setXpath($this->xpath);
Chris@0 593 }
Chris@0 594 }
Chris@0 595 }