rss.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 $contentA->nodeValue;
  57. }
  58. if ($contentB && !$contentA) {
  59. return $contentB->nodeValue;
  60. }
  61. if ($contentA && $contentB) {
  62. return mb_strlen($contentA->nodeValue) > mb_strlen($contentB->nodeValue) ?
  63. $contentA->nodeValue : $contentB->nodeValue;
  64. }
  65. }
  66. function get_description() {
  67. $summary = $this->elem->getElementsByTagName("description")->item(0);
  68. if ($summary) {
  69. return $summary->nodeValue;
  70. }
  71. }
  72. function get_categories() {
  73. $categories = $this->elem->getElementsByTagName("category");
  74. $cats = array();
  75. foreach ($categories as $cat) {
  76. array_push($cats, trim($cat->nodeValue));
  77. }
  78. $categories = $this->xpath->query("dc:subject", $this->elem);
  79. foreach ($categories as $cat) {
  80. array_push($cats, trim($cat->nodeValue));
  81. }
  82. return $cats;
  83. }
  84. function get_enclosures() {
  85. $enclosures = $this->elem->getElementsByTagName("enclosure");
  86. $encs = array();
  87. foreach ($enclosures as $enclosure) {
  88. $enc = new FeedEnclosure();
  89. $enc->type = $enclosure->getAttribute("type");
  90. $enc->link = $enclosure->getAttribute("url");
  91. $enc->length = $enclosure->getAttribute("length");
  92. $enc->height = $enclosure->getAttribute("height");
  93. $enc->width = $enclosure->getAttribute("width");
  94. array_push($encs, $enc);
  95. }
  96. $enclosures = $this->xpath->query("media:content", $this->elem);
  97. foreach ($enclosures as $enclosure) {
  98. $enc = new FeedEnclosure();
  99. $enc->type = $enclosure->getAttribute("type");
  100. $enc->link = $enclosure->getAttribute("url");
  101. $enc->length = $enclosure->getAttribute("length");
  102. $enc->height = $enclosure->getAttribute("height");
  103. $enc->width = $enclosure->getAttribute("width");
  104. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  105. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  106. array_push($encs, $enc);
  107. }
  108. $enclosures = $this->xpath->query("media:group", $this->elem);
  109. foreach ($enclosures as $enclosure) {
  110. $enc = new FeedEnclosure();
  111. $content = $this->xpath->query("media:content", $enclosure)->item(0);
  112. if ($content) {
  113. $enc->type = $content->getAttribute("type");
  114. $enc->link = $content->getAttribute("url");
  115. $enc->length = $content->getAttribute("length");
  116. $enc->height = $content->getAttribute("height");
  117. $enc->width = $content->getAttribute("width");
  118. $desc = $this->xpath->query("media:description", $content)->item(0);
  119. if ($desc) {
  120. $enc->title = strip_tags($desc->nodeValue);
  121. } else {
  122. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  123. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  124. }
  125. array_push($encs, $enc);
  126. }
  127. }
  128. $enclosures = $this->xpath->query("media:thumbnail", $this->elem);
  129. foreach ($enclosures as $enclosure) {
  130. $enc = new FeedEnclosure();
  131. $enc->type = "image/generic";
  132. $enc->link = $enclosure->getAttribute("url");
  133. $enc->height = $enclosure->getAttribute("height");
  134. $enc->width = $enclosure->getAttribute("width");
  135. array_push($encs, $enc);
  136. }
  137. return $encs;
  138. }
  139. }
  140. ?>