]> git.wh0rd.org - tt-rss.git/blobdiff - classes/feeditem/atom.php
fix previous to not crash if document first child is not a DOMElement or whatever
[tt-rss.git] / classes / feeditem / atom.php
index 244fb1f84671ac61de286ea06ea8441f2e0b4b07..8c3e0f8a748b029b8c81b9df86cce89269efabdd 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 class FeedItem_Atom extends FeedItem_Common {
+       const NS_XML = "http://www.w3.org/XML/1998/namespace";
 
        function get_id() {
                $id = $this->elem->getElementsByTagName("id")->item(0);
@@ -43,9 +44,9 @@ class FeedItem_Atom extends FeedItem_Common {
                                $base = $this->xpath->evaluate("string(ancestor-or-self::*[@xml:base][1]/@xml:base)", $link);
 
                                if ($base)
-                                       return rewrite_relative_url($base, $link->getAttribute("href"));
+                                       return rewrite_relative_url($base, trim($link->getAttribute("href")));
                                else
-                                       return $link->getAttribute("href");
+                                       return trim($link->getAttribute("href"));
 
                        }
                }
@@ -55,7 +56,7 @@ class FeedItem_Atom extends FeedItem_Common {
                $title = $this->elem->getElementsByTagName("title")->item(0);
 
                if ($title) {
-                       return $title->nodeValue;
+                       return trim($title->nodeValue);
                }
        }
 
@@ -69,13 +70,13 @@ class FeedItem_Atom extends FeedItem_Common {
                                                $child = $content->childNodes->item($i);
 
                                                if ($child->hasChildNodes()) {
-                                                       return $this->doc->saveXML($child);
+                                                       return $this->doc->saveHTML($child);
                                                }
                                        }
                                }
                        }
 
-                       return $content->nodeValue;
+                       return $this->subtree_or_text($content);
                }
        }
 
@@ -89,13 +90,13 @@ class FeedItem_Atom extends FeedItem_Common {
                                                $child = $content->childNodes->item($i);
 
                                                if ($child->hasChildNodes()) {
-                                                       return $this->doc->saveXML($child);
+                                                       return $this->doc->saveHTML($child);
                                                }
                                        }
                                }
                        }
 
-                       return $content->nodeValue;
+                       return $this->subtree_or_text($content);
                }
 
        }
@@ -106,13 +107,13 @@ class FeedItem_Atom extends FeedItem_Common {
 
                foreach ($categories as $cat) {
                        if ($cat->hasAttribute("term"))
-                               array_push($cats, $cat->getAttribute("term"));
+                               array_push($cats, trim($cat->getAttribute("term")));
                }
 
                $categories = $this->xpath->query("dc:subject", $this->elem);
 
                foreach ($categories as $cat) {
-                       array_push($cats, $cat->nodeValue);
+                       array_push($cats, trim($cat->nodeValue));
                }
 
                return $cats;
@@ -145,6 +146,8 @@ class FeedItem_Atom extends FeedItem_Common {
                        $enc->type = $enclosure->getAttribute("type");
                        $enc->link = $enclosure->getAttribute("url");
                        $enc->length = $enclosure->getAttribute("length");
+                       $enc->height = $enclosure->getAttribute("height");
+                       $enc->width = $enclosure->getAttribute("width");
 
                        $desc = $this->xpath->query("media:description", $enclosure)->item(0);
                        if ($desc) $enc->title = strip_tags($desc->nodeValue);
@@ -160,19 +163,23 @@ class FeedItem_Atom extends FeedItem_Common {
 
                        $content = $this->xpath->query("media:content", $enclosure)->item(0);
 
-                       $enc->type = $content->getAttribute("type");
-                       $enc->link = $content->getAttribute("url");
-                       $enc->length = $content->getAttribute("length");
+                       if ($content) {
+                               $enc->type = $content->getAttribute("type");
+                               $enc->link = $content->getAttribute("url");
+                               $enc->length = $content->getAttribute("length");
+                               $enc->height = $content->getAttribute("height");
+                               $enc->width = $content->getAttribute("width");
+
+                               $desc = $this->xpath->query("media:description", $content)->item(0);
+                               if ($desc) {
+                                       $enc->title = strip_tags($desc->nodeValue);
+                               } else {
+                                       $desc = $this->xpath->query("media:description", $enclosure)->item(0);
+                                       if ($desc) $enc->title = strip_tags($desc->nodeValue);
+                               }
 
-                       $desc = $this->xpath->query("media:description", $content)->item(0);
-                       if ($desc) {
-                               $enc->title = strip_tags($desc->nodeValue);
-                       } else {
-                               $desc = $this->xpath->query("media:description", $enclosure)->item(0);
-                               if ($desc) $enc->title = strip_tags($desc->nodeValue);
+                               array_push($encs, $enc);
                        }
-
-                       array_push($encs, $enc);
                }
 
                $enclosures = $this->xpath->query("media:thumbnail", $this->elem);
@@ -182,6 +189,8 @@ class FeedItem_Atom extends FeedItem_Common {
 
                        $enc->type = "image/generic";
                        $enc->link = $enclosure->getAttribute("url");
+                       $enc->height = $enclosure->getAttribute("height");
+                       $enc->width = $enclosure->getAttribute("width");
 
                        array_push($encs, $enc);
                }
@@ -189,5 +198,18 @@ class FeedItem_Atom extends FeedItem_Common {
                return $encs;
        }
 
+       function get_language() {
+               $lang = $this->elem->getAttributeNS(self::NS_XML, "lang");
+
+               if (!empty($lang)) {
+                       return $lang;
+               } else {
+                       // Fall back to the language declared on the feed, if any.
+                       foreach ($this->doc->childNodes as $child) {
+                               if (method_exists($child, "getAttributeNS")) {
+                                       return $child->getAttributeNS(self::NS_XML, "lang");
+                               }
+                       }
+               }
+       }
 }
-?>