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'],
77 * @return MediaTransformError|ThumbnailImage|TransformParameterError
79 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
80 global $wgDjvuRenderer, $wgDjvuPostProcessor;
82 // Fetch XML and check it, to give a more informative error message than the one which
83 // normaliseParams will inevitably give.
84 $xml = $image->getMetadata();
86 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
87 wfMsg( 'djvu_no_xml' ) );
90 if ( !$this->normaliseParams( $image, $params ) ) {
91 return new TransformParameterError( $params );
93 $width = $params['width'];
94 $height = $params['height'];
95 $srcPath = $image->getPath();
96 $page = $params['page'];
97 if ( $page > $this->pageCount( $image ) ) {
98 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
101 if ( $flags & self
::TRANSFORM_LATER
) {
102 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
105 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
106 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
109 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
110 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
111 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
112 wfEscapeShellArg( $srcPath );
113 if ( $wgDjvuPostProcessor ) {
114 $cmd .= " | {$wgDjvuPostProcessor}";
116 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
117 wfProfileIn( 'ddjvu' );
118 wfDebug( __METHOD__
.": $cmd\n" );
120 $err = wfShellExec( $cmd, $retval );
121 wfProfileOut( 'ddjvu' );
123 $removed = $this->removeBadFile( $dstPath, $retval );
124 if ( $retval != 0 ||
$removed ) {
125 wfDebugLog( 'thumbnail',
126 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
127 wfHostname(), $retval, trim($err), $cmd ) );
128 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
130 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
135 * Cache an instance of DjVuImage in an Image object, return that instance
137 function getDjVuImage( $image, $path ) {
139 $deja = new DjVuImage( $path );
140 } elseif ( !isset( $image->dejaImage
) ) {
141 $deja = $image->dejaImage
= new DjVuImage( $path );
143 $deja = $image->dejaImage
;
149 * Cache a document tree for the DjVu XML metadata
152 function getMetaTree( $image , $gettext = false ) {
153 if ( isset( $image->dejaMetaTree
) ) {
154 return $image->dejaMetaTree
;
157 $metadata = $image->getMetadata();
158 if ( !$this->isMetadataValid( $image, $metadata ) ) {
159 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
162 wfProfileIn( __METHOD__
);
164 wfSuppressWarnings();
166 // Set to false rather than null to avoid further attempts
167 $image->dejaMetaTree
= false;
168 $image->djvuTextTree
= false;
169 $tree = new SimpleXMLElement( $metadata );
170 if( $tree->getName() == 'mw-djvu' ) {
171 foreach($tree->children() as $b){
172 if( $b->getName() == 'DjVuTxt' ) {
173 $image->djvuTextTree
= $b;
175 else if ( $b->getName() == 'DjVuXML' ) {
176 $image->dejaMetaTree
= $b;
180 $image->dejaMetaTree
= $tree;
182 } catch( Exception
$e ) {
183 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
186 wfProfileOut( __METHOD__
);
188 return $image->djvuTextTree
;
190 return $image->dejaMetaTree
;
194 function getImageSize( $image, $path ) {
195 return $this->getDjVuImage( $image, $path )->getImageSize();
198 function getThumbType( $ext, $mime, $params = null ) {
199 global $wgDjvuOutputExtension;
201 if ( !isset( $mime ) ) {
202 $magic = MimeMagic
::singleton();
203 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
205 return array( $wgDjvuOutputExtension, $mime );
208 function getMetadata( $image, $path ) {
209 wfDebug( "Getting DjVu metadata for $path\n" );
210 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
213 function getMetadataType( $image ) {
217 function isMetadataValid( $image, $metadata ) {
218 return !empty( $metadata ) && $metadata != serialize(array());
221 function pageCount( $image ) {
222 $tree = $this->getMetaTree( $image );
226 return count( $tree->xpath( '//OBJECT' ) );
229 function getPageDimensions( $image, $page ) {
230 $tree = $this->getMetaTree( $image );
235 $o = $tree->BODY
[0]->OBJECT[$page-1];
238 'width' => intval( $o['width'] ),
239 'height' => intval( $o['height'] )
246 function getPageText( $image, $page ){
247 $tree = $this->getMetaTree( $image, true );
252 $o = $tree->BODY
[0]->PAGE
[$page-1];