45 wfDebug(
"DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" );
67 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
83 'img_width' =>
'width',
94 if ( $name ===
'page' && trim( $value ) !== (
string)intval( $value ) ) {
99 return in_array( $name, [
'width',
'height',
'page' ] ) && $value > 0;
107 $page = $params[
'page'] ?? 1;
108 if ( !isset( $params[
'width'] ) ) {
112 return "page{$page}-{$params['width']}px";
121 if ( preg_match(
'/^page(\d+)-(\d+)px$/', $str, $m ) ) {
122 return [
'width' => $m[2],
'page' => $m[1] ];
134 'width' => $params[
'width'],
135 'page' => $params[
'page'],
147 public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
153 $width = $params[
'width'];
154 $height = $params[
'height'];
155 $page = $params[
'page'];
157 if ( $flags & self::TRANSFORM_LATER ) {
179 if ( $image->getSize() >= 1e7 ) {
182 'doWork' =>
static function () use ( $image ) {
183 return $image->getLocalRefPath();
189 $srcPath = $image->getLocalRefPath();
192 if ( $srcPath ===
false ) {
194 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
198 $params[
'width'], $params[
'height'],
203 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
204 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
205 $cmd =
'(' . Shell::escape(
209 "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
212 $cmd .=
" | {$wgDjvuPostProcessor}";
214 $cmd .=
' > ' . Shell::escape( $dstPath ) .
') 2>&1';
215 wfDebug( __METHOD__ .
": $cmd" );
220 if ( $retval != 0 || $removed ) {
243 $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
246 $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
259 $unser =
$file->getMetadataArray();
260 if ( isset( $unser[
'error'] ) ) {
262 } elseif ( isset( $unser[
'xml'] ) ) {
263 return $unser[
'xml'];
264 } elseif ( isset( $unser[
'_error'] )
265 && is_string( $unser[
'_error'] )
266 && substr( $unser[
'_error'], 0, 3 ) ===
'<?xml'
269 return $unser[
'_error'];
282 if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) {
283 return $image->getHandlerState( self::STATE_TEXT_TREE );
285 if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) {
286 return $image->getHandlerState( self::STATE_META_TREE );
291 wfDebug(
"DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow" );
297 $image->setHandlerState( self::STATE_TEXT_TREE, $trees[
'TextTree'] );
298 $image->setHandlerState( self::STATE_META_TREE, $trees[
'MetaTree'] );
301 return $trees[
'TextTree'];
303 return $trees[
'MetaTree'];
313 Wikimedia\suppressWarnings();
318 $tree =
new SimpleXMLElement( $xml, LIBXML_PARSEHUGE );
319 if ( $tree->getName() ==
'mw-djvu' ) {
321 foreach ( $tree->children() as $b ) {
322 if ( $b->getName() ==
'DjVuTxt' ) {
326 } elseif ( $b->getName() ==
'DjVuXML' ) {
333 }
catch ( Exception $e ) {
334 wfDebug(
"Bogus multipage XML metadata" );
336 Wikimedia\restoreWarnings();
338 return [
'MetaTree' => $metaTree,
'TextTree' => $textTree ];
344 if ( !isset(
$mime ) ) {
345 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
353 wfDebug(
"Getting DjVu metadata for $path" );
356 $xml = $djvuImage->retrieveMetaData();
357 if ( $xml ===
false ) {
359 $metadata = [
'error' =>
'Error extracting metadata' ];
361 $metadata = [
'xml' => $xml ];
363 return [
'metadata' => $metadata ] + $djvuImage->getImageSize();
377 return $info ? $info[
'pageCount'] :
false;
384 if ( $info && isset( $info[
'dimensionsByPage'][$index] ) ) {
385 return $info[
'dimensionsByPage'][$index];
392 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
393 return $cache->getWithSetCallback(
394 $cache->makeKey(
'file-djvu',
'dimensions',
$file->getSha1() ),
395 $cache::TTL_INDEFINITE,
396 function () use (
$file ) {
400 [
'pcTTL' => $cache::TTL_INDEFINITE ]
415 $count = count( $metatree->xpath(
'//OBJECT' ) );
416 for ( $i = 0; $i < $count; $i++ ) {
417 $o = $metatree->BODY[0]->OBJECT[$i];
420 'width' => (int)$o[
'width'],
421 'height' => (
int)$o[
'height'],
424 $dimsByPage[$i] =
false;
428 return [
'pageCount' => $count,
'dimensionsByPage' => $dimsByPage ];
442 $o = $tree->BODY[0]->PAGE[$page - 1];
443 return $o ? (string)$o[
'value'] :
false;
$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.
$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.
makeParamString( $params)
isExpensiveToThumbnail( $file)
True if creating thumbnails from the file is large or otherwise resource-intensive.
validateParam( $name, $value)
getDjVuImage( $state, $path)
Cache an instance of DjVuImage in a MediaHandlerState object, return that instance.
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.
getXMLMetadata(File $file)
Get metadata, unserializing it if necessary.
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.
extractTreesFromXML( $xml)
Extracts metadata and text trees from metadata XML in string form.
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.
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)
Changes the parameter array as necessary, ready for transformation.Should be idempotent....
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