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 {
|
2017-05-20 20:58:19 +00:00
|
|
|
public static $messages = [
|
2015-03-05 18:52:37 +00:00
|
|
|
'main' => 'pdf-file-page-warning',
|
|
|
|
'header' => 'pdf-file-page-warning-header',
|
|
|
|
'info' => 'pdf-file-page-warning-info',
|
|
|
|
'footer' => 'pdf-file-page-warning-footer',
|
2017-05-20 20:58:19 +00:00
|
|
|
];
|
2007-09-06 06:32:08 +00:00
|
|
|
|
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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $file
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $file
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
function validateParam( $name, $value ) {
|
2017-06-20 07:18:50 +00:00
|
|
|
if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
|
2014-05-15 00:32:10 +00:00
|
|
|
// Extra junk on the end of page, probably actually a caption
|
|
|
|
// e.g. [[File:Foo.pdf|thumb|Page 3 of the document shows foo]]
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-20 20:58:19 +00:00
|
|
|
if ( in_array( $name, [ '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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param array $params
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param string $str
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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 ) ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
return [ '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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param array $params
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
function getScriptParams( $params ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
return [
|
2008-02-04 06:40:50 +00:00
|
|
|
'width' => $params['width'],
|
|
|
|
'page' => $params['page'],
|
2017-05-20 20:58:19 +00:00
|
|
|
];
|
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() {
|
2017-05-20 20:58:19 +00:00
|
|
|
return [
|
2008-02-04 06:40:50 +00:00
|
|
|
'img_width' => 'width',
|
|
|
|
'img_page' => 'page',
|
2017-05-20 20:58:19 +00:00
|
|
|
];
|
2008-02-04 06:40:50 +00:00
|
|
|
}
|
2010-10-12 13:48:00 +00:00
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @param string $msg
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param string $dstPath
|
|
|
|
* @param string $dstUrl
|
|
|
|
* @param array $params
|
|
|
|
* @param int $flags
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return MediaTransformError|MediaTransformOutput|ThumbnailImage|TransformParameterError
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
|
2014-05-13 19:45:15 +00:00
|
|
|
global $wgPdfProcessor, $wgPdfPostProcessor, $wgPdfHandlerDpi, $wgPdfHandlerJpegQuality;
|
2007-09-06 06:32:08 +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
|
|
|
|
2016-06-13 08:56:31 +00:00
|
|
|
$width = (int)$params['width'];
|
|
|
|
$height = (int)$params['height'];
|
|
|
|
$page = (int)$params['page'];
|
2007-09-06 06:32:08 +00:00
|
|
|
|
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
|
|
|
|
2014-03-15 17:50:37 +00:00
|
|
|
// Thumbnail extraction is very inefficient for large files.
|
|
|
|
// Provide a way to pool count limit the number of downloaders.
|
|
|
|
if ( $image->getSize() >= 1e7 ) { // 10MB
|
2014-04-11 23:07:06 +00:00
|
|
|
$work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
|
2017-05-20 20:58:19 +00:00
|
|
|
[
|
2017-06-20 07:18:50 +00:00
|
|
|
'doWork' => function () use ( $image ) {
|
2014-03-15 17:50:37 +00:00
|
|
|
return $image->getLocalRefPath();
|
|
|
|
}
|
2017-05-20 20:58:19 +00:00
|
|
|
]
|
2014-03-15 17:50:37 +00:00
|
|
|
);
|
|
|
|
$srcPath = $work->execute();
|
|
|
|
} else {
|
|
|
|
$srcPath = $image->getLocalRefPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $srcPath === false ) { // could not download original
|
|
|
|
return $this->doThumbError( $width, $height, 'filemissing' );
|
|
|
|
}
|
2012-02-05 20:29:51 +00:00
|
|
|
|
2014-01-27 20:57:33 +00:00
|
|
|
$cmd = '(' . wfEscapeShellArg(
|
|
|
|
$wgPdfProcessor,
|
|
|
|
"-sDEVICE=jpeg",
|
|
|
|
"-sOutputFile=-",
|
|
|
|
"-dFirstPage={$page}",
|
|
|
|
"-dLastPage={$page}",
|
2016-06-13 08:52:21 +00:00
|
|
|
"-dSAFER",
|
2014-01-27 20:57:33 +00:00
|
|
|
"-r{$wgPdfHandlerDpi}",
|
|
|
|
"-dBATCH",
|
|
|
|
"-dNOPAUSE",
|
|
|
|
"-q",
|
|
|
|
$srcPath
|
|
|
|
);
|
|
|
|
$cmd .= " | " . wfEscapeShellArg(
|
|
|
|
$wgPdfPostProcessor,
|
|
|
|
"-depth",
|
|
|
|
"8",
|
2014-05-13 19:45:15 +00:00
|
|
|
"-quality",
|
|
|
|
$wgPdfHandlerJpegQuality,
|
2014-01-27 20:57:33 +00:00
|
|
|
"-resize",
|
|
|
|
$width,
|
|
|
|
"-",
|
|
|
|
$dstPath
|
|
|
|
);
|
|
|
|
$cmd .= ")";
|
2007-09-06 06:32:08 +00:00
|
|
|
|
2010-10-12 13:48:00 +00:00
|
|
|
wfDebug( __METHOD__ . ": $cmd\n" );
|
2011-01-23 10:33:37 +00:00
|
|
|
$retval = '';
|
2014-01-13 15:14:13 +00:00
|
|
|
$err = wfShellExecWithStderr( $cmd, $retval );
|
2007-09-06 06:32:08 +00:00
|
|
|
|
|
|
|
$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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param string $path
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
}
|
|
|
|
|
2017-05-20 20:58:19 +00:00
|
|
|
$work = new PoolCounterWorkViaCallback(
|
|
|
|
'PdfHandler-unserialize-metadata',
|
|
|
|
$image->getName(),
|
|
|
|
[
|
2017-06-20 07:18:50 +00:00
|
|
|
'doWork' => function () use ( $image, $metadata ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
$image->pdfMetaArray = unserialize( $metadata );
|
|
|
|
wfRestoreWarnings();
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
2015-04-24 20:17:12 +00:00
|
|
|
$work->execute();
|
2007-09-06 06:32:08 +00:00
|
|
|
|
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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param string $path
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param string $ext
|
|
|
|
* @param string $mime
|
|
|
|
* @param null $params
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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 );
|
|
|
|
}
|
2017-05-20 20:58:19 +00:00
|
|
|
return [ $wgPdfOutputExtension, $mime ];
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param string $path
|
2012-07-26 22:29:49 +00:00
|
|
|
* @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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param string $metadata
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2007-09-06 06:32:08 +00:00
|
|
|
function isMetadataValid( $image, $metadata ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
if ( !$metadata || $metadata === serialize( [] ) ) {
|
2012-07-11 01:42:28 +00:00
|
|
|
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
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
2015-03-17 17:38:28 +00:00
|
|
|
* @param bool|IContextSource $context Context to use (optional)
|
2016-10-05 04:51:57 +00:00
|
|
|
* @return bool|array
|
2012-07-26 22:29:49 +00:00
|
|
|
*/
|
2015-03-17 17:38:28 +00:00
|
|
|
function formatMetadata( $image, $context = false ) {
|
2012-07-11 01:42:28 +00:00
|
|
|
$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.
|
2015-03-18 19:04:42 +00:00
|
|
|
return $this->formatMetadataHelper( $meta['mergedMetadata'], $context );
|
2012-07-11 01:42:28 +00:00
|
|
|
}
|
|
|
|
|
2012-10-04 16:34:25 +00:00
|
|
|
/**
|
2015-10-27 19:05:54 +00:00
|
|
|
* @param File $image
|
2012-10-04 16:34:25 +00:00
|
|
|
* @return bool|int
|
|
|
|
*/
|
2015-10-27 19:05:54 +00:00
|
|
|
function pageCount( File $image ) {
|
2016-10-05 04:51:57 +00:00
|
|
|
$info = $this->getDimensionInfo( $image );
|
|
|
|
|
|
|
|
return $info ? $info['pageCount'] : false;
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param int $page
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return array|bool
|
|
|
|
*/
|
2015-10-27 19:39:11 +00:00
|
|
|
function getPageDimensions( File $image, $page ) {
|
2016-10-05 04:51:57 +00:00
|
|
|
$index = $page; // MW starts pages at 1, as they are stored here
|
|
|
|
|
|
|
|
$info = $this->getDimensionInfo( $image );
|
|
|
|
if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
|
|
|
|
return $info['dimensionsByPage'][$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDimensionInfo( File $file ) {
|
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
|
|
|
return $cache->getWithSetCallback(
|
|
|
|
$cache->makeKey( 'file-pdf', 'dimensions', $file->getSha1() ),
|
|
|
|
$cache::TTL_INDEFINITE,
|
|
|
|
function () use ( $file ) {
|
|
|
|
$data = $this->getMetaArray( $file );
|
2017-05-20 20:58:19 +00:00
|
|
|
if ( !$data || !isset( $data['Pages'] ) ) {
|
2016-10-05 04:51:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unset( $data['text'] ); // lower peak RAM
|
|
|
|
|
|
|
|
$dimsByPage = [];
|
|
|
|
$count = intval( $data['Pages'] );
|
|
|
|
for ( $i = 1; $i <= $count; $i++ ) {
|
|
|
|
$dimsByPage[$i] = PdfImage::getPageSize( $data, $i );
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
|
|
|
|
},
|
|
|
|
[ 'pcTTL' => $cache::TTL_INDEFINITE ]
|
|
|
|
);
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
2009-09-16 13:50:09 +00:00
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param File $image
|
|
|
|
* @param int $page
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-10-27 20:07:51 +00:00
|
|
|
function getPageText( File $image, $page ) {
|
2016-10-05 04:51:57 +00:00
|
|
|
$data = $this->getMetaArray( $image );
|
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
|
|
|
}
|
|
|
|
|
2015-03-05 18:52:37 +00:00
|
|
|
/**
|
|
|
|
* Adds a warning about PDFs being potentially dangerous to the file
|
|
|
|
* page. Multiple messages with this base will be used.
|
|
|
|
* @param File $file
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getWarningConfig( $file ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
return [
|
2015-03-05 18:52:37 +00:00
|
|
|
'messages' => self::$messages,
|
|
|
|
'link' => '//www.mediawiki.org/wiki/Special:MyLanguage/Help:Security/PDF_files',
|
|
|
|
'module' => 'pdfhandler.messages',
|
2017-05-20 20:58:19 +00:00
|
|
|
];
|
2015-03-05 18:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a module with the warning messages in it.
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param ResourceLoader &$resourceLoader
|
2015-03-05 18:52:37 +00:00
|
|
|
*/
|
|
|
|
static function registerWarningModule( &$resourceLoader ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
$resourceLoader->register( 'pdfhandler.messages', [
|
2015-03-05 18:52:37 +00:00
|
|
|
'messages' => array_values( self::$messages ),
|
2017-05-20 20:58:19 +00:00
|
|
|
] );
|
2015-03-05 18:52:37 +00:00
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|