40 wfDebug(
"DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
62 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
78 'img_width' =>
'width',
89 if ( $name ===
'page' && trim( $value ) !== (
string)intval( $value ) ) {
94 if ( in_array( $name, [
'width',
'height',
'page' ] ) ) {
110 $page = $params[
'page'] ?? 1;
111 if ( !isset( $params[
'width'] ) ) {
115 return "page{$page}-{$params['width']}px";
124 if ( preg_match(
'/^page(\d+)-(\d+)px$/', $str, $m ) ) {
125 return [
'width' => $m[2],
'page' => $m[1] ];
137 'width' => $params[
'width'],
138 'page' => $params[
'page'],
150 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
156 $width = $params[
'width'];
157 $height = $params[
'height'];
158 $page = $params[
'page'];
160 if ( $flags & self::TRANSFORM_LATER ) {
182 if ( $image->getSize() >= 1e7 ) {
185 'doWork' => function () use ( $image ) {
186 return $image->getLocalRefPath();
192 $srcPath = $image->getLocalRefPath();
195 if ( $srcPath ===
false ) {
197 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
201 $params[
'width'], $params[
'height'],
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(
212 "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
215 $cmd .=
" | {$wgDjvuPostProcessor}";
217 $cmd .=
' > ' . Shell::escape( $dstPath ) .
') 2>&1';
218 wfDebug( __METHOD__ .
": $cmd\n" );
223 if ( $retval != 0 || $removed ) {
248 } elseif ( !isset( $image->dejaImage ) ) {
251 $deja = $image->dejaImage;
265 $metadata =
$file->getMetadata();
266 if ( substr( $metadata, 0, 3 ) ===
'<?xml' ) {
271 Wikimedia\suppressWarnings();
273 Wikimedia\restoreWarnings();
274 if ( is_array( $unser ) ) {
275 if ( isset( $unser[
'error'] ) ) {
277 } elseif ( isset( $unser[
'xml'] ) ) {
278 return $unser[
'xml'];
281 throw new MWException(
"Error unserializing DjVu metadata." );
297 if ( $gettext && isset( $image->djvuTextTree ) ) {
298 return $image->djvuTextTree;
300 if ( !$gettext && isset( $image->dejaMetaTree ) ) {
301 return $image->dejaMetaTree;
306 wfDebug(
"DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
312 $image->djvuTextTree = $trees[
'TextTree'];
313 $image->dejaMetaTree = $trees[
'MetaTree'];
316 return $image->djvuTextTree;
318 return $image->dejaMetaTree;
328 Wikimedia\suppressWarnings();
333 $tree =
new SimpleXMLElement( $metadata, LIBXML_PARSEHUGE );
334 if ( $tree->getName() ==
'mw-djvu' ) {
336 foreach ( $tree->children() as $b ) {
337 if ( $b->getName() ==
'DjVuTxt' ) {
341 } elseif ( $b->getName() ==
'DjVuXML' ) {
348 }
catch ( Exception $e ) {
349 wfDebug(
"Bogus multipage XML metadata\n" );
351 Wikimedia\restoreWarnings();
353 return [
'MetaTree' => $metaTree,
'TextTree' => $textTree ];
363 if ( !isset( $mime ) ) {
364 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
372 wfDebug(
"Getting DjVu metadata for $path\n" );
375 if ( $xml ===
false ) {
377 return serialize( [
'error' =>
'Error extracting metadata' ] );
388 return !empty( $metadata ) && $metadata !=
serialize( [] );
394 return $info ? $info[
'pageCount'] :
false;
401 if ( $info && isset( $info[
'dimensionsByPage'][$index] ) ) {
402 return $info[
'dimensionsByPage'][$index];
409 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
410 return $cache->getWithSetCallback(
411 $cache->makeKey(
'file-djvu',
'dimensions',
$file->getSha1() ),
412 $cache::TTL_INDEFINITE,
413 function () use (
$file ) {
417 [
'pcTTL' => $cache::TTL_INDEFINITE ]
432 $count = count( $metatree->xpath(
'//OBJECT' ) );
433 for ( $i = 0; $i < $count; $i++ ) {
434 $o = $metatree->BODY[0]->OBJECT[$i];
437 'width' => (int)$o[
'width'],
438 'height' => (
int)$o[
'height'],
441 $dimsByPage[$i] =
false;
445 return [
'pageCount' => $count,
'dimensionsByPage' => $dimsByPage ];
459 $o = $tree->BODY[0]->PAGE[$page - 1];
unserialize( $serialized)
$wgDjvuRenderer
Path of the ddjvu DJVU renderer Enable this and $wgDjvuDump to enable djvu rendering example: $wgDjvu...
$wgDjvuOutputExtension
File extension for the DJVU post processor output.
$wgDjvuToXML
Path of the djvutoxml executable This works like djvudump except much, much slower as of version 3....
$wgDjvuPostProcessor
Shell command for the DJVU post processor Default: pnmtojpeg, since ddjvu generates ppm output Set th...
$wgDjvuDump
Path of the djvudump executable Enable this and $wgDjvuRenderer to enable djvu rendering example: $wg...
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.
getDimensionInfoFromMetaTree( $metatree)
Given an XML metadata tree, returns dimension information about the document.
getDjVuImage( $image, $path)
Cache an instance of DjVuImage in an Image object, return that instance.
makeParamString( $params)
isExpensiveToThumbnail( $file)
True if creating thumbnails from the file is large or otherwise resource-intensive.
validateParam( $name, $value)
getDimensionInfo(File $file)
const EXPENSIVE_SIZE_LIMIT
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)
isMetadataValid( $image, $metadata)
Check if the metadata string is valid for this handler.
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 XML metadata.
getThumbType( $ext, $mime, $params=null)
Get the thumbnail extension and MIME type for a given source MIME type.
getUnserializedMetadata(File $file)
Get metadata, unserializing it if necessary.
extractTreesFromMetadata( $metadata)
Extracts metadata and text trees from metadata XML in string form.
getMetadata( $image, $path)
Get handler-specific metadata which will be saved in the img_metadata field.
getImageSize( $image, $path)
Get an image size array like that returned by getimagesize(), or false if it can't be determined.
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Implements some public methods and some protected utility functions which are required by multiple ch...
Media handler abstract base class for images.
normaliseParams( $image, &$params)
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
if(!is_readable( $file)) $ext