MediaWiki master
DjVuHandler.php
Go to the documentation of this file.
1<?php
29
36 private const EXPENSIVE_SIZE_LIMIT = 10_485_760; // 10MiB
37
38 // Constants for getHandlerState
39 private const STATE_DJVU_IMAGE = 'djvuImage';
40 private const STATE_TEXT_TREE = 'djvuTextTree';
41 private const STATE_META_TREE = 'djvuMetaTree';
42 private const CACHE_VERSION = 'v2';
43
47 public function isEnabled() {
48 $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer );
49 $djvuDump = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuDump );
50 if ( !$djvuRenderer || !$djvuDump ) {
51 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" );
52
53 return false;
54 }
55 return true;
56 }
57
62 public function mustRender( $file ) {
63 return true;
64 }
65
71 public function isExpensiveToThumbnail( $file ) {
72 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
73 }
74
79 public function isMultiPage( $file ) {
80 return true;
81 }
82
86 public function getParamMap() {
87 return [
88 'img_width' => 'width',
89 'img_page' => 'page',
90 ];
91 }
92
98 public function validateParam( $name, $value ) {
99 if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
100 // Extra junk on the end of page, probably actually a caption
101 // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
102 return false;
103 }
104 return in_array( $name, [ 'width', 'height', 'page' ] ) && $value > 0;
105 }
106
111 public function makeParamString( $params ) {
112 $page = $params['page'] ?? 1;
113 if ( !isset( $params['width'] ) ) {
114 return false;
115 }
116
117 return "page{$page}-{$params['width']}px";
118 }
119
124 public function parseParamString( $str ) {
125 $m = false;
126 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
127 return [ 'width' => $m[2], 'page' => $m[1] ];
128 }
129 return false;
130 }
131
136 protected function getScriptParams( $params ) {
137 return [
138 'width' => $params['width'],
139 'page' => $params['page'],
140 ];
141 }
142
151 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
152 $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer );
153 $djvuPostProcessor = MediaWikiServices::getInstance()->getMainConfig()
154 ->get( MainConfigNames::DjvuPostProcessor );
155 if ( !$this->normaliseParams( $image, $params ) ) {
156 return new TransformParameterError( $params );
157 }
158 $width = $params['width'];
159 $height = $params['height'];
160 $page = $params['page'];
161
162 if ( $flags & self::TRANSFORM_LATER ) {
163 $params = [
164 'width' => $width,
165 'height' => $height,
166 'page' => $page
167 ];
168
169 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
170 }
171
172 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
173 return new MediaTransformError(
174 'thumbnail_error',
175 $width,
176 $height,
177 wfMessage( 'thumbnail_dest_directory' )
178 );
179 }
180
181 // Get local copy source for shell scripts
182 // Thumbnail extraction is very inefficient for large files.
183 // Provide a way to pool count limit the number of downloaders.
184 if ( $image->getSize() >= 1e7 ) { // 10 MB
185 $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
186 [
187 'doWork' => static function () use ( $image ) {
188 return $image->getLocalRefPath();
189 }
190 ]
191 );
192 $srcPath = $work->execute();
193 } else {
194 $srcPath = $image->getLocalRefPath();
195 }
196
197 if ( $srcPath === false ) { // Failed to get local copy
198 wfDebugLog( 'thumbnail',
199 sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
200 wfHostname(), $image->getName() ) );
201
202 return new MediaTransformError( 'thumbnail_error',
203 $params['width'], $params['height'],
204 wfMessage( 'filemissing' )
205 );
206 }
207
208 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
209 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
210 $cmd = '(' . Shell::escape(
211 $djvuRenderer,
212 "-format=ppm",
213 "-page={$page}",
214 "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
215 $srcPath );
216 if ( $djvuPostProcessor ) {
217 $cmd .= " | {$djvuPostProcessor}";
218 }
219 $cmd .= ' > ' . Shell::escape( $dstPath ) . ') 2>&1';
220 wfDebug( __METHOD__ . ": $cmd" );
221 $retval = 0;
222 $err = wfShellExec( $cmd, $retval );
223
224 $removed = $this->removeBadFile( $dstPath, $retval );
225 if ( $retval !== 0 || $removed ) {
226 $this->logErrorForExternalProcess( $retval, $err, $cmd );
227 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
228 }
229 $params = [
230 'width' => $width,
231 'height' => $height,
232 'page' => $page
233 ];
234
235 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
236 }
237
246 private function getDjVuImage( $state, $path ) {
247 $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
248 if ( !$deja ) {
249 $deja = new DjVuImage( $path );
250 $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
251 }
252 return $deja;
253 }
254
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 }
272 if ( isset( $unser['_error'] ) ) {
273 return false;
274 }
275 return $unser;
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 unset( $metadata['text'] );
299 }
300 return $metadata;
301 }
302
303 public function getThumbType( $ext, $mime, $params = null ) {
304 $djvuOutputExtension = MediaWikiServices::getInstance()->getMainConfig()
305 ->get( MainConfigNames::DjvuOutputExtension );
306 static $djvuMime = null;
307 if ( $djvuMime === null ) {
308 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
309 $djvuMime = $magic->getMimeTypeFromExtensionOrNull( $djvuOutputExtension );
310 }
311
312 return [ $djvuOutputExtension, $djvuMime ];
313 }
314
315 public function getSizeAndMetadata( $state, $path ) {
316 wfDebug( "Getting DjVu metadata for $path" );
317
318 $djvuImage = $this->getDjVuImage( $state, $path );
319 $metadata = $djvuImage->retrieveMetaData();
320 if ( $metadata === false ) {
321 // Special value so that we don't repetitively try and decode a broken file.
322 $metadata = [ 'error' => 'Error extracting metadata' ];
323 }
324 return [ 'metadata' => $metadata ] + $djvuImage->getImageSize();
325 }
326
327 public function getMetadataType( $image ) {
328 // historical reasons
329 return 'djvuxml';
330 }
331
332 public function isFileMetadataValid( $image ) {
333 return $image->getMetadataArray() ? self::METADATA_GOOD : self::METADATA_BAD;
334 }
335
336 public function pageCount( File $image ) {
337 $info = $this->getDimensionInfo( $image );
338
339 return $info ? $info['pageCount'] : false;
340 }
341
342 public function getPageDimensions( File $image, $page ) {
343 $index = $page - 1; // MW starts pages at 1
344
345 $info = $this->getDimensionInfo( $image );
346 if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
347 return $info['dimensionsByPage'][$index];
348 }
349
350 return false;
351 }
352
353 protected function getDimensionInfo( File $file ) {
354 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
355 return $cache->getWithSetCallback(
356 $cache->makeKey( 'file-djvu', 'dimensions', self::CACHE_VERSION, $file->getSha1() ),
357 $cache::TTL_INDEFINITE,
358 function () use ( $file ) {
359 $tree = $this->getMetaTree( $file );
360 return $this->getDimensionInfoFromMetaTree( $tree );
361 },
362 [ 'pcTTL' => $cache::TTL_INDEFINITE ]
363 );
364 }
365
371 protected function getDimensionInfoFromMetaTree( $metatree ) {
372 if ( !$metatree ) {
373 return false;
374 }
375 $dimsByPage = [];
376
377 if ( !isset( $metatree['data'] ) || !$metatree['data'] ) {
378 return false;
379 }
380 foreach ( $metatree['data']['pages'] as $page ) {
381 if ( !$page ) {
382 $dimsByPage[] = false;
383 } else {
384 $dimsByPage[] = [
385 'width' => (int)$page['width'],
386 'height' => (int)$page['height'],
387 ];
388 }
389 }
390 return [
391 'pageCount' => count( $metatree['data']['pages'] ),
392 'dimensionsByPage' => $dimsByPage
393 ];
394 }
395
401 public function getPageText( File $image, $page ) {
402 $tree = $this->getMetaTree( $image, true );
403 if ( !$tree ) {
404 return false;
405 }
406 if ( isset( $tree['text'] ) && isset( $tree['text'][$page - 1] ) ) {
407 return $tree['text'][$page - 1];
408 }
409 return false;
410 }
411
412 public function useSplitMetadata() {
413 return true;
414 }
415}
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.
Handler for DjVu images.
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)
isMultiPage( $file)
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.
mustRender( $file)
parseParamString( $str)
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Definition DjVuImage.php:41
Media handler abstract base class for images.
normaliseParams( $image, &$params)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
logErrorForExternalProcess( $retval, $err, $cmd)
Log an error that occurred in an external process.
const METADATA_GOOD
removeBadFile( $dstPath, $retval=0)
Check for zero-sized thumbnails.
Basic media transform error class.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
getMetadataItems(array $itemNames)
Get multiple elements of the unserialized handler-specific metadata.
Definition File.php:814
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition File.php:2412
getSize()
Return the size of the image file, in bytes Overridden by LocalFile, UnregisteredLocalFile STUB.
Definition File.php:865
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Convenience class for dealing with PoolCounter using callbacks.
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
Executes shell commands.
Definition Shell.php:46
Media transform output for images.
Shortcut class for parameter validation errors.