Open main menu

Extension:IconLink

MediaWiki extensions manual
Crystal Clear action run.svg
IconLink
Release status: stable
Implementation Parser extension, Parser function
Description Prepends an icon to a link.
Author(s) Robert Hänel (roberthaeneltalk)
Latest version 1.0 (2009-05-01)
MediaWiki only tested with 1.11
License No license specified
Download No link
Hooks used
LanguageGetMagic
Translate the IconLink extension if it is available at translatewiki.net
Check usage and version matrix.

The IconLink extension places an icon in front of an external link. By default the favicon image of the linked site is used but it's also possible to specify the URL of an image to show instead. The width and height of the icon can also be specified. Therefore this extension can be seen as an enhanced version of Extension:FaviconLink, but implemented as a parser function so that it can be used in templates too.

Contents

UsageEdit

{{#iconlink: url | description | icon | width | height}}

where

  • url is the URL you want to create a link to
  • description is the text to show instead of the URL (optional)
  • icon is the URL of the image to show before the link (optional); if omitted, the favicon of the site you're linking is shown
  • width and height define the dimensions of the icon (both optional); if omitted, the icon will have a size of 16x16 pixels

RequirementsEdit

The extension needs at least PHP 5.x ( function get_headers ).

Download instructionsEdit

Please cut and paste the code found below and place it in $IP/extensions/IconLink/IconLink.php.

$IP stands for the Installation Path (or "directory") of your MediaWiki installation, the same directory that holds LocalSettings.php, index.php etc..

InstallationEdit

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/IconLink/IconLink.php");

CodeEdit

IconLink.phpEdit

<?php
#
# IconLink extension for MediaWiki
#
# Installation:
#	create a new directory extensions/IconLink and copy all files there
#	add the following to LocalSettings.php:
#	require_once( "extensions/IconLink/IconLink.php" );
#
# Usage:
#	{{#iconlink: url | description | icon | width | height}}
#	where
#	- url is the URL you want to create a link to
#	- description is the text to show instead of the URL (optional)
#	- icon is the URL of the image to show before the link (optional)
#	  if omitted, the favicon of the site you're linking is shown
#	- width and height define the dimensions of the icon (both optional)
#	  if omitted, the icon will have a size of 16x16 pixels
#

if(! defined('MEDIAWIKI')) {
	die("This is a MediaWiki extension and can not be used standalone.\n");
}
	
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'IconLink',
	'description' => 'Prepends an icon to a link.',
	'version' => '1.0',
	'author' => 'Robert H&auml;nel'
);

require_once('IconLink.code.php');
	
$wgExtensionFunctions[] = "wfIconLinkSetup";
$wgHooks['LanguageGetMagic'][] = 'wfIconLinkMagic';

function wfIconLinkSetup() {
	global $wgParser;
	$wgParser->setFunctionHook('iconlink', 'wfIconLinkRender');
}

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

IconLink.code.phpEdit

<?php

function wfIconLinkRender(&$parser, $url = '', $description = '', $icon = '', $width = 16, $height = 16) {
	if (empty($url)) {
		return '';
	}
	$url = htmlspecialchars($url);
	if (empty($description)) {
		$description = $url;
	} 
	$description = htmlspecialchars($description);
	if (empty($icon)) {
		# extract the first part of the URL (up until the host name) and append
		# the name of the favicon file
		$regex = '|^(http(s)?://[^/]+)|i'; 
		preg_match($regex, $url, $result);
		$icon = $result[0] . '/favicon.ico';
	}
	$icon = htmlspecialchars($icon);
	if (! ctype_digit($width)) {
		$width = 16;
	}
	if (! ctype_digit($height)) {
		$height = 16;
	}

	# check the http return code after trying to access the icon file
	# if it is 200 the file exists
	$headers = @get_headers($icon);
	$iconExists = (preg_match("|200|", $headers[0]) != 0);
	global $wgIconLinkDefaultIcon;
	if (! $iconExists) { $icon = $wgIconLinkDefaultIcon; }
	
	$html = "<img class='iconlink' src='$icon' width='$width' height='$height' />";
	$html .= " <a href='$url'>$description</a>";

	return $parser->insertStripItem($html, $parser->mStripState);
}