User:Marksw/WordOfTheDay

 
A proposal to move this page to MediaWiki.org was rejected.

WordOfTheDay is a MediaWiki extension that displays the Dictionary.com Word of the Day on your Wiki. Dictionary.com provide an RSS feed for their Word of the Day, so it's a fairly simple task to get this and parse it.

In order to improve performance the extension makes use of caching. You can specify the cache directory and cache lifetime yourself; the default settings are /tmp as the cache directory and 2 hours as the cache lifetime.

Rather than use magpieRSS like everyone else (I'm sure it's a great library), I've opted to use two PEAR classes:

  1. XML_RSS
  2. Cache_Lite

I've chosen these because 1) I think PEAR is great, and 2), it will probably be more future-proof than Magpie. See below for installation details.

Syntax edit

This extension takes no parameters.

<wotd></wotd>

Example edit

<wotd></wotd>

Result edit

This extension produces HTML ouput along the lines of the following:

Word of the day: <a href="http://dictionary.reference.com/wordof ...
           ... theday/archive/2005/08/22.html" target="_blank">schadenfreude</a>

Source edit

Copy this source code and save it into a file called WordOfTheDay.php.

<?php
/*
** WordOfTheDay.php
** Dictionary.com Word Of The Day MediaWiki Extension
**
** Requires two PEAR classes:
** - XML_RSS module
** - Cache_Lite 
** Get them from http://pear.php.net/
**
** Version 1.0 by Mark Sweeting 2005/08/21
**     http://meta.wikimedia.org/wiki/User:Marksw/WordOfTheDay
*/
require_once 'Cache/Lite.php';

$wgExtensionFunctions[] = "wfWordOfTheDayExtension";

function wfWordOfTheDayExtension()
{
   global $wgParser;
   $wgParser->setHook( "wotd", "renderWOTD" );
}

function renderWOTD( $input="", $argv=array() )
{
   // prepare the cache
   $id = 'wotdid';
   $data = array();

   // Set a few options
   $options = array(
       'cacheDir' => '/tmp/',      // must include trailing slash...
       'lifeTime' => 7200          // time in seconds 2 hours
   );

   // Create a Cache_Lite object
   $Cache_Lite = new Cache_Lite($options);

   // Test if there is a valide cache for this id
   if ($data = $Cache_Lite->get($id))
   {
      // Cache hit
      $data = unserialize($data);
   }
   else
   {
      // Cache miss, so get the RSS file and parse it

      // include the XML_RSS package
      require_once 'XML/RSS.php';

      $rss =& new XML_RSS("http://dictionary.reference.com/wordoftheday/wotd.rss");
      $rss->parse();
      foreach ($rss->getItems() as $item)
      {
         $data = array();
         $data['link'] = $item['link'];
         list($data['title'], $tmp) = explode(":",$item['title']);
         // only get the first item
         break;
      }
      $Cache_Lite->save(serialize($data));
   }

   // make the output nice and pretty
   $output =  "Word of the day: <a href=\"" . $data['link'] . "\" target=\"_blank\">" . $data['title'] . "</a>\n";
   return $output;
}
?>

Installing the extension edit

First you need to get the two PEAR packages.

If you already have PEAR installed on your server (and have the appropriate include path setup) then all you need to do is install these two packages with the following commands (on the command line of your web server):

>pear install XML_RSS

and

>pear install Cache_Lite

This should download the files you need and set everything up.

Once PEAR is set up, copy the code above into a file called WordOfTheDay.php and save this into your extensions directory.

Then add the following line to the end of your LocalSettings.php file:

include("extensions/WordOfTheDay.php");

and you are ready to go.

Caveat edit

There may be a problem with page caching that prevents this from working properly all the time. I'm not too sure about this at the moment, but will keep you posted. -- User:Marksw, 25 August 2005

Update: There is a problem with page caching that prevents this from working properly all the time. I tried the fix suggested to disable caching, but this doesn't seem to work. I'll keep on looking... -- User:Marksw, 05 September 2005

I think all we can do is wait for the next version of mediawiki. This seems to have better cache control. Marksw 15:19, 27 September 2005 (UTC)