MediaWiki  master
DjVuHandler.php
Go to the documentation of this file.
1 <?php
26 
32 class DjVuHandler extends ImageHandler {
33  private const EXPENSIVE_SIZE_LIMIT = 10485760; // 10MiB
34 
35  // Constants for getHandlerState
36  private const STATE_DJVU_IMAGE = 'djvuImage';
37  private const STATE_TEXT_TREE = 'djvuTextTree';
38  private const STATE_META_TREE = 'djvuMetaTree';
39  private const CACHE_VERSION = 'v2';
40 
44  public function isEnabled() {
45  $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( 'DjvuRenderer' );
46  $djvuDump = MediaWikiServices::getInstance()->getMainConfig()->get( 'DjvuDump' );
47  if ( !$djvuRenderer || !$djvuDump ) {
48  wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" );
49 
50  return false;
51  } else {
52  return true;
53  }
54  }
55 
60  public function mustRender( $file ) {
61  return true;
62  }
63 
69  public function isExpensiveToThumbnail( $file ) {
70  return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
71  }
72 
77  public function isMultiPage( $file ) {
78  return true;
79  }
80 
84  public function getParamMap() {
85  return [
86  'img_width' => 'width',
87  'img_page' => 'page',
88  ];
89  }
90 
96  public function validateParam( $name, $value ) {
97  if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
98  // Extra junk on the end of page, probably actually a caption
99  // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
100  return false;
101  }
102  return in_array( $name, [ 'width', 'height', 'page' ] ) && $value > 0;
103  }
104 
109  public function makeParamString( $params ) {
110  $page = $params['page'] ?? 1;
111  if ( !isset( $params['width'] ) ) {
112  return false;
113  }
114 
115  return "page{$page}-{$params['width']}px";
116  }
117 
122  public function parseParamString( $str ) {
123  $m = false;
124  if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
125  return [ 'width' => $m[2], 'page' => $m[1] ];
126  } else {
127  return false;
128  }
129  }
130 
135  protected function getScriptParams( $params ) {
136  return [
137  'width' => $params['width'],
138  'page' => $params['page'],
139  ];
140  }
141 
150  public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
151  $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( 'DjvuRenderer' );
152  $djvuPostProcessor = MediaWikiServices::getInstance()->getMainConfig()->get( 'DjvuPostProcessor' );
153  if ( !$this->normaliseParams( $image, $params ) ) {
154  return new TransformParameterError( $params );
155  }
156  $width = $params['width'];
157  $height = $params['height'];
158  $page = $params['page'];
159 
160  if ( $flags & self::TRANSFORM_LATER ) {
161  $params = [
162  'width' => $width,
163  'height' => $height,
164  'page' => $page
165  ];
166 
167  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
168  }
169 
170  if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
171  return new MediaTransformError(
172  'thumbnail_error',
173  $width,
174  $height,
175  wfMessage( 'thumbnail_dest_directory' )
176  );
177  }
178 
179  // Get local copy source for shell scripts
180  // Thumbnail extraction is very inefficient for large files.
181  // Provide a way to pool count limit the number of downloaders.
182  if ( $image->getSize() >= 1e7 ) { // 10 MB
183  $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
184  [
185  'doWork' => static function () use ( $image ) {
186  return $image->getLocalRefPath();
187  }
188  ]
189  );
190  $srcPath = $work->execute();
191  } else {
192  $srcPath = $image->getLocalRefPath();
193  }
194 
195  if ( $srcPath === false ) { // Failed to get local copy
196  wfDebugLog( 'thumbnail',
197  sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
198  wfHostname(), $image->getName() ) );
199 
200  return new MediaTransformError( 'thumbnail_error',
201  $params['width'], $params['height'],
202  wfMessage( 'filemissing' )
203  );
204  }
205 
206  # Use a subshell (brackets) to aggregate stderr from both pipeline commands
207  # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
208  $cmd = '(' . Shell::escape(
209  $djvuRenderer,
210  "-format=ppm",
211  "-page={$page}",
212  "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
213  $srcPath );
214  if ( $djvuPostProcessor ) {
215  $cmd .= " | {$djvuPostProcessor}";
216  }
217  $cmd .= ' > ' . Shell::escape( $dstPath ) . ') 2>&1';
218  wfDebug( __METHOD__ . ": $cmd" );
219  $retval = '';
220  $err = wfShellExec( $cmd, $retval );
221 
222  $removed = $this->removeBadFile( $dstPath, $retval );
223  if ( $retval != 0 || $removed ) {
224  $this->logErrorForExternalProcess( $retval, $err, $cmd );
225  return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
226  } else {
227  $params = [
228  'width' => $width,
229  'height' => $height,
230  'page' => $page
231  ];
232 
233  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
234  }
235  }
236 
245  private function getDjVuImage( $state, $path ) {
246  $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
247  if ( !$deja ) {
248  $deja = new DjVuImage( $path );
249  $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
250  }
251  return $deja;
252  }
253 
262  private function getMetadataInternal( File $file, $gettext ) {
263  $itemNames = [ 'error', '_error', 'data' ];
264  if ( $gettext ) {
265  $itemNames[] = 'text';
266  }
267  $unser = $file->getMetadataItems( $itemNames );
268 
269  if ( isset( $unser['error'] ) ) {
270  return false;
271  } elseif ( isset( $unser['_error'] ) ) {
272  return false;
273  } else {
274  return $unser;
275  }
276  }
277 
284  public function getMetaTree( $image, $gettext = false ) {
285  if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) {
286  return $image->getHandlerState( self::STATE_TEXT_TREE );
287  }
288  if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) {
289  return $image->getHandlerState( self::STATE_META_TREE );
290  }
291 
292  $metadata = $this->getMetadataInternal( $image, $gettext );
293  if ( !$metadata ) {
294  return false;
295  }
296 
297  if ( $gettext ) {
298  return $metadata;
299  } else {
300  unset( $metadata['text'] );
301  return $metadata;
302  }
303  }
304 
305  public function getThumbType( $ext, $mime, $params = null ) {
306  $djvuOutputExtension = MediaWikiServices::getInstance()->getMainConfig()->get( 'DjvuOutputExtension' );
307  static $mime;
308  if ( !isset( $mime ) ) {
309  $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
310  $mime = $magic->getMimeTypeFromExtensionOrNull( $djvuOutputExtension );
311  }
312 
313  return [ $djvuOutputExtension, $mime ];
314  }
315 
316  public function getSizeAndMetadata( $state, $path ) {
317  wfDebug( "Getting DjVu metadata for $path" );
318 
319  $djvuImage = $this->getDjVuImage( $state, $path );
320  $metadata = $djvuImage->retrieveMetaData();
321  if ( $metadata === false ) {
322  // Special value so that we don't repetitively try and decode a broken file.
323  $metadata = [ 'error' => 'Error extracting metadata' ];
324  }
325  return [ 'metadata' => $metadata ] + $djvuImage->getImageSize();
326  }
327 
328  public function getMetadataType( $image ) {
329  // historical reasons
330  return 'djvuxml';
331  }
332 
333  public function isFileMetadataValid( $image ) {
334  return $image->getMetadataArray() ? self::METADATA_GOOD : self::METADATA_BAD;
335  }
336 
337  public function pageCount( File $image ) {
338  $info = $this->getDimensionInfo( $image );
339 
340  return $info ? $info['pageCount'] : false;
341  }
342 
343  public function getPageDimensions( File $image, $page ) {
344  $index = $page - 1; // MW starts pages at 1
345 
346  $info = $this->getDimensionInfo( $image );
347  if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
348  return $info['dimensionsByPage'][$index];
349  }
350 
351  return false;
352  }
353 
354  protected function getDimensionInfo( File $file ) {
355  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
356  return $cache->getWithSetCallback(
357  $cache->makeKey( 'file-djvu', 'dimensions', self::CACHE_VERSION, $file->getSha1() ),
358  $cache::TTL_INDEFINITE,
359  function () use ( $file ) {
360  $tree = $this->getMetaTree( $file );
361  return $this->getDimensionInfoFromMetaTree( $tree );
362  },
363  [ 'pcTTL' => $cache::TTL_INDEFINITE ]
364  );
365  }
366 
372  protected function getDimensionInfoFromMetaTree( $metatree ) {
373  if ( !$metatree ) {
374  return false;
375  }
376  $dimsByPage = [];
377 
378  if ( !isset( $metatree['data'] ) || !$metatree['data'] ) {
379  return false;
380  }
381  foreach ( $metatree['data']['pages'] as $page ) {
382  if ( !$page ) {
383  $dimsByPage[] = false;
384  } else {
385  $dimsByPage[] = [
386  'width' => (int)$page['width'],
387  'height' => (int)$page['height'],
388  ];
389  }
390  }
391  return [
392  'pageCount' => count( $metatree['data']['pages'] ),
393  'dimensionsByPage' => $dimsByPage
394  ];
395  }
396 
402  public function getPageText( File $image, $page ) {
403  $tree = $this->getMetaTree( $image, true );
404  if ( !$tree ) {
405  return false;
406  }
407  if ( isset( $tree['text'] ) && isset( $tree['text'][$page - 1] ) ) {
408  return $tree['text'][$page - 1];
409  }
410  return false;
411  }
412 
413  public function useSplitMetadata() {
414  return true;
415  }
416 }
MediaHandler\removeBadFile
removeBadFile( $dstPath, $retval=0)
Check for zero-sized thumbnails.
Definition: MediaHandler.php:925
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:45
MediaTransformError
Basic media transform error class.
Definition: MediaTransformError.php:31
ThumbnailImage
Media transform output for images.
Definition: ThumbnailImage.php:32
DjVuHandler\getParamMap
getParamMap()
Definition: DjVuHandler.php:84
DjVuHandler\getDimensionInfoFromMetaTree
getDimensionInfoFromMetaTree( $metatree)
Given the metadata, returns dimension information about the document.
Definition: DjVuHandler.php:372
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:1731
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
DjVuHandler\isMultiPage
isMultiPage( $file)
Definition: DjVuHandler.php:77
DjVuHandler\parseParamString
parseParamString( $str)
Definition: DjVuHandler.php:122
DjVuHandler\pageCount
pageCount(File $image)
Page count for a multi-page document, false if unsupported or unknown.
Definition: DjVuHandler.php:337
DjVuHandler\isEnabled
isEnabled()
Definition: DjVuHandler.php:44
DjVuImage
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Definition: DjVuImage.php:39
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
wfHostname
wfHostname()
Get host name of the current machine, for use in error reporting.
Definition: GlobalFunctions.php:1230
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1167
PoolCounterWorkViaCallback
Convenience class for dealing with PoolCounters using callbacks.
Definition: PoolCounterWorkViaCallback.php:31
DjVuHandler\makeParamString
makeParamString( $params)
Definition: DjVuHandler.php:109
DjVuHandler\CACHE_VERSION
const CACHE_VERSION
Definition: DjVuHandler.php:39
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:958
DjVuHandler\EXPENSIVE_SIZE_LIMIT
const EXPENSIVE_SIZE_LIMIT
Definition: DjVuHandler.php:33
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
DjVuHandler\STATE_DJVU_IMAGE
const STATE_DJVU_IMAGE
Definition: DjVuHandler.php:36
PoolCounterWork\execute
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
Definition: PoolCounterWork.php:127
DjVuHandler\getDjVuImage
getDjVuImage( $state, $path)
Cache an instance of DjVuImage in a MediaHandlerState object, return that instance.
Definition: DjVuHandler.php:245
DjVuHandler\isFileMetadataValid
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
Definition: DjVuHandler.php:333
DjVuHandler\getPageDimensions
getPageDimensions(File $image, $page)
Get an associative array of page dimensions Currently "width" and "height" are understood,...
Definition: DjVuHandler.php:343
ImageHandler
Media handler abstract base class for images.
Definition: ImageHandler.php:31
DjVuHandler\isExpensiveToThumbnail
isExpensiveToThumbnail( $file)
True if creating thumbnails from the file is large or otherwise resource-intensive.
Definition: DjVuHandler.php:69
DjVuHandler\validateParam
validateParam( $name, $value)
Definition: DjVuHandler.php:96
DjVuHandler\getMetadataInternal
getMetadataInternal(File $file, $gettext)
Get metadata, unserializing it if necessary.
Definition: DjVuHandler.php:262
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
ImageHandler\normaliseParams
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
Definition: ImageHandler.php:105
DjVuHandler\STATE_META_TREE
const STATE_META_TREE
Definition: DjVuHandler.php:38
TransformParameterError
Shortcut class for parameter validation errors.
Definition: TransformParameterError.php:30
DjVuHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: DjVuHandler.php:328
DjVuHandler\getScriptParams
getScriptParams( $params)
Definition: DjVuHandler.php:135
DjVuHandler\useSplitMetadata
useSplitMetadata()
If this returns true, LocalFile may split metadata up and store its constituent items separately.
Definition: DjVuHandler.php:413
DjVuHandler\getSizeAndMetadata
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.
Definition: DjVuHandler.php:316
DjVuHandler
Handler for DjVu images.
Definition: DjVuHandler.php:32
$cache
$cache
Definition: mcc.php:33
DjVuHandler\getThumbType
getThumbType( $ext, $mime, $params=null)
Get the thumbnail extension and MIME type for a given source MIME type.
Definition: DjVuHandler.php:305
MediaHandler\logErrorForExternalProcess
logErrorForExternalProcess( $retval, $err, $cmd)
Log an error that occurred in an external process.
Definition: MediaHandler.php:1008
DjVuHandler\doTransform
doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)
Definition: DjVuHandler.php:150
$path
$path
Definition: NoLocalSettings.php:25
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:41
DjVuHandler\getMetaTree
getMetaTree( $image, $gettext=false)
Cache a document tree for the DjVu metadata.
Definition: DjVuHandler.php:284
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
DjVuHandler\STATE_TEXT_TREE
const STATE_TEXT_TREE
Definition: DjVuHandler.php:37
$mime
$mime
Definition: router.php:60
DjVuHandler\getPageText
getPageText(File $image, $page)
Definition: DjVuHandler.php:402
DjVuHandler\mustRender
mustRender( $file)
Definition: DjVuHandler.php:60
DjVuHandler\getDimensionInfo
getDimensionInfo(File $file)
Definition: DjVuHandler.php:354
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:40
wfShellExec
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
Definition: GlobalFunctions.php:1894