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->getLocalRefPath();
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
193 * @param $gettext Boolean: DOCUMENT (Default: false)
196 function getMetaTree( $image , $gettext = false ) {
197 if ( isset( $image->dejaMetaTree
) ) {
198 return $image->dejaMetaTree
;
201 $metadata = $image->getMetadata();
202 if ( !$this->isMetadataValid( $image, $metadata ) ) {
203 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
206 wfProfileIn( __METHOD__
);
208 wfSuppressWarnings();
210 // Set to false rather than null to avoid further attempts
211 $image->dejaMetaTree
= false;
212 $image->djvuTextTree
= false;
213 $tree = new SimpleXMLElement( $metadata );
214 if( $tree->getName() == 'mw-djvu' ) {
215 foreach($tree->children() as $b){
216 if( $b->getName() == 'DjVuTxt' ) {
217 $image->djvuTextTree
= $b;
219 elseif ( $b->getName() == 'DjVuXML' ) {
220 $image->dejaMetaTree
= $b;
224 $image->dejaMetaTree
= $tree;
226 } catch( Exception
$e ) {
227 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
230 wfProfileOut( __METHOD__
);
232 return $image->djvuTextTree
;
234 return $image->dejaMetaTree
;
238 function getImageSize( $image, $path ) {
239 return $this->getDjVuImage( $image, $path )->getImageSize();
242 function getThumbType( $ext, $mime, $params = null ) {
243 global $wgDjvuOutputExtension;
245 if ( !isset( $mime ) ) {
246 $magic = MimeMagic
::singleton();
247 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
249 return array( $wgDjvuOutputExtension, $mime );
252 function getMetadata( $image, $path ) {
253 wfDebug( "Getting DjVu metadata for $path\n" );
254 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
257 function getMetadataType( $image ) {
261 function isMetadataValid( $image, $metadata ) {
262 return !empty( $metadata ) && $metadata != serialize(array());
265 function pageCount( $image ) {
266 $tree = $this->getMetaTree( $image );
270 return count( $tree->xpath( '//OBJECT' ) );
273 function getPageDimensions( $image, $page ) {
274 $tree = $this->getMetaTree( $image );
279 $o = $tree->BODY
[0]->OBJECT[$page-1];
282 'width' => intval( $o['width'] ),
283 'height' => intval( $o['height'] )
290 function getPageText( $image, $page ){
291 $tree = $this->getMetaTree( $image, true );
296 $o = $tree->BODY
[0]->PAGE
[$page-1];