Extension:Ipernity

MediaWiki extensions manual
Ipernity
Release status: unmaintained
Implementation Tag , Hook
Description Allows to embed Ipernity images and links to the photo page on Ipernity
Author(s) Luigi Rosa (Hypertrektalk)
Latest version 0.0.2 (2014-01-12)
MediaWiki 1.22
PHP 5.2+
Database changes No
License Creative Commons Attribution Share Alike 3.0
Download See the code section
Example See the usage section
‎<ipernity>

The Ipernity extension embeds images from Ipernity.

UsageEdit

<ipernity>{id}|{size}|{alignment}</ipernity>

Parameters:

  1. {id} the Ipernity photo ID. It can be read on the URL of the photo page: http://www.ipernity.com/doc/username/123456789
  2. {size} the size of the image. It can be either original or one of the sizes of the photo. If none is specified, 500 is assumed. If the size does not exist, an error message is displayed with the list of available sizes.
  3. {alignment} the alignment of the image. It can be right, left or center. If none is specified, left is assumed.

InstallationEdit

  • Copy the code into files and place the file(s) in a directory called Ipernity in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php :
    require_once "$IP/extensions/Ipernity/Ipernity.php";
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

CodeEdit

Ipernity.php
<?php

/*
 * Ipernity MediaWiki extension 0.0.2
 * Author: Luigi Rosa <lrosa@venus.it>
 *
 * Tag:
 *   <ipernity>photo_id|size|alignment</ipernity>
 *
 * Parameters:
 *   size: if specified, must be one of the supported labels of Ipernity or 'original' for original image
 *         default: 500
 *         examples: 75x, 250x, 100, 240, 500, 560, 640, 800, 1024, 1600, 2048
 *         if the size doies not exists, a list of available sizes is printed.
 *
 *   alignment: one of right, center, left (default)
 *
 * Example:
 *   To include the photo from url http://www.ipernity.com/doc/lrosa/29458779
 *   <ipernity>29458779</ipernity> 
 *
 * This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 License
 *
 * If you use this extension you must comply to Ipernity Terms and Conditions
 *   http://www.ipernity.com/about/api.tou
 *
 * Plugin based on Flickr Extension by Edward Simpson
 * http://wiki.edsimpson.co.uk/index.php/Flickr_Extension 
 *
 * 0.0.2 20140112 added i18n translation
 *
 */

// Check if we are being called directly
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

// http://www.mediawiki.org/wiki/Manual:$wgExtensionFunctions
$wgExtensionFunctions[] = 'wfIpernity';

// http://www.mediawiki.org/wiki/Manual:$wgExtensionCredits
$wgExtensionCredits['parserhook'][] = array(
	'path' => __FILE__,
	'name' => 'Ipernity',
	//'description' => 'Embeds Ipernity image and links to photo page on Ipernity via &lt;ipernity&gt; tag',
	'descriptionmsg' => 'ipernity-desc',
	'version' => '0.0.2', 
	'author' => 'Luigi Rosa',
	'url' => 'https://www.mediawiki.org/wiki/Extension:Ipernity',
	'license-name' => 'Creative Commons Attribution-ShareAlike 3.0'
);

// http://www.mediawiki.org/wiki/Manual:$wgExtensionMessagesFiles
$wgExtensionMessagesFiles['Ipernity'] = dirname(__FILE__) . '/Ipernity.i18n.php';
 
function wfIpernity() {
	global $wgParser;
	$wgParser->setHook('ipernity', 'renderIpernity');
}
 
