Просмотр исходного кода

Zend_Feed_Reader: Adds full support for collecting ALL entry category data from ALL RSS/RDF and Atom feeds.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19159 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 16 лет назад
Родитель
Сommit
3048a6dc24

+ 25 - 0
library/Zend/Feed/Reader/Entry/Atom.php

@@ -74,6 +74,9 @@ class Zend_Feed_Reader_Entry_Atom extends Zend_Feed_Reader_EntryAbstract impleme
 
         $threadClass = Zend_Feed_Reader::getPluginLoader()->getClassName('Thread_Entry');
         $this->_extensions['Thread_Entry'] = new $threadClass($entry, $entryKey, $type);
+        
+        $threadClass = Zend_Feed_Reader::getPluginLoader()->getClassName('DublinCore_Entry');
+        $this->_extensions['DublinCore_Entry'] = new $threadClass($entry, $entryKey, $type);
     }
 
     /**
@@ -341,6 +344,28 @@ class Zend_Feed_Reader_Entry_Atom extends Zend_Feed_Reader_EntryAbstract impleme
 
         return $this->_data['commentfeedlink'];
     }
+    
+    /**
+     * Get category data as a Zend_Feed_Reader_Collection_Category object
+     *
+     * @return Zend_Feed_Reader_Collection_Category
+     */
+    public function getCategories()
+    {
+        if (array_key_exists('categories', $this->_data)) {
+            return $this->_data['categories'];
+        }
+
+        $categoryCollection = $this->getExtension('Atom')->getCategories();
+        
+        if (count($categoryCollection) == 0) {
+            $categoryCollection = $this->getExtension('DublinCore')->getCategories();
+        }
+
+        $this->_data['categories'] = $categoryCollection;
+
+        return $this->_data['categories'];
+    }
 
     /**
      * Set the XPath query (incl. on all Extensions)

+ 7 - 0
library/Zend/Feed/Reader/Entry/Rss.php

@@ -69,6 +69,9 @@ require_once 'Zend/Feed/Reader/Extension/Thread/Entry.php';
  */
 require_once 'Zend/Date.php';
 
