Commit b0c9a297 authored by Alexia's avatar Alexia

Initial import of PDFEmbed

parent 9aae3687
.DS_Store
<?php
/**
* PDFEmbed
* PDFEmbed Hooks
*
* @author Alexia E. Smith
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package PDFEmbed
* @link http://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
class PDFEmbed {
/**
* Sets up this extensions parser functions.
*
* @access public
* @param object Parser object passed as a reference.
* @return boolean true
*/
static public function onParserFirstCallInit(Parser &$parser) {
$parser->setHook('pdf', 'PDFEmbed::generateTag');
return true;
}
/**
* Generates the PDF object tag.
*
* @access public
* @param string Namespace prefixed article of the PDF file to display.
* @param array Arguments on the tag.
* @param object Parser object.
* @param object PPFrame object.
* @return string HTML
*/
static public function generateTag($file, $args = array(), Parser $parser, PPFrame $frame) {
global $pdfEmbed, $wgRequest, $wgUser;
$parser->disableCache();
if ($wgRequest->getVal('action') == 'edit' || $wgRequest->getVal('action') == 'submit') {
$user = $wgUser;
} else {
$user = User::newFromName($parser->getRevisionUser());
}
if ($user === false) {
return self::error('embed_pdf_invalid_user');
}
if (!$user->isAllowed('embed_pdf')) {
return self::error('embed_pdf_no_permission');
}
if (empty($file) || !preg_match('#(.+?)\.pdf#is', $file)) {
return self::error('embed_pdf_blank_file');
}
$file = wfFindFile(Title::newFromText($file));
$width = ($args['width'] > 0 ? intval($args['width']) : intval($pdfEmbed['width']));
$height = ($args['height'] > 0 ? intval($args['height']) : intval($pdfEmbed['height']));
if ($file !== false) {
return self::embed($file, $width, $height);
} else {
return self::error('embed_pdf_invalid_file');
}
}
/**
* Returns a standard error message.
*
* @access public
* @param object File object.
* @param integer Width of the object.
* @param integer Height of the object.
* @return string HTML object.
*/
static private function embed(File $file, $width, $height) {
return "<object width='{$width}' height='{$height}' data='".$file->getFullUrl()."' type='application/pdf'>".wfMessage('pdf_not_supported', $file->getFullUrl(), $file->getName())->plain()."</object>";
}
/**
* Returns a standard error message.
*
* @access public
* @param string Error message key to display.
* @return string HTML error message.
*/
static private function error($messageKey) {
return Xml::span(wfMessage($messageKey)->plain(), 'error');
}
}
?>
\ No newline at end of file
<?php
/**
* PDFEmbed
* PDFEmbed Language Strings
*
* @author Alexia E. Smith
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package PDFEmbed
* @link http://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
$messages = array();
$messages['en'] = [
'pdfembed' => 'PDF Embed',
'pdfembed_description' => 'Media handler extension for PDF files.',
'embed_pdf_no_permission' => 'You do not have permission to embed PDF files.',
'embed_pdf_blank_file' => 'The URL or file path given to embed a PDF is blank.',
'embed_pdf_invalid_user' => 'An invalid user was specified to permission testing to embed this PDF.',
'pdf_not_supported' => 'The PDF file did not load properly or your web browser does not support viewing PDF files. Download <a href="$1">$2</a> directly to your device.'
];
$magicWords = array();
$magicWords['en'] = array();
?>
\ No newline at end of file
<?php
/**
* PDFEmbed
* PDFEmbed Mediawiki Settings
*
* @author Alexia E. Smith
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package PDFEmbed
* @link http://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
/******************************************/
/* Credits */
/******************************************/
$credits = array(
'path' => __FILE__,
'name' => 'PDFEmbed',
'author' => 'Alexia E. Smith',
'url' => 'http://www.mediawiki.org/wiki/Extension:PDFEmbed',
'descriptionmsg' => 'pdfembed_description',
'version' => '1.0'
);
$wgExtensionCredits['media'][] = $credits;
/******************************************/
/* Language Strings, Page Aliases, Hooks */
/******************************************/
$extDir = dirname(__FILE__);
$wgAvailableRights[] = 'embed_pdf';
$wgExtensionMessagesFiles['PDFEmbed'] = "{$extDir}/PDFEmbed.i18n.php";
$wgAutoloadClasses['PDFEmbed'] = "{$extDir}/PDFEmbed.hooks.php";
$wgAutoloadClasses['PDFHandler'] = "{$extDir}/classes/PDFHandler.class.php";
$wgHooks['ParserFirstCallInit'][] = 'PDFEmbed::onParserFirstCallInit';
//Future
//$wgMediaHandlers['application/pdf'] = 'PDFHandler';
/******************************************/
/* Initialize Settings */
/******************************************/
//All settings are include order friendly. Meaning the extension include can come before or after the settings and not override them.
//Setup sysop to have this by default.
if (!array_key_exists('embed_pdf', $wgGroupPermissions['sysop'])) {
$wgGroupPermissions['sysop']['embed_pdf'] = true;
}
//Add the PDF file extension into the allowed list.
if (!in_array('pdf', $wgFileExtensions)) {
$wgFileExtensions[] = 'pdf';
}
if (!isset($pdfEmbed['width'])) {
$pdfEmbed['width'] = 800;
}
if (!isset($pdfEmbed['height'])) {
$pdfEmbed['height'] = 1090;
}
?>
\ No newline at end of file
<?php
/**
* PDFEmbed
* PDFHandler Media Handler Class
*
* @author Alexia E. Smith
* @license LGPLv3 http://opensource.org/licenses/lgpl-3.0.html
* @package PDFEmbed
* @link http://www.mediawiki.org/wiki/Extension:PDFEmbed
*
**/
class PDFHandler {
/**
* Sets up this extensions parser functions.
*
* @access public
* @param object Parser object passed as a reference.
* @return boolean true
*/
static public function onParserFirstCallInit(Parser &$parser) {
$parser->setHook('pdf', 'PDFEmbed::generateTag');
return true;
}
/**
* Generates the PDF object tag.
*
* @access public
* @param string Namespace prefixed article of the PDF file to display.
* @param array Arguments on the tag.
* @param object Parser object.
* @param object PPFrame object.
* @return string HTML
*/
static public function generateTag($file, $args = array(), Parser $parser, PPFrame $frame) {
global $pdfEmbed, $wgUser;
$parser->disableCache();
if (!$wgUser->isAllowed('pdf')) {
Please register or sign in to reply
return self::error('embed_pdf_no_permission');
}
if (empty($file)) {
return self::error('embed_pdf_blank_file');
}
$file = wfFindFile(Title::newFromText($file));
$width = ($args['width'] > 0 ? intval($args['width']) : intval($pdfEmbed['width']));
$height = ($args['height'] > 0 ? intval($args['height']) : intval($pdfEmbed['height']));
if ($file !== false) {
return self::embed($file, $width, $height);
} else {
return self::error('embed_pdf_invalid_file');
}
}
/**
* Returns a standard error message.
*
* @access public
* @param object File object.
* @param integer Width of the object.
* @param integer Height of the object.
* @return string HTML object.
*/
static private function embed(File $file, $width, $height) {
return "<object width='{$width}' height='{$height}' data='".urlencode($file->getFullUrl())." type='application/pdf'></object>";
}
/**
* Returns a standard error message.
*
* @access public
* @param string Error message key to display.
* @return string HTML error message.
*/
static private function error($messageKey) {
return Xml::span(wfMessage($messageKey)->plain(), 'error');
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment