3 * Handler for DjVu images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Shell\Shell
;
27 * Handler for DjVu images
31 class DjVuHandler
extends ImageHandler
{
32 const EXPENSIVE_SIZE_LIMIT
= 10485760; // 10MiB
37 public function isEnabled() {
38 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
39 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
40 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
52 public function mustRender( $file ) {
57 * True if creating thumbnails from the file is large or otherwise resource-intensive.
61 public function isExpensiveToThumbnail( $file ) {
62 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT
;
69 public function isMultiPage( $file ) {
76 public function getParamMap() {
78 'img_width' => 'width',
88 public function validateParam( $name, $value ) {
89 if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
90 // Extra junk on the end of page, probably actually a caption
91 // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
94 if ( in_array( $name, [ 'width', 'height', 'page' ] ) ) {
106 * @param array $params
107 * @return bool|string
109 public function makeParamString( $params ) {
110 $page = $params['page'] ??
1;
111 if ( !isset( $params['width'] ) ) {
115 return "page{$page}-{$params['width']}px";
122 public function parseParamString( $str ) {
124 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
125 return [ 'width' => $m[2], 'page' => $m[1] ];
132 * @param array $params
135 protected function getScriptParams( $params ) {
137 'width' => $params['width'],
138 'page' => $params['page'],
144 * @param string $dstPath
145 * @param string $dstUrl
146 * @param array $params
148 * @return MediaTransformError|ThumbnailImage|TransformParameterError
150 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
151 global $wgDjvuRenderer, $wgDjvuPostProcessor;
153 if ( !$this->normaliseParams( $image, $params ) ) {
154 return new TransformParameterError( $params );
156 $width = $params['width'];
157 $height = $params['height'];
158 $page = $params['page'];
160 if ( $flags & self
::TRANSFORM_LATER
) {
167 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
170 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
171 return new MediaTransformError(
175 wfMessage( 'thumbnail_dest_directory' )
179 // Get local copy source for shell scripts
180 // Thumbnail extraction is very inefficient for large files.
181 // Provide a way to pool count limit the number of downloaders.
182 if ( $image->getSize() >= 1e7
) { // 10MB
183 $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
185 'doWork' => function () use ( $image ) {
186 return $image->getLocalRefPath();
190 $srcPath = $work->execute();
192 $srcPath = $image->getLocalRefPath();
195 if ( $srcPath === false ) { // Failed to get local copy
196 wfDebugLog( 'thumbnail',
197 sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
198 wfHostname(), $image->getName() ) );
200 return new MediaTransformError( 'thumbnail_error',
201 $params['width'], $params['height'],
202 wfMessage( 'filemissing' )
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']}",
214 if ( $wgDjvuPostProcessor ) {
215 $cmd .= " | {$wgDjvuPostProcessor}";
217 $cmd .= ' > ' . Shell
::escape( $dstPath ) . ') 2>&1';
218 wfDebug( __METHOD__
. ": $cmd\n" );
220 $err = wfShellExec( $cmd, $retval );
222 $removed = $this->removeBadFile( $dstPath, $retval );
223 if ( $retval != 0 ||
$removed ) {
224 $this->logErrorForExternalProcess( $retval, $err, $cmd );
225 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
233 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
238 * Cache an instance of DjVuImage in an Image object, return that instance
240 * @param File|FSFile $image
241 * @param string $path
244 function getDjVuImage( $image, $path ) {
246 $deja = new DjVuImage( $path );
247 } elseif ( !isset( $image->dejaImage
) ) {
248 $deja = $image->dejaImage
= new DjVuImage( $path );
250 $deja = $image->dejaImage
;
257 * Get metadata, unserializing it if necessary.
259 * @param File $file The DjVu file in question
260 * @return string XML metadata as a string.
261 * @throws MWException
263 private function getUnserializedMetadata( File
$file ) {
264 $metadata = $file->getMetadata();
265 if ( substr( $metadata, 0, 3 ) === '<?xml' ) {
266 // Old style. Not serialized but instead just a raw string of XML.
270 Wikimedia\
suppressWarnings();
271 $unser = unserialize( $metadata );
272 Wikimedia\restoreWarnings
();
273 if ( is_array( $unser ) ) {
274 if ( isset( $unser['error'] ) ) {
276 } elseif ( isset( $unser['xml'] ) ) {
277 return $unser['xml'];
279 // Should never ever reach here.
280 throw new MWException( "Error unserializing DjVu metadata." );
284 // unserialize failed. Guess it wasn't really serialized after all,
289 * Cache a document tree for the DjVu XML metadata
291 * @param bool $gettext DOCUMENT (Default: false)
292 * @return bool|SimpleXMLElement
294 public function getMetaTree( $image, $gettext = false ) {
295 if ( $gettext && isset( $image->djvuTextTree
) ) {
296 return $image->djvuTextTree
;
298 if ( !$gettext && isset( $image->dejaMetaTree
) ) {
299 return $image->dejaMetaTree
;
302 $metadata = $this->getUnserializedMetadata( $image );
303 if ( !$this->isMetadataValid( $image, $metadata ) ) {
304 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
309 $trees = $this->extractTreesFromMetadata( $metadata );
310 $image->djvuTextTree
= $trees['TextTree'];
311 $image->dejaMetaTree
= $trees['MetaTree'];
314 return $image->djvuTextTree
;
316 return $image->dejaMetaTree
;
321 * Extracts metadata and text trees from metadata XML in string form
322 * @param string $metadata XML metadata as a string
325 protected function extractTreesFromMetadata( $metadata ) {
326 Wikimedia\
suppressWarnings();
328 // Set to false rather than null to avoid further attempts
331 $tree = new SimpleXMLElement( $metadata, LIBXML_PARSEHUGE
);
332 if ( $tree->getName() == 'mw-djvu' ) {
333 /** @var SimpleXMLElement $b */
334 foreach ( $tree->children() as $b ) {
335 if ( $b->getName() == 'DjVuTxt' ) {
336 // @todo File::djvuTextTree and File::dejaMetaTree are declared
337 // dynamically. Add a public File::$data to facilitate this?
339 } elseif ( $b->getName() == 'DjVuXML' ) {
346 } catch ( Exception
$e ) {
347 wfDebug( "Bogus multipage XML metadata\n" );
349 Wikimedia\restoreWarnings
();
351 return [ 'MetaTree' => $metaTree, 'TextTree' => $textTree ];
354 function getImageSize( $image, $path ) {
355 return $this->getDjVuImage( $image, $path )->getImageSize();
358 public function getThumbType( $ext, $mime, $params = null ) {
359 global $wgDjvuOutputExtension;
361 if ( !isset( $mime ) ) {
362 $magic = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer();
363 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
366 return [ $wgDjvuOutputExtension, $mime ];
369 public function getMetadata( $image, $path ) {
370 wfDebug( "Getting DjVu metadata for $path\n" );
372 $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
373 if ( $xml === false ) {
374 // Special value so that we don't repetitively try and decode a broken file.
375 return serialize( [ 'error' => 'Error extracting metadata' ] );
377 return serialize( [ 'xml' => $xml ] );
381 function getMetadataType( $image ) {
385 public function isMetadataValid( $image, $metadata ) {
386 return !empty( $metadata ) && $metadata != serialize( [] );
389 public function pageCount( File
$image ) {
390 $info = $this->getDimensionInfo( $image );
392 return $info ?
$info['pageCount'] : false;
395 public function getPageDimensions( File
$image, $page ) {
396 $index = $page - 1; // MW starts pages at 1
398 $info = $this->getDimensionInfo( $image );
399 if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
400 return $info['dimensionsByPage'][$index];
406 protected function getDimensionInfo( File
$file ) {
407 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
408 return $cache->getWithSetCallback(
409 $cache->makeKey( 'file-djvu', 'dimensions', $file->getSha1() ),
410 $cache::TTL_INDEFINITE
,
411 function () use ( $file ) {
412 $tree = $this->getMetaTree( $file );
413 return $this->getDimensionInfoFromMetaTree( $tree );
415 [ 'pcTTL' => $cache::TTL_INDEFINITE
]
420 * Given an XML metadata tree, returns dimension information about the document
421 * @param bool|SimpleXMLElement $metatree The file's XML metadata tree
424 protected function getDimensionInfoFromMetaTree( $metatree ) {
430 $count = count( $metatree->xpath( '//OBJECT' ) );
431 for ( $i = 0; $i < $count; $i++
) {
432 $o = $metatree->BODY
[0]->OBJECT[$i];
435 'width' => (int)$o['width'],
436 'height' => (int)$o['height'],
439 $dimsByPage[$i] = false;
443 return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
448 * @param int $page Page number to get information for
449 * @return bool|string Page text or false when no text found.
451 function getPageText( File
$image, $page ) {
452 $tree = $this->getMetaTree( $image, true );
457 $o = $tree->BODY
[0]->PAGE
[$page - 1];