From Meta, a Wikimedia project coordination wiki
A proposal to move this page to was rejected.


Screenshot of MediaLinks in action

I wanted to include links to sound samples on one of my pages. I liked the idea of displaying small icons for the different media formats instead of the plain link name or other text. I could not find a way to achieve this with MediaWiki by itself, so I wrote this little extension that should even work for any link you provide. See the screen shot to fully understand what I mean :-)

How the extension works[edit]

The extension is registered for the <medialink> tag. This tag may include WikiText link code. The extension finds the URL for this link and determines the file type by the file extension. It looks for an appropriate link icon in /skins/common/images/icon and generates the link's HTML code. Link icons must consist of the base name linkicon-, followed by the respective file extension in lowercase letters. They must be PNG files (e.g. linkicon-mp3.png). At least one default icon must be present, which is called linkicon.png.

Of course, you will have to create and upload such files for your site.

How to use the extension[edit]

To enable the extension, copy MediaLinks.php to the extensions directory of your MediaWiki installation. After that, you may register the extension by adding

require_once( "extensions/MediaLinks.php" );

to your LocalSettings.php file.

From now on, you may use the <medialink> tag as follows:

<medialink title="Play sample">[[Media:Sample.mp3]]</medialink>

The second option only works with MediaWiki 1.5 or later, as it requires tag parameters to be passed to the extension function.

Customize it[edit]

The only thing you should need to customize are the link icons. I've already created three of them, which are shown here. Right click and save them as templates for your own icons.


Version 0.1

  • Initial version

To Do[edit]

  • Backport it, so it can be used in pre-1.5 versions

The Code[edit]

This is what goes into MediaLinks.php:

## MediaLinks.php
## version 0.1
## Written by Thorsten Dittmar <>
## Extension for MediaWiki that generates links to media files, displaying a mime-type icon instead of
## the link name.

## This is the "main" function
$wgExtensionFunctions[] = 'mediaLink_Install';

## This installs the extension so it looks for the <medialink> tag
function mediaLink_Install() 
    global $wgParser, $cshLanguages;
    # register the extension with the WikiText parser

## Returns the file extension for the given file name in lowercase 
## letters without ".".
## For example, the result for name "media.MP3" is "mp3"
function mediaLink_GetFileExt($name)
    $n = strrpos($name,'.');
    return strtolower($n ? substr($name,$n+1) : '');

## Returns only the name for the given file.
function mediaLink_GetFileName($name)
    $n = strrpos($name,'/');
    return ($n ? substr($name,$n+1) : '');

## Returns the path of the link icon that corresponds to the
## given file name.
## Format for link icon names:
## linkicon- + extension + .png
## Fallback icon:
## linkicon.png
function mediaLink_GetLinkIcon($name,$title) 
    global $wgStylePath;
    global $wgStyleDirectory;
    // Determine the file's extension and assemble required
    // icon file name
    $fileext = mediaLink_GetFileExt($name);
    $try = array('linkicon-'.$fileext.'.png','linkicon.png');
    // Search until one of the icon files is found
    foreach ($try as $icon) 
        $path = '/common/images/icons/'.$icon;
        $filepath = $wgStyleDirectory.$path;
        if (file_exists($filepath))
           $s0 = getImageSize($filepath);
           $s1 = $wgStylePath.$path;
           return "<img src=\"$s1\" ".$s0[3]." style=\"display: inline;\" alt=\"$title\" title=\"$title\" />";
    return $name;

## Renders the <medialink> tag.
function renderMediaLink($source, $argv)
    global $wgTitle;
    global $wgUser;
    global $wgParser;
    global $wgOut;

    // The $source parameter determines the link. An empty
    // destination is ignored.
    $destination = trim($source);
    if ($destination === "") return "";
    // A new parser will do the work for us
    $parserOptions = ParserOptions::newFromUser($wgUser);
    $parser = &new Parser();
    // Parse the link code
    $output = & $parser->parse($destination, $wgTitle, $parserOptions);
    $linkoutput = $output->getText();

    // Extract the link
    $find_string = '.*href="([^"]*)".*';
    // Create the "real" link
    $icon = mediaLink_GetLinkIcon($out_url[1],$argv['title']);
    $output = "<a href=\"$out_url[1]\">$icon</a>";
    return $output;