Extension:Flickr

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
Flickr

Release status: stable

Implementation Tag
Description Embed specific Flickr images in MediaWiki pages.
Author(s) Edward Simpson
Last Version 0.2.1 (10/31/07)
License No license specified
Download Original page
Example Original page and Extended syntax page

Contents

[edit] What can this extension do?

This extension displays images from Flickr. Upload your photos to Flickr and then share them in your wiki!

Actually there are some reasons for wanting to use this:

  1. bandwidth
  2. centralised storage
  3. share the comments and your other photos with your wiki visitors

And to the right here is a demo of Flickr in MediaWiki.

[edit] Flickr API Key

To use the Flickr API, you need a key. Your type of key will depend on whether you are using the key for commercial or non-commercial purposes and is unique to you. Unfortunately I cannot distribute mine with the code as the API has its own terms of use and obviously I can only vouch for myself.

To get your API key, apply for a key online. I can't remember the full details but I do remember it didn't take long to get my key.

[edit] Installation

This code does not require PHPFlickr or PEAR unlike other Flickr extensions.

Add this line at the end of LocalSettings.php:

require_once('extensions/Flickr.php');

Copy the following code into extensions/Flickr.php:

<?php
# Flickr MediaWiki extension 0.2.1
# 
# Tag :
#   <flickr>photoid</flickr>
# Ex :
#   from url http://www.flickr.com/photos/king-edward/391211597/
#   <flickr>391211597</flickr>
#
# This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 License
# For more information or the latest version visit
# http://wiki.edsimpson.co.uk/index.php/Flickr_Extension 
#
 
$wgExtensionFunctions[] = 'wfFlickr';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Flickr',
        'description' => 'Display Flickr image and link to photo page on Flickr as per their terms',
        'author' => 'Edward Simpson',
        'url' => 'http://wiki.edsimpson.co.uk/index.php/Flickr_Extension'
);
 
function wfFlickr() {
        global $wgParser;
        $wgParser->setHook('flickr', 'renderFlickr');
}
 
