Merge "Typo fix"
[mediawiki.git] / includes / media / DjVu.php
blobb9e89d9d221e7b440a129cf635547954e75af882
1 <?php
2 /**
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
20 * @file
21 * @ingroup Media
24 /**
25 * Handler for DjVu images
27 * @ingroup Media
29 class DjVuHandler extends ImageHandler {
31 /**
32 * @return bool
34 function isEnabled() {
35 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
36 if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
37 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
38 return false;
39 } else {
40 return true;
44 /**
45 * @param $file
46 * @return bool
48 function mustRender( $file ) {
49 return true;
52 /**
53 * @param $file
54 * @return bool
56 function isMultiPage( $file ) {
57 return true;
60 /**
61 * @return array
63 function getParamMap() {
64 return array(
65 'img_width' => 'width',
66 'img_page' => 'page',
70 /**
71 * @param $name
72 * @param $value
73 * @return bool
75 function validateParam( $name, $value ) {
76 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
77 if ( $value <= 0 ) {
78 return false;
79 } else {
80 return true;
82 } else {
83 return false;
87 /**
88 * @param $params
89 * @return bool|string
91 function makeParamString( $params ) {
92 $page = isset( $params['page'] ) ? $params['page'] : 1;
93 if ( !isset( $params['width'] ) ) {
94 return false;
96 return "page{$page}-{$params['width']}px";
99 /**
100 * @param $str
101 * @return array|bool
103 function parseParamString( $str ) {
104 $m = false;
105 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
106 return array( 'width' => $m[2], 'page' => $m[1] );
107 } else {
108 return false;
113 * @param $params
114 * @return array
116 function getScriptParams( $params ) {
117 return array(
118 'width' => $params['width'],
119 'page' => $params['page'],
124 * @param $image File
125 * @param $dstPath
126 * @param $dstUrl
127 * @param $params
128 * @param int $flags
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();
137 if ( !$xml ) {
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 wfMessage( 'djvu_no_xml' )->text() );
144 if ( !$this->normaliseParams( $image, $params ) ) {
145 return new TransformParameterError( $params );
147 $width = $params['width'];
148 $height = $params['height'];
149 $page = $params['page'];
150 if ( $page > $this->pageCount( $image ) ) {
151 return new MediaTransformError(
152 'thumbnail_error',
153 $width,
154 $height,
155 wfMessage( 'djvu_page_error' )->text()
159 if ( $flags & self::TRANSFORM_LATER ) {
160 $params = array(
161 'width' => $width,
162 'height' => $height,
163 'page' => $page
165 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
168 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
169 return new MediaTransformError(
170 'thumbnail_error',
171 $width,
172 $height,
173 wfMessage( 'thumbnail_dest_directory' )->text()
177 $srcPath = $image->getLocalRefPath();
178 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
179 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
180 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
181 " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
182 wfEscapeShellArg( $srcPath );
183 if ( $wgDjvuPostProcessor ) {
184 $cmd .= " | {$wgDjvuPostProcessor}";
186 $cmd .= ' > ' . wfEscapeShellArg( $dstPath ) . ') 2>&1';
187 wfProfileIn( 'ddjvu' );
188 wfDebug( __METHOD__ . ": $cmd\n" );
189 $retval = '';
190 $err = wfShellExec( $cmd, $retval );
191 wfProfileOut( 'ddjvu' );
193 $removed = $this->removeBadFile( $dstPath, $retval );
194 if ( $retval != 0 || $removed ) {
195 wfDebugLog( 'thumbnail',
196 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
197 wfHostname(), $retval, trim( $err ), $cmd ) );
198 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
199 } else {
200 $params = array(
201 'width' => $width,
202 'height' => $height,
203 'page' => $page
205 return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
210 * Cache an instance of DjVuImage in an Image object, return that instance
212 * @return DjVuImage
214 function getDjVuImage( $image, $path ) {
215 if ( !$image ) {
216 $deja = new DjVuImage( $path );
217 } elseif ( !isset( $image->dejaImage ) ) {
218 $deja = $image->dejaImage = new DjVuImage( $path );
219 } else {
220 $deja = $image->dejaImage;
222 return $deja;
226 * Cache a document tree for the DjVu XML metadata
227 * @param $image File
228 * @param $gettext Boolean: DOCUMENT (Default: false)
229 * @return bool
231 function getMetaTree( $image, $gettext = false ) {
232 if ( isset( $image->dejaMetaTree ) ) {
233 return $image->dejaMetaTree;
236 $metadata = $image->getMetadata();
237 if ( !$this->isMetadataValid( $image, $metadata ) ) {
238 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
239 return false;
241 wfProfileIn( __METHOD__ );
243 wfSuppressWarnings();
244 try {
245 // Set to false rather than null to avoid further attempts
246 $image->dejaMetaTree = false;
247 $image->djvuTextTree = false;
248 $tree = new SimpleXMLElement( $metadata );
249 if ( $tree->getName() == 'mw-djvu' ) {
250 foreach ( $tree->children() as $b ) {
251 if ( $b->getName() == 'DjVuTxt' ) {
252 $image->djvuTextTree = $b;
253 } elseif ( $b->getName() == 'DjVuXML' ) {
254 $image->dejaMetaTree = $b;
257 } else {
258 $image->dejaMetaTree = $tree;
260 } catch ( Exception $e ) {
261 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
263 wfRestoreWarnings();
264 wfProfileOut( __METHOD__ );
265 if ( $gettext ) {
266 return $image->djvuTextTree;
267 } else {
268 return $image->dejaMetaTree;
272 function getImageSize( $image, $path ) {
273 return $this->getDjVuImage( $image, $path )->getImageSize();
276 function getThumbType( $ext, $mime, $params = null ) {
277 global $wgDjvuOutputExtension;
278 static $mime;
279 if ( !isset( $mime ) ) {
280 $magic = MimeMagic::singleton();
281 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
283 return array( $wgDjvuOutputExtension, $mime );
286 function getMetadata( $image, $path ) {
287 wfDebug( "Getting DjVu metadata for $path\n" );
288 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
291 function getMetadataType( $image ) {
292 return 'djvuxml';
295 function isMetadataValid( $image, $metadata ) {
296 return !empty( $metadata ) && $metadata != serialize( array() );
299 function pageCount( $image ) {
300 $tree = $this->getMetaTree( $image );
301 if ( !$tree ) {
302 return false;
304 return count( $tree->xpath( '//OBJECT' ) );
307 function getPageDimensions( $image, $page ) {
308 $tree = $this->getMetaTree( $image );
309 if ( !$tree ) {
310 return false;
313 $o = $tree->BODY[0]->OBJECT[$page - 1];
314 if ( $o ) {
315 return array(
316 'width' => intval( $o['width'] ),
317 'height' => intval( $o['height'] )
319 } else {
320 return false;
324 function getPageText( $image, $page ) {
325 $tree = $this->getMetaTree( $image, true );
326 if ( !$tree ) {
327 return false;
330 $o = $tree->BODY[0]->PAGE[$page - 1];
331 if ( $o ) {
332 $txt = $o['value'];
333 return $txt;
334 } else {
335 return false;