* @param File $file
* @return bool
*/
- function mustRender( $file ) {
+ public function mustRender( $file ) {
return true;
}
* @param File $file
* @return bool
*/
- function isMultiPage( $file ) {
+ public function isMultiPage( $file ) {
return true;
}
/**
* @return array
*/
- function getParamMap() {
- return array(
+ public function getParamMap() {
+ return [
'img_width' => 'width',
'img_page' => 'page',
- );
+ ];
}
/**
* @param mixed $value
* @return bool
*/
- function validateParam( $name, $value ) {
+ public function validateParam( $name, $value ) {
if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
// Extra junk on the end of page, probably actually a caption
// e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
return false;
}
- if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
+ if ( in_array( $name, [ 'width', 'height', 'page' ] ) ) {
if ( $value <= 0 ) {
return false;
} else {
* @param array $params
* @return bool|string
*/
- function makeParamString( $params ) {
+ public function makeParamString( $params ) {
$page = isset( $params['page'] ) ? $params['page'] : 1;
if ( !isset( $params['width'] ) ) {
return false;
* @param string $str
* @return array|bool
*/
- function parseParamString( $str ) {
+ public function parseParamString( $str ) {
$m = false;
if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
- return array( 'width' => $m[2], 'page' => $m[1] );
+ return [ 'width' => $m[2], 'page' => $m[1] ];
} else {
return false;
}
* @return array
*/
function getScriptParams( $params ) {
- return array(
+ return [
'width' => $params['width'],
'page' => $params['page'],
- );
+ ];
}
/**
$page = $params['page'];
if ( $flags & self::TRANSFORM_LATER ) {
- $params = array(
+ $params = [
'width' => $width,
'height' => $height,
'page' => $page
- );
+ ];
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
}
// Provide a way to pool count limit the number of downloaders.
if ( $image->getSize() >= 1e7 ) { // 10MB
$work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
- array(
+ [
'doWork' => function () use ( $image ) {
return $image->getLocalRefPath();
}
- )
+ ]
);
$srcPath = $work->execute();
} else {
$this->logErrorForExternalProcess( $retval, $err, $cmd );
return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
} else {
- $params = array(
+ $params = [
'width' => $width,
'height' => $height,
'page' => $page
- );
+ ];
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
}
return $metadata;
}
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
$unser = unserialize( $metadata );
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( is_array( $unser ) ) {
if ( isset( $unser['error'] ) ) {
return false;
* @param bool $gettext DOCUMENT (Default: false)
* @return bool|SimpleXMLElement
*/
- function getMetaTree( $image, $gettext = false ) {
+ public function getMetaTree( $image, $gettext = false ) {
if ( $gettext && isset( $image->djvuTextTree ) ) {
return $image->djvuTextTree;
}
return false;
}
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
try {
// Set to false rather than null to avoid further attempts
$image->dejaMetaTree = false;
$image->djvuTextTree = false;
- $tree = new SimpleXMLElement( $metadata );
+ $tree = new SimpleXMLElement( $metadata, LIBXML_PARSEHUGE );
if ( $tree->getName() == 'mw-djvu' ) {
/** @var SimpleXMLElement $b */
foreach ( $tree->children() as $b ) {
} catch ( Exception $e ) {
wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
}
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( $gettext ) {
return $image->djvuTextTree;
} else {
$mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
}
- return array( $wgDjvuOutputExtension, $mime );
+ return [ $wgDjvuOutputExtension, $mime ];
}
function getMetadata( $image, $path ) {
$xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
if ( $xml === false ) {
// Special value so that we don't repetitively try and decode a broken file.
- return serialize( array( 'error' => 'Error extracting metadata' ) );
+ return serialize( [ 'error' => 'Error extracting metadata' ] );
} else {
- return serialize( array( 'xml' => $xml ) );
+ return serialize( [ 'xml' => $xml ] );
}
}
}
function isMetadataValid( $image, $metadata ) {
- return !empty( $metadata ) && $metadata != serialize( array() );
+ return !empty( $metadata ) && $metadata != serialize( [] );
}
- function pageCount( $image ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
+ function pageCount( File $image ) {
+ $info = $this->getDimensionInfo( $image );
- return count( $tree->xpath( '//OBJECT' ) );
+ return $info ? $info['pageCount'] : false;
}
- function getPageDimensions( $image, $page ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
+ function getPageDimensions( File $image, $page ) {
+ $index = $page - 1; // MW starts pages at 1
- $o = $tree->BODY[0]->OBJECT[$page - 1];
- if ( $o ) {
- return array(
- 'width' => intval( $o['width'] ),
- 'height' => intval( $o['height'] )
- );
- } else {
- return false;
+ $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-djvu', 'dimensions', $file->getSha1() ),
+ $cache::TTL_INDEFINITE,
+ function () use ( $file ) {
+ $tree = $this->getMetaTree( $file );
+ if ( !$tree ) {
+ return false;
+ }
+
+ $dimsByPage = [];
+ $count = count( $tree->xpath( '//OBJECT' ) );
+ for ( $i = 0; $i < $count; $i++ ) {
+ $o = $tree->BODY[0]->OBJECT[$i];
+ if ( $o ) {
+ $dimsByPage[$i] = [
+ 'width' => (int)$o['width'],
+ 'height' => (int)$o['height'],
+ ];
+ } else {
+ $dimsByPage[$i] = false;
+ }
+ }
+
+ return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
+ },
+ [ 'pcTTL' => $cache::TTL_INDEFINITE ]
+ );
}
/**
* @param int $page Page number to get information for
* @return bool|string Page text or false when no text found.
*/
- function getPageText( $image, $page ) {
+ function getPageText( File $image, $page ) {
$tree = $this->getMetaTree( $image, true );
if ( !$tree ) {
return false;