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
{
34 function isEnabled() {
35 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
36 if ( !$wgDjvuRenderer ||
( !$wgDjvuDump && !$wgDjvuToXML ) ) {
37 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'] ) ) {
96 return "page{$page}-{$params['width']}px";
103 function parseParamString( $str ) {
105 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
106 return array( 'width' => $m[2], 'page' => $m[1] );
116 function getScriptParams( $params ) {
118 'width' => $params['width'],
119 'page' => $params['page'],
129 * @return MediaTransformError|ThumbnailImage|TransformParameterError
131 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
132 global $wgDjvuRenderer, $wgDjvuPostProcessor;
134 // Fetch XML and check it, to give a more informative error message than the one which
135 // normaliseParams will inevitably give.
136 $xml = $image->getMetadata();
138 $width = isset( $params['width'] ) ?
$params['width'] : 0;
139 $height = isset( $params['height'] ) ?
$params['height'] : 0;
140 return new MediaTransformError( 'thumbnail_error', $width, $height,
141 wfMsg( 'djvu_no_xml' ) );
144 if ( !$this->normaliseParams( $image, $params ) ) {
145 return new TransformParameterError( $params );
147 $width = $params['width'];
148 $height = $params['height'];
149 $srcPath = $image->getLocalRefPath();
150 $page = $params['page'];
151 if ( $page > $this->pageCount( $image ) ) {
152 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
155 if ( $flags & self
::TRANSFORM_LATER
) {
156 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
159 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__
) ) {
160 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
163 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
164 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
165 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
166 " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
167 wfEscapeShellArg( $srcPath );
168 if ( $wgDjvuPostProcessor ) {
169 $cmd .= " | {$wgDjvuPostProcessor}";
171 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
172 wfProfileIn( 'ddjvu' );
173 wfDebug( __METHOD__
.": $cmd\n" );
175 $err = wfShellExec( $cmd, $retval );
176 wfProfileOut( 'ddjvu' );
178 $removed = $this->removeBadFile( $dstPath, $retval );
179 if ( $retval != 0 ||
$removed ) {
180 wfDebugLog( 'thumbnail',
181 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
182 wfHostname(), $retval, trim($err), $cmd ) );
183 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
185 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
190 * Cache an instance of DjVuImage in an Image object, return that instance
194 function getDjVuImage( $image, $path ) {
196 $deja = new DjVuImage( $path );
197 } elseif ( !isset( $image->dejaImage
) ) {
198 $deja = $image->dejaImage
= new DjVuImage( $path );
200 $deja = $image->dejaImage
;
206 * Cache a document tree for the DjVu XML metadata
208 * @param $gettext Boolean: DOCUMENT (Default: false)
211 function getMetaTree( $image , $gettext = false ) {
212 if ( isset( $image->dejaMetaTree
) ) {
213 return $image->dejaMetaTree
;
216 $metadata = $image->getMetadata();
217 if ( !$this->isMetadataValid( $image, $metadata ) ) {
218 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
221 wfProfileIn( __METHOD__
);
223 wfSuppressWarnings();
225 // Set to false rather than null to avoid further attempts
226 $image->dejaMetaTree
= false;
227 $image->djvuTextTree
= false;
228 $tree = new SimpleXMLElement( $metadata );
229 if( $tree->getName() == 'mw-djvu' ) {
230 foreach($tree->children() as $b){
231 if( $b->getName() == 'DjVuTxt' ) {
232 $image->djvuTextTree
= $b;
234 elseif ( $b->getName() == 'DjVuXML' ) {
235 $image->dejaMetaTree
= $b;
239 $image->dejaMetaTree
= $tree;
241 } catch( Exception
$e ) {
242 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
245 wfProfileOut( __METHOD__
);
247 return $image->djvuTextTree
;
249 return $image->dejaMetaTree
;
253 function getImageSize( $image, $path ) {
254 return $this->getDjVuImage( $image, $path )->getImageSize();
257 function getThumbType( $ext, $mime, $params = null ) {
258 global $wgDjvuOutputExtension;
260 if ( !isset( $mime ) ) {
261 $magic = MimeMagic
::singleton();
262 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
264 return array( $wgDjvuOutputExtension, $mime );
267 function getMetadata( $image, $path ) {
268 wfDebug( "Getting DjVu metadata for $path\n" );
269 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
272 function getMetadataType( $image ) {
276 function isMetadataValid( $image, $metadata ) {
277 return !empty( $metadata ) && $metadata != serialize(array());
280 function pageCount( $image ) {
281 $tree = $this->getMetaTree( $image );
285 return count( $tree->xpath( '//OBJECT' ) );
288 function getPageDimensions( $image, $page ) {
289 $tree = $this->getMetaTree( $image );
294 $o = $tree->BODY
[0]->OBJECT[$page-1];
297 'width' => intval( $o['width'] ),
298 'height' => intval( $o['height'] )
305 function getPageText( $image, $page ){
306 $tree = $this->getMetaTree( $image, true );
311 $o = $tree->BODY
[0]->PAGE
[$page-1];