Extension:SimpleEmbed

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.svg
SimpleEmbed

Release status:Extension status stable

ImplementationTemplate:Extension#type Tag, Media
DescriptionTemplate:Extension#description Allows for embedding files on a page
Author(s)Template:Extension#username Kim Eik (netbraintalk)
Latest versionTemplate:Extension#version 0.1 (2013-08-14)
MediaWikiTemplate:Extension#mediawiki 1.21+
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license Public domain
Download See the code section
TagsTemplate:Extension#tags
<embed>

Translate the SimpleEmbed extension if it is available at translatewiki.net

Check usage and version matrix.

The SimpleEmbed extension adds the <embed> tag for embedding files. It supports remote and local (uploaded to MediaWiki) files.

This extension is based on the EmbedAll extension.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/SimpleEmbed/SimpleEmbed.php";
    
  • YesY Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Usage[edit]

  • <embed>http://some.site.com/with/a/document.pdf</embed>
  • <embed width="250">Your_uploaded_document.pdf</embed>
Options
  • width
  • height
You can embed all types of "embeddable" files, not just PDF's.

Code[edit]

SimpleEmbed.php
<?php

/**
 * Example usage:
 *
 *	<embed>{{PAGENAME}}</embed>
 *	<embed>http://localhost/mw/images/d/de/Sample.pdf</embed>
 *	<embed>Sample.pdf</embed>
 *
 */

$wgExtensionCredits['parserhook'][] = array(
	'name' => 'SimpleEmbed',
	'author' => 'Kim Eik',
	'version' => '0.1',
	'url' => 'https://www.mediawiki.org/wiki/Extension:SimpleEmbed',
	'description' => 'Allows for embedding files on a page',
);

$wgExtensionFunctions[] = 'registerEmbedHandler';

function registerEmbedHandler ()
{
	global $wgParser;
	$wgParser->setHook( 'embed', 'embed' );
}

function isValidUrl($url){
	return preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url);
}

function parseAsUrlOrWikiText($text,Parser $parser, PPFrame $frame){
	if(isValidUrl($text)){
		return $text;
	}else{
		$text = html_entity_decode($parser->recursiveTagParse($text,$frame));
		if(!isValidUrl($text)){
			$file = wfFindFile($text);
			return $file ? $file->getFullUrl() : false;
		}
		return $text;
	}
}

function embed ( $input, $argv, Parser $parser, PPFrame $frame )
{
	$path = parseAsUrlOrWikiText($input,$parser,$frame);
	if (!$path){
		return "<span style=\"color: red;\">Invalid URI: $input</span>";
	}

	$width = isset($argv['width']) ? htmlspecialchars( $argv['width'] ) : '1000';
	$height = isset($argv['height']) ? htmlspecialchars( $argv['height'] ) : '700';

	return '<iframe src="'.$path.'" width="'.$width.'" height="'.$height.'"></iframe>';
}