Extension:LinkedImage

From MediaWiki.org

Jump to: navigation, search
Crystal Clear app error.png

This extension is obsolete!
It has been replaced by core functionality in the MediaWiki software (which was added in version 1.14 (r41727 & r41789)).

         

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
LinkedImage

Release status: stable

Implementation  Tag
Description
Author(s)  Alexander Kraus
Alan Trick
Last Version  0.3
MediaWiki  1.5+
License No license specified
Download see below
changelog

check usage (experimental)

LinkedImage is a MediaWiki extension that requires at least version 1.5 of MediaWiki.

Usually any displayed image in MediaWiki is linked to its own article. This extension was created to provide a possibility to display an image that is linked to an article other than its own one.

The image to be used must reside on the local wiki, not a 'commons'-style repository.

Contents

[edit] Parameters

Parameter Type Description Example
wikipage mandatory specifies the link target
wikipage=Main_page
tooltip optional Text to display, while the user focuses the image with the cursor
tooltip=Sampletext
img_src mandatory this param sets the image to display
img_src=Image:Sample.gif
img_width optional You can alter the default image width.

By default the real imagesize would be used. If you set this param, this width is set. Values like '10%' are valid too.

img_width=10px
img_height optional You can alter the default image height.

By default the real imagesize would be used. If you set this param, this height is set. Values like '10%' are valid too.

img_height=10px
img_alt optional alternative image text if the user disabled images in the browser
img_alt=Text

[edit] Usage - Example



<linkedimage>
wikipage=Main_Page
tooltip=Main Page
img_src=Image:Sample.gif
img_width=10%
img_height=10px
img_alt=Sampletext
</linkedimage>


[edit] Installation

  • copy LinkedImages.php to the extensions folder
  • add require_once($IP . '/extensions/LinkedImages.php'); to the end of LocalSettings.php

[edit] LinkedImages.php code

Version: 0.3

<?php
/**
 * This file contains the main include file for the LinkedImage extension of 
 * MediaWiki. 
 *
 * Usage: require_once("path/to/LinkedImage.php"); in LocalSettings.php
 *
 * This extension requires MediaWiki 1.5 or higher.
 *
 * @file
 * @ingroup Extensions
 * @author Alexander Kraus <kraus.alexander@gmail.com>
 * @author Alan Trick <alan.trick@twu.ca>
 * @copyright Public domain
 * @license Public domain
 * @version 0.3
 */
 
/**
 * Register the LinkedImage extension with MediaWiki
 */
$wgExtensionFunctions[] = 'wfLinkedImage';
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'LinkedImage',
	'version' => '0.3',
	'author' => array( 'Alexander Kraus', 'Alan Trick' ),
	'description' => 'Allows client-side clickable images with custom link targets etc. using <tt>&lt;linkedimage&gt;</tt> tag',
	'url' => 'http://www.mediawiki.org/wiki/Extension:LinkedImage',
);
 
/**
 * Sets the tag that this extension looks for and the function by which it
 * operates
 */
function wfLinkedImage() {
	global $wgParser, $wgMessageCache;
 
	$wgMessageCache->addMessages( array(
		'linkedimage_nowikipage' => 'LinkedImage: No link target specified! e.g. \'wikipage=Main_page\'',
		'linkedimage_noimg' => 'LinkedImage: No image specified! e.g. \'img_src=Image:LinkedImage.png\''
	) );
	$wgParser->setHook('linkedimage', 'renderLinkedImage');
}
 
function renderLinkedImage( $input ) {
	$img = new LinkedImage();
 
	$img->getBoxOption($img->wikipage, $input, 'wikipage');
	$img->getBoxOption($img->tooltip, $input, 'tooltip');
	$img->getBoxOption($img->img_src, $input, 'img_src');
	$img->getBoxOption($img->img_height, $input, 'img_height');
	$img->getBoxOption($img->img_width, $input, 'img_width');
	$img->getBoxOption($img->img_alt, $input, 'img_alt');
	$img->getBoxOption($img->img_border, $input, 'img_border');
 
	// render and return linked image ...
	return $img->render();
}
 
class LinkedImage {
	public $wikipage = '';
	public $tooltip = '';
	public $img_src = '';
	public $img_alt = '';
	public $img_height = '';
	public $img_width = ''; 
	public $img_border = '';
 
	private function getTooltipHTML() {
		if( $this->tooltip != '' ) {
			return " title='{$this->tooltip}'";
		} else return '';
	}
 
	public function getURL() { 
		global $wgArticlePath, $wgParser;
		$page = str_replace( '{{PAGENAMEE}}', $wgParser->mTitle->getSubpageUrlForm(), $this->wikipage );
		return str_replace( "$1", $page, $wgArticlePath);
	}
 
	public function getImg() {
		// alt and src are required, alt may be empty though
		$r = "<img src='{$this->image->getUrl()}' alt='{$this->img_alt}'";
		if( $this->img_width != '' ) $r .= " width='{$this->img_width}'";
		if( $this->img_height != '' ) $r .= " height='{$this->img_height}'";
		if( $this->img_border != '' ) $r .= " border='{$this->img_border}'";
		return $r . ' />';
	}
 
	public function render() {
		// sanity checking
		if( $this->wikipage == '' ) {
			return htmlspecialchars( wfMsg( 'linkedimage_nowikipage' ) );
		}
		if( $this->img_src == '' ) {
			return htmlspecialchars( wfMsg( 'linkedimage_noimg' ) );
		}
		// create MediaWiki image object ...
		$this->image = new Image( Title::newFromText( $this->img_src ) );
		// return link
		return "<a href='{$this->getURL()}'{$this->getTooltipHTML()}>{$this->getImg()}</a>";
	}
 
	public function getBoxOption( &$value, &$input, $name ) {
		if( preg_match( "/^\s*$name\s*=\s*(.*)/mi", $input, $matches ) ) {
			$value = htmlspecialchars($matches[1], ENT_QUOTES);
		}
	}
}

[edit] Suggested Changing

I suggest to modify this for more usage, please help improve it.

  • Enable external-linked images
  • Enable non-linked images
  • Cancel the default height/width, so that the image will keep the ratio unless both height and width are set (or make a function to calculate it)

Parameters:

exlink - make an external link, such as exlink=http://www.mediawiki.org/
wikipage - optional now, the image will have no link if both wikipage and exlink is unset

Unfortunately, I worked it under v2.0, please help moving it to the current version. I have marked all my changes with /////.

I publish it into Public Domain. -- zayoo

<?php
 
/**
 * This file contains the main include file for the LinkedImage extension of 
 * MediaWiki. 
 *
 * Usage: require_once("path/to/LinkedImage.php"); in LocalSettings.php
 *
 * This extension requires MediaWiki 1.5 or higher.
 *
 * @author Alexander Kraus <kraus.alexander@gmail.com>
 * @copyright Public domain
 * @license Public domain
 * @package MediaWikiExtensions
 * @version 0.2
 * @hacked by zayoo 2009 for exlink, nonlink and keeping ratio
 */
 
/**
 * Register the LinkedImage extension with MediaWiki
 */ 
$wgExtensionFunctions[] = 'wfLinkedImage';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'LinkedImage',
'author' => 'Alexander Kraus',
'url' => 'http://meta.wikimedia.org/wiki/LinkedImage',
);
 
//renderLinkedImage();
 
/**
 * Sets the tag that this extension looks for and the function by which it
 * operates
 */
function wfLinkedImage()
{
    global $wgParser, $wgMessageCache;
 
    $wgMessageCache->addMessages( array(
		'linkedimage_nowikipage'=> 'LinkedImage: No link target specified! e.g. \'wikipage=Main_page\'',
		'linkedimage_noimg' 	=> 'LinkedImage: No image specified! e.g. \'img_src=Image:LinkedImage.png\''
					)
	);
 
    $wgParser->setHook('linkedimage', 'renderLinkedImage');
}
 
function renderLinkedImage($input)
{
    $linkedimage=new LinkedImage();
 
	$linkedimage->getBoxOption($linkedimage->wikipage,   $input,'wikipage');
    $linkedimage->getBoxOption($linkedimage->tooltip,    $input,'tooltip');
    $linkedimage->getBoxOption($linkedimage->exlink,    $input,'exlink'); /////
    $linkedimage->getBoxOption($linkedimage->img_src,    $input,'img_src');
    $linkedimage->getBoxOption($linkedimage->img_height, $input,'img_height');
    $linkedimage->getBoxOption($linkedimage->img_width,  $input,'img_width');
    $linkedimage->getBoxOption($linkedimage->img_alt,    $input,'img_alt');
    $linkedimage->getBoxOption($linkedimage->img_border, $input,'img_border');
 
    // render and return linked image ...
    return $linkedimage->render();
}
 
class LinkedImage {
   var $wikipage;
   var $tooltip;
   var $exlink; /////
   var $img_src;
   var $img_alt;
   var $img_height;
   var $img_width; 
   var $img_border;
 
   public function LinkedImage() {
    $this->setWikipage('');
    $this->setTooltip('');
    $this->setExlink(''); /////
    $this->setImg_src('');
    $this->setImg_alt('');
    $this->setImg_height('');
    $this->setImg_width('');
    $this->setImg_border('');
   }
 
