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() { return true; }
22 function isMultiPage() { 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] );
60 function getScriptParams( $params ) {
62 'width' => $params['width'],
63 'page' => $params['page'],
67 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
68 global $wgDjvuRenderer, $wgDjvuPostProcessor;
70 // Fetch XML and check it, to give a more informative error message than the one which
71 // normaliseParams will inevitably give.
72 $xml = $image->getMetadata();
74 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
75 wfMsg( 'djvu_no_xml' ) );
78 if ( !$this->normaliseParams( $image, $params ) ) {
79 return new TransformParameterError( $params );
81 $width = $params['width'];
82 $height = $params['height'];
83 $srcPath = $image->getPath();
84 $page = $params['page'];
85 if ( $page > $this->pageCount( $image ) ) {
86 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
89 if ( $flags & self
::TRANSFORM_LATER
) {
90 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
93 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
94 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
97 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
98 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
99 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
100 wfEscapeShellArg( $srcPath );
101 if ( $wgDjvuPostProcessor ) {
102 $cmd .= " | {$wgDjvuPostProcessor}";
104 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
105 wfProfileIn( 'ddjvu' );
106 wfDebug( __METHOD__
.": $cmd\n" );
107 $err = wfShellExec( $cmd, $retval );
108 wfProfileOut( 'ddjvu' );
110 $removed = $this->removeBadFile( $dstPath, $retval );
111 if ( $retval != 0 ||
$removed ) {
112 wfDebugLog( 'thumbnail',
113 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
114 wfHostname(), $retval, trim($err), $cmd ) );
115 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
117 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
122 * Cache an instance of DjVuImage in an Image object, return that instance
124 function getDjVuImage( $image, $path ) {
126 $deja = new DjVuImage( $path );
127 } elseif ( !isset( $image->dejaImage
) ) {
128 $deja = $image->dejaImage
= new DjVuImage( $path );
130 $deja = $image->dejaImage
;
136 * Cache a document tree for the DjVu XML metadata
138 function getMetaTree( $image ) {
139 if ( isset( $image->dejaMetaTree
) ) {
140 return $image->dejaMetaTree
;
143 $metadata = $image->getMetadata();
144 if ( !$this->isMetadataValid( $image, $metadata ) ) {
145 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
148 wfProfileIn( __METHOD__
);
150 wfSuppressWarnings();
152 $image->dejaMetaTree
= new SimpleXMLElement( $metadata );
153 } catch( Exception
$e ) {
154 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
155 // Set to false rather than null to avoid further attempts
156 $image->dejaMetaTree
= false;
159 wfProfileOut( __METHOD__
);
160 return $image->dejaMetaTree
;
163 function getImageSize( $image, $path ) {
164 return $this->getDjVuImage( $image, $path )->getImageSize();
167 function getThumbType( $ext, $mime ) {
168 global $wgDjvuOutputExtension;
170 if ( !isset( $mime ) ) {
171 $magic = MimeMagic
::singleton();
172 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
174 return array( $wgDjvuOutputExtension, $mime );
177 function getMetadata( $image, $path ) {
178 wfDebug( "Getting DjVu metadata for $path\n" );
179 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
182 function getMetadataType( $image ) {
186 function isMetadataValid( $image, $metadata ) {
187 return !empty( $metadata ) && $metadata != serialize(array());
190 function pageCount( $image ) {
191 $tree = $this->getMetaTree( $image );
195 return count( $tree->xpath( '//OBJECT' ) );
198 function getPageDimensions( $image, $page ) {
199 $tree = $this->getMetaTree( $image );
204 $o = $tree->BODY
[0]->OBJECT[$page-1];
207 'width' => intval( $o['width'] ),
208 'height' => intval( $o['height'] )