Wednesday, February 13, 2008

Including Picasa Slideshows in MediaWiki

Unfortunately I was unable to find a MediaWiki Extension for Picasa slideshows.

Taking the idea (and PHP-code) from the MediaWiki GoogleCalendar Extension, I created a Picasa-Extension. It's very easy to get it running. Like all/most other MediaWiki Extensions the file should be placed in the /extensions directory.

Save the code below as /extensions/Picasa.php:

<?php
/**
* Picasa Slideshows
*
* Tag:
* <picasa>picasaweb-URL</picasa>
*
* Example:
* from url http://picasaweb.google.com/tester/GoogleSNewSantaMonicaOffice?authkey=3V8IljiqEO0
* <picasa>http://picasaweb.google.com/tester/GoogleSNewSantaMonicaOffice?authkey=3V8IljiqEO0</picasa>
*/

$wgExtensionFunctions[] = 'wfPicasa';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'Picasa Slideshow',
'description' => 'Display a Picasa Slideshow',
'author' => 'Niels de Vos (based on GoogleCalendar from Kasper Souren)',
'url' => 'http://nixpanic.blogger.com/'
);

function wfPicasa() {
global $wgParser;
$wgParser->setHook('picasa', 'renderPicasaSlideshow');
}

/* The callback function for converting the input text to HTML output */
function renderPicasaSlideshow($input) {
/* some defaults */
$width = 800;
$height = 600;
$feed_start = 'http://picasaweb.google.com/data/feed/';
$url_feed = '/data/feed/base/user';
$api_feed = '/data/feed/api/user';

/* convert the input to a string */
$input = "$input";

/* if the input is a direct URL to the album, give an error */
if (strpos($input, $feed_start) !== 0) {
$input = picasaGetFeed($input);
}

/* if the input is an RSS-feed, replace '../base/..' by '../api/..' */
$input = str_replace($url_feed, $api_feed, $input);

/* the flash-applet expects a URL-encoded parameter (?) */
$input = urlencode($input);

$output = '<embed type="application/x-shockwave-flash" src="http://picasaweb.google.com/s/c/bin/slideshow.swf" ' .
'width="' . $width . '" height="' . $height . '" ' .
'flashvars="' .
'host=picasaweb.google.com&' .
'captions=1&RGB=0x000000&' .
'feed=' . $input . '" ' .
'pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';

return $output . "\n";
}

/* variable and functions needed for parsing the HTML to get a RSS-feed */
$picasaFeedURL = '';

function picasaGetFeed($url)
{
global $picasaFeedURL;

$xp = xml_parser_create();
xml_set_element_handler($xp, 'picasaXEStart', 'picasaXEEnd');

$fp = fopen($url, 'r');
while ($data = fread($fp, 4096)) {
xml_parse($xp, $data);
}
fclose($fp);

xml_parser_free($xp);

return $picasaFeedURL;
}

function picasaXEEnd($parser, $name) {}

function picasaXEStart($parser, $name, $attrs)
{
global $picasaFeedURL;

if ($name == 'LINK') {
if (array_key_exists('TYPE', $attrs) && array_key_exists('HREF', $attrs)) {
if ($attrs['TYPE'] == 'application/rss+xml') {
$picasaFeedURL = $attrs['HREF'];
return;
}
}
}
}
?>


Now include in LocalSetting.php:

/* Picasa slideshow extension */
require_once("$IP/extensions/Picasa.php");


Edit an article and write the Picasa-URL of an album between <picasa></picasa> tags. As an example a test album of Google could be used (http://picasaweb.google.com/tester/GoogleSNewSantaMonicaOffice?authkey=3V8IljiqEO0):

<picasa>http://picasaweb.google.com/tester/GoogleSNewSantaMonicaOffice?authkey=3V8IljiqEO0</picasa>


The result should look like the slideshow on http://googleblog.blogspot.com/2007/05/oh-places-youll-go.html.