6 class DjVuHandler
extends ImageHandler
{
8 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
9 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
10 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
17 function mustRender() { return true; }
18 function isMultiPage() { return true; }
20 function getParamMap() {
22 'img_width' => 'width',
27 function validateParam( $name, $value ) {
28 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
39 function makeParamString( $params ) {
40 $page = isset( $params['page'] ) ?
$params['page'] : 1;
41 if ( !isset( $params['width'] ) ) {
44 return "page{$page}-{$params['width']}px";
47 function parseParamString( $str ) {
49 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
50 return array( 'width' => $m[2], 'page' => $m[1] );
56 function getScriptParams( $params ) {
58 'width' => $params['width'],
59 'page' => $params['page'],
63 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
64 global $wgDjvuRenderer, $wgDjvuPostProcessor;
66 // Fetch XML and check it, to give a more informative error message than the one which
67 // normaliseParams will inevitably give.
68 $xml = $image->getMetadata();
70 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
71 wfMsg( 'djvu_no_xml' ) );
74 if ( !$this->normaliseParams( $image, $params ) ) {
75 return new TransformParameterError( $params );
77 $width = $params['width'];
78 $height = $params['height'];
79 $srcPath = $image->getPath();
80 $page = $params['page'];
81 if ( $page > $this->pageCount( $image ) ) {
82 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
85 if ( $flags & self
::TRANSFORM_LATER
) {
86 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
89 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
90 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
93 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
94 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
95 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
96 wfEscapeShellArg( $srcPath );
97 if ( $wgDjvuPostProcessor ) {
98 $cmd .= " | {$wgDjvuPostProcessor}";
100 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
101 wfProfileIn( 'ddjvu' );
102 wfDebug( __METHOD__
.": $cmd\n" );
103 $err = wfShellExec( $cmd, $retval );
104 wfProfileOut( 'ddjvu' );
106 $removed = $this->removeBadFile( $dstPath, $retval );
107 if ( $retval != 0 ||
$removed ) {
108 wfDebugLog( 'thumbnail',
109 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
110 wfHostname(), $retval, trim($err), $cmd ) );
111 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
113 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
118 * Cache an instance of DjVuImage in an Image object, return that instance
120 function getDjVuImage( $image, $path ) {
122 $deja = new DjVuImage( $path );
123 } elseif ( !isset( $image->dejaImage
) ) {
124 $deja = $image->dejaImage
= new DjVuImage( $path );
126 $deja = $image->dejaImage
;
132 * Cache a document tree for the DjVu XML metadata
134 function getMetaTree( $image ) {
135 if ( isset( $image->dejaMetaTree
) ) {
136 return $image->dejaMetaTree
;
139 $metadata = $image->getMetadata();
140 if ( !$this->isMetadataValid( $image, $metadata ) ) {
141 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
144 wfProfileIn( __METHOD__
);
146 wfSuppressWarnings();
148 $image->dejaMetaTree
= new SimpleXMLElement( $metadata );
149 } catch( Exception
$e ) {
150 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
151 // Set to false rather than null to avoid further attempts
152 $image->dejaMetaTree
= false;
155 wfProfileOut( __METHOD__
);
156 return $image->dejaMetaTree
;
159 function getImageSize( $image, $path ) {
160 return $this->getDjVuImage( $image, $path )->getImageSize();
163 function getThumbType( $ext, $mime ) {
164 global $wgDjvuOutputExtension;
166 if ( !isset( $mime ) ) {
167 $magic = MimeMagic
::singleton();
168 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
170 return array( $wgDjvuOutputExtension, $mime );
173 function getMetadata( $image, $path ) {
174 wfDebug( "Getting DjVu metadata for $path\n" );
175 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
178 function getMetadataType( $image ) {
182 function isMetadataValid( $image, $metadata ) {
183 return !empty( $metadata ) && $metadata != serialize(array());
186 function pageCount( $image ) {
187 $tree = $this->getMetaTree( $image );
191 return count( $tree->xpath( '//OBJECT' ) );
194 function getPageDimensions( $image, $page ) {
195 $tree = $this->getMetaTree( $image );
200 $o = $tree->BODY
[0]->OBJECT[$page-1];
203 'width' => intval( $o['width'] ),
204 'height' => intval( $o['height'] )