// Where real fun is
function renderIpernity($text, $params, $parser, $frame) {
	// DO NOT change this key
	$IpernityAPIkey = "889870c6000028832d01abf298e82a58";
	
	// some cleaning
	$text = trim($text);
	
	// empty text? exit
	if ('' == $text) {
		return "<strong class='error'>" . wfMessage( 'ipernity-enoid' )->parse() . "</strong>";
	}
	
	// explode parameters
	$aText = explode("|", $text, 3);
	
	$photoId = $aText[0];
 
	if (!is_numeric($aText[0])) {
		return "<strong class='error'>" . wfMessage( 'ipernity-eidnonum' )->parse() . "</strong>";
	}
	
	// check size parameter and set default
	if (isset($aText[1])) {
		$sizeTag = strtolower($aText[1]); 
	} else {
		$sizeTag = '500';
	}

	// check alignment parameter and set default
	if (isset($aText[2])) {
		$alignmentTag = strtolower($aText[2]); 
	} else {
		$alignmentTag = 'left';
	}
	if (!in_array($alignmentTag, array('left', 'right', 'center'))) $alignmentTag = 'left';

	// call Ipernity API to get image info
	$url = "http://api.ipernity.com/api/doc.get/json?api_key=${IpernityAPIkey}&doc_id=$photoId";
	# use cURL if we can't use file_get_contents
	if (ini_get('allow_url_fopen')) {
		$jresp = file_get_contents($url);
	} else {
		$ch = curl_init();
		$timeout = 5; 
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$jresp = curl_exec($ch);
		curl_close($ch);
	}
	$aImgInfo = json_decode($jresp, TRUE);

	// error?
	if ($aImgInfo['api']['status'] != 'ok') {
		return "<strong class='error'>" . wfMessage( 'ipernity-eapi' )->parse() . $aImgInfo['api']['message'] . " (" . $aImgInfo['api']['code'] . "</strong>";
	}
	
	$displayOK = FALSE;

	// original size?
	if ('original' == $sizeTag) {
		$displayOK = TRUE;
		$imgURL = $aImgInfo['doc']['original']['url'];
		$imgH = $aImgInfo['doc']['original']['h'];
		$imgW = $aImgInfo['doc']['original']['w'];
	} else { //scan all available sizes
		$aSizes = array();
		foreach ($aImgInfo['doc']['thumbs']['thumb'] as $key=>$aThumb) {
			$aSizes[] = $aThumb['label'];
			if ($aThumb['label'] == $sizeTag) {
				$imgURL = $aThumb['url'];
				$imgH = $aThumb['h'];
				$imgW = $aThumb['w'];
				$displayOK = TRUE;
			}
		}
	}
	
	// if the size does not exist, print an error
	if (!$displayOK) {
		return "<strong class='error'>" . wfMessage( 'ipernity-enosize' )->parse() . implode(', ', $aSizes) . ")</strong>";
	}
	
	$imgLink = $aImgInfo['doc']['link'];
	$imgDesc = str_replace('"', '&quot;', $aImgInfo['doc']['title'] . wfMessage( 'ipernity-by' )->parse() . $aImgInfo['doc']['owner']);
	
	// let's display the image 
	if ('left' == $alignmentTag or 'right' == $alignmentTag) {
		$class = "float$alignmentTag";
	} else {
		$class = "floatnone";
	}
		
	$retval = "<div class=\"$class\"><span><a href=\"$imgLink\" class=\"image\" title=\"\"><img alt=\"$imgDesc\" src=\"$imgURL\"></a></span></div>";
	
	if ('center' == $alignmentTag) $retval = "<div class=\"center\">$retval</div>";
 	
	return $retval;
	
}
Ipernity.i18n.php
<?php

/*
 * Ipernity MediaWiki extension 0.0.2
 * Author: Luigi Rosa <lrosa@venus.it>
 *
 * This code is licensed under the Creative Commons Attribution-ShareAlike 3.0 License
 *
 * If you use this extension you must comply to Ipernity Terms and Conditions
 *   http://www.ipernity.com/about/api.tou
 *
 *
 * 0.0.2 20140112 added i18n translation
 *
 */

$messages = array();
 
/** 
 * English
 * @author Luigi Rosa - 20140112
 */
$messages['en'] = array(
   'ipernity-desc' => 'Embeds Ipernity image and links to photo page on Ipernity via &lt;ipernity&gt; tag',
   'ipernity-enoid' => 'Ipernity Error: no ID provided',
   'ipernity-eidnonum' => 'Ipernity Error: first parameter (photo ID) must be numeric',
   'ipernity-eapi' => 'Ipernity API returned an error: ',
   'ipernity-enosize' => 'Ipernity: size does not exist. Available sizes: ',
   'ipernity-by' => ' by ',
);

/** 
 * Message documentation (Message documentation)
 * @author Luigi Rosa - 20140112
 */
$messages['qqq'] = array(
   'ipernity-desc' => 'Description of the [[mw:Extension:Ipernity|Ipernity extension]]. It is shown in [[:Special:Version]].'
);

/** 
 * German (Deutsch)
 * @author Kghlbn
 */
$messages['en'] = array(
   'ipernity-desc' => 'Ermöglich das Einbetten von Fotos aus dem Repositorium „Ipernity“ und verlinkt auf deren dortige Seite',
   'ipernity-enoid' => 'Ipernity-Fehler: Es wurde keine Fotokennung angegeben',
   'ipernity-eidnonum' => 'Ipernity-Fehler: Der erste Parameter (Fotokennung) muss numerisch sein.',
   'ipernity-eapi' => 'Die API von Ipernity hat folgenden Fehler ausgegeben: ',
   'ipernity-enosize' => 'Ipernity: Die Fotogröße gibt es nicht. Verfügbare Größen: ',
   'ipernity-by' => ' von ',
);

/** 
 * Italian (italiano)
 * @author Luigi Rosa - 20140112
 */
$messages['it'] = array(
   'ipernity-desc' => 'Permette di includere le immagini pubblicate su Ipernity',
   'ipernity-enoid' => 'Errore di Ipernity: non &egrave; stato fornito nessun ID',
   'ipernity-eidnonum' => 'Errore di Ipernity: il primo parametro (ID della foto) deve essere numerico',
   'ipernity-eapi' => 'Le API di Ipernity hanno restituito un errore: ',
   'ipernity-enosize' => 'Ipernity: la dimensione indicata non esiste. Dimensioni diponibili: ',
   'ipernity-by' => ' di ',
);