Ver código fonte

Added support for locating feed links and returning absolute URLs instead of relatives - fixes ZF-8330

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19105 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 16 anos atrás
pai
commit
a3785daa68

+ 1 - 1
library/Zend/Feed/Reader.php

@@ -405,7 +405,7 @@ class Zend_Feed_Reader
         }
         $feedSet = new Zend_Feed_Reader_FeedSet;
         $links = $dom->getElementsByTagName('link');
-        $feedSet->addLinks($links);
+        $feedSet->addLinks($links, $uri);
         return $feedSet;
     }
 

+ 27 - 5
library/Zend/Feed/Reader/FeedSet.php

@@ -25,6 +25,11 @@
 require_once 'Zend/Feed/Reader.php';
 
 /**
+ * @see Zend_Uri
+ */
+require_once 'Zend/Uri.php';
+
+/**
  * @category   Zend
  * @package    Zend_Feed_Reader
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
@@ -52,9 +57,10 @@ class Zend_Feed_Reader_FeedSet extends ArrayObject
      * loaded automatically when each links 'feed' array key is accessed.
      *
      * @param DOMNodeList $links
+     * @param string $uri
      * @return void
      */
-    public function addLinks(DOMNodeList $links)
+    public function addLinks(DOMNodeList $links, $uri)
     {
         foreach ($links as $link) {
             if (strtolower($link->getAttribute('rel')) !== 'alternate'
@@ -62,19 +68,35 @@ class Zend_Feed_Reader_FeedSet extends ArrayObject
                 continue;
             }
             if (!isset($this->rss) && $link->getAttribute('type') == 'application/rss+xml') {
-                $this->rss = trim($link->getAttribute('href'));
+                $this->rss = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
             } elseif(!isset($this->atom) && $link->getAttribute('type') == 'application/atom+xml') {
-                $this->atom = trim($link->getAttribute('href'));
+                $this->atom = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
             } elseif(!isset($this->rdf) && $link->getAttribute('type') == 'application/rdf+xml') {
-                $this->rdf = trim($link->getAttribute('href'));
+                $this->rdf = $this->_absolutiseUri(trim($link->getAttribute('href')), $uri);
             }
             $this[] = new self(array(
                 'rel' => 'alternate',
                 'type' => $link->getAttribute('type'),
-                'href' => trim($link->getAttribute('href')),
+                'href' => $this->_absolutiseUri(trim($link->getAttribute('href')), $uri),
             ));
         }
     }
+    
+    /**
+     *  Attempt to turn a relative URI into an absolute URI
+     */
+    protected function _absolutiseUri($link, $uri = null)
+    {
+        if (!Zend_Uri::check($link)) {
+            if (!is_null($uri)) {
+                $link = rtrim($uri, '/') . '/' . ltrim($link, '/');
+                if (!Zend_Uri::check($link)) {
+                    $link = null;
+                }
+            }
+        }
+        return $link;
+    }
 
     /**
      * Supports lazy loading of feeds using Zend_Feed_Reader::import() but

+ 20 - 0
tests/Zend/Feed/ReaderTest.php

@@ -249,6 +249,26 @@ class Zend_Feed_ReaderTest extends PHPUnit_Framework_TestCase
         }
         $this->assertEquals('http://feeds.feedburner.com/jonnyken/infoblog', $links->rss);
     }
+    
+    /**
+     * @group ZF-8330
+     */
+    public function testGetsFeedLinksAndNormalisesRelativeUrls()
+    {
+        if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
+            || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
+        ) {
+            $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
+            return;
+        }
+
+        try {
+            $links = Zend_Feed_Reader::findFeedLinks('http://meiobit.com');
+        } catch(Exception $e) {
+            $this->fail($e->getMessage());
+        }
+        $this->assertEquals('http://meiobit.com/rss.xml', $links->rss);
+    }
 
     public function testAddsPrefixPath()
     {