Author: as
Date: Fri Oct 26 17:20:37 2007
New Revision: 6589

Log:
- Added support for RSS2 enclosure and tutorial on how to use it for creating
  and parsing podcasts.

Added:
    trunk/Feed/tests/rss2/regression/generate/enclosure/
    trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.in
    trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.out
    trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.in
    trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.out
    trunk/Feed/tests/rss2/regression/parse/enclosure/
    trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.in
    trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.out
    trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.in
    trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.out
Modified:
    trunk/Feed/ChangeLog
    trunk/Feed/docs/tutorial.txt
    trunk/Feed/src/interfaces/processor.php
    trunk/Feed/src/nodes/element.php
    trunk/Feed/src/processors/rss2.php
    trunk/Feed/src/structs/feed_schema.php
    trunk/Feed/tests/regression_test.php

Modified: trunk/Feed/ChangeLog
==============================================================================
--- trunk/Feed/ChangeLog [iso-8859-1] (original)
+++ trunk/Feed/ChangeLog [iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -12,6 +12,8 @@
 - Added regression tests based on the ones from Template.
 - Added ezcFeedSchema to define different feed types.
 - Added ezcFeedElement instead of ezcFeedItem.
+- Added support for RSS2 enclosure and tutorial on how to use it for creating
+  and parsing podcasts.
 
 
 1.0beta1 - Monday 18 December 2006

Modified: trunk/Feed/docs/tutorial.txt
==============================================================================
--- trunk/Feed/docs/tutorial.txt [iso-8859-1] (original)
+++ trunk/Feed/docs/tutorial.txt [iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -101,9 +101,81 @@
     $item = $feed->add( 'item' );
     $item->title = 'Item title';
 
-- generate an XML document from the ezcFeed object. Example: ::
+- generate an XML document from the ezcFeed object. Example::
 
     $xml = $feed->generate();
+
+
+Applications
+============
+
+Podcasts
+--------
+
+A podcast is a collection of media files distributed over the Internet using
+feeds.
+
+The Feed component supports creating and parsing feeds which define podcasts.
+
+
+Parse an RSS2 podcast
+`````````````````````
+
+The following example shows how to parse an RSS2 podcast::
+
+    $feed = ezcFeed::parse( 'http://www.example.com/podcast.xml' );
+
+    $media = array();
+    foreach ( $feed->items as $item )
+    {
+        if ( isset( $item->enclosure[0] ) )
+        {
+            $enclosure = $item->enclosure[0];
+
+            $media[] = array(
+               'url' => isset( $enclosure->url ) ? $enclosure->url : 'not 
defined',
+               'length' => isset( $enclosure->length ) ? $enclosure->length : 
'not defined',
+               'type' => isset( $enclosure->type ) ? $enclosure->type : 'not 
defined' );
+        }
+    }
+
+After running the code, the array $media will contain the URL, length and type
+of the media files specified in the feed.
+
+
+Create an RSS2 podcast
+``````````````````````
+
+The following example shows how to create an RSS2 podcast with 2 media files::
+
+    $feed = new ezcFeed( 'rss2' );
+
+    $feed->title = 'Feed title';
+
+    $link = $feed->add( 'link' );
+    $link->set( 'Feed link' );
+
+    $feed->description = 'Feed description';
+
+    $item = $feed->add( 'item' );
+    $item->title = 'Title for media file 1';
+    $item->link = 'Link for media file 1';
+    $item->description = 'Description for media file 1';
+
+    $enclosure = $item->add( 'enclosure' );
+    $enclosure->url = 'http://www.example.com/media_file1.mp3';
+    $enclosure->length = 4300;
+    $enclosure->type = 'audio/x-mp3';
+
+    $item = $feed->add( 'item' );
+    $item->title = 'Title for media file 2';
+    $item->link = 'Link for media file 2';
+    $item->description = 'Description for media file 2';
+
+    $enclosure = $item->add( 'enclosure' );
+    $enclosure->url = 'http://www.example.com/media_file2.mp3';
+    $enclosure->length = 6500;
+    $enclosure->type = 'audio/x-mp3';
 
 
 Specifications and RFCs

Modified: trunk/Feed/src/interfaces/processor.php
==============================================================================
--- trunk/Feed/src/interfaces/processor.php [iso-8859-1] (original)
+++ trunk/Feed/src/interfaces/processor.php [iso-8859-1] Fri Oct 26 17:20:37 
2007
@@ -108,7 +108,7 @@
      * @param mixed|array(mixed) $value The value(s) for $element
      * @param array(string=>mixed) $attributes The attributes to add to the 
node
      */
-    public function generateMetaDataWithAttributes( DOMNode $root, $element, 
$value, array $attributes )
+    public function generateMetaDataWithAttributes( DOMNode $root, $element, 
$value = false, array $attributes )
     {
         if ( !is_array( $value ) )
         {
@@ -116,7 +116,14 @@
         }
         foreach ( $value as $valueElement )
         {
-            $meta = $this->xml->createElement( $element, $valueElement );
+            if ( $valueElement === false )
+            {
+                $meta = $this->xml->createElement( $element );
+            }
+            else
+            {
+                $meta = $this->xml->createElement( $element, $valueElement );
+            }
             foreach ( $attributes as $attrName => $attrValue )
             {
                 $attr = $this->xml->createAttribute( $attrName );

Modified: trunk/Feed/src/nodes/element.php
==============================================================================
--- trunk/Feed/src/nodes/element.php [iso-8859-1] (original)
+++ trunk/Feed/src/nodes/element.php [iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -93,7 +93,7 @@
      */
     public function __toString()
     {
-        return '' . $this->data['#'];
+        return isset( $this->data['#'] ) ? '' . $this->data['#'] : '';
     }
 
     /**
@@ -103,7 +103,10 @@
      */
     public function set( $value )
     {
-        $this->data['#'] = $value;
+        if ( $this->schema['#'] !== 'none' )
+        {
+            $this->data['#'] = $value;
+        }
     }
 
     /**
@@ -125,7 +128,7 @@
      */
     public function add( $name )
     {
-        $element = new ezcFeedElement( $this->schema );
+        $element = new ezcFeedElement( $this->schema['NODES'][$name] );
         $this->data[$name][] = $element;
         return $element;
     }

Modified: trunk/Feed/src/processors/rss2.php
==============================================================================
--- trunk/Feed/src/processors/rss2.php [iso-8859-1] (original)
+++ trunk/Feed/src/processors/rss2.php [iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -61,6 +61,7 @@
                                                      'description' => array( 
'#' => 'string' ),
                                                      'width'       => array( 
'#' => 'string' ),
                                                      'height'      => array( 
'#' => 'string' ),
+
                                                      'REQUIRED'    => array( 
'url', 'title', 'link' ),
                                                      'OPTIONAL'    => array( 
'description', 'width', 'height' ),
                                                      ), ),
@@ -79,7 +80,13 @@
                                                      'author'       => array( 
'#' => 'string' ),
                                                      'category'     => array( 
'#' => 'string' ),
                                                      'comments'     => array( 
'#' => 'string' ),
-                                                     'enclosure'    => array( 
'#' => 'string' ),
+                                                     'enclosure'    => array( 
'#' => 'none',
+                                                                              
'ATTRIBUTES' => array( 'url'    => 'string',
+                                                                               
                      'length' => 'string',
+                                                                               
                      'type'   => 'string' ),
+                                                                              
//'MULTI' => 'enclosures'
+                                                                              
),
+
                                                      'guid'         => array( 
'#' => 'string',
                                                                               
'ATTRIBUTES' => array( 'isPermaLink' => 'string' ) ),
 
@@ -197,7 +204,7 @@
 
             foreach ( $data as $dataNode )
             {
-                $this->generateNode( $element, $dataNode );
+                $this->generateNode( $this->channel, $element, $dataNode );
             }
         }
     }
@@ -232,7 +239,7 @@
 
                         foreach ( $data as $dataNode )
                         {
-                            $this->generateNode( $element, $dataNode );
+                            $this->generateNode( $this->channel, $element, 
$dataNode );
                         }
                         break;
                 }
@@ -243,11 +250,12 @@
     /**
      * Creates an XML node in the XML document being generated.
      *
+     * @param DOMNode $root The root in which to create the node $element
      * @param string $element The name of the node to create
      * @param array(string=>mixed) $dataNode The data for the node to create
      * @ignore
      */
-    protected function generateNode( $element, $dataNode )
+    protected function generateNode( DOMNode $root, $element, $dataNode )
     {
         $attributes = array();
         foreach ( $this->schema->getAttributes( $element ) as $attribute => 
$type )
@@ -257,13 +265,14 @@
                 $attributes[$attribute] = $dataNode->$attribute;
             }
         }
+
         if ( count( $attributes ) >= 1 )
         {
-            $this->generateMetaDataWithAttributes( $this->channel, $element, 
$dataNode, $attributes );
+            $this->generateMetaDataWithAttributes( $root, $element, $dataNode, 
$attributes );
         }
         else
         {
-            $this->generateMetaData( $this->channel, $element, $dataNode );
+            $this->generateMetaData( $root, $element, $dataNode );
         }
     }
 
@@ -305,7 +314,8 @@
             {
                 $normalizedAttribute = ezcFeedTools::normalizeName( 
$attribute, $this->schema->getItemsMap() );
 
-                $metaData = $this->schema->isMulti( 'item', $attribute ) ? 
$this->get( $this->schema->getMulti( 'item', $attribute ) ) : $item->$attribute;
+                $metaData = $this->schema->isMulti( 'item', $attribute ) ? 
$this->get( $this->schema->getMulti( 'item', $attribute ), $attribute ) : 
$item->$attribute;
+
                 if ( !is_null( $metaData ) )
                 {
                     // @todo Add hooks
@@ -318,6 +328,30 @@
 
                         case 'published':
                             $this->generateMetaData( $itemTag, 
$normalizedAttribute, date( 'D, d M Y H:i:s O', $metaData ) );
+                            break;
+
+                        case 'enclosure':
+                            foreach ( $metaData as $dataNode )
+                            {
+                                $attributes = array();
+                                foreach ( $this->schema->getAttributes( 
'item', 'enclosure' ) as $attribute => $type )
+                                {
+                                    if ( isset( $dataNode->$attribute ) )
+                                    {
+                                        $attributes[$attribute] = 
$dataNode->$attribute;
+                                    }
+                                }
+
+                                if ( count( $attributes ) >= 1 )
+                                {
+                                    $this->generateMetaDataWithAttributes( 
$itemTag, $normalizedAttribute, false, $attributes );
+                                }
+                                else
+                                {
+                                    // @todo Raise exception
+                                }
+                            }
+
                             break;
 
                         default:
@@ -472,7 +506,6 @@
                     case 'description':
                     case 'author':
                     case 'comments':
-                    case 'enclosure':
                     case 'guid':
                     case 'source':
                         $element->$tagName = $itemChild->textContent;
@@ -483,6 +516,7 @@
                         break;
 
                     case 'category':
+                    case 'enclosure':
                         $subElement = $element->add( $tagName );
                         $subElement->set( $itemChild->textContent );
                         break;
@@ -493,7 +527,7 @@
 
                 foreach ( ezcFeedTools::getAttributes( $itemChild ) as $key => 
$value )
                 {
-                    if ( in_array( $tagName, array( 'category' ) ) )
+                    if ( in_array( $tagName, array( 'category', 'enclosure' ) 
) )
                     {
                         $subElement->$key = $value;
                     }

Modified: trunk/Feed/src/structs/feed_schema.php
==============================================================================
--- trunk/Feed/src/structs/feed_schema.php [iso-8859-1] (original)
+++ trunk/Feed/src/structs/feed_schema.php [iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -39,15 +39,26 @@
      * schema.
      *
      * @param string $element The schema element
-     * @return array(string)
-     */
-    public function getAttributes( $element )
+     * @param string $subElement The subelement of $element
+     * @return array(string)
+     */
+    public function getAttributes( $element, $subElement = null )
     {
         $result = array();
 
-        if ( isset( $this->schema[$element]['ATTRIBUTES'] ) )
-        {
-            $result = $this->schema[$element]['ATTRIBUTES'];
+        if ( $subElement === null )
+        {
+            if ( isset( $this->schema[$element]['ATTRIBUTES'] ) )
+            {
+                $result = $this->schema[$element]['ATTRIBUTES'];
+            }
+        }
+        else
+        {
+            if ( isset( 
$this->schema[$element]['NODES'][$subElement]['ATTRIBUTES'] ) )
+            {
+                $result = 
$this->schema[$element]['NODES'][$subElement]['ATTRIBUTES'];
+            }
         }
 
         return $result;
@@ -155,6 +166,27 @@
     }
 
     /**
+     * Returns if $element does not accept a value for the root node. If 
$subElement
+     * is present then returns if the subelement $subElement of element 
$element
+     * accepts a value for the root node.
+     *
+     * @param string $element The schema element
+     * @param string $subElement The subelement of $element
+     * @return bool
+     */
+    public function isEmpty( $element, $subElement = null )
+    {
+        if ( $subElement === null )
+        {
+            return $this->schema[$element]['#'] === 'none';
+        }
+        else
+        {
+            return $this->schema[$element]['NODES'][$subElement]['#'] === 
'none';
+        }
+    }
+
+    /**
      * Returns the subschema which defines the element $element.
      *
      * @param string $element The schema element

Modified: trunk/Feed/tests/regression_test.php
==============================================================================
--- trunk/Feed/tests/regression_test.php [iso-8859-1] (original)
+++ trunk/Feed/tests/regression_test.php [iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -125,7 +125,19 @@
                                 }
                                 else
                                 {
-                                    $element->$subKey = $subValue;
+                                    if ( is_array( $subValue ) )
+                                    {
+                                        $subElement = $element->add( $subKey );
+                                        $subElement->set( 'yyy' );
+                                        foreach ( $subValue as $subSubKey => 
$subSubValue )
+                                        {
+                                            $subElement->$subSubKey = 
$subSubValue;
+                                        }
+                                    }
+                                    else
+                                    {
+                                        $element->$subKey = $subValue;
+                                    }
                                 }
                             }
                         }

Added: trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.in
==============================================================================
--- trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.in (added)
+++ trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.in 
[iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -1,0 +1,12 @@
+<?php
+return array( 'title' => 'Feed title',
+              'link'  => array( 'Feed link' ),
+              'description' => 'Feed description',
+                         'item' => array( array( 'title' => 'Item title',
+                                      'link' => 'Item link',
+                                      'description' => 'Item description',
+                                      'enclosure' => array( 'url' => 
'Enclosure url',
+                                                            'length' => 1200,
+                                                            'type' => 
'Enclosure type' ) ), ),
+            );
+?>

Added: trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.out
==============================================================================
--- trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.out (added)
+++ trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure.out 
[iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -1,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Feed title</title>
+    <link>Feed link</link>
+    <description>Feed description</description>
+    <pubDate>XXX</pubDate>
+    <generator>eZ Components</generator>
+    <docs>http://www.rssboard.org/rss-specification</docs>
+    <item>
+      <title>Item title</title>
+      <link>Item link</link>
+      <description>Item description</description>
+      <enclosure url="Enclosure url" length="1200" type="Enclosure type"/>
+    </item>
+  </channel>
+</rss>

Added: trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.in
==============================================================================
--- trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.in 
(added)
+++ trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.in 
[iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -1,0 +1,18 @@
+<?php
+return array( 'title' => 'Feed title',
+              'link'  => array( 'Feed link' ),
+              'description' => 'Feed description',
+                         'item' => array( array( 'title' => 'Item title 1',
+                                      'link' => 'Item link 1',
+                                      'description' => 'Item description 1',
+                                      'enclosure' => array( 'url' => 
'Enclosure url 1',
+                                                            'length' => 1200,
+                                                            'type' => 
'Enclosure type 1' ) ),
+                               array( 'title' => 'Item title 2',
+                                      'link' => 'Item link 2',
+                                      'description' => 'Item description 2',
+                                      'enclosure' => array( 'url' => 
'Enclosure url 2',
+                                                            'length' => 2400,
+                                                            'type' => 
'Enclosure type 2' ) ), ),
+            );
+?>

Added: 
trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.out
==============================================================================
--- trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.out 
(added)
+++ trunk/Feed/tests/rss2/regression/generate/enclosure/enclosure_multiple.out 
[iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -1,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Feed title</title>
+    <link>Feed link</link>
+    <description>Feed description</description>
+    <pubDate>XXX</pubDate>
+    <generator>eZ Components</generator>
+    <docs>http://www.rssboard.org/rss-specification</docs>
+    <item>
+      <title>Item title 1</title>
+      <link>Item link 1</link>
+      <description>Item description 1</description>
+      <enclosure url="Enclosure url 1" length="1200" type="Enclosure type 1"/>
+    </item>
+    <item>
+      <title>Item title 2</title>
+      <link>Item link 2</link>
+      <description>Item description 2</description>
+      <enclosure url="Enclosure url 2" length="2400" type="Enclosure type 2"/>
+    </item>
+  </channel>
+</rss>

Added: trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.in
==============================================================================
--- trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.in (added)
+++ trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.in [iso-8859-1] 
Fri Oct 26 17:20:37 2007
@@ -1,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Feed title</title>
+    <link>Feed link</link>
+    <description>Feed description</description>
+    <pubDate>XXX</pubDate>
+    <generator>eZ Components</generator>
+    <docs>http://www.rssboard.org/rss-specification</docs>
+    <item>
+      <title>Item title</title>
+      <link>Item link</link>
+      <description>Item description</description>
+      <enclosure url="Enclosure url" length="1200" type="Enclosure type"/>
+    </item>
+  </channel>
+</rss> 

Added: trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.out
==============================================================================
--- trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.out (added)
+++ trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure.out [iso-8859-1] 
Fri Oct 26 17:20:37 2007
@@ -1,0 +1,21 @@
+<?php
+$feed = new ezcFeed( 'rss2' );
+$feed->title = 'Feed title';
+
+$link = $feed->add( 'link' );
+$link->set( 'Feed link' );
+
+$feed->description = 'Feed description';
+
+$item = $feed->add( 'item' );
+$item->title = 'Item title';
+$item->link = 'Item link';
+$item->description = 'Item description';
+
+$enclosure = $item->add( 'enclosure' );
+$enclosure->url = 'Enclosure url';
+$enclosure->length = 1200;
+$enclosure->type = 'Enclosure type';
+
+return $feed;
+?>

Added: trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.in
==============================================================================
--- trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.in 
(added)
+++ trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.in 
[iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -1,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Feed title</title>
+    <link>Feed link</link>
+    <description>Feed description</description>
+    <pubDate>XXX</pubDate>
+    <generator>eZ Components</generator>
+    <docs>http://www.rssboard.org/rss-specification</docs>
+    <item>
+      <title>Item title 1</title>
+      <link>Item link 1</link>
+      <description>Item description 1</description>
+      <enclosure url="Enclosure url 1" length="1200" type="Enclosure type 1"/>
+    </item>
+    <item>
+      <title>Item title 2</title>
+      <link>Item link 2</link>
+      <description>Item description 2</description>
+      <enclosure url="Enclosure url 2" length="2400" type="Enclosure type 2"/>
+    </item>
+  </channel>
+</rss> 

Added: trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.out
==============================================================================
--- trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.out 
(added)
+++ trunk/Feed/tests/rss2/regression/parse/enclosure/enclosure_multiple.out 
[iso-8859-1] Fri Oct 26 17:20:37 2007
@@ -1,0 +1,31 @@
+<?php
+$feed = new ezcFeed( 'rss2' );
+$feed->title = 'Feed title';
+
+$link = $feed->add( 'link' );
+$link->set( 'Feed link' );
+
+$feed->description = 'Feed description';
+
+$item = $feed->add( 'item' );
+$item->title = 'Item title 1';
+$item->link = 'Item link 1';
+$item->description = 'Item description 1';
+
+$enclosure = $item->add( 'enclosure' );
+$enclosure->url = 'Enclosure url 1';
+$enclosure->length = 1200;
+$enclosure->type = 'Enclosure type 1';
+
+$item = $feed->add( 'item' );
+$item->title = 'Item title 2';
+$item->link = 'Item link 2';
+$item->description = 'Item description 2';
+
+$enclosure = $item->add( 'enclosure' );
+$enclosure->url = 'Enclosure url 2';
+$enclosure->length = 2400;
+$enclosure->type = 'Enclosure type 2';
+
+return $feed;
+?>


-- 
svn-components mailing list
[EMAIL PROTECTED]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to