2007-09-06 06:32:08 +00:00
|
|
|
<?php
|
2019-07-03 13:31:43 +00:00
|
|
|
|
2021-02-11 02:10:30 +00:00
|
|
|
namespace MediaWiki\Extension\PdfHandler;
|
|
|
|
|
|
|
|
use File;
|
|
|
|
use IContextSource;
|
|
|
|
use ImageHandler;
|
|
|
|
use MediaTransformError;
|
|
|
|
use MediaTransformOutput;
|
2019-07-03 13:31:43 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-02-11 02:10:30 +00:00
|
|
|
use PoolCounterWorkViaCallback;
|
|
|
|
use ResourceLoader;
|
|
|
|
use ThumbnailImage;
|
|
|
|
use TransformParameterError;
|
2019-07-03 13:31:43 +00:00
|
|
|
|
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 {
|
2021-02-11 02:14:30 +00:00
|
|
|
private const 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
|
|
|
|
2021-02-11 02:14:30 +00:00
|
|
|
/**
|
|
|
|
* 10MB is considered a large file
|
|
|
|
*/
|
|
|
|
private const LARGE_FILE = 1e7;
|
|
|
|
|
2021-06-07 03:47:53 +00:00
|
|
|
/**
|
|
|
|
* Key for getHandlerState for value of type PdfImage
|
|
|
|
*/
|
|
|
|
private const STATE_PDF_IMAGE = 'pdfImage';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Key for getHandlerState for dimension info
|
|
|
|
*/
|
|
|
|
private const STATE_DIMENSION_INFO = 'pdfDimensionInfo';
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function mustRender( $file ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function isMultiPage( $file ) {
|
2010-10-12 13:48:00 +00:00
|
|
|
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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function makeParamString( $params ) {
|
2019-03-17 05:17:42 +00:00
|
|
|
$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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function parseParamString( $str ) {
|
2018-02-25 01:57:17 +00:00
|
|
|
$m = [];
|
2007-09-06 06:32:08 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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
|
|
|
|
2020-11-20 12:05:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset
|
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 ) {
|
2018-02-24 22:24:04 +00:00
|
|
|
return new ThumbnailImage( $image, $dstUrl, false, [
|
|
|
|
'width' => $width,
|
|
|
|
'height' => $height,
|
|
|
|
'page' => $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.
|
2021-02-11 02:14:30 +00:00
|
|
|
if ( $image->getSize() >= self::LARGE_FILE ) {
|
2014-04-11 23:07:06 +00:00
|
|
|
$work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
|
2017-05-20 20:58:19 +00:00
|
|
|
[
|
2021-05-12 14:10:36 +00:00
|
|
|
'doWork' => static 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();
|
|
|
|
}
|
|
|
|
|
2021-02-11 02:14:30 +00:00
|
|
|
if ( $srcPath === false ) {
|
|
|
|
// could not download original
|
2014-03-15 17:50:37 +00:00
|
|
|
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=-",
|
2019-10-22 14:32:17 +00:00
|
|
|
"-sstdout=%stderr",
|
2014-01-27 20:57:33 +00:00
|
|
|
"-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 {
|
2018-02-24 22:24:04 +00:00
|
|
|
return new ThumbnailImage( $image, $dstUrl, $dstPath, [
|
|
|
|
'width' => $width,
|
|
|
|
'height' => $height,
|
|
|
|
'page' => $page,
|
|
|
|
] );
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-26 22:29:49 +00:00
|
|
|
/**
|
2021-06-07 03:47:53 +00:00
|
|
|
* @param \MediaHandlerState $state
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param string $path
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return PdfImage
|
|
|
|
*/
|
2021-06-07 03:47:53 +00:00
|
|
|
private function getPdfImage( $state, $path ) {
|
|
|
|
$pdfimg = $state->getHandlerState( self::STATE_PDF_IMAGE );
|
|
|
|
if ( !$pdfimg ) {
|
2007-09-06 06:32:08 +00:00
|
|
|
$pdfimg = new PdfImage( $path );
|
2021-06-07 03:47:53 +00:00
|
|
|
$state->setHandlerState( self::STATE_PDF_IMAGE, $pdfimg );
|
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
|
|
|
/**
|
2021-06-07 03:47:53 +00:00
|
|
|
* @param \MediaHandlerState $state
|
2017-10-14 13:47:04 +00:00
|
|
|
* @param string $path
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return array|bool
|
|
|
|
*/
|
2021-06-07 03:47:53 +00:00
|
|
|
public function getSizeAndMetadata( $state, $path ) {
|
|
|
|
$metadata = $this->getPdfImage( $state, $path )->retrieveMetaData();
|
|
|
|
$sizes = PdfImage::getPageSize( $metadata, 1 );
|
|
|
|
if ( $sizes ) {
|
|
|
|
return $sizes + [ 'metadata' => $metadata ];
|
|
|
|
} else {
|
|
|
|
return [ 'metadata' => $metadata ];
|
|
|
|
}
|
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 $ext
|
|
|
|
* @param string $mime
|
|
|
|
* @param null $params
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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 ) ) {
|
2021-02-11 02:10:30 +00:00
|
|
|
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
|
2007-09-06 06:32:08 +00:00
|
|
|
$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
|
|
|
/**
|
2021-06-07 03:47:53 +00:00
|
|
|
* @param File $file
|
2012-07-26 22:29:49 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-07 03:47:53 +00:00
|
|
|
public function isFileMetadataValid( $file ) {
|
|
|
|
$data = $file->getMetadataItems( [ 'mergedMetadata', 'pages' ] );
|
|
|
|
if ( !isset( $data['pages'] ) ) {
|
2012-07-11 01:42:28 +00:00
|
|
|
return self::METADATA_BAD;
|
2021-06-07 03:47:53 +00:00
|
|
|
} elseif ( !isset( $data['mergedMetadata'] ) ) {
|
2012-07-11 01:42:28 +00:00
|
|
|
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
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function formatMetadata( $image, $context = false ) {
|
2021-06-07 03:47:53 +00:00
|
|
|
$mergedMetadata = $image->getMetadataItem( 'mergedMetadata' );
|
2012-07-11 01:42:28 +00:00
|
|
|
|
2021-06-07 03:47:53 +00:00
|
|
|
if ( !is_array( $mergedMetadata ) || !count( $mergedMetadata ) ) {
|
2012-07-11 01:42:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inherited from MediaHandler.
|
2021-06-07 03:47:53 +00:00
|
|
|
return $this->formatMetadataHelper( $mergedMetadata, $context );
|
2012-07-11 01:42:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 18:26:58 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
protected function formatTag( string $key, $vals, $context = false ) {
|
|
|
|
switch ( $key ) {
|
|
|
|
case 'pdf-Producer':
|
|
|
|
case 'pdf-Version':
|
|
|
|
return htmlspecialchars( $vals );
|
|
|
|
case 'pdf-PageSize':
|
|
|
|
foreach ( $vals as &$val ) {
|
|
|
|
$val = htmlspecialchars( $val );
|
|
|
|
}
|
|
|
|
return $vals;
|
|
|
|
case 'pdf-Encrypted':
|
|
|
|
// @todo: The value isn't i18n-ised; should be done here.
|
|
|
|
// For reference, if encrypted this field's value looks like:
|
|
|
|
// "yes (print:yes copy:no change:no addNotes:no)"
|
|
|
|
return htmlspecialchars( $vals );
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Use default formatting
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public 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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function getPageDimensions( File $image, $page ) {
|
2021-02-11 02:14:30 +00:00
|
|
|
// MW starts pages at 1, as they are stored here
|
|
|
|
$index = $page;
|
2016-10-05 04:51:57 +00:00
|
|
|
|
|
|
|
$info = $this->getDimensionInfo( $image );
|
|
|
|
if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
|
|
|
|
return $info['dimensionsByPage'][$index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDimensionInfo( File $file ) {
|
2021-06-07 03:47:53 +00:00
|
|
|
$info = $file->getHandlerState( self::STATE_DIMENSION_INFO );
|
|
|
|
if ( !$info ) {
|
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
|
|
|
$info = $cache->getWithSetCallback(
|
|
|
|
$cache->makeKey( 'file-pdf', 'dimensions', $file->getSha1() ),
|
|
|
|
$cache::TTL_INDEFINITE,
|
|
|
|
static function () use ( $file ) {
|
|
|
|
$data = $file->getMetadataItems( PdfImage::ITEMS_FOR_PAGE_SIZE );
|
|
|
|
if ( !$data || !isset( $data['Pages'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-11 02:14:30 +00:00
|
|
|
|
2021-06-07 03:47:53 +00:00
|
|
|
$dimsByPage = [];
|
|
|
|
$count = intval( $data['Pages'] );
|
|
|
|
for ( $i = 1; $i <= $count; $i++ ) {
|
|
|
|
$dimsByPage[$i] = PdfImage::getPageSize( $data, $i );
|
|
|
|
}
|
2016-10-05 04:51:57 +00:00
|
|
|
|
2021-06-07 03:47:53 +00:00
|
|
|
return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
|
2016-10-05 04:51:57 +00:00
|
|
|
}
|
2021-06-07 03:47:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$file->setHandlerState( self::STATE_DIMENSION_INFO, $info );
|
|
|
|
return $info;
|
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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function getPageText( File $image, $page ) {
|
2021-06-07 03:47:53 +00:00
|
|
|
$pageTexts = $image->getMetadataItem( 'text' );
|
|
|
|
if ( !is_array( $pageTexts ) || !isset( $pageTexts[$page - 1] ) ) {
|
2009-09-16 13:50:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-07 03:47:53 +00:00
|
|
|
return $pageTexts[$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
|
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public function getWarningConfig( $file ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
return [
|
2021-02-11 02:14:30 +00:00
|
|
|
'messages' => self::MESSAGES,
|
2015-03-05 18:52:37 +00:00
|
|
|
'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
|
|
|
*/
|
2018-11-01 20:44:19 +00:00
|
|
|
public static function registerWarningModule( &$resourceLoader ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
$resourceLoader->register( 'pdfhandler.messages', [
|
2021-02-11 02:14:30 +00:00
|
|
|
'messages' => array_values( self::MESSAGES ),
|
2017-05-20 20:58:19 +00:00
|
|
|
] );
|
2015-03-05 18:52:37 +00:00
|
|
|
}
|
2021-06-07 03:47:53 +00:00
|
|
|
|
|
|
|
public function useSplitMetadata() {
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-06 06:32:08 +00:00
|
|
|
}
|