merged master
[lhc/web/wiklou.git] / includes / media / DjVu.php
1 <?php
2 /**
3 * Handler for DjVu images.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Handler for DjVu images
26 *
27 * @ingroup Media
28 */
29 class DjVuHandler extends ImageHandler {
30
31 /**
32 * @return bool
33 */
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;
41 }
42 }
43
44 /**
45 * @param $file
46 * @return bool
47 */
48 function mustRender( $file ) {
49 return true;
50 }
51
52 /**
53 * @param $file
54 * @return bool
55 */
56 function isMultiPage( $file ) {
57 return true;
58 }
59
60 /**
61 * @return array
62 */
63 function getParamMap() {
64 return array(
65 'img_width' => 'width',
66 'img_page' => 'page',
67 );
68 }
69
70 /**
71 * @param $name
72 * @param $value
73 * @return bool
74 */
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;
81 }
82 } else {
83 return false;
84 }
85 }
86
87 /**
88 * @param $params
89 * @return bool|string
90 */
91 function makeParamString( $params ) {
92 $page = isset( $params['page'] ) ? $params['page'] : 1;
93 if ( !isset( $params['width'] ) ) {
94 return false;
95 }
96 return "page{$page}-{$params['width']}px";
97 }
98
99 /**
100 * @param $str
101 * @return array|bool
102 */
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;
109 }
110 }
111
112 /**
113 * @param $params
114 * @return array
115 */
116 function getScriptParams( $params ) {
117 return array(
118 'width' => $params['width'],
119 'page' => $params['page'],
120 );
121 }
122
123 /**
124 * @param $image File
125 * @param $dstPath
126 * @param $dstUrl
127 * @param $params
128 * @param int $flags
129 * @return MediaTransformError|ThumbnailImage|TransformParameterError
130 */
131 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
132 global $wgDjvuRenderer, $wgDjvuPostProcessor;
133
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() );
142 }
143
144 if ( !$this->normaliseParams( $image, $params ) ) {
145 return new TransformParameterError( $params );
146 }
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()
156 );
157 }
158
159 if ( $flags & self::TRANSFORM_LATER ) {
160 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
161 }
162
163 if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
164 return new MediaTransformError(
165 'thumbnail_error',
166 $width,
167 $height,
168 wfMessage( 'thumbnail_dest_directory' )->text()
169 );
170 }
171
172 $srcPath = $image->getLocalRefPath();
173 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
174 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
175 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
176 " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
177 wfEscapeShellArg( $srcPath );
178 if ( $wgDjvuPostProcessor ) {
179 $cmd .= " | {$wgDjvuPostProcessor}";
180 }
181 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
182 wfProfileIn( 'ddjvu' );
183 wfDebug( __METHOD__.": $cmd\n" );
184 $retval = '';
185 $err = wfShellExec( $cmd, $retval );
186 wfProfileOut( 'ddjvu' );
187
188 $removed = $this->removeBadFile( $dstPath, $retval );
189 if ( $retval != 0 || $removed ) {
190 wfDebugLog( 'thumbnail',
191 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
192 wfHostname(), $retval, trim($err), $cmd ) );
193 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
194 } else {
195 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
196 }
197 }
198
199 /**
200 * Cache an instance of DjVuImage in an Image object, return that instance
201 *
202 * @return DjVuImage
203 */
204 function getDjVuImage( $image, $path ) {
205 if ( !$image ) {
206 $deja = new DjVuImage( $path );
207 } elseif ( !isset( $image->dejaImage ) ) {
208 $deja = $image->dejaImage = new DjVuImage( $path );
209 } else {
210 $deja = $image->dejaImage;
211 }
212 return $deja;
213 }
214
215 /**
216 * Cache a document tree for the DjVu XML metadata
217 * @param $image File
218 * @param $gettext Boolean: DOCUMENT (Default: false)
219 * @return bool
220 */
221 function getMetaTree( $image , $gettext = false ) {
222 if ( isset( $image->dejaMetaTree ) ) {
223 return $image->dejaMetaTree;
224 }
225
226 $metadata = $image->getMetadata();
227 if ( !$this->isMetadataValid( $image, $metadata ) ) {
228 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
229 return false;
230 }
231 wfProfileIn( __METHOD__ );
232
233 wfSuppressWarnings();
234 try {
235 // Set to false rather than null to avoid further attempts
236 $image->dejaMetaTree = false;
237 $image->djvuTextTree = false;
238 $tree = new SimpleXMLElement( $metadata );
239 if( $tree->getName() == 'mw-djvu' ) {
240 foreach($tree->children() as $b){
241 if( $b->getName() == 'DjVuTxt' ) {
242 $image->djvuTextTree = $b;
243 }
244 elseif ( $b->getName() == 'DjVuXML' ) {
245 $image->dejaMetaTree = $b;
246 }
247 }
248 } else {
249 $image->dejaMetaTree = $tree;
250 }
251 } catch( Exception $e ) {
252 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
253 }
254 wfRestoreWarnings();
255 wfProfileOut( __METHOD__ );
256 if( $gettext ) {
257 return $image->djvuTextTree;
258 } else {
259 return $image->dejaMetaTree;
260 }
261 }
262
263 function getImageSize( $image, $path ) {
264 return $this->getDjVuImage( $image, $path )->getImageSize();
265 }
266
267 function getThumbType( $ext, $mime, $params = null ) {
268 global $wgDjvuOutputExtension;
269 static $mime;
270 if ( !isset( $mime ) ) {
271 $magic = MimeMagic::singleton();
272 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
273 }
274 return array( $wgDjvuOutputExtension, $mime );
275 }
276
277 function getMetadata( $image, $path ) {
278 wfDebug( "Getting DjVu metadata for $path\n" );
279 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
280 }
281
282 function getMetadataType( $image ) {
283 return 'djvuxml';
284 }
285
286 function isMetadataValid( $image, $metadata ) {
287 return !empty( $metadata ) && $metadata != serialize(array());
288 }
289
290 function pageCount( $image ) {
291 $tree = $this->getMetaTree( $image );
292 if ( !$tree ) {
293 return false;
294 }
295 return count( $tree->xpath( '//OBJECT' ) );
296 }
297
298 function getPageDimensions( $image, $page ) {
299 $tree = $this->getMetaTree( $image );
300 if ( !$tree ) {
301 return false;
302 }
303
304 $o = $tree->BODY[0]->OBJECT[$page-1];
305 if ( $o ) {
306 return array(
307 'width' => intval( $o['width'] ),
308 'height' => intval( $o['height'] )
309 );
310 } else {
311 return false;
312 }
313 }
314
315 function getPageText( $image, $page ){
316 $tree = $this->getMetaTree( $image, true );
317 if ( !$tree ) {
318 return false;
319 }
320
321 $o = $tree->BODY[0]->PAGE[$page-1];
322 if ( $o ) {
323 $txt = $o['value'];
324 return $txt;
325 } else {
326 return false;
327 }
328
329 }
330
331 }