Extension:GoogleImage

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
GoogleImage
Release status: stable
Implementation Tag
Description This extension allows to display Google Drive Images in your wiki.
Author(s) Bertrand Gorge (Bertrandgorgetalk)
Latest version 1.0
License No license specified
Download see below
Translate the GoogleImage extension if it is available at translatewiki.net
Check usage and version matrix.

GoogleImage displays images from Google Drive. Just put your images in your drive, and then share them in your wiki! Note: you might want to set the proper access rights on your images depending on the target of your wiki.

Installation[edit]

Add this line at the end of LocalSettings.php:

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

Copy the following code into $IP/extensions/GoogleImage.php:

Code[edit]

<?php
/**
 * GoogleImage extension
 * Tag: <googleimage>docid</googleimage>

 * <googleimage>http://docs.google.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/edit</googleimage>
 * <googleimage>https://drive.google.com/a/crossknowledge.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/view</googleimage>
 * <googleimage>0B6yjbA3TP1ZfNU1pa2J4NVgyd3c</googleimage>
 * with:
 * http://drive.google.com/uc?export=view&id=0B6yjbA3TP1ZfNU1pa2J4NVgyd3c

 * @author Bertrand Gorge
 * @version 1.0
 * @link http://www.mediawiki.org/wiki/Extension:GoogleImage
 */

$wgExtensionFunctions[] = 'wfGoogleImage';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Google Image',
        'version' => '1.0',
        'description' => 'Display Google Image from the drive',
        'author' => 'Bertrand Gorge',
        'url' => 'http://www.mediawiki.org/wiki/Extension:GoogleImage'
);

function wfGoogleImage()
{
	global $wgParser;

	$wgParser->setHook('googleImage', 'renderGoogleImage');
	$wgParser->setHook('driveimage', 'renderDriveImage');
}

# The callback function for converting the input text to HTML output
function renderGoogleImage($input)
{
	if (empty($input))
		return '';

	// <googleimage>http://docs.google.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/edit</googleimage>
	// <googleimage>https://drive.google.com/a/crossknowledge.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/view</googleimage>
	// <googleimage>0B6yjbA3TP1ZfNU1pa2J4NVgyd3c</googleimage>
	$matches = array();
	if (preg_match('@d/([^/]+)/@', $input, $matches))
		$fileId = $matches[1];
	else
		$fileId = $input;

	$output = '<img src="http://drive.google.com/uc?export=view&id='.htmlspecialchars($fileId).'"></img>';

    return $output;
}

# The callback function for converting the input text to HTML output
function renderDriveImage($input)
{
	// <googleimage>http://docs.google.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/edit</googleimage>
	// <googleimage>https://drive.google.com/a/crossknowledge.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/view</googleimage>
	// <googleimage>0B6yjbA3TP1ZfNU1pa2J4NVgyd3c</googleimage>
	$matches = array();
	if (preg_match('@d/([^/]+)/@', $input, $matches))
		$fileId = $matches[1];
	else
		$fileId = $input;

	$output = '<img src="http://drive.google.com/uc?export=view&id='.htmlspecialchars($fileId).'"></img>';

    return $output;
}

That's all :-)

Usage[edit]

The extension can be used as follow:

<googleimage>http://docs.google.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/edit</googleimage>
<googleimage>https://drive.google.com/a/crossknowledge.com/file/d/0B6yjbA3TP1ZfNU1pa2J4NVgyd3c/view</googleimage>
<googleimage>0B6yjbA3TP1ZfNU1pa2J4NVgyd3c</googleimage>

See also[edit]