2007-09-06 06:32:08 +00:00
|
|
|
<?php
|
2010-10-12 13:48:00 +00:00
|
|
|
/**
|
|
|
|
* Copyright © 2007 Martin Seidel (Xarax) <jodeldi@gmx.de>
|
|
|
|
*
|
|
|
|
* Inspired by djvuhandler from Tim Starling
|
|
|
|
* Modified and written by Xarax
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
class PdfHandler extends ImageHandler {
|
|
|
|
|
|
|
|
function isEnabled() {
|
2010-10-12 13:48:00 +00:00
|
|
|
global $wgPdfProcessor, $wgPdfPostProcessor, $wgPdfInfo;
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !isset( $wgPdfProcessor ) || !isset( $wgPdfPostProcessor ) || !isset( $wgPdfInfo ) ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
wfDebug( "PdfHandler is disabled, please set the following\n" );
|
|
|
|
wfDebug( "variables in LocalSettings.php:\n" );
|
|
|
|
wfDebug( "\$wgPdfProcessor, \$wgPdfPostProcessor, \$wgPdfInfo\n" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
function mustRender( $file ) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
function isMultiPage( $file ) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
function validateParam( $name, $value ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
|
2008-01-11 17:40:39 +00:00
|
|
|
return ( $value <= 0 ) ? false : true;
|
2010-10-12 13:48:00 +00:00
|
|
|
} else {
|
2007-09-06 06:32:08 +00:00
|
|
|
return false;
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeParamString( $params ) {
|
|
|
|
$page = isset( $params['page'] ) ? $params['page'] : 1;
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !isset( $params['width'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
return "page{$page}-{$params['width']}px";
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseParamString( $str ) {
|
|
|
|
$m = false;
|
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
return array( 'width' => $m[2], 'page' => $m[1] );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getScriptParams( $params ) {
|
|
|
|
return array(
|
2008-02-04 06:40:50 +00:00
|
|
|
'width' => $params['width'],
|
|
|
|
'page' => $params['page'],
|
2007-09-06 06:32:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParamMap() {
|
2008-02-04 06:40:50 +00:00
|
|
|
return array(
|
|
|
|
'img_width' => 'width',
|
|
|
|
'img_page' => 'page',
|
|
|
|
);
|
|
|
|
}
|
2010-10-12 13:48:00 +00:00
|
|
|
|
2008-02-05 00:16:54 +00:00
|
|
|
protected function doThumbError( $width, $height, $msg ) {
|
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
|
|
|
$width, $height, wfMsgForContent( $msg ) );
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
global $wgPdfProcessor, $wgPdfPostProcessor, $wgPdfHandlerDpi;
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2008-02-04 07:29:29 +00:00
|
|
|
$metadata = $image->getMetadata();
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !$metadata ) {
|
2008-02-05 00:16:54 +00:00
|
|
|
return $this->doThumbError( @$params['width'], @$params['height'], 'pdf_no_metadata' );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2008-01-11 17:40:39 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !$this->normaliseParams( $image, $params ) ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
return new TransformParameterError( $params );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
$width = $params['width'];
|
|
|
|
$height = $params['height'];
|
|
|
|
$srcPath = $image->getPath();
|
|
|
|
$page = $params['page'];
|
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( $page > $this->pageCount( $image ) ) {
|
2008-02-05 00:16:54 +00:00
|
|
|
return $this->doTHumbError( $width, $height, 'pdf_page_error' );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2008-01-11 17:40:39 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( $flags & self::TRANSFORM_LATER ) {
|
2008-01-11 17:40:39 +00:00
|
|
|
return new ThumbnailImage( $image, $dstUrl, $width,
|
2010-10-12 13:48:00 +00:00
|
|
|
$height, $dstPath, $page );
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
|
2008-02-05 00:16:54 +00:00
|
|
|
return $this->doThumbError( $width, $height, 'thumbnail_dest_directory' );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
$cmd = '(' . wfEscapeShellArg( $wgPdfProcessor );
|
|
|
|
$cmd .= " -sDEVICE=jpeg -sOutputFile=- -dFirstPage={$page} -dLastPage={$page}";
|
|
|
|
$cmd .= " -r{$wgPdfHandlerDpi} -dBATCH -dNOPAUSE -q ". wfEscapeShellArg( $srcPath );
|
|
|
|
$cmd .= " | " . wfEscapeShellArg( $wgPdfPostProcessor );
|
|
|
|
$cmd .= " -depth 8 -resize {$width} - ";
|
|
|
|
$cmd .= wfEscapeShellArg( $dstPath ) . ")";
|
2008-02-05 00:16:54 +00:00
|
|
|
$cmd .= " 2>&1";
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
wfProfileIn( 'PdfHandler' );
|
2010-10-12 13:48:00 +00:00
|
|
|
wfDebug( __METHOD__ . ": $cmd\n" );
|
2011-01-23 10:33:37 +00:00
|
|
|
$retval = '';
|
2007-09-06 06:32:08 +00:00
|
|
|
$err = wfShellExec( $cmd, $retval );
|
|
|
|
wfProfileOut( 'PdfHandler' );
|
|
|
|
|
|
|
|
$removed = $this->removeBadFile( $dstPath, $retval );
|
|
|
|
|
|
|
|
if ( $retval != 0 || $removed ) {
|
|
|
|
wfDebugLog( 'thumbnail',
|
|
|
|
sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
|
2010-10-12 13:48:00 +00:00
|
|
|
wfHostname(), $retval, trim( $err ), $cmd ) );
|
2007-09-06 06:32:08 +00:00
|
|
|
return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
|
|
|
|
} else {
|
|
|
|
return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPdfImage( $image, $path ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !$image ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
$pdfimg = new PdfImage( $path );
|
2010-10-12 13:48:00 +00:00
|
|
|
} elseif ( !isset( $image->pdfImage ) ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
$pdfimg = $image->pdfImage = new PdfImage( $path );
|
2010-10-12 13:48:00 +00:00
|
|
|
} else {
|
2007-09-06 06:32:08 +00:00
|
|
|
$pdfimg = $image->pdfImage;
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
return $pdfimg;
|
|
|
|
}
|
|
|
|
|
2008-02-04 07:29:29 +00:00
|
|
|
function getMetaArray( $image ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( isset( $image->pdfMetaArray ) ) {
|
2008-02-04 07:29:29 +00:00
|
|
|
return $image->pdfMetaArray;
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
$metadata = $image->getMetadata();
|
|
|
|
|
|
|
|
if ( !$this->isMetadataValid( $image, $metadata ) ) {
|
2008-02-04 07:29:29 +00:00
|
|
|
wfDebug( "Pdf metadata is invalid or missing, should have been fixed in upgradeRow\n" );
|
2008-02-04 06:40:50 +00:00
|
|
|
return false;
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
2008-02-04 06:40:50 +00:00
|
|
|
wfSuppressWarnings();
|
2008-02-04 07:29:29 +00:00
|
|
|
$image->pdfMetaArray = unserialize( $metadata );
|
2007-09-06 06:32:08 +00:00
|
|
|
wfRestoreWarnings();
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
2008-02-04 07:29:29 +00:00
|
|
|
return $image->pdfMetaArray;
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getImageSize( $image, $path ) {
|
|
|
|
return $this->getPdfImage( $image, $path )->getImageSize();
|
|
|
|
}
|
|
|
|
|
2010-06-23 18:23:58 +00:00
|
|
|
function getThumbType( $ext, $mime, $params = null ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
global $wgPdfOutputExtension;
|
2008-02-04 06:40:50 +00:00
|
|
|
static $mime;
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
if ( !isset( $mime ) ) {
|
|
|
|
$magic = MimeMagic::singleton();
|
|
|
|
$mime = $magic->guessTypesForExtension( $wgPdfOutputExtension );
|
|
|
|
}
|
|
|
|
return array( $wgPdfOutputExtension, $mime );
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMetadata( $image, $path ) {
|
2008-02-04 07:29:29 +00:00
|
|
|
return serialize( $this->getPdfImage( $image, $path )->retrieveMetaData() );
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isMetadataValid( $image, $metadata ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
return !empty( $metadata ) && $metadata != serialize( array() );
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function pageCount( $image ) {
|
2008-02-04 07:29:29 +00:00
|
|
|
$data = $this->getMetaArray( $image );
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( !$data ) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-02-04 07:29:29 +00:00
|
|
|
return intval( $data['Pages'] );
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPageDimensions( $image, $page ) {
|
2008-02-04 07:29:29 +00:00
|
|
|
$data = $this->getMetaArray( $image );
|
|
|
|
return PdfImage::getPageSize( $data, $page );
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
2009-09-16 13:50:09 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
function getPageText( $image, $page ) {
|
2009-09-16 13:50:09 +00:00
|
|
|
$data = $this->getMetaArray( $image, true );
|
|
|
|
if ( !$data ) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-12 13:48:00 +00:00
|
|
|
if( !isset( $data['text'] ) ) {
|
2009-09-16 13:50:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-10-12 13:48:00 +00:00
|
|
|
if( !isset( $data['text'][$page - 1] ) ) {
|
2009-09-16 13:50:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-10-12 13:48:00 +00:00
|
|
|
return $data['text'][$page - 1];
|
2009-09-16 13:50:09 +00:00
|
|
|
}
|
|
|
|
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|