rss.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. class FeedItem_RSS extends FeedItem_Common {
  3. function get_id() {
  4. $id = $this->elem->getElementsByTagName("guid")->item(0);
  5. if ($id) {
  6. return $id->nodeValue;
  7. } else {
  8. return $this->get_link();
  9. }
  10. }
  11. function get_date() {
  12. $pubDate = $this->elem->getElementsByTagName("pubDate")->item(0);
  13. if ($pubDate) {
  14. return strtotime($pubDate->nodeValue);
  15. }
  16. $date = $this->xpath->query("dc:date", $this->elem)->item(0);
  17. if ($date) {
  18. return strtotime($date->nodeValue);
  19. }
  20. }
  21. function get_link() {
  22. $links = $this->xpath->query("atom:link", $this->elem);
  23. foreach ($links as $link) {
  24. if ($link && $link->hasAttribute("href") &&
  25. (!$link->hasAttribute("rel")
  26. || $link->getAttribute("rel") == "alternate"
  27. || $link->getAttribute("rel") == "standout")) {
  28. return trim($link->getAttribute("href"));
  29. }
  30. }
  31. $link = $this->elem->getElementsByTagName("guid")->item(0);
  32. if ($link && $link->hasAttributes() && $link->getAttribute("isPermaLink") == "true") {
  33. return trim($link->nodeValue);
  34. }
  35. $link = $this->elem->getElementsByTagName("link")->item(0);
  36. if ($link) {
  37. return trim($link->nodeValue);
  38. }
  39. }
  40. function get_title() {
  41. $title = $this->xpath->query("title", $this->elem)->item(0);
  42. if ($title) {
  43. return trim($title->nodeValue);
  44. }
  45. // if the document has a default namespace then querying for
  46. // title would fail because of reasons so let's try the old way
  47. $title = $this->elem->getElementsByTagName("title")->item(0);
  48. if ($title) {
  49. return trim($title->nodeValue);
  50. }
  51. }
  52. function get_content() {
  53. $contentA = $this->xpath->query("content:encoded", $this->elem)->item(0);
  54. $contentB = $this->elem->getElementsByTagName("description")->item(0);
  55. if ($contentA && !$contentB) {
  56. return $this->subtree_or_text($contentA);
  57. }
  58. if ($contentB && !$contentA) {
  59. return $this->subtree_or_text($contentB);
  60. }
  61. if ($contentA && $contentB) {
  62. $resultA = $this->subtree_or_text($contentA);
  63. $resultB = $this->subtree_or_text($contentB);
  64. return mb_strlen($resultA) > mb_strlen($resultB) ? $resultA : $resultB;
  65. }
  66. }
  67. function get_description() {
  68. $summary = $this->elem->getElementsByTagName("description")->item(0);
  69. if ($summary) {
  70. return $summary->nodeValue;
  71. }
  72. }
  73. function get_categories() {
  74. $categories = $this->elem->getElementsByTagName("category");
  75. $cats = array();
  76. foreach ($categories as $cat) {
  77. array_push($cats, trim($cat->nodeValue));
  78. }
  79. $categories = $this->xpath->query("dc:subject", $this->elem);
  80. foreach ($categories as $cat) {
  81. array_push($cats, trim($cat->nodeValue));
  82. }
  83. return $cats;
  84. }
  85. function get_enclosures() {
  86. $enclosures = $this->elem->getElementsByTagName("enclosure");
  87. $encs = array();
  88. foreach ($enclosures as $enclosure) {
  89. $enc = new FeedEnclosure();
  90. $enc->type = $enclosure->getAttribute("type");
  91. $enc->link = $enclosure->getAttribute("url");
  92. $enc->length = $enclosure->getAttribute("length");
  93. $enc->height = $enclosure->getAttribute("height");
  94. $enc->width = $enclosure->getAttribute("width");
  95. array_push($encs, $enc);
  96. }
  97. $enclosures = $this->xpath->query("media:content", $this->elem);
  98. foreach ($enclosures as $enclosure) {
  99. $enc = new FeedEnclosure();
  100. $enc->type = $enclosure->getAttribute("type");
  101. $enc->link = $enclosure->getAttribute("url");
  102. $enc->length = $enclosure->getAttribute("length");
  103. $enc->height = $enclosure->getAttribute("height");
  104. $enc->width = $enclosure->getAttribute("width");
  105. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  106. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  107. array_push($encs, $enc);
  108. }
  109. $enclosures = $this->xpath->query("media:group", $this->elem);
  110. foreach ($enclosures as $enclosure) {
  111. $enc = new FeedEnclosure();
  112. $content = $this->xpath->query("media:content", $enclosure)->item(0);
  113. if ($content) {
  114. $enc->type = $content->getAttribute("type");
  115. $enc->link = $content->getAttribute("url");
  116. $enc->length = $content->getAttribute("length");
  117. $enc->height = $content->getAttribute("height");
  118. $enc->width = $content->getAttribute("width");
  119. $desc = $this->xpath->query("media:description", $content)->item(0);
  120. if ($desc) {
  121. $enc->title = strip_tags($desc->nodeValue);
  122. } else {
  123. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  124. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  125. }
  126. array_push($encs, $enc);
  127. }
  128. }
  129. $enclosures = $this->xpath->query("media:thumbnail", $this->elem);
  130. foreach ($enclosures as $enclosure) {
  131. $enc = new FeedEnclosure();
  132. $enc->type = "image/generic";
  133. $enc->link = $enclosure->getAttribute("url");
  134. $enc->height = $enclosure->getAttribute("height");
  135. $enc->width = $enclosure->getAttribute("width");
  136. array_push($encs, $enc);
  137. }
  138. return $encs;
  139. }
  140. }