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 $width = isset( $params['width'] ) ?
$params['width'] : 0;
124 $height = isset( $params['height'] ) ?
$params['height'] : 0;
125 return new MediaTransformError( 'thumbnail_error', $width, $height,
126 wfMsg( 'djvu_no_xml' ) );
129 if ( !$this->normaliseParams( $image, $params ) ) {
130 return new TransformParameterError( $params );
132 $width = $params['width'];
133 $height = $params['height'];
134 $srcPath = $image->getPath();
135 $page = $params['page'];
136 if ( $page > $this->pageCount( $image ) ) {
137 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
140 if ( $flags & self
::TRANSFORM_LATER
) {
141 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
144 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
145 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
148 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
149 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
150 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
151 " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
152 wfEscapeShellArg( $srcPath );
153 if ( $wgDjvuPostProcessor ) {
154 $cmd .= " | {$wgDjvuPostProcessor}";
156 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
157 wfProfileIn( 'ddjvu' );
158 wfDebug( __METHOD__
.": $cmd\n" );
160 $err = wfShellExec( $cmd, $retval );
161 wfProfileOut( 'ddjvu' );
163 $removed = $this->removeBadFile( $dstPath, $retval );
164 if ( $retval != 0 ||
$removed ) {
165 wfDebugLog( 'thumbnail',
166 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
167 wfHostname(), $retval, trim($err), $cmd ) );
168 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
170 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
175 * Cache an instance of DjVuImage in an Image object, return that instance
179 function getDjVuImage( $image, $path ) {
181 $deja = new DjVuImage( $path );
182 } elseif ( !isset( $image->dejaImage
) ) {
183 $deja = $image->dejaImage
= new DjVuImage( $path );
185 $deja = $image->dejaImage
;
191 * Cache a document tree for the DjVu XML metadata
194 function getMetaTree( $image , $gettext = false ) {
195 if ( isset( $image->dejaMetaTree
) ) {
196 return $image->dejaMetaTree
;
199 $metadata = $image->getMetadata();
200 if ( !$this->isMetadataValid( $image, $metadata ) ) {
201 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
204 wfProfileIn( __METHOD__
);
206 wfSuppressWarnings();
208 // Set to false rather than null to avoid further attempts
209 $image->dejaMetaTree
= false;
210 $image->djvuTextTree
= false;
211 $tree = new SimpleXMLElement( $metadata );
212 if( $tree->getName() == 'mw-djvu' ) {
213 foreach($tree->children() as $b){
214 if( $b->getName() == 'DjVuTxt' ) {
215 $image->djvuTextTree
= $b;
217 elseif ( $b->getName() == 'DjVuXML' ) {
218 $image->dejaMetaTree
= $b;
222 $image->dejaMetaTree
= $tree;
224 } catch( Exception
$e ) {
225 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
228 wfProfileOut( __METHOD__
);
230 return $image->djvuTextTree
;
232 return $image->dejaMetaTree
;
236 function getImageSize( $image, $path ) {
237 return $this->getDjVuImage( $image, $path )->getImageSize();
240 function getThumbType( $ext, $mime, $params = null ) {
241 global $wgDjvuOutputExtension;
243 if ( !isset( $mime ) ) {
244 $magic = MimeMagic
::singleton();
245 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
247 return array( $wgDjvuOutputExtension, $mime );
250 function getMetadata( $image, $path ) {
251 wfDebug( "Getting DjVu metadata for $path\n" );
252 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
255 function getMetadataType( $image ) {
259 function isMetadataValid( $image, $metadata ) {
260 return !empty( $metadata ) && $metadata != serialize(array());
263 function pageCount( $image ) {
264 $tree = $this->getMetaTree( $image );
268 return count( $tree->xpath( '//OBJECT' ) );
271 function getPageDimensions( $image, $page ) {
272 $tree = $this->getMetaTree( $image );
277 $o = $tree->BODY
[0]->OBJECT[$page-1];
280 'width' => intval( $o['width'] ),
281 'height' => intval( $o['height'] )
288 function getPageText( $image, $page ){
289 $tree = $this->getMetaTree( $image, true );
294 $o = $tree->BODY
[0]->PAGE
[$page-1];