* @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 );
}
* @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;
}
// 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 ) {
$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 ) {
- global $wgMemc;
+ function pageCount( File $image ) {
+ $info = $this->getDimensionInfo( $image );
- $key = wfMemcKey( 'file-djvu', 'pageCount', $image->getSha1() );
+ return $info ? $info['pageCount'] : false;
+ }
- $count = $wgMemc->get( $key );
- if ( $count === false ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
- $count = count( $tree->xpath( '//OBJECT' ) );
- $wgMemc->set( $key, $count );
+ function getPageDimensions( File $image, $page ) {
+ $index = $page - 1; // MW starts pages at 1
+
+ $info = $this->getDimensionInfo( $image );
+ if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
+ return $info['dimensionsByPage'][$index];
}
- return $count;
+ return false;
}
- function getPageDimensions( $image, $page ) {
- global $wgMemc;
+ protected function getDimensionInfo( File $file ) {
+ $that = $this;
- $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() );
-
- $dimsByPage = $wgMemc->get( $key );
- if ( !is_array( $dimsByPage ) ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
-
- $dimsByPage = array();
- $count = count( $tree->xpath( '//OBJECT' ) );
- for ( $i = 0; $i < $count; ++$i ) {
- $o = $tree->BODY[0]->OBJECT[$i];
- if ( $o ) {
- $dimsByPage[$i] = array(
- 'width' => (int)$o['width'],
- 'height' => (int)$o['height']
- );
- } else {
- $dimsByPage[$i] = false;
+ return ObjectCache::getMainWANInstance()->getWithSetCallback(
+ wfMemcKey( 'file-djvu', 'dimensions', $file->getSha1() ),
+ WANObjectCache::TTL_INDEFINITE,
+ function () use ( $that, $file ) {
+ $tree = $that->getMetaTree( $file );
+ if ( !$tree ) {
+ return false;
}
- }
-
- $wgMemc->set( $key, $dimsByPage );
- }
- $index = $page - 1; // MW starts pages at 1
+ $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 isset( $dimsByPage[$index] ) ? $dimsByPage[$index] : false;
+ return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
+ }
+ );
}
/**
* @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;