Author: as Date: Mon Mar 3 12:40:52 2008 New Revision: 7477 Log: - Added a feed creator example application.
Added: trunk/Feed/docs/examples/ trunk/Feed/docs/examples/feed_creator/ trunk/Feed/docs/examples/feed_creator/data/ trunk/Feed/docs/examples/feed_creator/data/news.txt (with props) trunk/Feed/docs/examples/feed_creator/data/news.xml trunk/Feed/docs/examples/feed_creator/feed_creator.php (with props) Modified: trunk/Feed/ChangeLog trunk/Feed/docs/tutorial.txt Modified: trunk/Feed/ChangeLog ============================================================================== --- trunk/Feed/ChangeLog [iso-8859-1] (original) +++ trunk/Feed/ChangeLog [iso-8859-1] Mon Mar 3 12:40:52 2008 @@ -25,6 +25,7 @@ - Added the required xmlns attribute when creating RSS1 feeds. - Added parse support for version 0.93 and 0.94 RSS feeds. - The RSS2 guid item attribute is accessed as id through ezcFeed. +- Added a feed creator example application. 1.0beta1 - Monday 18 December 2006 Added: trunk/Feed/docs/examples/feed_creator/data/news.txt ============================================================================== --- trunk/Feed/docs/examples/feed_creator/data/news.txt (added) +++ trunk/Feed/docs/examples/feed_creator/data/news.txt [iso-8859-1] Mon Mar 3 12:40:52 2008 @@ -1,0 +1,23 @@ +eZ news feeds +http://ez.no +Derick [EMAIL PROTECTED] +This RSS feed contains news feeds for eZ Publish and eZ Components. + +eZ Components 2007.1 released +http://components.ez.no +Derick [EMAIL PROTECTED] +The new release of eZ Components include Workflow, Authentication... + +eZ Publish 4.0 released +http://publish.ez.no +Derick [EMAIL PROTECTED] +The new release of eZ Publish is based on PHP 5.... + +eZ Find.0 released +http://find.ez.no +Derick [EMAIL PROTECTED] +A new product in the eZ family of open-source solutions... Propchange: trunk/Feed/docs/examples/feed_creator/data/news.txt ------------------------------------------------------------------------------ svn:eol-style = native Added: trunk/Feed/docs/examples/feed_creator/data/news.xml ============================================================================== --- trunk/Feed/docs/examples/feed_creator/data/news.xml (added) +++ trunk/Feed/docs/examples/feed_creator/data/news.xml [iso-8859-1] Mon Mar 3 12:40:52 2008 @@ -1,0 +1,33 @@ +<?xml version="1.0" encoding="utf-8"?> +<rss version="2.0"> + <channel> + <title>eZ news feeds</title> + <link>http://ez.no</link> + <description>This RSS feed contains news feeds for eZ Publish and eZ Components.</description> + <managingEditor>[EMAIL PROTECTED] (Derick)</managingEditor> + <pubDate>Mon, 03 Mar 2008 12:39:14 +0100</pubDate> + <generator>eZ Components</generator> + <docs>http://www.rssboard.org/rss-specification</docs> + <item> + <title>eZ Components 2007.1 released</title> + <link>http://components.ez.no</link> + <description>The new release of eZ Components include Workflow, Authentication...</description> + <author>[EMAIL PROTECTED] (Derick)</author> + <guid isPermaLink="true">http://components.ez.no</guid> + </item> + <item> + <title>eZ Publish 4.0 released</title> + <link>http://publish.ez.no</link> + <description>The new release of eZ Publish is based on PHP 5....</description> + <author>[EMAIL PROTECTED] (Derick)</author> + <guid isPermaLink="true">http://publish.ez.no</guid> + </item> + <item> + <title>eZ Find.0 released</title> + <link>http://find.ez.no</link> + <description>A new product in the eZ family of open-source solutions...</description> + <author>[EMAIL PROTECTED] (Derick)</author> + <guid isPermaLink="true">http://find.ez.no</guid> + </item> + </channel> +</rss> Added: trunk/Feed/docs/examples/feed_creator/feed_creator.php ============================================================================== --- trunk/Feed/docs/examples/feed_creator/feed_creator.php (added) +++ trunk/Feed/docs/examples/feed_creator/feed_creator.php [iso-8859-1] Mon Mar 3 12:40:52 2008 @@ -1,0 +1,206 @@ +<?php +// Required for the eZ Components autoload mechanism. +// The components must be from SVN and the trunk directory must be in the path. +// For PEAR installations, use: require_once 'ezc/Base/base.php'; +require_once "Base/src/base.php"; + +/** + * Required for the eZ Components autoload mechanism. + * + * @param string $className A class to autoload + */ +function __autoload( $className ) +{ + ezcBase::autoload( $className ); +} + +// ************************************************************************* + +echo "eZ Components feed creator\n"; +if ( count( $argv ) < 3 ) +{ + echo "\tFirst parameter: feed type (rss1, rss2 or atom)\n"; + echo "\tSecond parameter: txt file name\n"; + die(); +} + +$feedType = $argv[1]; +$sourceFile = $argv[2]; + +$data = readDataFile( $sourceFile ); +$xml = createFeed( $feedType, $data ); + +$destFile = substr( $sourceFile, 0, strrpos( $sourceFile, '.' ) ) . '.xml'; +echo "Creating xml file {$destFile} with contents:\n\n"; +file_put_contents( $destFile, $xml ); +echo $xml . "\n\n"; + +// ************************************************************************* + +/** + * Reads data from a file and returns an array to be used with the function + * createFeed(). + * + * The format of the returned array is: + * <code> + * array( 'title' => 'Feed title', + * 'link' => 'Feed link', + * 'authorName' => 'Feed author name', + * 'authorEmail' => 'Feed author email', + * 'description' => 'Feed description', + * 'items' => array( + * 0 => array( 'title' => 'Item 0 title', + * 'link' => 'Item 0 link', + * 'authorName' => 'Item 0 author name', + * 'authorEmail' => 'Item 0 author email', + * 'description' => 'Item 0 description', + * ), + * 1 => array( 'title' => 'Item 1 title', + * 'link' => 'Item 1 link', + * 'authorName' => 'Item 1 author name', + * 'authorEmail' => 'Item 1 author email', + * 'description' => 'Item 1 description', + * ), + * ) + * ); + * </code> + * + * @throws ezcBaseFileNotFoundException + * If $fileName is not found or cannot be opened + * + * @param string $fileName A file name containing a full path + * @return array(mixed) + */ +function readDataFile( $fileName ) +{ + if ( !file_exists( $fileName ) ) + { + throw new ezcBaseFileNotFoundException( $fileName ); + } + + if ( ( $fh = @fopen( $fileName, 'r' ) ) === false ) + { + throw new ezcBaseFilePermissionException( $fileName, ezcBaseFileException::READ ); + } + + $data = array(); + $data['title'] = trim( fgets( $fh ) ); + $data['link'] = trim( fgets( $fh ) ); + $data['authorName'] = trim( fgets( $fh ) ); + $data['authorEmail'] = trim( fgets( $fh ) ); + $data['description'] = trim( fgets( $fh ) ); + $empty = fgets( $fh ); + + $data['item'] = array(); + $i = 0; + while ( !feof( $fh ) ) + { + $data['item'][$i] = array(); + $data['item'][$i]['title'] = trim( fgets( $fh ) ); + $data['item'][$i]['link'] = trim( fgets( $fh ) ); + $data['item'][$i]['authorName'] = trim( fgets( $fh ) ); + $data['item'][$i]['authorEmail'] = trim( fgets( $fh ) ); + $data['item'][$i]['description'] = trim( fgets( $fh ) ); + $empty = fgets( $fh ); + $i++; + } + fclose( $fh ); + return $data; +} + +/** + * Uses the array $data to create a feed of type $feedType ('rss1', 'rss2' or + * 'atom') and returns it as a string. + * + * The format of the $data array is: + * <code> + * array( 'title' => 'Feed title', + * 'link' => 'Feed link', + * 'description' => 'Feed description', + * 'items' => array( + * 0 => array( 'title' => 'Item 0 title', + * 'link' => 'Item 0 link', + * 'description' => 'Item 0 description', + * ), + * 1 => array( 'title' => 'Item 1 title', + * 'link' => 'Item 1 link', + * 'description' => 'Item 1 description', + * ), + * ) + * ); + * </code> + * + * @param string $feedType The type of the feed to create ('rss1', 'rss2' or 'atom') + * @param array(mixed) $data Data for the elements of the feed + * @return string + */ +function createFeed( $feedType, $data ) +{ + $feed = new ezcFeed( $feedType ); + $feed->title = $data['title']; + $feed->description = $data['description']; + + switch ( $feedType ) + { + case 'atom': + $link = $feed->add( 'link' ); + $link->href = $data['link']; + $feed->id = $data['link']; + $feed->updated = time(); + $author = $feed->add( 'author' ); + $author->name = $data['authorName']; + $author->email = $data['authorEmail']; + break; + + case 'rss1': + $feed->id = $data['link']; + $link = $feed->add( 'link' ); + $link->set( $data['link'] ); + break; + + case 'rss2': + $link = $feed->add( 'link' ); + $link->set( $data['link'] ); + $feed->author = $data['authorEmail'] . ' (' . $data['authorName'] . ')'; + break; + } + + foreach ( $data['item'] as $dataItem ) + { + $item = $feed->add( 'item' ); + $item->title = $dataItem['title']; + $item->description = $dataItem['description']; + + switch ( $feedType ) + { + case 'atom': + $item->id = $dataItem['link']; + $link = $item->add( 'link' ); + $link->href = $dataItem['link']; + $link->rel = 'alternate'; + $item->updated = time(); + $author = $item->add( 'author' ); + $author->name = $dataItem['authorName']; + $author->email = $dataItem['authorEmail']; + break; + + case 'rss1': + $item->id = $dataItem['link']; + $link = $item->add( 'link' ); + $link->set( $dataItem['link'] ); + break; + + case 'rss2': + $id = $item->add( 'id' ); + $id->set( $dataItem['link'] ); + $id->isPermaLink = true; + $link = $item->add( 'link' ); + $link->set( $dataItem['link'] ); + $item->author = $dataItem['authorEmail'] . ' (' . $dataItem['authorName'] . ')'; + break; + } + } + + return $feed->generate(); +} +?> Propchange: trunk/Feed/docs/examples/feed_creator/feed_creator.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/Feed/docs/tutorial.txt ============================================================================== --- trunk/Feed/docs/tutorial.txt [iso-8859-1] (original) +++ trunk/Feed/docs/tutorial.txt [iso-8859-1] Mon Mar 3 12:40:52 2008 @@ -17,7 +17,7 @@ ================== An XML feed is an XML document with a certain structure, which lists a series -of "entries" or "items". +of *entries* or *items*. Example ------- @@ -424,6 +424,56 @@ - RSS1 and RSS2 have attributes for the image, while ATOM does not have them. +Feed creator example +-------------------- + +In the sub-directory *Feed/docs/examples* there is a **feed_creator** +application which can be used to create simple XML feeds from minimal text +files. + +The structure of the text files accepted by this application is:: + + Feed title + Feed link + Feed author name + Feed author email + Feed description + + Item 1 title + Item 1 link + Item 1 author name + Item 1 author email + Item 1 author description + + Item 2 title + Item 2 link + Item 2 author name + Item 2 author email + Item 2 author description + + .. etc + +An example of an input text file: + +.. include:: examples/feed_creator/data/news.txt + :literal: + +The **feed_creator** application will read an input file with the above +structure and output an XML feed of the chosen type (rss1, rss2 or atom). An +XML file will also be written in the same directory as the input file, with +the name of the input file plus the *.xml* extension. + +Example of usage (current directory is the `feed_creator` directory):: + + php feed_creator.php rss2 data/news.txt + +After running this command, the file *data/news.xml* will be created, +containing an RSS2 feed with the values read from *data/news.txt*: + +.. include:: examples/feed_creator/data/news.xml + :literal: + + Best practices ============== -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components