A single class for RSS feed generation


As of the latest release (1.1.6.1), Wikka has a quite limited RSS support, both as output and input. Before thinking of possible extensions, we need to implement in a more consistent way the available RSS-related services.

RSS as input

As for RSS embedding, which can be used to feed content to Wikka from external servers, we are switching to a new parser (like Magpie): Magpie is less buggy and way more flexible than the current one (Onyx).

RSS as output

As for RSS generation, at the moment (1.1.6.1) Wikka has two separate handlers to generate feeds for page revisions and recently changed pages.

The problem
There are many issues with the current feed generation tools:
- feed generation is hardcoded in these handlers, so it cannot be easily modified or extended;
- these handlers produce feeds based on different standards: RSS 2.0 (page revisions) and RSS 0.92 (recently changed pages)
- only the recently changed pages has a dedicated stylesheet.

The solution
Instead of manually fixing the current handlers, why not create some general feed generation utility that we can adapt to the specific kind of output to be produced? And instead of writing it ourselves and reinventing the wheel, why not simply write a wrapper around an existing feed generation class? There are many examples of light, flexible and GPL'ed PHP classes for feed generation that we could integrate with Wikka.

One that looks quite promising is: http://feedcreator.org/

Here's the feature list reported from its website:

FeedCreator.class.php provides an easy way to create RSS feeds from within PHP using ease to use classes.

Features:
  • creates valid feeds according to RSS 0.91, 1.0 or 2.0 as well as PIE 0.1 (deprecated), OPML 1.0, Unix mbox, ATOM 0.3, or customizable HTML or Javascript format.
  • configurable feed caching
  • very well documented and easy to use
  • feed image
  • includes almost all RSS 0.91 attributes
  • decide which version to create during runtime
  • easy to use and well documented class interface
  • intelligently truncates strings when needed


Now, what we need to do is simply integrate such a class into Wikka and create a wrapper method to provide more flexibility and configurability. This will allow us to create on the fly new handlers for specific kinds of output and in multiple formats. The number and choice of formats to be displayed might be determined by Wiki Admins in the system configuration.

Test implementation: a new feed.xml.php handler

This is a replacement for the current recentchanges feed.

Installation and setup
Download the FeedCreator class. Unzip the package and save the class in a comfortable location, e.g. 3rdparty/plugins/feedcreator/feedcreator.class.php.
Add the following line to wikka.config.php, immediately before the GeSHi related entries:
	"feedcreator_path" => "3rdparty/plugins/feedcreator",

Usage
Append /feed.xml or /feed.xml?f=FORMAT (/feed.xml&f=FORMAT if mod_rewrite is disabled) to a page URL, where FORMAT can be any of the following: RSS0.91, RSS1.0, RSS2.0, ATOM1.0. If no format or an unknown format is specified, then RSS2.0 is used.

