Browse Source

Added preliminary support for extracting category data from RSS 2.0 feeds.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19147 44c647ce-9c0f-0410-b52a-842ac1e357ba
padraic 16 years ago
parent
commit
df9c5931c0
22 changed files with 352 additions and 2 deletions
  1. 33 0
      library/Zend/Feed/Reader/Collection.php
  2. 53 0
      library/Zend/Feed/Reader/Collection/Category.php
  3. 41 0
      library/Zend/Feed/Reader/Collection/CollectionAbstract.php
  4. 38 0
      library/Zend/Feed/Reader/Entry/Rss.php
  5. 34 2
      tests/Zend/Feed/Reader/Entry/RssTest.php
  6. 10 0
      tests/Zend/Feed/Reader/Entry/_files/Atom/commentcount/plain/atom10/rss20.xml
  7. 7 0
      tests/Zend/Feed/Reader/Entry/_files/Atom/commentcount/plain/none/rss20.xml
  8. 9 0
      tests/Zend/Feed/Reader/Entry/_files/Atom/commentcount/plain/thread10/rss20.xml
  9. 9 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss090.xml
  10. 7 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss091.xml
  11. 7 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss092.xml
  12. 7 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss093.xml
  13. 7 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss094.xml
  14. 9 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss10.xml
  15. 7 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss20.xml
  16. 12 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss090.xml
  17. 10 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss091.xml
  18. 10 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss092.xml
  19. 10 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss093.xml
  20. 10 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss094.xml
  21. 12 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss10.xml
  22. 10 0
      tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/rss20.xml

+ 33 - 0
library/Zend/Feed/Reader/Collection.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Feed_Reader
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed_Reader
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Reader_Collection extends ArrayObject
+{
+
+    
+
+}

+ 53 - 0
library/Zend/Feed/Reader/Collection/Category.php

@@ -0,0 +1,53 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Feed_Reader
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+ 
+/**
+ * @see Zend_Feed_Reader_Collection_CollectionAbstract
+ */
+require_once 'Zend/Feed/Reader/Collection/CollectionAbstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed_Reader
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Reader_Collection_Category
+extends Zend_Feed_Reader_Collection_CollectionAbstract
+{
+
+    /**
+     * Return a simple array of the most relevant slice of
+     * the collection values. For example, feed categories contain
+     * the category name, domain/URI, and other data. This method would
+     * merely return the most useful data - i.e. the category names.
+     *
+     * @return array
+     */
+    public function getValues() {
+        $categories = array();
+        foreach ($this->getIterator() as $element) {
+            $categories[] = $element['label'];
+        }
+        return array_unique($categories);
+    }
+
+}

+ 41 - 0
library/Zend/Feed/Reader/Collection/CollectionAbstract.php

@@ -0,0 +1,41 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Feed_Reader
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed_Reader
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+abstract class Zend_Feed_Reader_Collection_CollectionAbstract extends ArrayObject
+{
+
+    /**
+     * Return a simple array of the most relevant slice of
+     * the collection values. For example, feed categories contain
+     * the category name, domain/URI, and other data. This method would
+     * merely return the most useful data - i.e. the category names.
+     *
+     * @return array
+     */
+    public abstract function getValues();
+
+}

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

@@ -69,6 +69,8 @@ require_once 'Zend/Feed/Reader/Extension/Thread/Entry.php';
  */
 require_once 'Zend/Date.php';
 
+require_once 'Zend/Feed/Reader/Collection/Category.php';
+
 /**
  * @category   Zend
  * @package    Zend_Feed_Reader
@@ -458,6 +460,42 @@ class Zend_Feed_Reader_Entry_Rss extends Zend_Feed_Reader_EntryAbstract implemen
 
         return $this->_data['links'];
     }
+    
+    /**
+     * 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->getType() !== Zend_Feed_Reader::TYPE_RSS_10 &&
+            $this->getType() !== Zend_Feed_Reader::TYPE_RSS_090) {
+            $list = $this->_xpath->query($this->_xpathQueryRss.'//category');
+        } else {
+            $list = $this->_xpath->query($this->_xpathQueryRdf.'//rss:category');
+        }
+
+        if (!$list->length) {
+            $categoryCollection = new Zend_Feed_Reader_Collection_Category;
+        } else {
+            $categoryCollection = new Zend_Feed_Reader_Collection_Category;
+            foreach ($list as $category) {
+                $categoryCollection[] = array(
+                    'term' => $category->nodeValue,
+                    'scheme' => $category->getAttribute('domain'),
+                    'label' => $category->nodeValue,
+                );
+            }
+        }
+
+        $this->_data['categories'] = $categoryCollection;
+
+        return $this->_data['categories'];
+    }
 
     /**
      * Get a permalink to the entry

+ 34 - 2
tests/Zend/Feed/Reader/Entry/RssTest.php

@@ -36,6 +36,8 @@ class Zend_Feed_Reader_Entry_RssTest extends PHPUnit_Framework_TestCase
 {
 
     protected $_feedSamplePath = null;
+    
+    protected $_expectedCats = array();
 
     public function setup()
     {
@@ -51,6 +53,23 @@ class Zend_Feed_Reader_Entry_RssTest 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' => 'topic2',
+                'scheme' => 'http://example.com/schema1',
+                'label' => 'topic2'
+            )
+        );
     }
     
     public function teardown()
@@ -2482,7 +2501,7 @@ class Zend_Feed_Reader_Entry_RssTest extends PHPUnit_Framework_TestCase
      * Get CommentFeedLink (Unencoded Text)
      */
 
- // Atom 1.0
+    // RSS
 
     public function testGetsCommentFeedLinkFromRss20_WellFormedWeb10()
     {
@@ -2676,5 +2695,18 @@ class Zend_Feed_Reader_Entry_RssTest extends PHPUnit_Framework_TestCase
         $entry = $feed->current();
         $this->assertEquals(null, $entry->getCommentFeedLink());
     }
-
+    
+    /**
+     * Get category data
+     * @group ZFR001
+     */
+    public function testGetsCategoriesFromRss20()
+    {
+        $feed = Zend_Feed_Reader::importString(
+            file_get_contents($this->_feedSamplePath.'/category/plain/rss20.xml')
+        );
+        $entry = $feed->current();
+        $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
+        $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
+    }
 }

+ 10 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/commentcount/plain/atom10/rss20.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="2.0"
+    xmlns:atom="http://www.w3.org/2005/Atom"
+    xmlns:thread="http://purl.org/syndication/thread/1.0">
+    <channel>
+        <item>
+            <atom:link type="text/html" rel="replies" href="http://www.example.com/comments" thread:count="321" />
+        </item>
+    </channel>
+</rss>

+ 7 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/commentcount/plain/none/rss20.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="2.0">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rss>

+ 9 - 0
tests/Zend/Feed/Reader/Entry/_files/Atom/commentcount/plain/thread10/rss20.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="2.0"
+    xmlns:thread="http://purl.org/syndication/thread/1.0">
+    <channel>
+        <item>
+            <thread:total>321</thread:total>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rdf:RDF
+    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+    xmlns="http://my.netscape.com/rdf/simple/0.9/">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rdf:RDF>

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

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.91">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.92">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.93">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.94">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rdf:RDF
+    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+    xmlns="http://purl.org/rss/1.0/">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rdf:RDF>

+ 7 - 0
tests/Zend/Feed/Reader/Entry/_files/Rss/category/plain/none/rss20.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="2.0">
+    <channel>
+        <item>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rdf:RDF
+    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+    xmlns="http://my.netscape.com/rdf/simple/0.9/">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rdf:RDF>

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

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.91">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.92">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.93">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="0.94">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rss>

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

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rdf:RDF
+    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+    xmlns="http://purl.org/rss/1.0/">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rdf:RDF>

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

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="2.0">
+    <channel>
+        <item>
+            <category domain="http://example.com/schema1">topic1</category>
+            <category domain="http://example.com/schema2">topic1</category>
+            <category domain="http://example.com/schema1">topic2</category>
+        </item>
+    </channel>
+</rss>