10 class DjVuHandler
extends ImageHandler
{
11 function isEnabled() {
12 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
13 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
14 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
21 function mustRender( $file ) { return true; }
22 function isMultiPage( $file ) { return true; }
24 function getParamMap() {
26 'img_width' => 'width',
31 function validateParam( $name, $value ) {
32 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
43 function makeParamString( $params ) {
44 $page = isset( $params['page'] ) ?
$params['page'] : 1;
45 if ( !isset( $params['width'] ) ) {
48 return "page{$page}-{$params['width']}px";
51 function parseParamString( $str ) {
53 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
54 return array( 'width' => $m[2], 'page' => $m[1] );
55 } else if ( preg_match( '/^page(\d+)-djvutxt$/', $str, $m ) ) {
56 return array( 'djvutxt' => 1, 'page' => $m[1] );
62 function getScriptParams( $params ) {
64 'width' => $params['width'],
65 'page' => $params['page'],
70 function normaliseParams( $image, &$params ) {
72 if( $wgDjvuTxt && isset($params['djvutxt']) && $params['djvutxt']) {
73 if ( !isset( $params['page'] ) ) {
77 $params['height'] = 0;
80 else return parent
::normaliseParams( $image, $params );
83 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
84 global $wgDjvuRenderer, $wgDjvuPostProcessor, $wgDjvuTxt, $wgSed;
86 // Fetch XML and check it, to give a more informative error message than the one which
87 // normaliseParams will inevitably give.
88 $xml = $image->getMetadata();
90 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
91 wfMsg( 'djvu_no_xml' ) );
94 if ( !$this->normaliseParams( $image, $params ) ) {
95 return new TransformParameterError( $params );
97 $width = $params['width'];
98 $height = $params['height'];
99 $srcPath = $image->getPath();
100 $page = $params['page'];
101 if ( $page > $this->pageCount( $image ) ) {
102 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
105 if ( $flags & self
::TRANSFORM_LATER
) {
106 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
109 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
110 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
113 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
114 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
116 if( $params['djvutxt'] && $wgDjvuTxt && $wgSed ) {
118 $cmd = '(' . wfEscapeShellArg( $wgDjvuTxt ) . " --page={$page} " . wfEscapeShellArg( $srcPath );
119 #Escape < > & characters
120 $cmd .= ' | ' . wfEscapeShellArg( $wgSed ) . ' "s/\&/\&/g ; s/</\</g ; s/>/\>/g ; s/\"/\"/g "';
121 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
124 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
125 wfEscapeShellArg( $srcPath );
126 if ( $wgDjvuPostProcessor ) {
127 $cmd .= " | {$wgDjvuPostProcessor}";
129 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
131 wfProfileIn( 'ddjvu' );
132 wfDebug( __METHOD__
.": $cmd\n" );
133 $err = wfShellExec( $cmd, $retval );
134 wfProfileOut( 'ddjvu' );
136 $removed = $this->removeBadFile( $dstPath, $retval );
137 if ( $retval != 0 ||
$removed ) {
138 wfDebugLog( 'thumbnail',
139 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
140 wfHostname(), $retval, trim($err), $cmd ) );
141 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
143 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
148 * Cache an instance of DjVuImage in an Image object, return that instance
150 function getDjVuImage( $image, $path ) {
152 $deja = new DjVuImage( $path );
153 } elseif ( !isset( $image->dejaImage
) ) {
154 $deja = $image->dejaImage
= new DjVuImage( $path );
156 $deja = $image->dejaImage
;
162 * Cache a document tree for the DjVu XML metadata
164 function getMetaTree( $image ) {
165 if ( isset( $image->dejaMetaTree
) ) {
166 return $image->dejaMetaTree
;
169 $metadata = $image->getMetadata();
170 if ( !$this->isMetadataValid( $image, $metadata ) ) {
171 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
174 wfProfileIn( __METHOD__
);
176 wfSuppressWarnings();
178 $image->dejaMetaTree
= new SimpleXMLElement( $metadata );
179 } catch( Exception
$e ) {
180 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
181 // Set to false rather than null to avoid further attempts
182 $image->dejaMetaTree
= false;
185 wfProfileOut( __METHOD__
);
186 return $image->dejaMetaTree
;
189 function getImageSize( $image, $path ) {
190 return $this->getDjVuImage( $image, $path )->getImageSize();
193 function getThumbType( $ext, $mime ) {
194 global $wgDjvuOutputExtension;
196 if ( !isset( $mime ) ) {
197 $magic = MimeMagic
::singleton();
198 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
200 return array( $wgDjvuOutputExtension, $mime );
203 function getMetadata( $image, $path ) {
204 wfDebug( "Getting DjVu metadata for $path\n" );
205 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
208 function getMetadataType( $image ) {
212 function isMetadataValid( $image, $metadata ) {
213 return !empty( $metadata ) && $metadata != serialize(array());
216 function pageCount( $image ) {
217 $tree = $this->getMetaTree( $image );
221 return count( $tree->xpath( '//OBJECT' ) );
224 function getPageDimensions( $image, $page ) {
225 $tree = $this->getMetaTree( $image );
230 $o = $tree->BODY
[0]->OBJECT[$page-1];
233 'width' => intval( $o['width'] ),
234 'height' => intval( $o['height'] )