Add page dimension caching and avoid metadata tree loading use in doTransform()

Bug: T147296
Change-Id: Ic27f0797317f3467305f953ca6b7ae729a566041
This commit is contained in:
Aaron Schulz 2016-10-04 21:51:57 -07:00
parent 62b516aae6
commit fc5ca3c58c

View file

@ -146,16 +146,6 @@ class PdfHandler extends ImageHandler {
function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
global $wgPdfProcessor, $wgPdfPostProcessor, $wgPdfHandlerDpi, $wgPdfHandlerJpegQuality;
$metadata = $image->getMetadata();
if ( !$metadata ) {
return $this->doThumbError(
isset( $params['width'] ) ? $params['width'] : null,
isset( $params['height'] ) ? $params['height'] : null,
'pdf_no_metadata'
);
}
if ( !$this->normaliseParams( $image, $params ) ) {
return new TransformParameterError( $params );
}
@ -221,11 +211,9 @@ class PdfHandler extends ImageHandler {
);
$cmd .= ")";
wfProfileIn( 'PdfHandler' );
wfDebug( __METHOD__ . ": $cmd\n" );
$retval = '';
$err = wfShellExecWithStderr( $cmd, $retval );
wfProfileOut( 'PdfHandler' );
$removed = $this->removeBadFile( $dstPath, $retval );
@ -336,7 +324,7 @@ class PdfHandler extends ImageHandler {
/**
* @param $image File
* @param bool|IContextSource $context Context to use (optional)
* @return bool|int
* @return bool|array
*/
function formatMetadata( $image, $context = false ) {
$meta = $image->getMetadata();
@ -364,11 +352,9 @@ class PdfHandler extends ImageHandler {
* @return bool|int
*/
function pageCount( File $image ) {
$data = $this->getMetaArray( $image );
if ( !$data || !isset( $data['Pages'] ) ) {
return false;
}
return intval( $data['Pages'] );
$info = $this->getDimensionInfo( $image );
return $info ? $info['pageCount'] : false;
}
/**
@ -377,8 +363,38 @@ class PdfHandler extends ImageHandler {
* @return array|bool
*/
function getPageDimensions( File $image, $page ) {
$data = $this->getMetaArray( $image );
return PdfImage::getPageSize( $data, $page );
$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 );
if ( !$data || !isset( $data['Pages'] ) ) {
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 ]
);
}
/**
@ -387,7 +403,7 @@ class PdfHandler extends ImageHandler {
* @return bool
*/
function getPageText( File $image, $page ) {
$data = $this->getMetaArray( $image, true );
$data = $this->getMetaArray( $image );
if ( !$data || !isset( $data['text'] ) || !isset( $data['text'][$page - 1] ) ) {
return false;
}