Extension:UploadPDF: Difference between revisions

Content deleted Content added
mNo edit summary
mNo edit summary
 
(10 intermediate revisions by 5 users not shown)
Line 1:
{{Archived extension|2815738}}
 
{{Extension|templatemode=
|name = UploadPDF
|status = beta
|type1 = special
|type2 =
|hook1 = UploadCreateFromRequest
|hook2 =
|username = Floyd_williams
|author = <!-- add only if different from user name -->
|description = Originally created to convert pdf engineering drawings into images for a wiki
|image =
|imagesize =
|version = 0.0.1
|update =
|mediawiki = 1.19.0
|php =
|license = GPL
|download =
|readme =
|changelog =
|parameters = <!-- configuration parameters, for use in LocalSettings.php -->
|tags =
|rights =
|example =
|compatibility = 1.19
}}
 
==What can this extension do?==
This extension plugs into Upload series, and automatically translates pdf files into jpeg images during the upload process.
 
==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.
 
==Download instructions==
<!-- Revise these instructions if code is available via a download site. -->
Please copy and paste the code found [[#Code|below]] and place it in <code>$IP/extensions/UploadPDF/UploadPDF.php</code>. ''Note: [[Manual:$IP|$IP]] stands for the root directory of your MediaWiki installation, the same directory that holds [[Manual:LocalSettings.php|LocalSettings.php]]''.
 
==Installation==
To install this extension, add the following to [[Manual:LocalSettings.php|LocalSettings.php]]:
<source lang="php">
require_once("$IP/extensions/UploadPDF/UploadPDF.php");
</source>
Not sure if you require the <code> $wgFileExtensions[]= 'pdf'; </code> 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 [http://www.ghostscript.com/download/gsdnld.html]. If you need it for windows,
or OS\X, download at the previous link, and change the appropriate lines below:
<source lang="php">
shell_exec("gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile=$newjpgfile -dJPEGQ=100 -q $tempfile -c quit");
shell_exec("rm $tempfile");
</source>
in '''UploadPDF-body.php''' to the appropriate commands for the operating system:
''Windows ''
*gs->gswin32c
*rm->del
''OS/2''
*gs->gsos2
 
===Configuration parameters===
'''''Not yet Implemented:'''''
*$wgUploadPDFToggle - If true, places a toggle to turn on and off auto conversion of pdf to jpeg.
*$wgUploadPDFdefault - If true, sets the default value of toggle to On.
 
==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'''
<source lang="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 my extension, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/UploadPDF/UploadPDF.php" );
EOT;
exit( 1 );
}
$wgExtensionCredits['UploadPDF'][] = array(
'path' => __FILE__,
'name' => 'UploadPDF',
'author' => 'Floyd Williams',
'url' => 'http://www.mediawiki.org/wiki/Extension:UploadPDF',
'descriptionmsg' => 'This extension adds an automatic conversion of pdf to image files automatically.
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 compatiblity with other upload extensions.
This extensions is only a partial replacment of upload class.',
'version' => '0.0.1',
);
$dir = dirname(__FILE__) . '/';
$wgAutoloadClasses['UploadPDF'] = '/var/wiki/extensions/UploadPDF/UploadPDF-body.php'; # Location of the UploadPDF class (Tell MediaWiki to load this file)
 
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';
</source>
 
'''UploadPDF-body.php'''
<source lang="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 -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] );}
}
}
</source>