   private function setWikipage($value){ 	$this->wikipage=$value; }
   private function getWikipage(){       	return $this->wikipage; }
 
   private function setTooltip($value){  	$this->tooltip=$value; }
   private function getTooltip(){		 	return $this->tooltip; }
   private function getTooltipHTML(){
   		if ($this->tooltip != '') {
			return 'title="'.$this->getTooltip().'" ';
   		} else {
   			return '';
   		}
   }
 
   private function setExlink($value){  	$this->exlink=$value; } /////
   private function getExlink(){		 	return $this->exlink; }
   private function getExlinkHTML(){
   		if ($this->exlink != '') {
			return $this->getExlink();
   		} else {
   			return '';
   		}
   } /////
 
   private function setImg_src($value){  	$this->img_src=$value; }
   private function getImg_src(){		 	return $this->img_src; }
   private function getImg_srcHTML($getImageUrl=false){
   		if ($this->img_src != '') {
   			if ($getImageUrl) {
				return 'src="'.$this->image->getUrl().'" ';
   			} else {
   				return 'src="'.$this->img_src.'" ';
   			}
   		} else {
   			return '';
   		}
   }
 
 
   private function setImg_alt($value){  	$this->img_alt=$value; }
   private function getImg_alt(){		 	return $this->img_alt; }
   private function getImg_altHTML(){
   		if ($this->img_alt != '') {
   			return 'alt="'.$this->img_alt.'" ';
   		} else {
   			return '';
   		}
   }
 
   private function setImg_height($value){ 	$this->img_heigth=$value;}
   private function getImg_height(){		return $this->img_height;}
   private function getImg_heightHTML(){
   		if ($this->img_height != '') {
   			return 'height="'.$this->img_height.'" ';
   		} else {
   			//return 'height="'.$this->image->getHeight().'" '; /////
   		}
   }
 
   private function setImg_width($value){	$this->img_width=$value; }
   private function getImg_width(){			return $this->img_width; }
   private function getImg_widthHTML(){
   		if ($this->img_width != '') {
   			return 'width="'.$this->img_width.'" ';
   		} else {
   			//return 'width="'.$this->image->getWidth().'" '; /////
   		}
   }
 
   private function setImg_border($value){	$this->img_border=$value; }
   private function getImg_border(){		return $this->img_border; }
   private function getImg_borderHTML(){
   		if ($this->img_border != '') {
   			return 'border="'.$this->img_border.'" ';
   		} else {
   			return $this->img_border;
   		}
   }
 
   public function render() {
		global $wgArticlePath;
 
		// check param wikipage existence ///// delete for exlink
		//if ($this->getWikipage() == '') {
		//	return htmlspecialchars( wfMsg( 'linkedimage_nowikipage' ) );
		//}
 
		// check param img_src existence
		if ($this->getImg_src() == '') {
			return htmlspecialchars( wfMsg( 'linkedimage_noimg' ) );
		}
 
        // create mediawiki image object ...
        $this->image = new Image( Title::newFromText( $this->img_src ) );
 
		// return link ...
		if($this->wikipage==''&&$this->getExlinkHTML()=='') /////
			return '<img '. $this->getImg_srcHTML(true) . $this->getImg_altHTML() . $this->getImg_widthHTML() . $this->getImg_heightHTML() . $this->getImg_borderHTML() . '>';
		elseif($this->getExlinkHTML()!='')
			return '<a target="_blank" href="'.$this->getExlinkHTML().'" '.$this->getTooltipHTML().'><img '. $this->getImg_srcHTML(true) . $this->getImg_altHTML() . $this->getImg_widthHTML() . $this->getImg_heightHTML() . $this->getImg_borderHTML() . '></a>';
		else
			return '<a href="'.str_replace( "$1", $this->wikipage, $wgArticlePath ).'" '.$this->getTooltipHTML().'><img '. $this->getImg_srcHTML(true) . $this->getImg_altHTML() . $this->getImg_widthHTML() . $this->getImg_heightHTML() . $this->getImg_borderHTML() . '></a>';
   	/////
   } // End render()
 
   public function getBoxOption(&$value,&$input,$name,$isNumber=false) {
      if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {
		if($isNumber) {
			$value=intval($matches[1]);
		} else {
			$value=htmlspecialchars($matches[1]);
		}
	  }
   } // End getBoxOption()
 
}

[edit] Changelog

Version 0.3
fixed XSS vulnerbility, cleaned up code, added support for {{PAGENAMEE}} in URL

[edit] See also