Extension:ImageMap (McNaught)

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
ImageMap (McNaught)

Release status: stable

Implementation Tag
Description ImageMap implementation. (see Extension:ImageMap)
Author(s) Shannon McNaught
MediaWiki 1.5
Download Download Imagemap extension files
Example See below

Contents

[edit] Introduction

Consider Choosing an ImageMap Extension before deciding on which ImageMap extension to use.

ImageMap is a MediaWiki 1.5 extension. This extension was built to add Client Side ImageMaps to MediaWiki.

Note: Most up to date version of this extension can be found at: http://www.chekmate.org/wiki/index.php/Projects

Another version is located at m:User:TeamChaos/HtmlMaps

Note: For MediaWiki 1.9 there is a similar extension: ImageMap. This extension does not use image map information in HTML format loaded from files. Rather, it includes image map information inline between the imagemap tags.

Another version, that includes the functionality of both this extension and the extension in the above note is available on Mediawiki at Imagemap (Alternate). This version provides for full map file checking to exclude any non-map HTML statements. It also provides proper linking to map files in a Wiki Family.

[edit] Change History

  • 15:25, 24 July 2006 (UTC) - Extension now configured so it no longer caches the wiki page. (Smcnaught)
  • 02:20, 24 July 2006 (UTC) - If whitespace was in the map file, it created a <pre> underneath the image map graphic. This has been corrected. (Smcnaught)
  • 06:09, 18 July 2006 (UTC) - Added better variable definition for $mapfile using global variable $IP (Smcnaught)
  • 01:56, 18 July 2006 (UTC) - Added Error checking to check if $mapfile exists. (Smcnaught)
  • 01:56, 18 July 2006 (UTC) - Added a random component to timestamp as multiple imagemaps on a single page did not work properly. (Smcnaught)

[edit] Maintainer

Shannon McNaught (smcnaught) - I am also available on irc.chekmate.org #MediaWiki

[edit] Homepage

ChekMate Technical Focus Group

[edit] License

ImageMap.php 
  - Adds ImageMap functionality to MediaWiki

Copyright (C) 2006  Shannon McNaught

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

[edit] Installation

  • Copy ImageMap.php to "extensions" folder.
  • Change " $mapfile = "/var/www/html/$Mapurl";" within file to point to the parent directory where your wiki folder is located.
  • Add a line require_once( "extensions/ImageMap.php" ); into "LocalSettings.php".
  • Set up MediaWiki to allow users to upload MAP files, set up something like the following in "LocalSettings.php":
/**
  * This is the list of preferred extensions for uploading files. Uploading files
  * with extensions not in this list will trigger a warning.
  */
 $wgFileExtensions = array( 'png', 'gif', 'jpg', 'jpeg', 'pdf','map' );
  • Was an issue with MediaWiki not accepting the upload. Needed to change $wgVerifyMimeType to false in "./includes/DefaultSettings.php". Better solutions?
/** Determines if the mime type of uploaded files should be checked
  * @global boolean $wgVerifyMimeType
 */
 $wgVerifyMimeType= false;

[edit] Better solution

Look in Apache's mime.types file. You should find a mime type for text/plain. Add map to it - e.g.:

text/plain       asc txt map

This will allow basic mime validation as a text file. With this, you do not need to use $wgVerifyMimeType= false; at all.

If this creates a problem with existing *.map files for other uses, then use an extension like mpi instead. Put mpi into $wgFileExtensions and in mime.types. Then use the mpi extension in the <imagemap> input parameters.

[edit] Usage

 <ImageMap>Image=[[Media:Image.png]]|Map=[[Media:Imagemap.map]]</ImageMap> 
 <!--- Must use Media: and not Image: for the Parser to work properly. --->
 <ImageMap>Image=[[Media:Image.gif]]|Map=[[Media:Imagemap.map]]</ImageMap>
 <ImageMap>Image=[[Media:Image.jpg]]|Map=[[Media:Imagemap.map]]</ImageMap>

[edit] Sample

An online example can be seen at: http://www.chekmate.org/wiki/index.php/MW:_ImageMap_Extension#Samples

[edit] ImageMap.php

<?php
# ImageMap MediaWiki extension
#
# original by smcnaught 29.06.2005
# Installation:
#  * put this file (ImageMap.php) into the extension directory of your mediawiki installation
#  * add the following to the end of LocalSettings.php: include("extensions/ImageMap.php");
#
# Usage:
#  Use one section between <ImageMap>-tags for each feed. The ImageMap section may contain parameters
#  separated by a pipe ("|"), just like links and templates. These parameters are supported:
#
# Example:
#    <ImageMap>Image=ImageURL|Map=MapURL</ImageMap>
#
 
 
#install extension hook
$wgExtensionFunctions[] = "wfImageMapExtension";
 
