3 * Handler for DjVu images.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Handler for DjVu images
29 class DjVuHandler
extends ImageHandler
{
33 function isEnabled() {
34 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
35 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
36 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
48 function mustRender( $file ) {
56 function isMultiPage( $file ) {
63 function getParamMap() {
65 'img_width' => 'width',
75 function validateParam( $name, $value ) {
76 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
91 function makeParamString( $params ) {
92 $page = isset( $params['page'] ) ?
$params['page'] : 1;
93 if ( !isset( $params['width'] ) ) {
97 return "page{$page}-{$params['width']}px";
104 function parseParamString( $str ) {
106 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
107 return array( 'width' => $m[2], 'page' => $m[1] );
117 function getScriptParams( $params ) {
119 'width' => $params['width'],
120 'page' => $params['page'],
130 * @return MediaTransformError|ThumbnailImage|TransformParameterError
132 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
133 global $wgDjvuRenderer, $wgDjvuPostProcessor;
135 // Fetch XML and check it, to give a more informative error message than the one which
136 // normaliseParams will inevitably give.
137 $xml = $image->getMetadata();
139 $width = isset( $params['width'] ) ?
$params['width'] : 0;
140 $height = isset( $params['height'] ) ?
$params['height'] : 0;
142 return new MediaTransformError( 'thumbnail_error', $width, $height,
143 wfMessage( 'djvu_no_xml' )->text() );
146 if ( !$this->normaliseParams( $image, $params ) ) {
147 return new TransformParameterError( $params );
149 $width = $params['width'];
150 $height = $params['height'];
151 $page = $params['page'];
152 if ( $page > $this->pageCount( $image ) ) {
153 return new MediaTransformError(
157 wfMessage( 'djvu_page_error' )->text()
161 if ( $flags & self
::TRANSFORM_LATER
) {
168 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
171 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
172 return new MediaTransformError(
176 wfMessage( 'thumbnail_dest_directory' )->text()
180 $srcPath = $image->getLocalRefPath();
181 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
182 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
183 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
184 " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
185 wfEscapeShellArg( $srcPath );
186 if ( $wgDjvuPostProcessor ) {
187 $cmd .= " | {$wgDjvuPostProcessor}";
189 $cmd .= ' > ' . wfEscapeShellArg( $dstPath ) . ') 2>&1';
190 wfProfileIn( 'ddjvu' );
191 wfDebug( __METHOD__
. ": $cmd\n" );
193 $err = wfShellExec( $cmd, $retval );
194 wfProfileOut( 'ddjvu' );
196 $removed = $this->removeBadFile( $dstPath, $retval );
197 if ( $retval != 0 ||
$removed ) {
198 wfDebugLog( 'thumbnail',
199 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
200 wfHostname(), $retval, trim( $err ), $cmd ) );
202 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
210 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
215 * Cache an instance of DjVuImage in an Image object, return that instance
219 function getDjVuImage( $image, $path ) {
221 $deja = new DjVuImage( $path );
222 } elseif ( !isset( $image->dejaImage
) ) {
223 $deja = $image->dejaImage
= new DjVuImage( $path );
225 $deja = $image->dejaImage
;
232 * Cache a document tree for the DjVu XML metadata
234 * @param $gettext Boolean: DOCUMENT (Default: false)
237 function getMetaTree( $image, $gettext = false ) {
238 if ( isset( $image->dejaMetaTree
) ) {
239 return $image->dejaMetaTree
;
242 $metadata = $image->getMetadata();
243 if ( !$this->isMetadataValid( $image, $metadata ) ) {
244 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
248 wfProfileIn( __METHOD__
);
250 wfSuppressWarnings();
252 // Set to false rather than null to avoid further attempts
253 $image->dejaMetaTree
= false;
254 $image->djvuTextTree
= false;
255 $tree = new SimpleXMLElement( $metadata );
256 if ( $tree->getName() == 'mw-djvu' ) {
257 foreach ( $tree->children() as $b ) {
258 if ( $b->getName() == 'DjVuTxt' ) {
259 $image->djvuTextTree
= $b;
260 } elseif ( $b->getName() == 'DjVuXML' ) {
261 $image->dejaMetaTree
= $b;
265 $image->dejaMetaTree
= $tree;
267 } catch ( Exception
$e ) {
268 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
271 wfProfileOut( __METHOD__
);
273 return $image->djvuTextTree
;
275 return $image->dejaMetaTree
;
279 function getImageSize( $image, $path ) {
280 return $this->getDjVuImage( $image, $path )->getImageSize();
283 function getThumbType( $ext, $mime, $params = null ) {
284 global $wgDjvuOutputExtension;
286 if ( !isset( $mime ) ) {
287 $magic = MimeMagic
::singleton();
288 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
291 return array( $wgDjvuOutputExtension, $mime );
294 function getMetadata( $image, $path ) {
295 wfDebug( "Getting DjVu metadata for $path\n" );
297 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
300 function getMetadataType( $image ) {
304 function isMetadataValid( $image, $metadata ) {
305 return !empty( $metadata ) && $metadata != serialize( array() );
308 function pageCount( $image ) {
309 $tree = $this->getMetaTree( $image );
314 return count( $tree->xpath( '//OBJECT' ) );
317 function getPageDimensions( $image, $page ) {
318 $tree = $this->getMetaTree( $image );
323 $o = $tree->BODY
[0]->OBJECT[$page - 1];
326 'width' => intval( $o['width'] ),
327 'height' => intval( $o['height'] )
334 function getPageText( $image, $page ) {
335 $tree = $this->getMetaTree( $image, true );
340 $o = $tree->BODY
[0]->PAGE
[$page - 1];