3 * Handler for DjVu images
10 * Handler for DjVu images
14 class DjVuHandler
extends ImageHandler
{
15 function isEnabled() {
16 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
17 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
18 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
25 function mustRender( $file ) { return true; }
26 function isMultiPage( $file ) { return true; }
28 function getParamMap() {
30 'img_width' => 'width',
35 function validateParam( $name, $value ) {
36 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
47 function makeParamString( $params ) {
48 $page = isset( $params['page'] ) ?
$params['page'] : 1;
49 if ( !isset( $params['width'] ) ) {
52 return "page{$page}-{$params['width']}px";
55 function parseParamString( $str ) {
57 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
58 return array( 'width' => $m[2], 'page' => $m[1] );
64 function getScriptParams( $params ) {
66 'width' => $params['width'],
67 'page' => $params['page'],
71 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
72 global $wgDjvuRenderer, $wgDjvuPostProcessor;
74 // Fetch XML and check it, to give a more informative error message than the one which
75 // normaliseParams will inevitably give.
76 $xml = $image->getMetadata();
78 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
79 wfMsg( 'djvu_no_xml' ) );
82 if ( !$this->normaliseParams( $image, $params ) ) {
83 return new TransformParameterError( $params );
85 $width = $params['width'];
86 $height = $params['height'];
87 $srcPath = $image->getPath();
88 $page = $params['page'];
89 if ( $page > $this->pageCount( $image ) ) {
90 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
93 if ( $flags & self
::TRANSFORM_LATER
) {
94 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
97 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
98 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
101 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
102 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
103 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
104 wfEscapeShellArg( $srcPath );
105 if ( $wgDjvuPostProcessor ) {
106 $cmd .= " | {$wgDjvuPostProcessor}";
108 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
109 wfProfileIn( 'ddjvu' );
110 wfDebug( __METHOD__
.": $cmd\n" );
112 $err = wfShellExec( $cmd, $retval );
113 wfProfileOut( 'ddjvu' );
115 $removed = $this->removeBadFile( $dstPath, $retval );
116 if ( $retval != 0 ||
$removed ) {
117 wfDebugLog( 'thumbnail',
118 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
119 wfHostname(), $retval, trim($err), $cmd ) );
120 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
122 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
127 * Cache an instance of DjVuImage in an Image object, return that instance
129 function getDjVuImage( $image, $path ) {
131 $deja = new DjVuImage( $path );
132 } elseif ( !isset( $image->dejaImage
) ) {
133 $deja = $image->dejaImage
= new DjVuImage( $path );
135 $deja = $image->dejaImage
;
141 * Cache a document tree for the DjVu XML metadata
143 function getMetaTree( $image , $gettext = false ) {
144 if ( isset( $image->dejaMetaTree
) ) {
145 return $image->dejaMetaTree
;
148 $metadata = $image->getMetadata();
149 if ( !$this->isMetadataValid( $image, $metadata ) ) {
150 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
153 wfProfileIn( __METHOD__
);
155 wfSuppressWarnings();
157 // Set to false rather than null to avoid further attempts
158 $image->dejaMetaTree
= false;
159 $image->djvuTextTree
= false;
160 $tree = new SimpleXMLElement( $metadata );
161 if( $tree->getName() == 'mw-djvu' ) {
162 foreach($tree->children() as $b){
163 if( $b->getName() == 'DjVuTxt' ) {
164 $image->djvuTextTree
= $b;
166 else if ( $b->getName() == 'DjVuXML' ) {
167 $image->dejaMetaTree
= $b;
171 $image->dejaMetaTree
= $tree;
173 } catch( Exception
$e ) {
174 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
177 wfProfileOut( __METHOD__
);
179 return $image->djvuTextTree
;
181 return $image->dejaMetaTree
;
185 function getImageSize( $image, $path ) {
186 return $this->getDjVuImage( $image, $path )->getImageSize();
189 function getThumbType( $ext, $mime, $params = null ) {
190 global $wgDjvuOutputExtension;
192 if ( !isset( $mime ) ) {
193 $magic = MimeMagic
::singleton();
194 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
196 return array( $wgDjvuOutputExtension, $mime );
199 function getMetadata( $image, $path ) {
200 wfDebug( "Getting DjVu metadata for $path\n" );
201 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
204 function getMetadataType( $image ) {
208 function isMetadataValid( $image, $metadata ) {
209 return !empty( $metadata ) && $metadata != serialize(array());
212 function pageCount( $image ) {
213 $tree = $this->getMetaTree( $image );
217 return count( $tree->xpath( '//OBJECT' ) );
220 function getPageDimensions( $image, $page ) {
221 $tree = $this->getMetaTree( $image );
226 $o = $tree->BODY
[0]->OBJECT[$page-1];
229 'width' => intval( $o['width'] ),
230 'height' => intval( $o['height'] )
237 function getPageText( $image, $page ){
238 $tree = $this->getMetaTree( $image, true );
243 $o = $tree->BODY
[0]->PAGE
[$page-1];