KLIcon Extension

Jump to: navigation, search

KLicon is a MediaWiki extension used to insert an image into any article. It differs from the normal method by allowing you to link the image to any URL, or leave it unlinked. It is based on the Icon extension by Tim Laqua.

Usage

Use the tag {{#icon:parameters}} with the required parameter image and optional parameters link, width, height, alt, and css.

See Help:KLIcon for examples.

Code

<?php
/**
 * KLicon.php
 * This extension inserts an image that may be linked to any URL
 * written by Kennylucius http://kennylucius.com/
 * based on Tim Laqua's Icon 1.4, especially Language
 * LocalSettings.php file:
 * require_once("$IP/extensions/KLlinkparam.php");
 */

# Not a valid entry point, skip unless MEDIAWIKI is defined
if (!defined('MEDIAWIKI')) {
	echo "Icon extension";
	exit(1);
}

$wgExtensionCredits['other'][] = array(
	'name'  => 'Icon',
	'version'  => '0.4',
	'author' => 'Kenny Lucius',
	'url' => 'http://www.kennylucius.com/KLicon',
	'description' => 'Allows you to insert Images that are linked to any URL using {{#icon:}}.',
);

$wgExtensionFunctions[] = 'KLicon_Setup';
$wgHooks['LanguageGetMagic'][] = 'KLicon_Magic';

function KLicon_Setup() {
        global $wgParser, $wgMessageCache;
		
		//Add Messages from Tim Laqua's Icon 1.4
		require( dirname( __FILE__ ) . '/Icon.i18n.php' );
		foreach( $messages as $key => $value ) {
			  $wgMessageCache->addMessages( $messages[$key], $key );
		}

        $wgParser->setFunctionHook( 'icon', 'KLicon_Render' );
		return true;
}

function KLicon_Magic( &$magicWords, $langCode ) {
        $magicWords['icon'] = array( 0, 'icon' );
        return true;
}

function KLicon_Render() {
	$args = func_get_args();
	$parser = array_shift($args);
	$arg_names = array('image', 'alt', 'width', 'link', 'css', 'height');
	$thumb_dim = array();
	
	//look for named arguments, but process unnamed according to the
	//order in original Icon 1.4
	foreach($args as $idx=>$val) {
		$val = trim($val);
		$p = explode('=', $val, 2);
		$n = trim($p[0]);
		if($p[1] && ($v = trim($p[1])) && in_array(trim($p[0]), $arg_names)) {
			$arg[$n] = $v;
		}
		elseif(is_numeric($val)) {
			//if unnamed arg is numeric, must be width
			$arg['width'] = $val;
		}
		//unnamed args go in order of $arg_names
		elseif(! isset($arg[$arg_names[$idx]])) {
			$arg[$arg_names[$idx]] = $val;
		}
	}
	//return this if we give up
	$ret = '[[Image:'.$args['image'].']]';
	
	//Examine and modify the values of the named variables
	//get image URL
	if(! $arg['image']) return $ret;
	else {
		// if title fails, give up
		$imgTitle = Title::newFromText( $arg['image'], NS_IMAGE );
		if (!is_object($imgTitle)) return $ret;
		
		// InterWiki link?
		if ( $imgTitle->isLocal() ) {
			$image = Image::newFromName( $arg['image'] );
			if (!$image->exists())
				return $ret;
			$imgURL = $image->getURL();
		} else {
			$imgURL = $imgTitle->getFullURL();
		}
	}
	//set HTML anchor for link
	if($arg['link']) {
		if ( preg_match( '/^(?:' . wfUrlProtocols() . ')/', $arg['link'] ) ) {
			$tURL= Skin::makeInternalOrExternalUrl($arg['link']);
			$aClass ='class="plainlinks iconlink"';
			
			$link = "<a {$aClass} href=\"{$tURL}\">";
		} else {
			$linkTitle = Title::newFromText($arg['link']);
			// this might happen in templates...
			if (is_object( $linkTitle )) {
				if ( $linkTitle->isLocal() ) {
					$tURL = $linkTitle->getLocalUrl();
					$aClass='iconlink';
				} else {
					$tURL = $linkTitle->getFullURL();
					$aClass = 'class="extiw iconlink"';
				}
				
				$link = "<a {$aClass} href=\"${tURL}\">";
			}
		}
	}
	else $link = '';
	//sanitize alt and css
	if($arg['alt']) $alt = htmlspecialchars($arg['alt']);
	else $alt = '';
	if($arg['css']) $css = htmlspecialchars($arg['css']);
	else $css = '';

	//Create an IMG tag 
	if($arg['width'])  $thumb_dim['width']  = intval($arg['width']);
	if($arg['height']) $thumb_dim['height'] = intval($arg['height']);
	//if width or height is given, use thumbnail
	if (count($thumb_dim) > 0) {
		$thumb = $image->transform($thumb_dim);
		if ( $thumb->isError() ) {
			$imageString = wfMsgForContent('icon-badimage');
		} else {
			$imageString = $thumb->toHtml( array( 'title' => $alt, 'alt' => $alt ) );
		}
		if ($css) {
			$imageString = str_replace('/>', " style=\"{$css}\" />", $imageString);
		}
	
	}
	//no thumbnail, so just insert image
	else {
		$imageString = "<img class='iconimg' style=\"vertical-align: middle;{$css}\" src='{$imgURL}' alt=\"{$alt}\" title=\"{$alt}\" />";
	}
	//output the result
	if ($link) 	$output = $link . $imageString . '</a>';
	else 		$output = $imageString;
	return $parser->insertStripItem($output, $parser->mStripState);
}
 
Personal tools