Przeglądaj źródła

Added lastBuildDate RSS support to Zend_Feed_Reader/Writer - implements ZF-9595

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22107 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 15 lat temu
rodzic
commit
3ec54aa2d7

+ 10 - 0
library/Zend/Feed/Reader/Feed/Atom.php

@@ -158,6 +158,16 @@ class Zend_Feed_Reader_Feed_Atom extends Zend_Feed_Reader_FeedAbstract
     }
     }
 
 
     /**
     /**
+     * Get the feed lastBuild date. This is not implemented in Atom.
+     *
+     * @return string|null
+     */
+    public function getLastBuildDate()
+    {
+        return null;
+    }
+
+    /**
      * Get the feed description
      * Get the feed description
      *
      *
      * @return string|null
      * @return string|null

+ 54 - 0
library/Zend/Feed/Reader/Feed/Rss.php

@@ -271,6 +271,60 @@ class Zend_Feed_Reader_Feed_Rss extends Zend_Feed_Reader_FeedAbstract
     }
     }
 
 
     /**
     /**
+     * Get the feed lastBuild date
+     *
+     * @return Zend_Date
+     */
+    public function getLastBuildDate()
+    {
+        if (array_key_exists('lastBuildDate', $this->_data)) {
+            return $this->_data['lastBuildDate'];
+        }
+
+        $lastBuildDate = null;
+        $date = null;
+
+        if ($this->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
+            $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
+            $lastBuildDate = $this->_xpath->evaluate('string(/rss/channel/lastBuildDate)');
+            if ($lastBuildDate) {
+                $lastBuildDateParsed = strtotime($lastBuildDate);
+                if ($lastBuildDateParsed) {
+                    $date = new Zend_Date($lastBuildDateParsed);
+                } else {
+                    $dateStandards = array(Zend_Date::RSS, Zend_Date::RFC_822,
+                    Zend_Date::RFC_2822, Zend_Date::DATES);
+                    $date = new Zend_Date;
+                    foreach ($dateStandards as $standard) {
+                        try {
+                            $date->set($lastBuildDate, $standard);
+                            break;
+                        } catch (Zend_Date_Exception $e) {
+                            if ($standard == Zend_Date::DATES) {
+                                require_once 'Zend/Feed/Exception.php';
+                                throw new Zend_Feed_Exception(
+                                    'Could not load date due to unrecognised'
+                                    .' format (should follow RFC 822 or 2822):'
+                                    . $e->getMessage(),
+                                    0, $e
+                                );
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        if (!$date) {
+            $date = null;
+        }
+
+        $this->_data['lastBuildDate'] = $date;
+
+        return $this->_data['lastBuildDate'];
+    }
+
+    /**
      * Get the feed description
      * Get the feed description
      *
      *
      * @return string|null
      * @return string|null

+ 34 - 0
library/Zend/Feed/Writer/Feed/FeedAbstract.php

@@ -206,6 +206,27 @@ class Zend_Feed_Writer_Feed_FeedAbstract
     }
     }
 
 
     /**
     /**
+     * Set the feed last-build date. Ignored for Atom 1.0.
+     *
+     * @param null|integer|Zend_Date
+     */
+    public function setLastBuildDate($date = null)
+    {
+        $zdate = null;
+        if (is_null($date)) {
+            $zdate = new Zend_Date;
+        } elseif (ctype_digit($date) && strlen($date) == 10) {
+            $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
+        } elseif ($date instanceof Zend_Date) {
+            $zdate = $date;
+        } else {
+            require_once 'Zend/Feed/Exception.php';
+            throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
+        }
+        $this->_data['lastBuildDate'] = $zdate;
+    }
+
+    /**
      * Set the feed description
      * Set the feed description
      *
      *
      * @return string|null
      * @return string|null
@@ -533,6 +554,19 @@ class Zend_Feed_Writer_Feed_FeedAbstract
     }
     }
 
 
     /**
     /**
+     * Get the feed last-build date
+     *
+     * @return string|null
+     */
+    public function getLastBuildDate()
+    {
+        if (!array_key_exists('lastBuildDate', $this->_data)) {
+            return null;
+        }
+        return $this->_data['lastBuildDate'];
+    }
+
+    /**
      * Get the feed description
      * Get the feed description
      *
      *
      * @return string|null
      * @return string|null

+ 22 - 0
library/Zend/Feed/Writer/Renderer/Feed/Rss.php

@@ -82,6 +82,7 @@ class Zend_Feed_Writer_Renderer_Feed_Rss
         $this->_setImage($this->_dom, $channel);
         $this->_setImage($this->_dom, $channel);
         $this->_setDateCreated($this->_dom, $channel);
         $this->_setDateCreated($this->_dom, $channel);
         $this->_setDateModified($this->_dom, $channel);
         $this->_setDateModified($this->_dom, $channel);
+        $this->_setLastBuildDate($this->_dom, $channel);
         $this->_setGenerator($this->_dom, $channel);
         $this->_setGenerator($this->_dom, $channel);
         $this->_setLink($this->_dom, $channel);
         $this->_setLink($this->_dom, $channel);
         $this->_setAuthors($this->_dom, $channel);
         $this->_setAuthors($this->_dom, $channel);
@@ -440,6 +441,27 @@ class Zend_Feed_Writer_Renderer_Feed_Rss
             );
             );
         }
         }
     }
     }
+
+    /**
+     * Set date feed last build date
+     * 
+     * @param DOMDocument $dom 
+     * @param DOMElement $root 
+     * @return void
+     */
+    protected function _setLastBuildDate(DOMDocument $dom, DOMElement $root)
+    {
+        if(!$this->getDataContainer()->getLastBuildDate()) {
+            return;
+        }
+
+        $lastBuildDate = $dom->createElement('lastBuildDate');
+        $root->appendChild($lastBuildDate);
+        $text = $dom->createTextNode(
+            $this->getDataContainer()->getLastBuildDate()->get(Zend_Date::RSS)
+        );
+        $lastBuildDate->appendChild($text);
+    }
     
     
     /**
     /**
      * Set base URL to feed links
      * Set base URL to feed links

+ 11 - 0
tests/Zend/Feed/Reader/Feed/AtomTest.php

@@ -223,6 +223,17 @@ class Zend_Feed_Reader_Feed_AtomTest extends PHPUnit_Framework_TestCase
     }
     }
 
 
     /**
     /**
+     * Get Last Build Date (Unencoded Text)
+     */
+    public function testGetsLastBuildDateAlwaysReturnsNullForAtom()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/datemodified/plain/atom10.xml')
+        );
+        $this->assertNull($feed->getLastBuildDate());
+    }
+
+    /**
      * Get Generator (Unencoded Text)
      * Get Generator (Unencoded Text)
      */
      */
     public function testGetsGeneratorFromAtom03()
     public function testGetsGeneratorFromAtom03()

+ 21 - 0
tests/Zend/Feed/Reader/Feed/RssTest.php

@@ -2100,6 +2100,27 @@ class Zend_Feed_Reader_Feed_RssTest extends PHPUnit_Framework_TestCase
     }
     }
 
 
     /**
     /**
+     * Get Last Build Date (Unencoded Text)
+     */
+    public function testGetsLastBuildDateFromRss20()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/lastbuilddate/plain/rss20.xml')
+        );
+        $edate = new Zend_Date;
+        $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
+        $this->assertTrue($edate->equals($feed->getLastBuildDate()));
+    }
+
+    public function testGetsLastBuildDateFromRss20_None()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/lastbuilddate/plain/none/rss20.xml')
+        );
+        $this->assertEquals(null, $feed->getLastBuildDate());
+    }
+
+    /**
      * Get Date Modified (Unencoded Text)
      * Get Date Modified (Unencoded Text)
      */
      */
     public function testGetsDateModifiedFromRss20()
     public function testGetsDateModifiedFromRss20()