+/**
+ * @see Zend_Feed_Reader_Collection_Category
+ */
 require_once 'Zend/Feed/Reader/Collection/Category.php';
 
 /**
@@ -491,6 +494,10 @@ class Zend_Feed_Reader_Entry_Rss extends Zend_Feed_Reader_EntryAbstract implemen
         } else {
             $categoryCollection = $this->getExtension('DublinCore')->getCategories();
         }
+        
+        if (count($categoryCollection) == 0) {
+            $categoryCollection = $this->getExtension('Atom')->getCategories();
+        }
 
         $this->_data['categories'] = $categoryCollection;
 

+ 46 - 0
library/Zend/Feed/Reader/Extension/Atom/Entry.php

@@ -40,6 +40,11 @@ require_once 'Zend/Date.php';
 require_once 'Zend/Uri.php';
 
 /**
+ * @see Zend_Feed_Reader_Collection_Category
+ */
+require_once 'Zend/Feed/Reader/Collection/Category.php';
+
+/**
  * @category   Zend
  * @package    Zend_Feed_Reader
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
@@ -459,6 +464,47 @@ class Zend_Feed_Reader_Extension_Atom_Entry
 
         return $this->_data['commentfeedlink'];
     }
+    
+    /**
+     * Get all categories
+     *
+     * @return Zend_Feed_Reader_Collection_Category
+     */
+    public function getCategories()
+    {
+        if (array_key_exists('categories', $this->_data)) {
+            return $this->_data['categories'];
+        }
+
+        if ($this->_getAtomType() == Zend_Feed_Reader::TYPE_ATOM_10) {
+            $list = $this->_xpath->query($this->getXpathPrefix() . '//atom:category');
+        } else {
+            /**
+             * Since Atom 0.3 did not support categories, it would have used the
+             * Dublin Core extension. However there is a small possibility Atom 0.3
+             * may have been retrofittied to use Atom 1.0 instead.
+             */
+            $this->_xpath->registerNamespace('atom10', Zend_Feed_Reader::NAMESPACE_ATOM_10);
+            $list = $this->_xpath->query($this->getXpathPrefix() . '//atom10:category');
+        }
+
+        if ($list->length) {
+            $categoryCollection = new Zend_Feed_Reader_Collection_Category;
+            foreach ($list as $category) {
+                $categoryCollection[] = array(
+                    'term' => $category->getAttribute('term'),
+                    'scheme' => $category->getAttribute('scheme'),
+                    'label' => html_entity_decode($category->getAttribute('label'))
+                );
+            }
+        } else {
+            return new Zend_Feed_Reader_Collection_Category;
+        }
+
+        $this->_data['categories'] = $categoryCollection;
+
+        return $this->_data['categories'];
+    }
 
     /**
      *  Attempt to absolutise the URI, i.e. if a relative URI apply the

+ 104 - 0
tests/Zend/Feed/Reader/Entry/AtomTest.php

@@ -36,6 +36,10 @@ class Zend_Feed_Reader_Entry_AtomTest extends PHPUnit_Framework_TestCase
 {
 
     protected $_feedSamplePath = null;
+    
+    protected $_expectedCats = array();
+    
+    protected $_expectedCatsDc = array();
 
     public function setup()
     {
@@ -51,6 +55,35 @@ class Zend_Feed_Reader_Entry_AtomTest extends PHPUnit_Framework_TestCase
             }
         }
         Zend_Date::setOptions(array('format_type'=>'iso'));
+        $this->_expectedCats = array(
+            array(
+                'term' => 'topic1',
+                'scheme' => 'http://example.com/schema1',
+                'label' => 'topic1'
+            ),
+            array(
+                'term' => 'topic1',
+                'scheme' => 'http://example.com/schema2',
+                'label' => 'topic1'
+            ),
+            array(
+                'term' => 'cat_dog',
+                'scheme' => 'http://example.com/schema1',
+                'label' => 'Cat & Dog'
+            )
+        );
+        $this->_expectedCatsDc = array(
+            array(
+                'term' => 'topic1',
+                'scheme' => null,
+                'label' => 'topic1'
+            ),
+            array(
+                'term' => 'topic2',
+                'scheme' => null,
+                'label' => 'topic2'
+            )
+        );
     }
     
     public function teardown()
@@ -374,4 +407,75 @@ class Zend_Feed_Reader_Entry_AtomTest extends PHPUnit_Framework_TestCase
         $entry = $feed->current();
         $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
     }
+    
+    /**
+     * Get category data
+     */
+    
+    // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
+    
+    public function testGetsCategoriesFromAtom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromAtom03_Atom10Extension()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    // DC 1.0/1.1 for Atom 0.3
+    
+    public function testGetsCategoriesFromAtom03_Dc10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromAtom03_Dc11()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    // No Categories In Entry
+    
+    public function testGetsCategoriesFromAtom10_None()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals(array(), (array) $entry->getCategories());
+        $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromAtom03_None()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals(array(), (array) $entry->getCategories());
+        $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
+    }
+    
 }

+ 81 - 0
tests/Zend/Feed/Reader/Entry/RssTest.php

@@ -40,6 +40,8 @@ class Zend_Feed_Reader_Entry_RssTest extends PHPUnit_Framework_TestCase
     protected $_expectedCats = array();
     
     protected $_expectedCatsRdf = array();
+    
+    protected $_expectedCatsAtom = array();
 
     public function setup()
     {
@@ -84,6 +86,23 @@ class Zend_Feed_Reader_Entry_RssTest extends PHPUnit_Framework_TestCase
                 'label' => 'topic2'
             )
         );
+        $this->_expectedCatsAtom = array(
+            array(
+                'term' => 'topic1',
+                'scheme' => 'http://example.com/schema1',
+                'label' => 'topic1'
+            ),
+            array(
+                'term' => 'topic1',
+                'scheme' => 'http://example.com/schema2',
+                'label' => 'topic1'
+            ),
+            array(
+                'term' => 'cat_dog',
+                'scheme' => 'http://example.com/schema1',
+                'label' => 'Cat & Dog'
+            )
+        );
     }
     
     public function teardown()
