Personal tools

Extension:Image.php

From OrganicDesign Wiki

Jump to: navigation, search
<?php
#
# Extension:Image
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: [http://www.organicdesign.co.nz/User:Rob Rob]
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
define('IMAGE_VERSION','0.0.1');
 
$egImageMagic             = "image";
$wgExtensionFunctions[]        = 'efSetupImage';
$wgHooks['LanguageGetMagic'][] = 'efImageLanguageGetMagic';
 
$wgExtensionCredits['parserhook'][] = array(
	'name'        => 'Image',
	'author'      => '[http://www.organicdesign.co.nz/User:Rob Rob]',
	'description' => 'Better image handling',
	'url'         => 'http://www.organicdesign.co.nz/Extension:Image',
	'version'     => IMAGE_VERSION
	);
 
$wgHooks['ParserAfterTidy'][] = 'efFixEntitiesInUrl';
 
function efFixEntitiesInUrl(&$parser,&$text) {
    # cludge to fix urls where MW has converted & into &amp; (despite isHtml=true)
    preg_match_all("|(src='.+?')|", $text, $match, PREG_PATTERN_ORDER);
    $text = str_replace($match[0], str_replace('&amp;', '&', $match[0]), $text);
    return true;
 
}
 
class MyImage {
 
	# Properties
	var $prop1 = 'default value';
	var $prop2 = 'default value';
 
	# Constructor
	function __construct() {
		global $wgHooks,$wgParser,$egImageMagic,$egImageTag;
 
		# Add the parser-function
		$wgParser->setFunctionHook($egImageMagic,array($this,'magicImage'));
 
		}
 
	# Expand the image-magic
	function magicImage(&$parser) {
		global $egImageMagic, $wgScriptPath;
		# Populate $argv with both named and numeric parameters
		$argv = array();
		foreach (func_get_args() as $arg) if (!is_object($arg)) {
			if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
			}
 
        # resolve image title
        $image = Image::newFromTitle( Title::newFromText($argv['src'], NS_IMAGE) );
		# if width is specified use thumb.php
		if(preg_match('/(\d+)px/', $argv['width'], $match) != 0)
			$imgurl = "$wgScriptPath/thumb.php?f=".$argv['src']."&width=".$match[1];
        # otherwise use standard image size
        else $imgurl = $image->getURL();
 
		$imgtag	= "<img src='$imgurl'>";
 
		$text = "$imgtag";
 
#		$text = print_r($image, -1);
 
		# Return result with available parser flags
		return array(
			$text,
			'found'   => true,
			'noparse' => true,
			'isHTML'  => true
			);
 
		}
 
 
	# Needed in some versions to prevent Special:Version from breaking
	function __toString() { return 'MyImage'; }
 	}
 
# Called from $wgExtensionFunctions array when initialising extensions
function efSetupImage() {
	global $egImage;
	$egImage = new MyImage();
	}
 
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function efImageLanguageGetMagic(&$magicWords,$langCode = 0) {
	global $egImageMagic;
	$magicWords[$egImageMagic] = array(0,$egImageMagic);
	return true;
	}

The GNU Project Debian Linux Ubuntu Linux Wikipedia online encycopedia MediaWiki