Extension:UploadPDF
This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net . |
This extension is currently not actively maintained! Although it may still work, any bug reports or feature requests will more than likely be ignored. |
UploadPDF Release status: unmaintained |
|
---|---|
Implementation | Media |
Description | Adds an automatic conversion of PDF to JPEG files upon uploading files |
Author(s) | Floyd Williams (Floyd_williamstalk) |
Latest version | 0.0.1 |
MediaWiki | 1.19+ |
Database changes | No |
License | GNU General Public License 2.0 or later |
Download | See the code section |
The UploadPDF extension plugs into Upload series, and automatically converts PDF files into JPEG images during the upload process. This was originally intended for engineering drawings. This replaces functions of the Upload Class just enough to convert the file before wiki storage processing, Contains all the same hooks as its parent class, and therefore compatibility with other upload extensions. This extensions is only a partial replacement of upload class.
Usage
For use with Single Page PDF images and slides. Go to the default MediaWiki upload page, Special:Upload. Upload files as usual. This extension automatically detects the pdf extensions and makes the necessary changes, even to the destination filename. There are no added special pages with this extension, just a change in the Upload class used for the upload. This means all rights that apply to the Upload still apply.
Installation
- Copy the code into files and place the file(s) in a directory called
UploadPDF
in yourextensions/
folder. - Add the following code at the bottom of your LocalSettings.php file:
require_once "$IP/extensions/UploadPDF/UploadPDF.php";
- Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.
Note: Not sure if you require the $wgFileExtensions[]= 'pdf';
because this hook changes the file parameters processed by the UploadBase class before the File verification processes. Still recommended so the extension shows in allowed file types on the Upload page.
DESIGNED FOR UNIX\LINUX SHELL ONLY IN THIS VERSION This script requires ghostscript [1]. If you need it for windows, or OS\X, download at the previous link, and change the appropriate lines below:
shell_exec("gs -dSAFER -dNOPAUSE -sDEVICE=jpeg -sOutputFile=$newjpgfile -dJPEGQ=100 -q $tempfile -c quit");
shell_exec("rm $tempfile");
in UploadPDF-body.php to the appropriate commands for the operating system:
Windows
- gs->gswin32c
- rm->del
OS/2
- gs->gsos2
Code
This still has a lot of wfDebug statements. I'm new to php, and figured out some of the variable and function passing properties the hard way.
- UploadPDF.php
<?php
# Alert the user that this is not a valid entry point to MediaWiki if they try to access the special pages file directly.
if (!defined('MEDIAWIKI')) {
echo <<<EOT
To install this extension, put the following line at the end of LocalSettings.php:
require_once "\$IP/extensions/UploadPDF/UploadPDF.php";
EOT;
exit( 1 );
}
$wgExtensionCredits['UploadPDF'][] = array(
'path' => __FILE__,
'name' => 'UploadPDF',
'author' => 'Floyd Williams',
'url' => 'https://www.mediawiki.org/wiki/Extension:UploadPDF',
'descriptionmsg' => 'Adds an automatic conversion of PDF to JPEG files upon uploading files',
'version' => '0.0.1'
);
$dir = dirname( __FILE__ ) . '/';
$wgAutoloadClasses['UploadPDF'] = $dir . 'UploadPDF-body.php';
function pdfCheckIntercept($type, &$className ) {
global $wgAutoloadClasses;
wfDebug( __METHOD__ . "Detected type: $type\n");
if ($type == 'File') {
global $wgOut;
$request = RequestContext::getMain()->getRequest();
$upload = $request->getUpload( 'wpUploadFile');
$filename= $upload->getName();
if ($filename == null) {return 1;}
wfDebug( __METHOD__ . "Detected upload file: $filename\n");
$extension = pdfgetExtension ($filename);
wfDebug( __METHOD__ . "Detected upload file extension : $extension\n");
if ($extension == 'pdf'){$className = 'UploadPDF';
wfDebug( __METHOD__ . "Detected pdf upload file: $filename. Changed class to: $className\n");}
return 1;
}
return 1;
}
function pdfgetExtension ( $filename){
$bits = explode( '.', $filename );
$basename = array_shift( $bits );
if( count( $bits ) ) {return trim( $bits[count( $bits ) - 1] );}
}
$wgHooks['UploadCreateFromRequest'][]= 'pdfCheckIntercept';
- UploadPDF-body.php
<?php
/**
* Implements PDF file uploads
*
* @ingroup Upload
* @author Floyd Williams
*/
# This extension hooks into createFromRequest hook, pulls the request to check for the pdf extension.
# If pdf extension is found, it replaces the default classname from UploadFile, with this UploadPDF.
# It was the only way I Could find to access\change UploadBase::$mTempFile(protected)
# before it stores the upload, with minimal to the code. I need to convert the tempfile, and
# replace the $mTempFile used for the upload proccessing with the new $mTempFile.
class UploadPDF extends UploadBase {
/**
* @var WebRequestUpload
*/
protected $mUpload = null;
/**
* @param $request WebRequest
*/
function initializeFromRequest( &$request ) {
$upload = $request->getUpload( 'wpUploadFile' );
wfDebug( __METHOD__ . ": initializing request!\n" );
$desiredDestName = $request->getText( 'wpDestFile' );
if( !$desiredDestName )
$desiredDestName = $upload->getName();
return $this->initialize( $desiredDestName, $upload );
}
/**
* Initialize from a filename and a WebRequestUpload
* @param $name
* @param $webRequestUpload
*/
function initialize( $name, $webRequestUpload ) {
$this->mUpload = $webRequestUpload;
wfDebug( __METHOD__ . ": calling pdfconvert with paremeter" . $this->mUpload->getTempName('wpUploadFile') . "\n" );
$myarray = $this->pdfconvert($this->mUpload->getName(),$this->mUpload->getTempName('wpUploadFile') );
$mystring= 'Parameters for initializing path info 1:' . $myarray[0] . " 2:" . $myarray[1] . " 3:" . $myarray[2];
wfDebug( __METHOD__ . ": initializing pathinfo with $mystring\n" );
return $this->initializePathInfo( $myarray[0],$myarray[1],$myarray[2], true);
#initializepathinfo function call is changed to reflect the new tempfile for
#for storage by the new pdfconvert function
}
/**
* @param $request
* @return bool
*/
static function isValidRequest( $request ) {
# Allow all requests, even if no file is present, so that an error
# because a post_max_size or upload_max_filesize overflow
return true;
}
/**
* @return string
*/
public function getSourceType() {
return 'file';
}
/**
* @return array
*/
public function verifyUpload() {
# Check for a post_max_size or upload_max_size overflow, so that a
# proper error can be shown to the user
if ( is_null( $this->mTempPath ) || $this->isEmptyFile() ) {
if ( $this->mUpload->isIniSizeOverflow() ) {
return array(
'status' => UploadBase::FILE_TOO_LARGE,
'max' => min(
self::getMaxUploadSize( $this->getSourceType() ),
wfShorthandToInteger( ini_get( 'upload_max_filesize' ) ),
wfShorthandToInteger( ini_get( 'post_max_size' ) )
),
);
}
#wfDebug( __METHOD__ . ": pdf function call successful tmpfile:$tempfile\n" );
}
return parent::verifyUpload();
}
public function pdfconvert($destname, $tempfile){
#command
$newjpgfile= $this->pdfnoextensionpath($tempfile) . ".jpg";
wfDebug( __METHOD__ . ": converting $tempfile to $newjpgfile\n" );
if (file_exists($tempfile)){
wfDebug( __METHOD__ . ": file exists ! $tempfile\n" );
shell_exec("gs -dSAFER -dNOPAUSE -sDEVICE=jpeg -sOutputFile=$newjpgfile -dJPEGQ=100 -q $tempfile -c quit");
shell_exec("rm $tempfile");
} else {wfDebug( __METHOD__ . ": $tempfile does NOT exist!\n" );}
if (file_exists($newjpgfile)){wfDebug( __METHOD__ . ": conversion to $newjpgfile successful!\n" );}
else {wfDebug( __METHOD__ . ": conversion to $newjpgfile NOT successful!\n" );}
$destname= $this->pdfnoextensionpath($destname) . ".jpg";
return array($destname, $newjpgfile, filesize($newjpgfile));
}
public function pdfnoextensionpath($filename){
$bits = explode( '.', $filename );
$basename = array_shift( $bits );
return trim($basename);
# if( count( $bits ) ) {return trim( $bits[0] );}
}
}