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
25 * Handler for DjVu images
29 class DjVuHandler
extends ImageHandler
{
30 const EXPENSIVE_SIZE_LIMIT
= 10485760; // 10MiB
35 function isEnabled() {
36 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
37 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
38 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
50 function mustRender( $file ) {
55 * True if creating thumbnails from the file is large or otherwise resource-intensive.
59 public function isExpensiveToThumbnail( $file ) {
60 return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT
;
67 function isMultiPage( $file ) {
74 function getParamMap() {
76 'img_width' => 'width',
86 function validateParam( $name, $value ) {
87 if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
88 // Extra junk on the end of page, probably actually a caption
89 // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
92 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
104 * @param array $params
105 * @return bool|string
107 function makeParamString( $params ) {
108 $page = isset( $params['page'] ) ?
$params['page'] : 1;
109 if ( !isset( $params['width'] ) ) {
113 return "page{$page}-{$params['width']}px";
120 function parseParamString( $str ) {
122 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
123 return array( 'width' => $m[2], 'page' => $m[1] );
130 * @param array $params
133 function getScriptParams( $params ) {
135 'width' => $params['width'],
136 'page' => $params['page'],
142 * @param string $dstPath
143 * @param string $dstUrl
144 * @param array $params
146 * @return MediaTransformError|ThumbnailImage|TransformParameterError
148 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
149 global $wgDjvuRenderer, $wgDjvuPostProcessor;
151 if ( !$this->normaliseParams( $image, $params ) ) {
152 return new TransformParameterError( $params );
154 $width = $params['width'];
155 $height = $params['height'];
156 $page = $params['page'];
158 if ( $flags & self
::TRANSFORM_LATER
) {
165 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
168 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
169 return new MediaTransformError(
173 wfMessage( 'thumbnail_dest_directory' )->text()
177 // Get local copy source for shell scripts
178 // Thumbnail extraction is very inefficient for large files.
179 // Provide a way to pool count limit the number of downloaders.
180 if ( $image->getSize() >= 1e7
) { // 10MB
181 $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
183 'doWork' => function () use ( $image ) {
184 return $image->getLocalRefPath();
188 $srcPath = $work->execute();
190 $srcPath = $image->getLocalRefPath();
193 if ( $srcPath === false ) { // Failed to get local copy
194 wfDebugLog( 'thumbnail',
195 sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
196 wfHostname(), $image->getName() ) );
198 return new MediaTransformError( 'thumbnail_error',
199 $params['width'], $params['height'],
200 wfMessage( 'filemissing' )->text()
204 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
205 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
206 $cmd = '(' . wfEscapeShellArg(
210 "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
212 if ( $wgDjvuPostProcessor ) {
213 $cmd .= " | {$wgDjvuPostProcessor}";
215 $cmd .= ' > ' . wfEscapeShellArg( $dstPath ) . ') 2>&1';
216 wfDebug( __METHOD__
. ": $cmd\n" );
218 $err = wfShellExec( $cmd, $retval );
220 $removed = $this->removeBadFile( $dstPath, $retval );
221 if ( $retval != 0 ||
$removed ) {
222 $this->logErrorForExternalProcess( $retval, $err, $cmd );
223 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
231 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
236 * Cache an instance of DjVuImage in an Image object, return that instance
239 * @param string $path
242 function getDjVuImage( $image, $path ) {
244 $deja = new DjVuImage( $path );
245 } elseif ( !isset( $image->dejaImage
) ) {
246 $deja = $image->dejaImage
= new DjVuImage( $path );
248 $deja = $image->dejaImage
;
255 * Get metadata, unserializing it if neccessary.
257 * @param File $file The DjVu file in question
258 * @return string XML metadata as a string.
259 * @throws MWException
261 private function getUnserializedMetadata( File
$file ) {
262 $metadata = $file->getMetadata();
263 if ( substr( $metadata, 0, 3 ) === '<?xml' ) {
264 // Old style. Not serialized but instead just a raw string of XML.
268 MediaWiki\
suppressWarnings();
269 $unser = unserialize( $metadata );
270 MediaWiki\restoreWarnings
();
271 if ( is_array( $unser ) ) {
272 if ( isset( $unser['error'] ) ) {
274 } elseif ( isset( $unser['xml'] ) ) {
275 return $unser['xml'];
277 // Should never ever reach here.
278 throw new MWException( "Error unserializing DjVu metadata." );
282 // unserialize failed. Guess it wasn't really serialized after all,
287 * Cache a document tree for the DjVu XML metadata
289 * @param bool $gettext DOCUMENT (Default: false)
290 * @return bool|SimpleXMLElement
292 public function getMetaTree( $image, $gettext = false ) {
293 if ( $gettext && isset( $image->djvuTextTree
) ) {
294 return $image->djvuTextTree
;
296 if ( !$gettext && isset( $image->dejaMetaTree
) ) {
297 return $image->dejaMetaTree
;
300 $metadata = $this->getUnserializedMetadata( $image );
301 if ( !$this->isMetadataValid( $image, $metadata ) ) {
302 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
307 MediaWiki\
suppressWarnings();
309 // Set to false rather than null to avoid further attempts
310 $image->dejaMetaTree
= false;
311 $image->djvuTextTree
= false;
312 $tree = new SimpleXMLElement( $metadata, LIBXML_PARSEHUGE
);
313 if ( $tree->getName() == 'mw-djvu' ) {
314 /** @var SimpleXMLElement $b */
315 foreach ( $tree->children() as $b ) {
316 if ( $b->getName() == 'DjVuTxt' ) {
317 // @todo File::djvuTextTree and File::dejaMetaTree are declared
318 // dynamically. Add a public File::$data to facilitate this?
319 $image->djvuTextTree
= $b;
320 } elseif ( $b->getName() == 'DjVuXML' ) {
321 $image->dejaMetaTree
= $b;
325 $image->dejaMetaTree
= $tree;
327 } catch ( Exception
$e ) {
328 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
330 MediaWiki\restoreWarnings
();
332 return $image->djvuTextTree
;
334 return $image->dejaMetaTree
;
340 * @param string $path
341 * @return bool|array False on failure
343 function getImageSize( $image, $path ) {
344 return $this->getDjVuImage( $image, $path )->getImageSize();
347 function getThumbType( $ext, $mime, $params = null ) {
348 global $wgDjvuOutputExtension;
350 if ( !isset( $mime ) ) {
351 $magic = MimeMagic
::singleton();
352 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
355 return array( $wgDjvuOutputExtension, $mime );
358 function getMetadata( $image, $path ) {
359 wfDebug( "Getting DjVu metadata for $path\n" );
361 $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
362 if ( $xml === false ) {
363 // Special value so that we don't repetitively try and decode a broken file.
364 return serialize( array( 'error' => 'Error extracting metadata' ) );
366 return serialize( array( 'xml' => $xml ) );
370 function getMetadataType( $image ) {
374 function isMetadataValid( $image, $metadata ) {
375 return !empty( $metadata ) && $metadata != serialize( array() );
378 function pageCount( File
$image ) {
379 $info = $this->getDimensionInfo( $image );
381 return $info ?
$info['pageCount'] : false;
384 function getPageDimensions( File
$image, $page ) {
385 $index = $page - 1; // MW starts pages at 1
387 $info = $this->getDimensionInfo( $image );
388 if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
389 return $info['dimensionsByPage'][$index];
395 protected function getDimensionInfo( File
$file ) {
398 return ObjectCache
::getMainWANInstance()->getWithSetCallback(
399 wfMemcKey( 'file-djvu', 'dimensions', $file->getSha1() ),
400 WANObjectCache
::TTL_INDEFINITE
,
401 function () use ( $that, $file ) {
402 $tree = $that->getMetaTree( $file );
407 $dimsByPage = array();
408 $count = count( $tree->xpath( '//OBJECT' ) );
409 for ( $i = 0; $i < $count; ++
$i ) {
410 $o = $tree->BODY
[0]->OBJECT[$i];
412 $dimsByPage[$i] = array(
413 'width' => (int)$o['width'],
414 'height' => (int)$o['height']
417 $dimsByPage[$i] = false;
421 return array( 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage );
428 * @param int $page Page number to get information for
429 * @return bool|string Page text or false when no text found.
431 function getPageText( File
$image, $page ) {
432 $tree = $this->getMetaTree( $image, true );
437 $o = $tree->BODY
[0]->PAGE
[$page - 1];