MediaWiki REL1_39
DjVuHandler.php
Go to the documentation of this file.
1<?php
27
34 private const EXPENSIVE_SIZE_LIMIT = 10485760; // 10MiB
35
36 // Constants for getHandlerState
37 private const STATE_DJVU_IMAGE = 'djvuImage';
38 private const STATE_TEXT_TREE = 'djvuTextTree';
39 private const STATE_META_TREE = 'djvuMetaTree';
40 private const CACHE_VERSION = 'v2';
41
45 public function isEnabled() {
46 $djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer );
47 $djvuDump = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuDump );
48 if ( !$djvuRenderer || !$djvuDump ) {
49 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" );
50
51 return false;
52 } else {
53 return true;
54 }
55 }
56
61 public function mustRender( $file ) {
62 return true;
63 }
64
70 public function isExpensiveToThumbnail( $file ) {
71 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
72 }
73
78 public function isMultiPage( $file ) {
79 return true;
80 }
81
85 public function getParamMap() {
86 return [
87 'img_width' => 'width',
88 'img_page' => 'page',
89 ];
90 }
91
97 public function validateParam( $name, $value ) {
98 if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
99 // Extra junk on the end of page, probably actually a caption
100 // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
101 return false;
102 }
103 return in_array( $name, [ 'width', 'height', 'page' ] ) && $value > 0;
104 }
105
110 public function makeParamString( $params ) {
111 $page = $params['page'] ?? 1;
112 if ( !isset( $params['width'] ) ) {
113 return false;
114 }
115
116 return "page{$page}-{$params['width']}px";
117 }
118
123 public function parseParamString( $str ) {
124 $m = false;
125 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
126 return [ 'width' => $m[2], 'page' => $m[1] ];
127 } else {
128 return false;
129 }
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 = '';
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 } else {
229 $params = [
230 'width' => $width,
231 'height' => $height,
232 'page' => $page
233 ];
234
235 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
236 }
237 }
238
247 private function getDjVuImage( $state, $path ) {
248 $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
249 if ( !$deja ) {
250 $deja = new DjVuImage( $path );
251 $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
252 }
253 return $deja;
254 }
255
264 private function getMetadataInternal( File $file, $gettext ) {
265 $itemNames = [ 'error', '_error', 'data' ];
266 if ( $gettext ) {
267 $itemNames[] = 'text';
268 }
269 $unser = $file->getMetadataItems( $itemNames );
270
271 if ( isset( $unser['error'] ) ) {
272 return false;
273 } elseif ( isset( $unser['_error'] ) ) {
274 return false;
275 } else {
276 return $unser;
277 }
278 }
279
286 public function getMetaTree( $image, $gettext = false ) {
287 if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) {
288 return $image->getHandlerState( self::STATE_TEXT_TREE );
289 }
290 if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) {
291 return $image->getHandlerState( self::STATE_META_TREE );
292 }
293
294 $metadata = $this->getMetadataInternal( $image, $gettext );
295 if ( !$metadata ) {
296 return false;
297 }
298
299 if ( $gettext ) {
300 return $metadata;
301 } else {
302 unset( $metadata['text'] );
303 return $metadata;
304 }
305 }
306
307 public function getThumbType( $ext, $mime, $params = null ) {
308 $djvuOutputExtension = MediaWikiServices::getInstance()->getMainConfig()
309 ->get( MainConfigNames::DjvuOutputExtension );
310 static $mime;
311 if ( !isset( $mime ) ) {
312 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
313 $mime = $magic->getMimeTypeFromExtensionOrNull( $djvuOutputExtension );
314 }
315
316 return [ $djvuOutputExtension, $mime ];
317 }
318
319 public function getSizeAndMetadata( $state, $path ) {
320 wfDebug( "Getting DjVu metadata for $path" );
321
322 $djvuImage = $this->getDjVuImage( $state, $path );
323 $metadata = $djvuImage->retrieveMetaData();
324 if ( $metadata === false ) {
325 // Special value so that we don't repetitively try and decode a broken file.
326 $metadata = [ 'error' => 'Error extracting metadata' ];
327 }
328 return [ 'metadata' => $metadata ] + $djvuImage->getImageSize();
329 }
330
331 public function getMetadataType( $image ) {
332 // historical reasons
333 return 'djvuxml';
334 }
335
336 public function isFileMetadataValid( $image ) {
337 return $image->getMetadataArray() ? self::METADATA_GOOD : self::METADATA_BAD;
338 }
339
340 public function pageCount( File $image ) {
341 $info = $this->getDimensionInfo( $image );
342
343 return $info ? $info['pageCount'] : false;
344 }
345
346 public function getPageDimensions( File $image, $page ) {
347 $index = $page - 1; // MW starts pages at 1
348
349 $info = $this->getDimensionInfo( $image );
350 if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
351 return $info['dimensionsByPage'][$index];
352 }
353
354 return false;
355 }
356
357 protected function getDimensionInfo( File $file ) {
358 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
359 return $cache->getWithSetCallback(
360 $cache->makeKey( 'file-djvu', 'dimensions', self::CACHE_VERSION, $file->getSha1() ),
361 $cache::TTL_INDEFINITE,
362 function () use ( $file ) {
363 $tree = $this->getMetaTree( $file );
364 return $this->getDimensionInfoFromMetaTree( $tree );
365 },
366 [ 'pcTTL' => $cache::TTL_INDEFINITE ]
367 );
368 }
369
375 protected function getDimensionInfoFromMetaTree( $metatree ) {
376 if ( !$metatree ) {
377 return false;
378 }
379 $dimsByPage = [];
380
381 if ( !isset( $metatree['data'] ) || !$metatree['data'] ) {
382 return false;
383 }
384 foreach ( $metatree['data']['pages'] as $page ) {
385 if ( !$page ) {
386 $dimsByPage[] = false;
387 } else {
388 $dimsByPage[] = [
389 'width' => (int)$page['width'],
390 'height' => (int)$page['height'],
391 ];
392 }
393 }
394 return [
395 'pageCount' => count( $metatree['data']['pages'] ),
396 'dimensionsByPage' => $dimsByPage
397 ];
398 }
399
405 public function getPageText( File $image, $page ) {
406 $tree = $this->getMetaTree( $image, true );
407 if ( !$tree ) {
408 return false;
409 }
410 if ( isset( $tree['text'] ) && isset( $tree['text'][$page - 1] ) ) {
411 return $tree['text'][$page - 1];
412 }
413 return false;
414 }
415
416 public function useSplitMetadata() {
417 return true;
418 }
419}
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
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:67
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.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Executes shell commands.
Definition Shell.php:46
Convenience class for dealing with PoolCounters using callbacks.
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
Media transform output for images.
Shortcut class for parameter validation errors.
$cache
Definition mcc.php:33
$mime
Definition router.php:60
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!is_readable( $file)) $ext
Definition router.php:48