#extension hook callback function
function wfImageMapExtension() {
  global $wgParser;
 
  #install parser hook for <ImageMap> tags
  $wgParser->setHook( "ImageMap", "renderImageMap" );
}
 
#parser hook callback function
function renderImageMap( $input ) {
  global $wgServer, $wgScriptPath, $wgTitle, $wgUrlProtocols, $wgUser, $IP;
 
# Do not cache this wiki page.
# for details see http://public.kitware.com/Wiki/User:Barre/MediaWiki/Extensions
  global $wgDBprefix;
  $ts = mktime();
  $now = gmdate("YmdHis", $ts + 120);
  $ns = $wgTitle->getNamespace();
  $ti = wfStrencode($wgTitle->getDBkey());
  $version = preg_replace("/^([1-9]).([1-9]).*/", "\\1\\2", $wgVersion);
  if ($version>14) $sql = "UPDATE $wgDBprefix"."page SET page_touched='$now' WHERE page_namespace=$ns AND page_title='$ti'";
  else             $sql = "UPDATE $wgDBprefix"."cur SET cur_touched='$now' WHERE cur_namespace=$ns AND cur_title='$ti'";
  wfQuery($sql, DB_WRITE, "");
 
  if (!$input) return "";
 
  $fields= explode("|",$input);
  $args= array();
  for ($i=0; $i<sizeof($fields); $i++) {
    $f= $fields[$i];
 
    if (strpos($f,"=")===False) $args[strtolower(trim($f))]= False;
    else {
      list($k,$v)= explode("=",$f,2);
      if (trim($v)==False) $args[strtolower(trim($k))] = False;
      else $args[strtolower(trim($k))]= trim($v);
    }
  }
  $timestamp = mktime() . rand(1,29);
 
  #get title from argument-array
 
  $ImageURL= @$args["image"];
  $ImageURL= trim($ImageURL);
  if ($ImageURL=='') {
    return "No Image";
  }
 
  $MapURL= @$args["map"];
  $MapURL= trim($MapURL);
  if ($MapURL=='') {
    return "No Map";
  }
  $localParser = new Parser();
  $parserOptions = ParserOptions::newFromUser( $wgUser );
 
  $html = $localParser->parse($ImageURL,$wgTitle,$parserOptions);
  $Imageurl = preg_replace('/^.*<a[\s]+href=*"(.*?)".*$/is', '\1' , $html->mText);
  $html = $localParser->parse($MapURL,$wgTitle,$parserOptions);
  $Mapurl = preg_replace('/^.*<a[\s]+href=*"(.*?)".*$/is', '\1' , $html->mText);
  $mapfile = "$IP/../$Mapurl";
  $lines = array_map('rtrim',file("$mapfile"));
 
  $output="<img src=\"$Imageurl\" usemap=\"#$timestamp\">";
 
  if (!file_exists($mapfile)) {
    $output = $output . "Path to mapfile is incorrect or file does not exist. mapfile should look like: /var/www/html/wiki/images/4/4d/Fish.map";
 
    # Enable for troubleshooting, otherwise comment to hide directory path.
    #   $output = $output . "$mapfile";
  }
 
  // Loop through our array, show HTML source as HTML source; and line numbers too.
  foreach ($lines as $line_num => $line) {
    if (preg_match ("/\sname=/i", $line)) {
      $replacestr = " name=\"$timestamp\"";
      $line = preg_replace('/\sNAME=\"[a-zA-Z0-9 ]+\"/i',$replacestr,$line);    
    }
    $line = preg_replace(array('/\s{2,}/','/^\s+/','/\s+$/'),array(' ','',''),$line);
    $output = $output . $line . "\n";
  }
  return $output;
 
}
 
?>

[edit] Other sites using this extension

[edit] Imagemapping Tools

  • Clickable Image Map Maker. This web page allows the user to select an image file on his own local hard drive, then creates a second page using that image, and allows the user to very quickly generate the HTML code for an imagemap to be used in his own files. It also offers some handy pop-up menus to speed the process of coding the actual href. Complicated maps can be created in less than a minute (no kidding). Requires a Javascript and frames capable browser, designed and tested on a Mac using Netscape Navigator.
  • Mapedit is a WYSIWYG editor for imagemaps, available for Microsoft Windows and the X Window System. Use Mapedit to generate, or convert to, NCSA, CERN, or client-side map files.
Personal tools