atom.php 4.5 KB

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