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 {
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $file
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-10-12 13:48:00 +00:00
|
|
|
function mustRender( $file ) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $file
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-10-12 13:48:00 +00:00
|
|
|
function isMultiPage( $file ) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
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' ) ) ) {
|
2013-01-31 10:50:00 +00:00
|
|
|
return ( $value > 0 );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2013-01-31 10:50:00 +00:00
|
|
|
return false;
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $params array
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
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";
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $str string
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $params array
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
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
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $width
|
|
|
|
* @param $height
|
|
|
|
* @param $msg
|
|
|
|
* @return MediaTransformError
|
|
|
|
*/
|
2008-02-05 00:16:54 +00:00
|
|
|
protected function doThumbError( $width, $height, $msg ) {
|
|
|
|
return new MediaTransformError( 'thumbnail_error',
|
2012-09-01 23:07:57 +00:00
|
|
|
$width, $height, wfMessage( $msg )->inContentLanguage()->text() );
|
2008-02-05 00:16:54 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $dstPath string
|
|
|
|
* @param $dstUrl string
|
|
|
|
* @param $params array
|
|
|
|
* @param $flags int
|
|
|
|
* @return MediaTransformError|MediaTransformOutput|ThumbnailImage|TransformParameterError
|
|
|
|
*/
|
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 ) {
|
2012-10-04 16:34:25 +00:00
|
|
|
return $this->doThumbError(
|
|
|
|
isset( $params['width'] ) ? $params['width'] : null,
|
|
|
|
isset( $params['height'] ) ? $params['height'] : null,
|
|
|
|
'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'];
|
|
|
|
$page = $params['page'];
|
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
if ( $page > $this->pageCount( $image ) ) {
|
2012-02-05 20:29:51 +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 ) {
|
2012-02-05 20:29:51 +00:00
|
|
|
return new ThumbnailImage( $image, $dstUrl, $width, $height, false, $page );
|
2010-10-12 13:48:00 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2011-07-25 22:09:05 +00:00
|
|
|
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
|
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
|
|
|
|
2012-02-05 20:29:51 +00:00
|
|
|
$srcPath = $image->getLocalRefPath();
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $path string
|
|
|
|
* @return PdfImage
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @return bool
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $path string
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
function getImageSize( $image, $path ) {
|
|
|
|
return $this->getPdfImage( $image, $path )->getImageSize();
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $ext
|
|
|
|
* @param $mime string
|
|
|
|
* @param $params null
|
|
|
|
* @return array
|
|
|
|
*/
|
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 );
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $path string
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $metadata string
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
function isMetadataValid( $image, $metadata ) {
|
2012-07-11 01:42:28 +00:00
|
|
|
if ( !$metadata || $metadata === serialize(array()) ) {
|
|
|
|
return self::METADATA_BAD;
|
|
|
|
} elseif ( strpos( $metadata, 'mergedMetadata' ) === false ) {
|
|
|
|
return self::METADATA_COMPATIBLE;
|
|
|
|
}
|
|
|
|
return self::METADATA_GOOD;
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @return bool|int
|
|
|
|
*/
|
2012-07-11 01:42:28 +00:00
|
|
|
function formatMetadata( $image ) {
|
|
|
|
$meta = $image->getMetadata();
|
|
|
|
|
|
|
|
if ( !$meta ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wfSuppressWarnings();
|
|
|
|
$meta = unserialize( $meta );
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
if ( !isset( $meta['mergedMetadata'] )
|
|
|
|
|| !is_array( $meta['mergedMetadata'] )
|
|
|
|
|| count( $meta['mergedMetadata'] ) < 1
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inherited from MediaHandler.
|
|
|
|
return $this->formatMetadataHelper( $meta['mergedMetadata'] );
|
|
|
|
}
|
|
|
|
|
2012-10-04 16:34:25 +00:00
|
|
|
/**
|
|
|
|
* @param $image
|
|
|
|
* @return bool|int
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $page int
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
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
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @param $image File
|
|
|
|
* @param $page int
|
|
|
|
* @return bool
|
|
|
|
*/
|
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 );
|
2013-01-31 10:50:00 +00:00
|
|
|
if ( !$data || !isset( $data['text'] ) || !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
|
|
|
}
|