# The callback function for converting the input text to HTML output
function renderFlickr($input) {
	$FlickrAPIKey = "INSERT YOUR API KEY HERE";
	# Start off by splitting $input
	$inp = explode("|", $input, 5);
 
	# Now check we have SOMEthing
	if (sizeof($inp) == 0) {
		                $output = "<strong class='error'>Flickr Error ( No ID ): Enter at least a PhotoID</strong>";
                return $output;
	}
 
	if (! is_numeric($inp[0])) {
		$output = "<strong class='error'>Flickr Error ( Not a valid ID ): PhotoID not numeric</strong>";
		return $output;
	}
 
	# Okay now deal with parameters
	$id = $inp[0]; 
	$inp = array_slice($inp, 1);
	if (sizeof($inp) > 0) {
	foreach($inp as $test) {
		if ($type == "" && in_array(strtolower($test), array("thumnail", "thumb", "frame"))) {
			$type = strtolower($test);
		} elseif ($location == "" && in_array(strtolower($test), array("right", "left", "center", "none"))) {
			$location = strtolower($test);
		} elseif ($size == "" && in_array(strtolower($test), array("m", "s", "t", "b", "-"))) {
			$size = strtolower($test);
		} elseif  ($caption == "") {
			$caption = $test;
		} else {
			$caption .= "|".$test;
		}
 
	}
	}
	if ($type == "thumbnail") {$type = "thumb";}
	if ($type == "") {$type = "none";}
	if ($location == "") {$location = "right";}
	if ($size == "-") {$size = "";}
	if ($size != "") {$size = "_".$size;}
 
	#First get image sizes
 
        $params = array(
                'api_key'       => $FlickrAPIKey,
                'method'        => 'flickr.photos.getSizes',
                'photo_id'      => $id,
                'format'        => 'php_serial',
        );
        $encoded_params = array();
        foreach ($params as $k => $v){
                $encoded_params[] = urlencode($k).'='.urlencode($v);
        }
        # Call API
        $url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
 
        #  use cURL if we can't use file_get_contents
        if(ini_get('allow_url_fopen')) {
                $rsp = file_get_contents($url);
        }
        else
        {
                $ch = curl_init();
                $timeout = 5; // set to zero for no timeout
                curl_setopt ($ch, CURLOPT_URL, $url);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                $rsp = curl_exec($ch);
                curl_close($ch);
        }
 
        $rsp_obj = unserialize($rsp);
        # display the formated HTML and photo link or return an error
        if ($rsp_obj['stat'] != 'ok'){
	        $photoid = $params['photo_id'];
                $error_msg = $rsp_obj['message'];
                $output = "<strong class='error'>Flickr Error ( $error_msg ): PhotoID $photoid</strong>";
		return $output;
	}
	foreach ($rsp_obj['sizes']['size'] as $size_obj) {
		if ($size == "_m" && $size_obj['label'] == "Small") { $width = $size_obj['width']; $height = $size_obj['height']; }
                if ($size == "_s" && $size_obj['label'] == "Square") { $width = $size_obj['width']; $height = $size_obj['height']; }
                if ($size == "_t" && $size_obj['label'] == "Thumbnail") { $width = $size_obj['width']; $height = $size_obj['height']; }
                if ($size == "_b" && $size_obj['label'] == "Large") { $width = $size_obj['width']; $height = $size_obj['height']; }
                if ($size == "" && $size_obj['label'] == "Medium") { $width = $size_obj['width']; $height = $size_obj['height']; }
	}
 
	#Now get full info
 
        $params = array(
        	'api_key'       => $FlickrAPIKey,
	        'method'        => 'flickr.photos.getInfo',
	        'photo_id'      => $id,
	        'format'        => 'php_serial',
	);
	$encoded_params = array();
	foreach ($params as $k => $v){
	        $encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	# Call API
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
        #  use cURL if we can't use file_get_contents
        if(ini_get('allow_url_fopen')) {
                $rsp = file_get_contents($url);
        }
        else
        {
                $ch = curl_init();
                $timeout = 5; // set to zero for no timeout
                curl_setopt ($ch, CURLOPT_URL, $url);
                curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                $rsp = curl_exec($ch);
                curl_close($ch);
        }
	$rsp_obj = unserialize($rsp);
	# display the formated HTML and photo link or return an error
	if ($rsp_obj['stat'] == 'ok'){
        	$title = $rsp_obj['photo']['title']['_content'];
	        $page = $rsp_obj['photo']['urls']['url']['0']['_content'];
	        $url = "http://farm" . $rsp_obj['photo']['farm'] . ".static.flickr.com/" . $rsp_obj['photo']['server'] . "/" . $rsp_obj['photo']['id'] . "_" . $rsp_obj['photo']['secret'] . $size . ".jpg";
	if ($caption =="") {$caption = $title;}
        if ($type == "thumb" || $type == "frame") {
		$location = "t".$location;
		if ($type == "thumb") {$magnify = '<div class="magnify" style="float:right"><a href="'.$page.'" class="internal" title="Enlarge"><img src="/skins/common/images/magnify-clip.png" width="15" height="11" alt="" /></a></div>';}
		$divwidth = $width + 2 . "px";
	        $output = <<<END
<div class="thumb $location"><div class="thumbinner" style="width:$divwidth;"><a href="$page" class="internal" title="$title"><img src="$url" alt="$title" width="$width" height="$height" longdesc="$page" class="thumbimage" /></a>  <div class="thumbcaption">$magnify$caption</div></div></div>
END;
	} else {
		if ($location == "left" || $location == "right"){
			$class = "float".$location;
		} else {$class = "floatnone";}
	        $output = <<<END
<div class="$class"><span><a href="$page" class="image" title=""><img alt="" longdesc="$page" src="$url"></a></span></div>
END;
		if ($location == "center") {$output = "<div class=\"center\">".$output."</div>";}
	}
	}else{
        	$photoid = $params['photo_id'];
	        $error_msg = $rsp_obj['message'];
        	$output = "<strong class='error'>Flickr Error ( $error_msg ): PhotoID $photoid</strong>";
	}
        return $output;
}

That's it. Please contact me if you have any questions or would like assistance in using this.

[edit] Download

Alternatively you can download the file from the link here.

[edit] Basic Usage

Usage is basically as follows : <flickr>{photoid}|{type}|{location}|{size}|{caption}</flickr>

Where:

{photoid} is the Flickr photo ID. This is the only compulsory parameter. All others are optional and will be detected automatically. Anything which isn't detected as on another parameter will be classified as part of the caption.

{type} is the image type as per this page (so 'thumb' / 'thumbnail' or 'frame' or nothing). Defaults to nothing.

{location} is the location as per this page (so 'right', 'left', 'center' or 'none'). Defaults to 'right'.

{size} is the size as per this page. Defaults to medium.

and {caption} is an image title. If left blank or not set then defaults to title of image from Flickr.

[edit] Examples and More Information

See Extended Flickr Extension Syntax for further examples and more information.

[edit] Changes to this code

To anyone who changes this code - the main page for this extension does not reside on MediaWiki.org - it is at http://wiki.edsimpson.co.uk/index.php/Flickr_Extension. Therefore please make all changes there.

Personal tools