atom.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. class FeedItem_Atom extends FeedItem_Common {
  3. const NS_XML = "http://www.w3.org/XML/1998/namespace";
  4. function get_id() {
  5. $id = $this->elem->getElementsByTagName("id")->item(0);
  6. if ($id) {
  7. return $id->nodeValue;
  8. } else {
  9. return $this->get_link();
  10. }
  11. }
  12. function get_date() {
  13. $updated = $this->elem->getElementsByTagName("updated")->item(0);
  14. if ($updated) {
  15. return strtotime($updated->nodeValue);
  16. }
  17. $published = $this->elem->getElementsByTagName("published")->item(0);
  18. if ($published) {
  19. return strtotime($published->nodeValue);
  20. }
  21. $date = $this->xpath->query("dc:date", $this->elem)->item(0);
  22. if ($date) {
  23. return strtotime($date->nodeValue);
  24. }
  25. }
  26. function get_link() {
  27. $links = $this->elem->getElementsByTagName("link");
  28. foreach ($links as $link) {
  29. if ($link && $link->hasAttribute("href") &&
  30. (!$link->hasAttribute("rel")
  31. || $link->getAttribute("rel") == "alternate"
  32. || $link->getAttribute("rel") == "standout")) {
  33. $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link);
  34. if ($base)
  35. return rewrite_relative_url($base, trim($link->getAttribute("href")));
  36. else
  37. return trim($link->getAttribute("href"));
  38. }
  39. }
  40. }
  41. function get_title() {
  42. $title = $this->elem->getElementsByTagName("title")->item(0);
  43. if ($title) {
  44. return trim($title->nodeValue);
  45. }
  46. }
  47. function get_content() {
  48. $content = $this->elem->getElementsByTagName("content")->item(0);
  49. if ($content) {
  50. if ($content->hasAttribute('type')) {
  51. if ($content->getAttribute('type') == 'xhtml') {
  52. for ($i = 0; $i < $content->childNodes->length; $i++) {
  53. $child = $content->childNodes->item($i);
  54. if ($child->hasChildNodes()) {
  55. return $this->doc->saveHTML($child);
  56. }
  57. }
  58. }
  59. }
  60. return $this->subtree_or_text($content);
  61. }
  62. }
  63. function get_description() {
  64. $content = $this->elem->getElementsByTagName("summary")->item(0);
  65. if ($content) {
  66. if ($content->hasAttribute('type')) {
  67. if ($content->getAttribute('type') == 'xhtml') {
  68. for ($i = 0; $i < $content->childNodes->length; $i++) {
  69. $child = $content->childNodes->item($i);
  70. if ($child->hasChildNodes()) {
  71. return $this->doc->saveHTML($child);
  72. }
  73. }
  74. }
  75. }
  76. return $this->subtree_or_text($content);
  77. }
  78. }
  79. function get_categories() {
  80. $categories = $this->elem->getElementsByTagName("category");
  81. $cats = array();
  82. foreach ($categories as $cat) {
  83. if ($cat->hasAttribute("term"))
  84. array_push($cats, trim($cat->getAttribute("term")));
  85. }
  86. $categories = $this->xpath->query("dc:subject", $this->elem);
  87. foreach ($categories as $cat) {
  88. array_push($cats, trim($cat->nodeValue));
  89. }
  90. return $cats;
  91. }
  92. function get_enclosures() {
  93. $links = $this->elem->getElementsByTagName("link");
  94. $encs = array();
  95. foreach ($links as $link) {
  96. if ($link && $link->hasAttribute("href") && $link->hasAttribute("rel")) {
  97. if ($link->getAttribute("rel") == "enclosure") {
  98. $enc = new FeedEnclosure();
  99. $enc->type = $link->getAttribute("type");
  100. $enc->link = $link->getAttribute("href");
  101. $enc->length = $link->getAttribute("length");
  102. array_push($encs, $enc);
  103. }
  104. }
  105. }
  106. $enclosures = $this->xpath->query("media:content", $this->elem);
  107. foreach ($enclosures as $enclosure) {
  108. $enc = new FeedEnclosure();
  109. $enc->type = $enclosure->getAttribute("type");
  110. $enc->link = $enclosure->getAttribute("url");
  111. $enc->length = $enclosure->getAttribute("length");
  112. $enc->height = $enclosure->getAttribute("height");
  113. $enc->width = $enclosure->getAttribute("width");
  114. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  115. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  116. array_push($encs, $enc);
  117. }
  118. $enclosures = $this->xpath->query("media:group", $this->elem);
  119. foreach ($enclosures as $enclosure) {
  120. $enc = new FeedEnclosure();
  121. $content = $this->xpath->query("media:content", $enclosure)->item(0);
  122. if ($content) {
  123. $enc->type = $content->getAttribute("type");
  124. $enc->link = $content->getAttribute("url");
  125. $enc->length = $content->getAttribute("length");
  126. $enc->height = $content->getAttribute("height");
  127. $enc->width = $content->getAttribute("width");
  128. $desc = $this->xpath->query("media:description", $content)->item(0);
  129. if ($desc) {
  130. $enc->title = strip_tags($desc->nodeValue);
  131. } else {
  132. $desc = $this->xpath->query("media:description", $enclosure)->item(0);
  133. if ($desc) $enc->title = strip_tags($desc->nodeValue);
  134. }
  135. array_push($encs, $enc);
  136. }
  137. }
  138. $enclosures = $this->xpath->query("media:thumbnail", $this->elem);
  139. foreach ($enclosures as $enclosure) {
  140. $enc = new FeedEnclosure();
  141. $enc->type = "image/generic";
  142. $enc->link = $enclosure->getAttribute("url");
  143. $enc->height = $enclosure->getAttribute("height");
  144. $enc->width = $enclosure->getAttribute("width");
  145. array_push($encs, $enc);
  146. }
  147. return $encs;
  148. }
  149. function get_language() {
  150. $lang = $this->elem->getAttributeNS(self::NS_XML, "lang");
  151. if (!empty($lang)) {
  152. return $lang;
  153. } else {
  154. // Fall back to the language declared on the feed, if any.
  155. return $this->elem->parentNode->getAttributeNS(self::NS_XML, "lang");
  156. }
  157. }
  158. }