@@ -2850,6 +2869,68 @@ class Zend_Feed_Reader_Entry_RssTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
     }
     
+    // Atom 1.0
+    
+    public function testGetsCategoriesFromRss090_Atom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10/rss090.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsAtom, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromRss091_Atom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10/rss091.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsAtom, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromRss092_Atom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10/rss092.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsAtom, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromRss093_Atom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10/rss093.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsAtom, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromRss094_Atom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10/rss094.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsAtom, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
+    public function testGetsCategoriesFromRss10_Atom10()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/atom10/rss10.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCatsAtom, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
+    }
+    
     // No Categories In Entry
     
     public function testGetsCategoriesFromRss20_None()

+ 9 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/category/plain/atom03.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed version="0.3" xmlns="http://purl.org/atom/ns#"
+    xmlns:atom10="http://www.w3.org/2005/Atom">
+    <entry>
+        <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+        <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+        <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+    </entry>
+</feed>

+ 8 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/category/plain/atom10.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+    <entry>
+        <category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+        <category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+        <category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+    </entry>
+</feed>

+ 8 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/category/plain/dc10/atom03.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed version="0.3" xmlns="http://purl.org/atom/ns#"
+    xmlns:dc="http://purl.org/dc/elements/1.0/">
+    <entry>
+        <dc:subject><![CDATA[topic1]]></dc:subject>
+        <dc:subject><![CDATA[topic2]]></dc:subject>
+    </entry>
+</feed>

+ 8 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/category/plain/dc11/atom03.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed version="0.3" xmlns="http://purl.org/atom/ns#"
+    xmlns:dc="http://purl.org/dc/elements/1.1/">
+    <entry>
+        <dc:subject><![CDATA[topic1]]></dc:subject>
+        <dc:subject><![CDATA[topic2]]></dc:subject>
+    </entry>
+</feed>

+ 5 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/category/plain/none/atom03.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed version="0.3" xmlns="http://purl.org/atom/ns#">
+    <entry>
+    </entry>
+</feed>

+ 5 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/category/plain/none/atom10.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+    <entry>
+    </entry>
+</feed>

+ 13 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/atom10/rss090.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rdf:RDF
+    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+    xmlns:atom10="http://www.w3.org/2005/Atom"
+    xmlns="http://my.netscape.com/rdf/simple/0.9/">
+    <channel>
+        <item>
+            <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+        </item>
+    </channel>
+</rdf:RDF>

+ 10 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/atom10/rss091.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.91" xmlns:atom10="http://www.w3.org/2005/Atom">
+    <channel>
+        <item>
+            <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+        </item>
+    </channel>
+</rss>

+ 10 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/atom10/rss092.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.92" xmlns:atom10="http://www.w3.org/2005/Atom">
+    <channel>
+        <item>
+            <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+        </item>
+    </channel>
+</rss>

+ 10 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/atom10/rss093.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.93" xmlns:atom10="http://www.w3.org/2005/Atom">
+    <channel>
+        <item>
+            <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+        </item>
+    </channel>
+</rss>

+ 10 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/atom10/rss094.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.94" xmlns:atom10="http://www.w3.org/2005/Atom">
+    <channel>
+        <item>
+            <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+        </item>
+    </channel>
+</rss>

+ 13 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/atom10/rss10.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rdf:RDF
+    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+    xmlns:atom10="http://www.w3.org/2005/Atom"
+    xmlns="http://purl.org/rss/1.0/">
+    <channel>
+        <item>
+            <atom10:category scheme="http://example.com/schema1" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema2" term="topic1" label="topic1"/>
+            <atom10:category scheme="http://example.com/schema1" term="cat_dog" label="Cat &amp; Dog"/>
+        </item>
+    </channel>
+</rdf:RDF>