3 * Handler for DjVu images
10 * Handler for DjVu images
14 class DjVuHandler
extends ImageHandler
{
19 function isEnabled() {
20 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
21 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
22 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
33 function mustRender( $file ) {
41 function isMultiPage( $file ) {
48 function getParamMap() {
50 'img_width' => 'width',
60 function validateParam( $name, $value ) {
61 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
76 function makeParamString( $params ) {
77 $page = isset( $params['page'] ) ?
$params['page'] : 1;
78 if ( !isset( $params['width'] ) ) {
81 return "page{$page}-{$params['width']}px";
88 function parseParamString( $str ) {
90 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
91 return array( 'width' => $m[2], 'page' => $m[1] );
101 function getScriptParams( $params ) {
103 'width' => $params['width'],
104 'page' => $params['page'],
114 * @return MediaTransformError|ThumbnailImage|TransformParameterError
116 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
117 global $wgDjvuRenderer, $wgDjvuPostProcessor;
119 // Fetch XML and check it, to give a more informative error message than the one which
120 // normaliseParams will inevitably give.
121 $xml = $image->getMetadata();
123 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
124 wfMsg( 'djvu_no_xml' ) );
127 if ( !$this->normaliseParams( $image, $params ) ) {
128 return new TransformParameterError( $params );
130 $width = $params['width'];
131 $height = $params['height'];
132 $srcPath = $image->getPath();
133 $page = $params['page'];
134 if ( $page > $this->pageCount( $image ) ) {
135 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
138 if ( $flags & self
::TRANSFORM_LATER
) {
139 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
142 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
143 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
146 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
147 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
148 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
149 wfEscapeShellArg( $srcPath );
150 if ( $wgDjvuPostProcessor ) {
151 $cmd .= " | {$wgDjvuPostProcessor}";
153 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
154 wfProfileIn( 'ddjvu' );
155 wfDebug( __METHOD__
.": $cmd\n" );
157 $err = wfShellExec( $cmd, $retval );
158 wfProfileOut( 'ddjvu' );
160 $removed = $this->removeBadFile( $dstPath, $retval );
161 if ( $retval != 0 ||
$removed ) {
162 wfDebugLog( 'thumbnail',
163 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
164 wfHostname(), $retval, trim($err), $cmd ) );
165 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
167 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
172 * Cache an instance of DjVuImage in an Image object, return that instance
176 function getDjVuImage( $image, $path ) {
178 $deja = new DjVuImage( $path );
179 } elseif ( !isset( $image->dejaImage
) ) {
180 $deja = $image->dejaImage
= new DjVuImage( $path );
182 $deja = $image->dejaImage
;
188 * Cache a document tree for the DjVu XML metadata
191 function getMetaTree( $image , $gettext = false ) {
192 if ( isset( $image->dejaMetaTree
) ) {
193 return $image->dejaMetaTree
;
196 $metadata = $image->getMetadata();
197 if ( !$this->isMetadataValid( $image, $metadata ) ) {
198 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
201 wfProfileIn( __METHOD__
);
203 wfSuppressWarnings();
205 // Set to false rather than null to avoid further attempts
206 $image->dejaMetaTree
= false;
207 $image->djvuTextTree
= false;
208 $tree = new SimpleXMLElement( $metadata );
209 if( $tree->getName() == 'mw-djvu' ) {
210 foreach($tree->children() as $b){
211 if( $b->getName() == 'DjVuTxt' ) {
212 $image->djvuTextTree
= $b;
214 elseif ( $b->getName() == 'DjVuXML' ) {
215 $image->dejaMetaTree
= $b;
219 $image->dejaMetaTree
= $tree;
221 } catch( Exception
$e ) {
222 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
225 wfProfileOut( __METHOD__
);
227 return $image->djvuTextTree
;
229 return $image->dejaMetaTree
;
233 function getImageSize( $image, $path ) {
234 return $this->getDjVuImage( $image, $path )->getImageSize();
237 function getThumbType( $ext, $mime, $params = null ) {
238 global $wgDjvuOutputExtension;
240 if ( !isset( $mime ) ) {
241 $magic = MimeMagic
::singleton();
242 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
244 return array( $wgDjvuOutputExtension, $mime );
247 function getMetadata( $image, $path ) {
248 wfDebug( "Getting DjVu metadata for $path\n" );
249 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
252 function getMetadataType( $image ) {
256 function isMetadataValid( $image, $metadata ) {
257 return !empty( $metadata ) && $metadata != serialize(array());
260 function pageCount( $image ) {
261 $tree = $this->getMetaTree( $image );
265 return count( $tree->xpath( '//OBJECT' ) );
268 function getPageDimensions( $image, $page ) {
269 $tree = $this->getMetaTree( $image );
274 $o = $tree->BODY
[0]->OBJECT[$page-1];
277 'width' => intval( $o['width'] ),
278 'height' => intval( $o['height'] )
285 function getPageText( $image, $page ){
286 $tree = $this->getMetaTree( $image, true );
291 $o = $tree->BODY
[0]->PAGE
[$page-1];