34 private const EXPENSIVE_SIZE_LIMIT = 10485760;
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';
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" );
71 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
87 'img_width' =>
'width',
98 if ( $name ===
'page' && trim( $value ) !== (
string)intval( $value ) ) {
103 return in_array( $name, [
'width',
'height',
'page' ] ) && $value > 0;
111 $page = $params[
'page'] ?? 1;
112 if ( !isset( $params[
'width'] ) ) {
116 return "page{$page}-{$params['width']}px";
125 if ( preg_match(
'/^page(\d+)-(\d+)px$/', $str, $m ) ) {
126 return [
'width' => $m[2],
'page' => $m[1] ];
138 'width' => $params[
'width'],
139 'page' => $params[
'page'],
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 );
158 $width = $params[
'width'];
159 $height = $params[
'height'];
160 $page = $params[
'page'];
162 if ( $flags & self::TRANSFORM_LATER ) {
184 if ( $image->getSize() >= 1e7 ) {
187 'doWork' =>
static function () use ( $image ) {
188 return $image->getLocalRefPath();
194 $srcPath = $image->getLocalRefPath();
197 if ( $srcPath ===
false ) {
199 sprintf(
'Thumbnail failed on %s: could not get local copy of "%s"',
203 $params[
'width'], $params[
'height'],
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(
214 "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
216 if ( $djvuPostProcessor ) {
217 $cmd .=
" | {$djvuPostProcessor}";
219 $cmd .=
' > ' . Shell::escape( $dstPath ) .
') 2>&1';
220 wfDebug( __METHOD__ .
": $cmd" );
225 if ( $retval != 0 || $removed ) {
247 private function getDjVuImage( $state,
$path ) {
248 $deja = $state->getHandlerState( self::STATE_DJVU_IMAGE );
251 $state->setHandlerState( self::STATE_DJVU_IMAGE, $deja );
264 private function getMetadataInternal(
File $file, $gettext ) {
265 $itemNames = [
'error',
'_error',
'data' ];
267 $itemNames[] =
'text';
269 $unser =
$file->getMetadataItems( $itemNames );
271 if ( isset( $unser[
'error'] ) ) {
273 } elseif ( isset( $unser[
'_error'] ) ) {
287 if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) {
288 return $image->getHandlerState( self::STATE_TEXT_TREE );
290 if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) {
291 return $image->getHandlerState( self::STATE_META_TREE );
294 $metadata = $this->getMetadataInternal( $image, $gettext );
302 unset( $metadata[
'text'] );
308 $djvuOutputExtension = MediaWikiServices::getInstance()->getMainConfig()
309 ->get( MainConfigNames::DjvuOutputExtension );
311 if ( !isset(
$mime ) ) {
312 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
313 $mime = $magic->getMimeTypeFromExtensionOrNull( $djvuOutputExtension );
316 return [ $djvuOutputExtension,
$mime ];
320 wfDebug(
"Getting DjVu metadata for $path" );
322 $djvuImage = $this->getDjVuImage( $state,
$path );
323 $metadata = $djvuImage->retrieveMetaData();
324 if ( $metadata ===
false ) {
326 $metadata = [
'error' =>
'Error extracting metadata' ];
328 return [
'metadata' => $metadata ] + $djvuImage->getImageSize();
343 return $info ? $info[
'pageCount'] :
false;
350 if ( $info && isset( $info[
'dimensionsByPage'][$index] ) ) {
351 return $info[
'dimensionsByPage'][$index];
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 ) {
366 [
'pcTTL' => $cache::TTL_INDEFINITE ]
381 if ( !isset( $metatree[
'data'] ) || !$metatree[
'data'] ) {
384 foreach ( $metatree[
'data'][
'pages'] as $page ) {
386 $dimsByPage[] =
false;
389 'width' => (int)$page[
'width'],
390 'height' => (
int)$page[
'height'],
395 'pageCount' => count( $metatree[
'data'][
'pages'] ),
396 'dimensionsByPage' => $dimsByPage
410 if ( isset( $tree[
'text'] ) && isset( $tree[
'text'][$page - 1] ) ) {
411 return $tree[
'text'][$page - 1];
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 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)
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.
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....
A class containing constants representing the names of configuration variables.
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