Extension:ImgLink

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
ImgLink

Release status: beta

Implementation Parser functions
Description Allows (small) images to be inlined in links
Author(s) Steve Hicks (KupopoTalk)
Last version 1.0 (11 April 2008)
MediaWiki 1.10
License No license specified
Download Extension:ImgLink#Code
Hooks used
LanguageGetMagic

Check usage (experimental)

Contents

[edit] What can this extension do?

Sometimes it's desirable to have small images/icons inline in text. This is possible for non-link text with simply [[Image:File.png|alt text]]. Unfortunately, this doesn't work inside a link. This is a quick extension I put together to allow link text to contain inline images.

[edit] Usage

Use the parser hook

{{#imglink:target page|link text|substitutions}}

where substitutions is a comma-separated list of search/replace pairs in the form

pattern=filename.png

or

pattern=filename.png=alt text

For example, suppose Image:Heart.png and Image:Spade.png are defined on the wiki. Then the following code would produce a single link with the images inlined:

{{#imglink:Michaels cuebid|2!h or 2!s|!h=Heart.png=H,!s=Spade.png=S}}

The link points to the page Michaels cuebid, and the text is "2H or 2S" where the H and S are replaced by the images, presumably of a small heart and spade suit icon.

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/ImgLink/ImgLink.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

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

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

[edit] Code

<?php
 
$wgExtensionFunctions[] = 'wfImgLinkPF_Setup';
$wgHooks['LanguageGetMagic'][] = 'wfImgLinkPF_Magic';
 
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'ImgLink',
    'author' => 'Steve Hicks',
    'url' => 'http://www.mediawiki.org/wiki/Extension:ImgLink',
    'description' => 'This extension allows template replacing of images in links'
    );
 
# Setup functions...
function wfImgLinkPF_Setup() {
  global $wgParser;
  $wgParser->setFunctionHook('imglink', 'wfImgLinkPF_Render');
}
 
function wfImgLinkPF_Magic(&$magicWords,$langCode) {
  $magicWords['imglink'] = array(0,'imglink');
  return true;
}
 
# Rendering function...
function wfImgLinkPF_Render(&$parser,$link='',$text='',$subs='') {
  # link is either internal or external
  # text contains patterns defined in subs
  # subs is of the form !c=Club.png=C, !d=Diamond.png=D
  # i.e. template -> <pattern>=<filename>[=<alt text>]
  #      subs     -> template[, subs]
  $subsarray = array_map('trim',explode(',',$subs));
  $patterns = array(); $templates = array();
  foreach($subsarray as $sub) {
    if (preg_match('/([^=]*)=([^=]*)=?(.*)?/',$sub,$matches)) {
      $patterns[] = $matches[1];
      $image = $matches[2];
      if ($matches[3]) { $alt = $matches[3]; } else { $alt = ''; }
      $imagetitle = Title::newFromText($image,NS_IMAGE);
      $imagehtml = $parser->makeImage($imagetitle,"|$alt");
      # There's gotta be a better way...?
      $imagehtml = preg_replace('/<\/?a[^>]*>/','',$imagehtml);
      if ($alt!='') $imagehtml .= "<span style='display:none'>$alt</span>";
      $templates[] = $imagehtml;
    }
  }  
  $internalhtml = str_replace($patterns,$templates,$text);
  # Currently we're only supporting internal links
  # Later we could do external links - c.f. ImageMapArea.php
  $linktitle = Title::newFromText($link);
  if (!$linktitle) return 'imglink_invalid_link'; # easy error handling
  $linkhref = $linktitle->escapeLocalURL()
     . $linktitle->getFragmentForURL();
 
  $html = "<a href='$linkhref'>$internalhtml</a>";
  return array($html, 'found'=>true, 'noparse'=>true);
}

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox