MediaWiki  1.27.2
DjVu.php
Go to the documentation of this file.
1 <?php
29 class DjVuHandler extends ImageHandler {
30  const EXPENSIVE_SIZE_LIMIT = 10485760; // 10MiB
31 
35  function isEnabled() {
37  if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
38  wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
39 
40  return false;
41  } else {
42  return true;
43  }
44  }
45 
50  public function mustRender( $file ) {
51  return true;
52  }
53 
59  public function isExpensiveToThumbnail( $file ) {
60  return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
61  }
62 
67  public function isMultiPage( $file ) {
68  return true;
69  }
70 
74  public function getParamMap() {
75  return [
76  'img_width' => 'width',
77  'img_page' => 'page',
78  ];
79  }
80 
86  public function validateParam( $name, $value ) {
87  if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
88  // Extra junk on the end of page, probably actually a caption
89  // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
90  return false;
91  }
92  if ( in_array( $name, [ 'width', 'height', 'page' ] ) ) {
93  if ( $value <= 0 ) {
94  return false;
95  } else {
96  return true;
97  }
98  } else {
99  return false;
100  }
101  }
102 
107  public function makeParamString( $params ) {
108  $page = isset( $params['page'] ) ? $params['page'] : 1;
109  if ( !isset( $params['width'] ) ) {
110  return false;
111  }
112 
113  return "page{$page}-{$params['width']}px";
114  }
115 
120  public function parseParamString( $str ) {
121  $m = false;
122  if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
123  return [ 'width' => $m[2], 'page' => $m[1] ];
124  } else {
125  return false;
126  }
127  }
128 
133  function getScriptParams( $params ) {
134  return [
135  'width' => $params['width'],
136  'page' => $params['page'],
137  ];
138  }
139 
148  function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
150 
151  if ( !$this->normaliseParams( $image, $params ) ) {
152  return new TransformParameterError( $params );
153  }
154  $width = $params['width'];
155  $height = $params['height'];
156  $page = $params['page'];
157 
158  if ( $flags & self::TRANSFORM_LATER ) {
159  $params = [
160  'width' => $width,
161  'height' => $height,
162  'page' => $page
163  ];
164 
165  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
166  }
167 
168  if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
169  return new MediaTransformError(
170  'thumbnail_error',
171  $width,
172  $height,
173  wfMessage( 'thumbnail_dest_directory' )->text()
174  );
175  }
176 
177  // Get local copy source for shell scripts
178  // Thumbnail extraction is very inefficient for large files.
179  // Provide a way to pool count limit the number of downloaders.
180  if ( $image->getSize() >= 1e7 ) { // 10MB
181  $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
182  [
183  'doWork' => function () use ( $image ) {
184  return $image->getLocalRefPath();
185  }
186  ]
187  );
188  $srcPath = $work->execute();
189  } else {
190  $srcPath = $image->getLocalRefPath();
191  }
192 
193  if ( $srcPath === false ) { // Failed to get local copy
194  wfDebugLog( 'thumbnail',
195  sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
196  wfHostname(), $image->getName() ) );
197 
198  return new MediaTransformError( 'thumbnail_error',
199  $params['width'], $params['height'],
200  wfMessage( 'filemissing' )->text()
201  );
202  }
203 
204  # Use a subshell (brackets) to aggregate stderr from both pipeline commands
205  # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
206  $cmd = '(' . wfEscapeShellArg(
207  $wgDjvuRenderer,
208  "-format=ppm",
209  "-page={$page}",
210  "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
211  $srcPath );
212  if ( $wgDjvuPostProcessor ) {
213  $cmd .= " | {$wgDjvuPostProcessor}";
214  }
215  $cmd .= ' > ' . wfEscapeShellArg( $dstPath ) . ') 2>&1';
216  wfDebug( __METHOD__ . ": $cmd\n" );
217  $retval = '';
218  $err = wfShellExec( $cmd, $retval );
219 
220  $removed = $this->removeBadFile( $dstPath, $retval );
221  if ( $retval != 0 || $removed ) {
222  $this->logErrorForExternalProcess( $retval, $err, $cmd );
223  return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
224  } else {
225  $params = [
226  'width' => $width,
227  'height' => $height,
228  'page' => $page
229  ];
230 
231  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
232  }
233  }
234 
242  function getDjVuImage( $image, $path ) {
243  if ( !$image ) {
244  $deja = new DjVuImage( $path );
245  } elseif ( !isset( $image->dejaImage ) ) {
246  $deja = $image->dejaImage = new DjVuImage( $path );
247  } else {
248  $deja = $image->dejaImage;
249  }
250 
251  return $deja;
252  }
253 
261  private function getUnserializedMetadata( File $file ) {
262  $metadata = $file->getMetadata();
263  if ( substr( $metadata, 0, 3 ) === '<?xml' ) {
264  // Old style. Not serialized but instead just a raw string of XML.
265  return $metadata;
266  }
267 
268  MediaWiki\suppressWarnings();
269  $unser = unserialize( $metadata );
270  MediaWiki\restoreWarnings();
271  if ( is_array( $unser ) ) {
272  if ( isset( $unser['error'] ) ) {
273  return false;
274  } elseif ( isset( $unser['xml'] ) ) {
275  return $unser['xml'];
276  } else {
277  // Should never ever reach here.
278  throw new MWException( "Error unserializing DjVu metadata." );
279  }
280  }
281 
282  // unserialize failed. Guess it wasn't really serialized after all,
283  return $metadata;
284  }
285 
292  public function getMetaTree( $image, $gettext = false ) {
293  if ( $gettext && isset( $image->djvuTextTree ) ) {
294  return $image->djvuTextTree;
295  }
296  if ( !$gettext && isset( $image->dejaMetaTree ) ) {
297  return $image->dejaMetaTree;
298  }
299 
300  $metadata = $this->getUnserializedMetadata( $image );
301  if ( !$this->isMetadataValid( $image, $metadata ) ) {
302  wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
303 
304  return false;
305  }
306 
307  MediaWiki\suppressWarnings();
308  try {
309  // Set to false rather than null to avoid further attempts
310  $image->dejaMetaTree = false;
311  $image->djvuTextTree = false;
312  $tree = new SimpleXMLElement( $metadata, LIBXML_PARSEHUGE );
313  if ( $tree->getName() == 'mw-djvu' ) {
315  foreach ( $tree->children() as $b ) {
316  if ( $b->getName() == 'DjVuTxt' ) {
317  // @todo File::djvuTextTree and File::dejaMetaTree are declared
318  // dynamically. Add a public File::$data to facilitate this?
319  $image->djvuTextTree = $b;
320  } elseif ( $b->getName() == 'DjVuXML' ) {
321  $image->dejaMetaTree = $b;
322  }
323  }
324  } else {
325  $image->dejaMetaTree = $tree;
326  }
327  } catch ( Exception $e ) {
328  wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
329  }
330  MediaWiki\restoreWarnings();
331  if ( $gettext ) {
332  return $image->djvuTextTree;
333  } else {
334  return $image->dejaMetaTree;
335  }
336  }
337 
343  function getImageSize( $image, $path ) {
344  return $this->getDjVuImage( $image, $path )->getImageSize();
345  }
346 
347  function getThumbType( $ext, $mime, $params = null ) {
349  static $mime;
350  if ( !isset( $mime ) ) {
351  $magic = MimeMagic::singleton();
352  $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
353  }
354 
355  return [ $wgDjvuOutputExtension, $mime ];
356  }
357 
358  function getMetadata( $image, $path ) {
359  wfDebug( "Getting DjVu metadata for $path\n" );
360 
361  $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
362  if ( $xml === false ) {
363  // Special value so that we don't repetitively try and decode a broken file.
364  return serialize( [ 'error' => 'Error extracting metadata' ] );
365  } else {
366  return serialize( [ 'xml' => $xml ] );
367  }
368  }
369 
370  function getMetadataType( $image ) {
371  return 'djvuxml';
372  }
373 
374  function isMetadataValid( $image, $metadata ) {
375  return !empty( $metadata ) && $metadata != serialize( [] );
376  }
377 
378  function pageCount( File $image ) {
379  $info = $this->getDimensionInfo( $image );
380 
381  return $info ? $info['pageCount'] : false;
382  }
383 
385  $index = $page - 1; // MW starts pages at 1
386 
387  $info = $this->getDimensionInfo( $image );
388  if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
389  return $info['dimensionsByPage'][$index];
390  }
391 
392  return false;
393  }
394 
395  protected function getDimensionInfo( File $file ) {
397  return $cache->getWithSetCallback(
398  $cache->makeKey( 'file-djvu', 'dimensions', $file->getSha1() ),
399  $cache::TTL_INDEFINITE,
400  function () use ( $file ) {
401  $tree = $this->getMetaTree( $file );
402  if ( !$tree ) {
403  return false;
404  }
405 
406  $dimsByPage = [];
407  $count = count( $tree->xpath( '//OBJECT' ) );
408  for ( $i = 0; $i < $count; $i++ ) {
409  $o = $tree->BODY[0]->OBJECT[$i];
410  if ( $o ) {
411  $dimsByPage[$i] = [
412  'width' => (int)$o['width'],
413  'height' => (int)$o['height'],
414  ];
415  } else {
416  $dimsByPage[$i] = false;
417  }
418  }
419 
420  return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
421  },
422  [ 'pcTTL' => $cache::TTL_INDEFINITE ]
423  );
424  }
425 
431  function getPageText( File $image, $page ) {
432  $tree = $this->getMetaTree( $image, true );
433  if ( !$tree ) {
434  return false;
435  }
436 
437  $o = $tree->BODY[0]->PAGE[$page - 1];
438  if ( $o ) {
439  $txt = $o['value'];
440 
441  return $txt;
442  } else {
443  return false;
444  }
445  }
446 }
isMultiPage($file)
Definition: DjVu.php:67
static getMainWANInstance()
Get the main WAN cache object.
parseParamString($str)
Definition: DjVu.php:120
getPageText(File $image, $page)
Definition: DjVu.php:431
getDimensionInfo(File $file)
Definition: DjVu.php:395
removeBadFile($dstPath, $retval=0)
Check for zero-sized thumbnails.
getUnserializedMetadata(File $file)
Get metadata, unserializing it if neccessary.
Definition: DjVu.php:261
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfMkdirParents($dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
getParamMap()
Definition: DjVu.php:74
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:366
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
execute($skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
wfHostname()
Fetch server name for use in error reporting etc.
$value
getMetadata($image, $path)
Definition: DjVu.php:358
pageCount(File $image)
Definition: DjVu.php:378
Handler for DjVu images.
Definition: DjVu.php:29
if($ext== 'php'||$ext== 'php5') $mime
Definition: router.php:65
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2548
getThumbType($ext, $mime, $params=null)
Definition: DjVu.php:347
wfShellExec($cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
getPageDimensions(File $image, $page)
Definition: DjVu.php:384
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
logErrorForExternalProcess($retval, $err, $cmd)
Log an error that occurred in an external process.
isEnabled()
Definition: DjVu.php:35
Shortcut class for parameter validation errors.
getImageSize($image, $path)
Definition: DjVu.php:343
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Definition: DjVuImage.php:36
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
Convenience class for dealing with PoolCounters using callbacks.
getDjVuImage($image, $path)
Cache an instance of DjVuImage in an Image object, return that instance.
Definition: DjVu.php:242
unserialize($serialized)
Definition: ApiMessage.php:102
const EXPENSIVE_SIZE_LIMIT
Definition: DjVu.php:30
getMetaTree($image, $gettext=false)
Cache a document tree for the DjVu XML metadata.
Definition: DjVu.php:292
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB.
Definition: File.php:638
Media transform output for images.
isMetadataValid($image, $metadata)
Definition: DjVu.php:374
$wgDjvuRenderer
Path of the ddjvu DJVU renderer Enable this and $wgDjvuDump to enable djvu rendering example: $wgDjvu...
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
$cache
Definition: mcc.php:33
$params
$wgDjvuOutputExtension
File extension for the DJVU post processor output.
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: File.php:2117
validateParam($name, $value)
Definition: DjVu.php:86
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
isExpensiveToThumbnail($file)
True if creating thumbnails from the file is large or otherwise resource-intensive.
Definition: DjVu.php:59
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getMetadataType($image)
Definition: DjVu.php:370
makeParamString($params)
Definition: DjVu.php:107
$wgDjvuDump
Path of the djvudump executable Enable this and $wgDjvuRenderer to enable djvu rendering example: $wg...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Media handler abstract base class for images.
mustRender($file)
Definition: DjVu.php:50
$wgDjvuToXML
Path of the djvutoxml executable This works like djvudump except much, much slower as of version 3...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:762
$count
wfEscapeShellArg()
Windows-compatible version of escapeshellarg() Windows doesn't recognise single-quotes in the shell...
doTransform($image, $dstPath, $dstUrl, $params, $flags=0)
Definition: DjVu.php:148
normaliseParams($image, &$params)
serialize()
Definition: ApiMessage.php:94
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
getScriptParams($params)
Definition: DjVu.php:133
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account incomplete not yet checked for validity & $retval
Definition: hooks.txt:242
Basic media transform error class.
$wgDjvuPostProcessor
Shell command for the DJVU post processor Default: pnmtojpeg, since ddjvu generates ppm output Set th...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2338
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310