MediaWiki  master
DjVuHandler.php
Go to the documentation of this file.
1 <?php
25 
31 class DjVuHandler extends ImageHandler {
32  private const EXPENSIVE_SIZE_LIMIT = 10485760; // 10MiB
33 
34  // Constants for getHandlerState
35  private const STATE_DJVU_IMAGE = 'djvuImage';
36  private const STATE_TEXT_TREE = 'djvuTextTree';
37  private const STATE_META_TREE = 'djvuMetaTree';
38 
42  public function isEnabled() {
44  if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
45  wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" );
46 
47  return false;
48  } else {
49  return true;
50  }
51  }
52 
57  public function mustRender( $file ) {
58  return true;
59  }
60 
66  public function isExpensiveToThumbnail( $file ) {
67  return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
68  }
69 
74  public function isMultiPage( $file ) {
75  return true;
76  }
77 
81  public function getParamMap() {
82  return [
83  'img_width' => 'width',
84  'img_page' => 'page',
85  ];
86  }
87 
93  public function validateParam( $name, $value ) {
94  if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
95  // Extra junk on the end of page, probably actually a caption
96  // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
97  return false;
98  }
99  if ( in_array( $name, [ 'width', 'height', 'page' ] ) ) {
100  if ( $value <= 0 ) {
101  return false;
102  } else {
103  return true;
104  }
105  } else {
106  return false;
107  }
108  }
109 
114  public function makeParamString( $params ) {
115  $page = $params['page'] ?? 1;
116  if ( !isset( $params['width'] ) ) {
117  return false;
118  }
119 
120  return "page{$page}-{$params['width']}px";
121  }
122 
127  public function parseParamString( $str ) {
128  $m = false;
129  if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
130  return [ 'width' => $m[2], 'page' => $m[1] ];
131  } else {
132  return false;
133  }
134  }
135 
140  protected function getScriptParams( $params ) {
141  return [
142  'width' => $params['width'],
143  'page' => $params['page'],
144  ];
145  }
146 
155  public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
157 
158  if ( !$this->normaliseParams( $image, $params ) ) {
159  return new TransformParameterError( $params );
160  }
161  $width = $params['width'];
162  $height = $params['height'];
163  $page = $params['page'];
164 
165  if ( $flags & self::TRANSFORM_LATER ) {
166  $params = [
167  'width' => $width,
168  'height' => $height,
169  'page' => $page
170  ];
171 
172  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
173  }
174 
175  if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
176  return new MediaTransformError(
177  'thumbnail_error',
178  $width,
179  $height,
180  wfMessage( 'thumbnail_dest_directory' )
181  );
182  }
183 
184  // Get local copy source for shell scripts
185  // Thumbnail extraction is very inefficient for large files.
186  // Provide a way to pool count limit the number of downloaders.
187  if ( $image->getSize() >= 1e7 ) { // 10MB
188  $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
189  [
190  'doWork' => static function () use ( $image ) {
191  return $image->getLocalRefPath();
192  }
193  ]
194  );
195  $srcPath = $work->execute();
196  } else {
197  $srcPath = $image->getLocalRefPath();
198  }
199 
200  if ( $srcPath === false ) { // Failed to get local copy
201  wfDebugLog( 'thumbnail',
202  sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
203  wfHostname(), $image->getName() ) );
204 
205  return new MediaTransformError( 'thumbnail_error',
206  $params['width'], $params['height'],
207  wfMessage( 'filemissing' )
208  );
209  }
210 
211  # Use a subshell (brackets) to aggregate stderr from both pipeline commands
212  # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
213  $cmd = '(' . Shell::escape(
215  "-format=ppm",
216  "-page={$page}",
217  "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
218  $srcPath );
219  if ( $wgDjvuPostProcessor ) {
220  $cmd .= " | {$wgDjvuPostProcessor}";
221  }
222  $cmd .= ' > ' . Shell::escape( $dstPath ) . ') 2>&1';
223  wfDebug( __METHOD__ . ": $cmd" );
224  $retval = '';
225  $err = wfShellExec( $cmd, $retval );
226 
227  $removed = $this->removeBadFile( $dstPath, $retval );
228  if ( $retval != 0 || $removed ) {
229  $this->logErrorForExternalProcess( $retval, $err, $cmd );
230  return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
231  } else {
232  $params = [
233  'width' => $width,
234  'height' => $height,
235  'page' => $page
236  ];
237 
238  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
239  }
240  }
241 
250  private function getDjVuImage( $state, $path ) {
251  $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
252  if ( !$deja ) {
253  $deja = new DjVuImage( $path );
254  $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
255  }
256  return $deja;
257  }
258 
266  private function getXMLMetadata( File $file ) {
267  $unser = $file->getMetadataArray();
268  if ( isset( $unser['error'] ) ) {
269  return false;
270  } elseif ( isset( $unser['xml'] ) ) {
271  return $unser['xml'];
272  } elseif ( isset( $unser['_error'] )
273  && is_string( $unser['_error'] )
274  && substr( $unser['_error'], 0, 3 ) === '<?xml'
275  ) {
276  // Old style. Not serialized but instead just a raw string of XML.
277  return $unser['_error'];
278  } else {
279  return false;
280  }
281  }
282 
289  public function getMetaTree( $image, $gettext = false ) {
290  if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) {
291  return $image->getHandlerState( self::STATE_TEXT_TREE );
292  }
293  if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) {
294  return $image->getHandlerState( self::STATE_META_TREE );
295  }
296 
297  $xml = $this->getXMLMetadata( $image );
298  if ( !$xml ) {
299  wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow" );
300 
301  return false;
302  }
303 
304  $trees = $this->extractTreesFromXML( $xml );
305  $image->setHandlerState( self::STATE_TEXT_TREE, $trees['TextTree'] );
306  $image->setHandlerState( self::STATE_META_TREE, $trees['MetaTree'] );
307 
308  if ( $gettext ) {
309  return $trees['TextTree'];
310  } else {
311  return $trees['MetaTree'];
312  }
313  }
314 
320  protected function extractTreesFromXML( $xml ) {
321  Wikimedia\suppressWarnings();
322  try {
323  // Set to false rather than null to avoid further attempts
324  $metaTree = false;
325  $textTree = false;
326  $tree = new SimpleXMLElement( $xml, LIBXML_PARSEHUGE );
327  if ( $tree->getName() == 'mw-djvu' ) {
329  foreach ( $tree->children() as $b ) {
330  if ( $b->getName() == 'DjVuTxt' ) {
331  // @todo File::djvuTextTree and File::dejaMetaTree are declared
332  // dynamically. Add a public File::$data to facilitate this?
333  $textTree = $b;
334  } elseif ( $b->getName() == 'DjVuXML' ) {
335  $metaTree = $b;
336  }
337  }
338  } else {
339  $metaTree = $tree;
340  }
341  } catch ( Exception $e ) {
342  wfDebug( "Bogus multipage XML metadata" );
343  }
344  Wikimedia\restoreWarnings();
345 
346  return [ 'MetaTree' => $metaTree, 'TextTree' => $textTree ];
347  }
348 
349  public function getThumbType( $ext, $mime, $params = null ) {
350  global $wgDjvuOutputExtension;
351  static $mime;
352  if ( !isset( $mime ) ) {
353  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
354  $mime = $magic->getMimeTypeFromExtensionOrNull( $wgDjvuOutputExtension );
355  }
356 
357  return [ $wgDjvuOutputExtension, $mime ];
358  }
359 
360  public function getSizeAndMetadata( $state, $path ) {
361  wfDebug( "Getting DjVu metadata for $path" );
362 
363  $djvuImage = $this->getDjVuImage( $state, $path );
364  $xml = $djvuImage->retrieveMetaData();
365  if ( $xml === false ) {
366  // Special value so that we don't repetitively try and decode a broken file.
367  $metadata = [ 'error' => 'Error extracting metadata' ];
368  } else {
369  $metadata = [ 'xml' => $xml ];
370  }
371  return [ 'metadata' => $metadata ] + $djvuImage->getImageSize();
372  }
373 
374  public function getMetadataType( $image ) {
375  return 'djvuxml';
376  }
377 
378  public function isFileMetadataValid( $image ) {
379  return $image->getMetadataArray() ? self::METADATA_GOOD : self::METADATA_BAD;
380  }
381 
382  public function pageCount( File $image ) {
383  $info = $this->getDimensionInfo( $image );
384 
385  return $info ? $info['pageCount'] : false;
386  }
387 
388  public function getPageDimensions( File $image, $page ) {
389  $index = $page - 1; // MW starts pages at 1
390 
391  $info = $this->getDimensionInfo( $image );
392  if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
393  return $info['dimensionsByPage'][$index];
394  }
395 
396  return false;
397  }
398 
399  protected function getDimensionInfo( File $file ) {
400  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
401  return $cache->getWithSetCallback(
402  $cache->makeKey( 'file-djvu', 'dimensions', $file->getSha1() ),
403  $cache::TTL_INDEFINITE,
404  function () use ( $file ) {
405  $tree = $this->getMetaTree( $file );
406  return $this->getDimensionInfoFromMetaTree( $tree );
407  },
408  [ 'pcTTL' => $cache::TTL_INDEFINITE ]
409  );
410  }
411 
417  protected function getDimensionInfoFromMetaTree( $metatree ) {
418  if ( !$metatree ) {
419  return false;
420  }
421 
422  $dimsByPage = [];
423  $count = count( $metatree->xpath( '//OBJECT' ) );
424  for ( $i = 0; $i < $count; $i++ ) {
425  $o = $metatree->BODY[0]->OBJECT[$i];
426  if ( $o ) {
427  $dimsByPage[$i] = [
428  'width' => (int)$o['width'],
429  'height' => (int)$o['height'],
430  ];
431  } else {
432  $dimsByPage[$i] = false;
433  }
434  }
435 
436  return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
437  }
438 
444  public function getPageText( File $image, $page ) {
445  $tree = $this->getMetaTree( $image, true );
446  if ( !$tree ) {
447  return false;
448  }
449 
450  $o = $tree->BODY[0]->PAGE[$page - 1];
451  return $o ? (string)$o['value'] : false;
452  }
453 }
MediaHandler\removeBadFile
removeBadFile( $dstPath, $retval=0)
Check for zero-sized thumbnails.
Definition: MediaHandler.php:926
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:29
DjVuHandler\getParamMap
getParamMap()
Definition: DjVuHandler.php:81
DjVuHandler\getDimensionInfoFromMetaTree
getDimensionInfoFromMetaTree( $metatree)
Given an XML metadata tree, returns dimension information about the document.
Definition: DjVuHandler.php:417
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:1774
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:180
DjVuHandler\isMultiPage
isMultiPage( $file)
Definition: DjVuHandler.php:74
DjVuHandler\parseParamString
parseParamString( $str)
Definition: DjVuHandler.php:127
$wgDjvuPostProcessor
$wgDjvuPostProcessor
Shell command for the DJVU post processor Default: pnmtojpeg, since ddjvu generates ppm output Set th...
Definition: DefaultSettings.php:1793
DjVuHandler\pageCount
pageCount(File $image)
Page count for a multi-page document, false if unsupported or unknown.
Definition: DjVuHandler.php:382
DjVuHandler\isEnabled
isEnabled()
Definition: DjVuHandler.php:42
DjVuImage
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Definition: DjVuImage.php:38
$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:1245
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1182
PoolCounterWorkViaCallback
Convenience class for dealing with PoolCounters using callbacks.
Definition: PoolCounterWorkViaCallback.php:31
DjVuHandler\makeParamString
makeParamString( $params)
Definition: DjVuHandler.php:114
$wgDjvuRenderer
$wgDjvuRenderer
Path of the ddjvu DJVU renderer Enable this and $wgDjvuDump to enable djvu rendering example: $wgDjvu...
Definition: DefaultSettings.php:1763
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:32
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:241
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:66
DjVuHandler\STATE_DJVU_IMAGE
const STATE_DJVU_IMAGE
Definition: DjVuHandler.php:35
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:250
DjVuHandler\isFileMetadataValid
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
Definition: DjVuHandler.php:378
$wgDjvuToXML
$wgDjvuToXML
Path of the djvutoxml executable This works like djvudump except much, much slower as of version 3....
Definition: DefaultSettings.php:1786
DjVuHandler\getPageDimensions
getPageDimensions(File $image, $page)
Get an associative array of page dimensions Currently "width" and "height" are understood,...
Definition: DjVuHandler.php:388
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:66
DjVuHandler\validateParam
validateParam( $name, $value)
Definition: DjVuHandler.php:93
$wgDjvuOutputExtension
$wgDjvuOutputExtension
File extension for the DJVU post processor output.
Definition: DefaultSettings.php:1798
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:113
DjVuHandler\STATE_META_TREE
const STATE_META_TREE
Definition: DjVuHandler.php:37
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:374
DjVuHandler\getScriptParams
getScriptParams( $params)
Definition: DjVuHandler.php:140
DjVuHandler\getSizeAndMetadata
getSizeAndMetadata( $state, $path)
Get image size information and metadata array.
Definition: DjVuHandler.php:360
DjVuHandler
Handler for DjVu images.
Definition: DjVuHandler.php:31
DjVuHandler\getXMLMetadata
getXMLMetadata(File $file)
Get metadata, unserializing it if necessary.
Definition: DjVuHandler.php:266
$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:349
MediaHandler\logErrorForExternalProcess
logErrorForExternalProcess( $retval, $err, $cmd)
Log an error that occurred in an external process.
Definition: MediaHandler.php:1009
DjVuHandler\doTransform
doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)
Definition: DjVuHandler.php:155
$path
$path
Definition: NoLocalSettings.php:25
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:40
DjVuHandler\getMetaTree
getMetaTree( $image, $gettext=false)
Cache a document tree for the DjVu XML metadata.
Definition: DjVuHandler.php:289
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
$wgDjvuDump
$wgDjvuDump
Path of the djvudump executable Enable this and $wgDjvuRenderer to enable djvu rendering example: $wg...
Definition: DefaultSettings.php:1756
DjVuHandler\STATE_TEXT_TREE
const STATE_TEXT_TREE
Definition: DjVuHandler.php:36
$mime
$mime
Definition: router.php:60
DjVuHandler\getPageText
getPageText(File $image, $page)
Definition: DjVuHandler.php:444
DjVuHandler\mustRender
mustRender( $file)
Definition: DjVuHandler.php:57
DjVuHandler\getDimensionInfo
getDimensionInfo(File $file)
Definition: DjVuHandler.php:399
DjVuHandler\extractTreesFromXML
extractTreesFromXML( $xml)
Extracts metadata and text trees from metadata XML in string form.
Definition: DjVuHandler.php:320
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:39
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:1936