6 class DjVuHandler
extends ImageHandler
{
8 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
9 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
10 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
17 function mustRender() { return true; }
18 function isMultiPage() { return true; }
20 function validateParam( $name, $value ) {
21 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
32 function makeParamString( $params ) {
33 $page = isset( $params['page'] ) ?
$params['page'] : 1;
34 if ( !isset( $params['width'] ) ) {
37 return "{$params['width']}px-page{$page}";
40 function parseParamString( $str ) {
42 if ( preg_match( '/^(\d+)px-page(\d+)$/', $str, $m ) ) {
43 return array( 'width' => $m[1], 'page' => $m[2] );
49 function getScriptParams( $params ) {
51 'width' => $params['width'],
52 'page' => $params['page'],
56 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
57 global $wgDjvuRenderer, $wgDjvuPostProcessor;
59 // Fetch XML and check it, to give a more informative error message than the one which
60 // normaliseParams will inevitably give.
61 $xml = $image->getMetadata();
63 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
64 wfMsg( 'djvu_no_xml' ) );
67 if ( !$this->normaliseParams( $image, $params ) ) {
68 return new TransformParameterError( $params );
70 $width = $params['width'];
71 $height = $params['height'];
72 $srcPath = $image->getImagePath();
73 $page = $params['page'];
74 $pageCount = $this->pageCount( $image );
75 if ( $page > $this->pageCount( $image ) ) {
76 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
79 if ( $flags & self
::TRANSFORM_LATER
) {
80 return new ThumbnailImage( $dstUrl, $width, $height, $dstPath );
83 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
84 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
87 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
88 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
89 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
90 wfEscapeShellArg( $srcPath );
91 if ( $wgDjvuPostProcessor ) {
92 $cmd .= " | {$wgDjvuPostProcessor}";
94 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
95 wfProfileIn( 'ddjvu' );
96 wfDebug( __METHOD__
.": $cmd\n" );
97 $err = wfShellExec( $cmd, $retval );
98 wfProfileOut( 'ddjvu' );
100 $removed = $this->removeBadFile( $dstPath, $retval );
101 if ( $retval != 0 ||
$removed ) {
102 wfDebugLog( 'thumbnail',
103 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
104 wfHostname(), $retval, trim($err), $cmd ) );
105 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
107 return new ThumbnailImage( $dstUrl, $width, $height, $dstPath );
112 * Cache an instance of DjVuImage in an Image object, return that instance
114 function getDjVuImage( $image, $path ) {
116 $deja = new DjVuImage( $path );
117 } elseif ( !isset( $image->dejaImage
) ) {
118 $deja = $image->dejaImage
= new DjVuImage( $path );
120 $deja = $image->dejaImage
;
126 * Cache a document tree for the DjVu XML metadata
128 function getMetaTree( $image ) {
129 if ( isset( $image->dejaMetaTree
) ) {
130 return $image->dejaMetaTree
;
133 $metadata = $image->getMetadata();
134 if ( !$this->isMetadataValid( $image, $metadata ) ) {
135 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
138 wfProfileIn( __METHOD__
);
140 wfSuppressWarnings();
142 $image->dejaMetaTree
= new SimpleXMLElement( $metadata );
143 } catch( Exception
$e ) {
144 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
145 // Set to false rather than null to avoid further attempts
146 $image->dejaMetaTree
= false;
149 wfProfileOut( __METHOD__
);
150 return $image->dejaMetaTree
;
153 function getImageSize( $image, $path ) {
154 return $this->getDjVuImage( $image, $path )->getImageSize();
157 function getThumbType( $ext, $mime ) {
158 global $wgDjvuOutputExtension;
160 if ( !isset( $mime ) ) {
161 $magic = MimeMagic
::singleton();
162 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
164 return array( $wgDjvuOutputExtension, $mime );
167 function getMetadata( $image, $path ) {
168 wfDebug( "Getting DjVu metadata for $path\n" );
169 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
172 function getMetadataType( $image ) {
176 function isMetadataValid( $image, $metadata ) {
177 return !empty( $metadata ) && $metadata != serialize(array());
180 function pageCount( $image ) {
181 $tree = $this->getMetaTree( $image );
185 return count( $tree->xpath( '//OBJECT' ) );
188 function getPageDimensions( $image, $page ) {
189 $tree = $this->getMetaTree( $image );
194 $o = $tree->BODY
[0]->OBJECT[$page-1];
197 'width' => intval( $o['width'] ),
198 'height' => intval( $o['height'] )