Extension:Flickr

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Flickr

Release status: stable

Implementation Tag
Description Allows to embed Flickr images and links to their photo page on Flickr
Author(s) Edward Simpson
Latest version 0.2.3 (2013-11-18)
Database changes No
License Creative Commons Attribution-ShareAlike 3.0
Download See the code section
Example Original page and Extended syntax page
Parameters
  • $egFlickrAPIKey

Translate the Flickr extension if possible

Check usage and version matrix; code metrics

The Flickr extension displays images from Flickr in pages. 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

Basic Usage[edit | edit source]

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.

Flickr API Key[edit | edit source]

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.

Installation[edit | edit source]

This code does not require PHPFlickr or PEAR unlike other Flickr extensions.
  • Copy the code into respective files and extract the files in a directory called Flickr in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository.
  • Add the following code at the bottom of your LocalSettings.php:
require_once( "$IP/extensions/Flickr/Flickr.php" );
$egFlickrAPIKey = 'INSERT YOUR API KEY HERE';
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

Code[edit | edit source]

Flickr.php
<?php
# Flickr MediaWiki extension 0.2.2
#
# 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
#

if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

$wgExtensionFunctions[] = 'wfFlickr';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Flickr',
        'descriptionmsg' => 'flickr-desc',
        'version' => '0.2.3',
        'author' => 'Edward Simpson',
        'url' => 'https://www.mediawiki.org/wiki/Extension:Flickr'
);

$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['Flickr'] = $dir . 'Flickr.i18n.php';

function wfFlickr() {
        global $wgParser;
        $wgParser->setHook('flickr', 'renderFlickr');
}

# The callback function for converting the input text to HTML output
function renderFlickr($input) {
        global $egFlickrAPIKey;

        # 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);
        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'       => $egFlickrAPIKey,
                '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'       => $egFlickrAPIKey,
                '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="' . "{$GLOBALS['wgScriptPath']}/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;
}
Flickr.i18n.php
<?php
/**
 * Internationalisation file for extension Flickr.
 *
 * @file
 * @ingroup Extensions
 */

 
$messages = array();
 
/** English
 * @author Edward Simpson
 */

$messages['en'] = array(
   'flickr-desc' => 'Allows to embed Flickr images and links to their photo page on Flickr',
);
 
/** Message documentation (Message documentation)
 * @author Kghbln
 */

$messages['qqq'] = array(
   'flickr-desc' => 'Description of the [[mw:Extension:Flickr|Flickr extension]]. It is shown in [[:Special:Version]].'
);

/** German (Deutsch)
 * @author Kghbln
 */

$messages['de'] = array(
   'flickr-desc' => 'Ermöglicht das Einbinden von Dateien aus Flickr sowie eines Links zu deren dortiger Seite',
);