Extension:GoogleImage

MediaWiki extensions manual
GoogleImage
Release status: unmaintained
Implementation Tag
Description Allows to display images stored in Google Drive
Author(s) Bertrand Gorge (Bertrandgorgetalk)
Latest version 1.0
MediaWiki
License No license specified
Download see below

The GoogleImage extension allows to display images stored in 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

  • Copy the code in a file called "GoogleImage.php" and place the file(s) in a directory called GoogleImage in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    require_once "$IP/extensions/GoogleImage/GoogleImage.php";
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code edit

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

 * <googleimage>https://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:
 * https://drive.google.com/uc?export=view&id=0B6yjbA3TP1ZfNU1pa2J4NVgyd3c

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

$wgExtensionFunctions[] = 'wfGoogleImage';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Google Image',
        'version' => '1.0',
        'description' => 'Allows to display images stored on GoogleDrive',
        'author' => 'Bertrand Gorge',
        'url' => 'https://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>https://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="https://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>https://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="https://drive.google.com/uc?export=view&id='.htmlspecialchars($fileId).'"></img>';

    return $output;
}

Usage edit

The extension can be used as follow:

<googleimage>https://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