+ 40 - 0
tests/Zend/Feed/Writer/FeedTest.php

@@ -262,6 +262,46 @@ class Zend_Feed_Writer_FeedTest extends PHPUnit_Framework_TestCase
         $this->assertTrue(is_null($writer->getDateModified()));
         $this->assertTrue(is_null($writer->getDateModified()));
     }
     }
 
 
+    public function testSetLastBuildDateDefaultsToCurrentTime()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $writer->setLastBuildDate();
+        $dateNow = new Zend_Date;
+        $this->assertTrue($dateNow->isLater($writer->getLastBuildDate()) || $dateNow->equals($writer->getLastBuildDate()));
+    }
+
+    public function testSetLastBuildDateUsesGivenUnixTimestamp()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $writer->setLastBuildDate(1234567890);
+        $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
+        $this->assertTrue($myDate->equals($writer->getLastBuildDate()));
+    }
+
+    public function testSetLastBuildDateUsesZendDateObject()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $writer->setLastBuildDate(new Zend_Date('1234567890', Zend_Date::TIMESTAMP));
+        $myDate = new Zend_Date('1234567890', Zend_Date::TIMESTAMP);
+        $this->assertTrue($myDate->equals($writer->getLastBuildDate()));
+    }
+
+    public function testSetLastBuildDateThrowsExceptionOnInvalidParameter()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        try {
+            $writer->setLastBuildDate('abc');
+            $this->fail();
+        } catch (Zend_Feed_Exception $e) {
+        }
+    }
+
+    public function testGetLastBuildDateReturnsNullIfDateNotSet()
+    {
+        $writer = new Zend_Feed_Writer_Feed;
+        $this->assertTrue(is_null($writer->getLastBuildDate()));
+    }
+
     public function testGetCopyrightReturnsNullIfDateNotSet()
     public function testGetCopyrightReturnsNullIfDateNotSet()
     {
     {
         $writer = new Zend_Feed_Writer_Feed;
         $writer = new Zend_Feed_Writer_Feed;

+ 9 - 0
tests/Zend/Feed/Writer/Renderer/Feed/RssTest.php

@@ -165,6 +165,15 @@ class Zend_Feed_Writer_Renderer_Feed_RssTest extends PHPUnit_Framework_TestCase
         $rssFeed->render();
         $rssFeed->render();
     }
     }
 
 
+    public function testFeedLastBuildDateHasBeenSet()
+    {
+        $this->_validWriter->setLastBuildDate(1234567890);
+        $rssFeed = new Zend_Feed_Writer_Renderer_Feed_Rss($this->_validWriter);
+        $rssFeed->render();
+        $feed = Zend_Feed_Reader::importString($rssFeed->saveXml());
+        $this->assertEquals(1234567890, $feed->getLastBuildDate()->get(Zend_Date::TIMESTAMP));
+    }
+
     public function testFeedGeneratorHasBeenSet()
     public function testFeedGeneratorHasBeenSet()
     {
     {
         $this->_validWriter->setGenerator('FooFeedBuilder', '1.00', 'http://www.example.com');
         $this->_validWriter->setGenerator('FooFeedBuilder', '1.00', 'http://www.example.com');