Extension:EmbedPDF

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
EmbedPDF

Release status: beta

Implementation Parser function
Description Allows to embed .pdf documents on a wiki page.
Author(s) Dmitry Shurupovtalk
Latest version 0.2
MediaWiki 1.12+
Database changes No
License GPL
Download see below
Tags
<pdf>
Hooks used
ParserFirstCallInit

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

Check usage and version matrix; code metrics

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

Installation[edit | edit source]

  1. Copy EmbedPDF.php to the new file EmbedPDF.php in your MediaWiki extensions directory.
  2. Enable the extension by adding this line to the bottom of your LocalSettings.php:
require_once("$IP/extensions/EmbedPDF.php");

Usage[edit | edit source]

  • <pdf>http://some.site.com/with/a/document.pdf</pdf>
  • <pdf>Your_uploaded_document.pdf</pdf>

Options[edit | edit source]

There are no options yet. EmbedPDF will create HTML <object> with predefined width (1000 px) and height (700 px). That's all.

Source of EmbedPDF.php[edit | edit source]

<?php
/**
 * MediaWiki EmbedPDF extension
 *
 * @file
 * @ingroup Extensions
 * @version 0.2
 * @author Dmitry Shurupov
 * @link https://www.mediawiki.org/wiki/Extension:EmbedPDF Documentation
 */
 
if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This is not a valid entry point to MediaWiki.' );
}
 
// Extension credits that will show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'EmbedPDF',
	'author' => 'Dmitry Shurupov',
	'version' => '0.2',
	'url' => 'https://www.mediawiki.org/wiki/Extension:EmbedPDF',
	'description' => 'Allows to embed .pdf documents on a wiki page.',
);
 
$wgHooks['ParserFirstCallInit'][] = 'registerEmbedPDFHandler';
// Register the <pdf> tag with the parser
function registerEmbedPDFHandler( &$parser ) {
	$parser->setHook( 'pdf', 'embedPDFHandler' );
	return true;
}
 
function makeHTMLforPDF( $path, $argv ) {
	// Use user-supplied values for the width and height parameters, if
	// they are set and also do some very basic input validation
	if ( empty( $argv['width'] ) ) {
		$width = '1000';
	} else {
		$width = ( is_numeric( $argv['width'] ) ? $argv['width'] : 1000 );
	}
 
	if ( empty( $argv['height'] ) ) {
		$height = '700';
	} else {
		$height = ( is_numeric( $argv['height'] ) ? $argv['height'] : 700 );
	}
 
	return '<object data="' . $path . '" width="' . $width . '" height="' .
		$height . '" type="application/pdf"></object>';
}
 
function embedPDFHandler( $input, $argv ) {
	if ( !$input ) {
		return '<span style="color: red;">Error: empty param in &lt;pdf&gt;!</span>';
	}
 
	if ( preg_match( '/^[^\/]+\.pdf$/i', $input ) ) {
		$img = wfFindFile( $input );
		if ( is_object( $img ) ) {
			return makeHTMLforPDF( $img->getURL(), $argv );
		}
	}
 
	if ( preg_match( '/^http\:\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@\?\^\=\%\&:\/\~\+\#]*[\w\-\@\?\^\=\%\&\/\~\+\#])?\.pdf$/i', $input ) ) {
		return makeHTMLforPDF( $input, $argv );
	} else {
		return '<span style="color: red;">Error: bad URI in &lt;pdf&gt;!</span>';
	}
}