Revert r101492, broken, see CR. Also revert followup r101496.
[mediawiki.git] / includes / media / DjVu.php
blobf4553b3afd1a9b1b2e3b513da6568fa835cf9288
1 <?php
2 /**
3 * Handler for DjVu images
5 * @file
6 * @ingroup Media
7 */
9 /**
10 * Handler for DjVu images
12 * @ingroup Media
14 class DjVuHandler extends ImageHandler {
16 /**
17 * @return bool
19 function isEnabled() {
20 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
21 if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
22 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
23 return false;
24 } else {
25 return true;
29 /**
30 * @param $file
31 * @return bool
33 function mustRender( $file ) {
34 return true;
37 /**
38 * @param $file
39 * @return bool
41 function isMultiPage( $file ) {
42 return true;
45 /**
46 * @return array
48 function getParamMap() {
49 return array(
50 'img_width' => 'width',
51 'img_page' => 'page',
55 /**
56 * @param $name
57 * @param $value
58 * @return bool
60 function validateParam( $name, $value ) {
61 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
62 if ( $value <= 0 ) {
63 return false;
64 } else {
65 return true;
67 } else {
68 return false;
72 /**
73 * @param $params
74 * @return bool|string
76 function makeParamString( $params ) {
77 $page = isset( $params['page'] ) ? $params['page'] : 1;
78 if ( !isset( $params['width'] ) ) {
79 return false;
81 return "page{$page}-{$params['width']}px";
84 /**
85 * @param $str
86 * @return array|bool
88 function parseParamString( $str ) {
89 $m = false;
90 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
91 return array( 'width' => $m[2], 'page' => $m[1] );
92 } else {
93 return false;
97 /**
98 * @param $params
99 * @return array
101 function getScriptParams( $params ) {
102 return array(
103 'width' => $params['width'],
104 'page' => $params['page'],
109 * @param $image File
110 * @param $dstPath
111 * @param $dstUrl
112 * @param $params
113 * @param int $flags
114 * @return MediaTransformError|ThumbnailImage|TransformParameterError
116 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
117 global $wgDjvuRenderer, $wgDjvuPostProcessor;
119 // Fetch XML and check it, to give a more informative error message than the one which
120 // normaliseParams will inevitably give.
121 $xml = $image->getMetadata();
122 if ( !$xml ) {
123 $width = isset( $params['width'] ) ? $params['width'] : 0;
124 $height = isset( $params['height'] ) ? $params['height'] : 0;
125 return new MediaTransformError( 'thumbnail_error', $width, $height,
126 wfMsg( 'djvu_no_xml' ) );
129 if ( !$this->normaliseParams( $image, $params ) ) {
130 return new TransformParameterError( $params );
132 $width = $params['width'];
133 $height = $params['height'];
134 $srcPath = $image->getPath();
135 $page = $params['page'];
136 if ( $page > $this->pageCount( $image ) ) {
137 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
140 if ( $flags & self::TRANSFORM_LATER ) {
141 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
144 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
145 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
148 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
149 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
150 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
151 " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
152 wfEscapeShellArg( $srcPath );
153 if ( $wgDjvuPostProcessor ) {
154 $cmd .= " | {$wgDjvuPostProcessor}";
156 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
157 wfProfileIn( 'ddjvu' );
158 wfDebug( __METHOD__.": $cmd\n" );
159 $retval = '';
160 $err = wfShellExec( $cmd, $retval );
161 wfProfileOut( 'ddjvu' );
163 $removed = $this->removeBadFile( $dstPath, $retval );
164 if ( $retval != 0 || $removed ) {
165 wfDebugLog( 'thumbnail',
166 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
167 wfHostname(), $retval, trim($err), $cmd ) );
168 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
169 } else {
170 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
175 * Cache an instance of DjVuImage in an Image object, return that instance
177 * @return DjVuImage
179 function getDjVuImage( $image, $path ) {
180 if ( !$image ) {
181 $deja = new DjVuImage( $path );
182 } elseif ( !isset( $image->dejaImage ) ) {
183 $deja = $image->dejaImage = new DjVuImage( $path );
184 } else {
185 $deja = $image->dejaImage;
187 return $deja;
191 * Cache a document tree for the DjVu XML metadata
192 * @param $image File
194 function getMetaTree( $image , $gettext = false ) {
195 if ( isset( $image->dejaMetaTree ) ) {
196 return $image->dejaMetaTree;
199 $metadata = $image->getMetadata();
200 if ( !$this->isMetadataValid( $image, $metadata ) ) {
201 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
202 return false;
204 wfProfileIn( __METHOD__ );
206 wfSuppressWarnings();
207 try {
208 // Set to false rather than null to avoid further attempts
209 $image->dejaMetaTree = false;
210 $image->djvuTextTree = false;
211 $tree = new SimpleXMLElement( $metadata );
212 if( $tree->getName() == 'mw-djvu' ) {
213 foreach($tree->children() as $b){
214 if( $b->getName() == 'DjVuTxt' ) {
215 $image->djvuTextTree = $b;
217 elseif ( $b->getName() == 'DjVuXML' ) {
218 $image->dejaMetaTree = $b;
221 } else {
222 $image->dejaMetaTree = $tree;
224 } catch( Exception $e ) {
225 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
227 wfRestoreWarnings();
228 wfProfileOut( __METHOD__ );
229 if( $gettext ) {
230 return $image->djvuTextTree;
231 } else {
232 return $image->dejaMetaTree;
236 function getImageSize( $image, $path ) {
237 return $this->getDjVuImage( $image, $path )->getImageSize();
240 function getThumbType( $ext, $mime, $params = null ) {
241 global $wgDjvuOutputExtension;
242 static $mime;
243 if ( !isset( $mime ) ) {
244 $magic = MimeMagic::singleton();
245 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
247 return array( $wgDjvuOutputExtension, $mime );
250 function getMetadata( $image, $path ) {
251 wfDebug( "Getting DjVu metadata for $path\n" );
252 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
255 function getMetadataType( $image ) {
256 return 'djvuxml';
259 function isMetadataValid( $image, $metadata ) {
260 return !empty( $metadata ) && $metadata != serialize(array());
263 function pageCount( $image ) {
264 $tree = $this->getMetaTree( $image );
265 if ( !$tree ) {
266 return false;
268 return count( $tree->xpath( '//OBJECT' ) );
271 function getPageDimensions( $image, $page ) {
272 $tree = $this->getMetaTree( $image );
273 if ( !$tree ) {
274 return false;
277 $o = $tree->BODY[0]->OBJECT[$page-1];
278 if ( $o ) {
279 return array(
280 'width' => intval( $o['width'] ),
281 'height' => intval( $o['height'] )
283 } else {
284 return false;
288 function getPageText( $image, $page ){
289 $tree = $this->getMetaTree( $image, true );
290 if ( !$tree ) {
291 return false;
294 $o = $tree->BODY[0]->PAGE[$page-1];
295 if ( $o ) {
296 $txt = $o['value'];
297 return $txt;
298 } else {
299 return false;