Validation
Changelog
To do
The code
Save the following code as handlers/page/feed.xml.php

  1. <?php
  2.  /**
  3.  * Creates a feed with recently changed pages
  4.  *
  5.  * This handler generates a list of recently changed pages on the current server. The output
  6.  * format can be specified in the URL.
  7.  *
  8.  * @package     Handlers
  9.  * @name        recentchanges.xml
  10.  *
  11.  * @author      {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
  12.  * @version     0.3.1
  13.  * @access  public
  14.  * @since   wikka 1.1.X.X
  15.  * @uses    wakka::config
  16.  * @uses    FeedCreator (1.7.2-ppt)
  17.  * @todo    - move defaults to wiki configuration files
  18.  *
  19.  * @input   string  $f  optional: output format, can be any of the following:
  20.  *              RSS0.91, RSS1.0, RSS2.0, ATOM1.0
  21.  *              default: RSS2.0
  22.  *              the default format can be overridden by providing a URL parameter 'f'.
  23.  * @output  feed for recently changed pages in the specified format.
  24.  */
  25.  
  26. //defaults
  27. define('VALID_FORMATS', "RSS0.91,RSS1.0,RSS2.0,ATOM1.0");
  28. define('DEFAULT_OUTPUT_FORMAT',"RSS2.0");
  29. define('DESCRIPTION_TRUNCATE_SIZE',"500"); #character limit to truncate description
  30. define('DESCRIPTION_HTML_SYNDICATE',"TRUE"); #Indicates whether the description field should be rendered in HTML
  31.  
  32. //stylesheets & images
  33. define('FEED_CSS',"http://wikka.jsnx.com/css/xml.css");
  34. define('IMAGE_URL',"http://wikka.jsnx.com/images/wikka_logo.jpg");
  35.  
  36. //i18n strings
  37. define('FEED_TITLE',"%s - recently changed pages");
  38. define('FEED_DESCRIPTION',"New and recently changed pages from %s");
  39. define('IMAGE_TITLE',"Wikka logo");
  40. define('IMAGE_DESCRIPTION',"Feed provided by Wikka");
  41.  
  42. //initialize variables
  43. $f = '';
  44.  
  45.  
  46. //get URL parameters
  47. $formats = explode(",",VALID_FORMATS);
  48. $f = (in_array($_GET['f'], $formats))? $_GET['f'] : DEFAULT_OUTPUT_FORMAT;
  49.  
  50. //create objects
  51. include_once($this->config['feedcreator_path'].'/feedcreator.class.php');
  52. $rss = new UniversalFeedCreator();
  53. $rss->useCached(); #make this configurable
  54. $rss->title = sprintf(FEED_TITLE, $this->GetConfigValue("wakka_name"));
  55. $rss->description = sprintf(FEED_DESCRIPTION, $this->GetConfigValue("wakka_name"));
  56. $rss->cssStyleSheet = FEED_CSS;
  57. $rss->descriptionTruncSize = DESCRIPTION_TRUNCATE_SIZE;
  58. $rss->descriptionHtmlSyndicated = DESCRIPTION_HTML_SYNDICATE;
  59. $rss->link = $this->GetConfigValue("base_url").$this->GetConfigValue("root_page");
  60. $rss->syndicationURL = $this->Href($this->method,'','f='.$f);
  61.  
  62. //feed image
  63. $image = new FeedImage();
  64. $image->title = IMAGE_TITLE;
  65. $image->url = IMAGE_URL;
  66. $image->link = $this->GetConfigValue("base_url").$this->GetConfigValue("root_page");
  67. $image->description = IMAGE_DESCRIPTION;
  68. $image->descriptionTruncSize =  DESCRIPTION_TRUNCATE_SIZE;
  69. $image->descriptionHtmlSyndicated = DESCRIPTION_HTML_SYNDICATE;
  70. $rss->image = $image;
  71.  
  72. //get feed items
  73. if ($pages = $this->LoadRecentlyChanged())
  74. {
  75.     $max = $this->GetConfigValue("xml_recent_changes");
  76.     $c = 0;
  77.     foreach ($pages as $page)
  78.     {
  79.         $c++;
  80.         if (($c <= $max) || !$max)
  81.         {
  82.             $item = new FeedItem();
  83.             $item->title = $this->GetConfigValue("wakka_name").' - '.$page['tag'];
  84.             $item->link = $this->Href("show", $page["tag"], "time=".urlencode($page["time"]));
  85.             $item->description = ($page['note'] ? ' - '.$page['note'] : '');
  86.             $item->date = date('r',strtotime($page['time']));
  87.             $item->source = $this->GetConfigValue("base_url");
  88.             if (($f == 'ATOM1.0' || $f == 'RSS1.0') && $this->LoadUser($page['user']))
  89.             {
  90.                 $item->author = $page['user']; # RSS0.91 and RSS2.0 require authorEmail
  91.             }
  92.             $rss->addItem($item);
  93.         }
  94.     }
  95. }
  96.  
  97. //print feed
  98. echo $rss->createFeed($f);
  99. ?>




Feedback welcome


CategoryDevelopmentSyndication
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki