User:Smcnaught/BlogEntry: ImageMap Extension

From ChekMate Security Group

Contents

MW: ImageMap Extension

Introduction

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

Maintainer

Shannon McNaught (smcnaught)

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.

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;

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>

Samples

Following Example using Image: Media:Fish33.gif and Map file: Media:Fish.map, Media:World-map.png and Map file Media:worldmap.map



Apps Apps Apps


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;
#  global $wgOutputEncoding;

  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;

}

?>