35 private const EXPENSIVE_SIZE_LIMIT = 10_485_760;
38 private const STATE_DJVU_IMAGE =
'djvuImage';
39 private const STATE_TEXT_TREE =
'djvuTextTree';
40 private const STATE_META_TREE =
'djvuMetaTree';
41 private const CACHE_VERSION =
'v2';
47 $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer );
48 $djvuDump = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuDump );
49 if ( !$djvuRenderer || !$djvuDump ) {
50 wfDebug(
"DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" );
71 return $file->
getSize() > static::EXPENSIVE_SIZE_LIMIT;
87 'img_width' =>
'width',
98 if ( $name ===
'page' && trim( $value ) !== (
string)intval( $value ) ) {
103 return in_array( $name, [
'width',
'height',
'page' ] ) && $value > 0;
112 if ( !isset(
$params[
'width'] ) ) {
116 return "page{$page}-{$params['width']}px";
125 if ( preg_match(
'/^page(\d+)-(\d+)px$/', $str, $m ) ) {
126 return [
'width' => $m[2],
'page' => $m[1] ];
151 $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer );
152 $djvuPostProcessor = MediaWikiServices::getInstance()->getMainConfig()
153 ->get( MainConfigNames::DjvuPostProcessor );
161 if ( $flags & self::TRANSFORM_LATER ) {
183 if ( $image->getSize() >= 1e7 ) {
186 'doWork' =>
static function () use ( $image ) {
187 return $image->getLocalRefPath();
193 $srcPath = $image->getLocalRefPath();
196 if ( $srcPath ===
false ) {
198 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
207 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
208 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
209 $cmd =
'(' . Shell::escape(
213 "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
215 if ( $djvuPostProcessor ) {
216 $cmd .=
" | {$djvuPostProcessor}";
218 $cmd .=
' > ' . Shell::escape( $dstPath ) .
') 2>&1';
219 wfDebug( __METHOD__ .
": $cmd" );
224 if ( $retval !== 0 || $removed ) {
245 private function getDjVuImage( $state,
$path ) {
246 $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
249 $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
261 private function getMetadataInternal(
File $file, $gettext ) {
262 $itemNames = [
'error',
'_error',
'data' ];
264 $itemNames[] =
'text';
268 if ( isset( $unser[
'error'] ) ) {
271 if ( isset( $unser[
'_error'] ) ) {
284 if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) {
285 return $image->getHandlerState( self::STATE_TEXT_TREE );
287 if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) {
288 return $image->getHandlerState( self::STATE_META_TREE );
291 $metadata = $this->getMetadataInternal( $image, $gettext );
297 unset( $metadata[
'text'] );
303 $djvuOutputExtension = MediaWikiServices::getInstance()->getMainConfig()
304 ->get( MainConfigNames::DjvuOutputExtension );
306 if ( !isset( $mime ) ) {
307 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
308 $mime = $magic->getMimeTypeFromExtensionOrNull( $djvuOutputExtension );
311 return [ $djvuOutputExtension, $mime ];
315 wfDebug(
"Getting DjVu metadata for $path" );
317 $djvuImage = $this->getDjVuImage( $state,
$path );
318 $metadata = $djvuImage->retrieveMetaData();
319 if ( $metadata ===
false ) {
321 $metadata = [
'error' =>
'Error extracting metadata' ];
323 return [
'metadata' => $metadata ] + $djvuImage->getImageSize();
338 return $info ? $info[
'pageCount'] :
false;
345 if ( $info && isset( $info[
'dimensionsByPage'][$index] ) ) {
346 return $info[
'dimensionsByPage'][$index];
353 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
354 return $cache->getWithSetCallback(
355 $cache->makeKey(
'file-djvu',
'dimensions', self::CACHE_VERSION, $file->
getSha1() ),
356 $cache::TTL_INDEFINITE,
357 function () use ( $file ) {
361 [
'pcTTL' => $cache::TTL_INDEFINITE ]
376 if ( !isset( $metatree[
'data'] ) || !$metatree[
'data'] ) {
379 foreach ( $metatree[
'data'][
'pages'] as $page ) {
381 $dimsByPage[] =
false;
384 'width' => (int)$page[
'width'],
385 'height' => (
int)$page[
'height'],
390 'pageCount' => count( $metatree[
'data'][
'pages'] ),
391 'dimensionsByPage' => $dimsByPage
405 if ( isset( $tree[
'text'] ) && isset( $tree[
'text'][$page - 1] ) ) {
406 return $tree[
'text'][$page - 1];
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
wfHostname()
Get host name of the current machine, for use in error reporting.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
getDimensionInfoFromMetaTree( $metatree)
Given the metadata, returns dimension information about the document.
makeParamString( $params)
isExpensiveToThumbnail( $file)
True if creating thumbnails from the file is large or otherwise resource-intensive.
validateParam( $name, $value)
getDimensionInfo(File $file)
getScriptParams( $params)
doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)
pageCount(File $image)
Page count for a multi-page document, false if unsupported or unknown.
getPageText(File $image, $page)
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.
getPageDimensions(File $image, $page)
Get an associative array of page dimensions Currently "width" and "height" are understood,...
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getMetaTree( $image, $gettext=false)
Cache a document tree for the DjVu metadata.
getThumbType( $ext, $mime, $params=null)
Get the thumbnail extension and MIME type for a given source MIME type.
useSplitMetadata()
If this returns true, LocalFile may split metadata up and store its constituent items separately.
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Implements some public methods and some protected utility functions which are required by multiple ch...
getSize()
Return the size of the image file, in bytes Overridden by LocalFile, UnregisteredLocalFile STUB.
getMetadataItems(array $itemNames)
Get multiple elements of the unserialized handler-specific metadata.
getSha1()
Get the SHA-1 base 36 hash of the file.
Media handler abstract base class for images.
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
A class containing constants representing the names of configuration variables.
Media transform output for images.