Merge "Removed "Disable search suggestions" from Mediawiki Preference"
[lhc/web/wiklou.git] / includes / media / DjVuImage.php
1 <?php
2 /**
3 * DjVu image handler.
4 *
5 * Copyright © 2006 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Media
25 */
26
27 /**
28 * Support for detecting/validating DjVu image files and getting
29 * some basic file metadata (resolution etc)
30 *
31 * File format docs are available in source package for DjVuLibre:
32 * http://djvulibre.djvuzone.org/
33 *
34 * @ingroup Media
35 */
36 class DjVuImage {
37 /**
38 * Constructor
39 *
40 * @param string $filename The DjVu file name.
41 */
42 function __construct( $filename ) {
43 $this->mFilename = $filename;
44 }
45
46 /**
47 * @const DJVUTXT_MEMORY_LIMIT Memory limit for the DjVu description software
48 */
49 const DJVUTXT_MEMORY_LIMIT = 300000;
50
51 /**
52 * Check if the given file is indeed a valid DjVu image file
53 * @return bool
54 */
55 public function isValid() {
56 $info = $this->getInfo();
57
58 return $info !== false;
59 }
60
61 /**
62 * Return data in the style of getimagesize()
63 * @return array or false on failure
64 */
65 public function getImageSize() {
66 $data = $this->getInfo();
67
68 if ( $data !== false ) {
69 $width = $data['width'];
70 $height = $data['height'];
71
72 return array( $width, $height, 'DjVu',
73 "width=\"$width\" height=\"$height\"" );
74 }
75
76 return false;
77 }
78
79 // ---------
80
81 /**
82 * For debugging; dump the IFF chunk structure
83 */
84 function dump() {
85 $file = fopen( $this->mFilename, 'rb' );
86 $header = fread( $file, 12 );
87 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
88 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
89 echo "$chunk $chunkLength\n";
90 $this->dumpForm( $file, $chunkLength, 1 );
91 fclose( $file );
92 }
93
94 private function dumpForm( $file, $length, $indent ) {
95 $start = ftell( $file );
96 $secondary = fread( $file, 4 );
97 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
98 while ( ftell( $file ) - $start < $length ) {
99 $chunkHeader = fread( $file, 8 );
100 if ( $chunkHeader == '' ) {
101 break;
102 }
103 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
104 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
105 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
106
107 if ( $chunk == 'FORM' ) {
108 $this->dumpForm( $file, $chunkLength, $indent + 1 );
109 } else {
110 fseek( $file, $chunkLength, SEEK_CUR );
111 if ( $chunkLength & 1 == 1 ) {
112 // Padding byte between chunks
113 fseek( $file, 1, SEEK_CUR );
114 }
115 }
116 }
117 }
118
119 function getInfo() {
120 wfSuppressWarnings();
121 $file = fopen( $this->mFilename, 'rb' );
122 wfRestoreWarnings();
123 if ( $file === false ) {
124 wfDebug( __METHOD__ . ": missing or failed file read\n" );
125
126 return false;
127 }
128
129 $header = fread( $file, 16 );
130 $info = false;
131
132 if ( strlen( $header ) < 16 ) {
133 wfDebug( __METHOD__ . ": too short file header\n" );
134 } else {
135 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
136 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
137
138 if ( $magic != 'AT&T' ) {
139 wfDebug( __METHOD__ . ": not a DjVu file\n" );
140 } elseif ( $subtype == 'DJVU' ) {
141 // Single-page document
142 $info = $this->getPageInfo( $file, $formLength );
143 } elseif ( $subtype == 'DJVM' ) {
144 // Multi-page document
145 $info = $this->getMultiPageInfo( $file, $formLength );
146 } else {
147 wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
148 }
149 }
150 fclose( $file );
151
152 return $info;
153 }
154
155 private function readChunk( $file ) {
156 $header = fread( $file, 8 );
157 if ( strlen( $header ) < 8 ) {
158 return array( false, 0 );
159 } else {
160 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
161 extract( unpack( 'a4chunk/Nlength', $header ) );
162
163 return array( $chunk, $length );
164 }
165 }
166
167 private function skipChunk( $file, $chunkLength ) {
168 fseek( $file, $chunkLength, SEEK_CUR );
169
170 if ( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
171 // padding byte
172 fseek( $file, 1, SEEK_CUR );
173 }
174 }
175
176 private function getMultiPageInfo( $file, $formLength ) {
177 // For now, we'll just look for the first page in the file
178 // and report its information, hoping others are the same size.
179 $start = ftell( $file );
180 do {
181 list( $chunk, $length ) = $this->readChunk( $file );
182 if ( !$chunk ) {
183 break;
184 }
185
186 if ( $chunk == 'FORM' ) {
187 $subtype = fread( $file, 4 );
188 if ( $subtype == 'DJVU' ) {
189 wfDebug( __METHOD__ . ": found first subpage\n" );
190
191 return $this->getPageInfo( $file, $length );
192 }
193 $this->skipChunk( $file, $length - 4 );
194 } else {
195 wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
196 $this->skipChunk( $file, $length );
197 }
198 } while ( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
199
200 wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
201
202 return false;
203 }
204
205 private function getPageInfo( $file, $formLength ) {
206 list( $chunk, $length ) = $this->readChunk( $file );
207 if ( $chunk != 'INFO' ) {
208 wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
209
210 return false;
211 }
212
213 if ( $length < 9 ) {
214 wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
215
216 return false;
217 }
218 $data = fread( $file, $length );
219 if ( strlen( $data ) < $length ) {
220 wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
221
222 return false;
223 }
224
225 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
226 extract( unpack(
227 'nwidth/' .
228 'nheight/' .
229 'Cminor/' .
230 'Cmajor/' .
231 'vresolution/' .
232 'Cgamma', $data ) );
233
234 # Newer files have rotation info in byte 10, but we don't use it yet.
235
236 return array(
237 'width' => $width,
238 'height' => $height,
239 'version' => "$major.$minor",
240 'resolution' => $resolution,
241 'gamma' => $gamma / 10.0 );
242 }
243
244 /**
245 * Return an XML string describing the DjVu image
246 * @return string
247 */
248 function retrieveMetaData() {
249 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
250 wfProfileIn( __METHOD__ );
251
252 if ( isset( $wgDjvuDump ) ) {
253 # djvudump is faster as of version 3.5
254 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
255 wfProfileIn( 'djvudump' );
256 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename );
257 $dump = wfShellExec( $cmd );
258 $xml = $this->convertDumpToXML( $dump );
259 wfProfileOut( 'djvudump' );
260 } elseif ( isset( $wgDjvuToXML ) ) {
261 wfProfileIn( 'djvutoxml' );
262 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
263 wfEscapeShellArg( $this->mFilename );
264 $xml = wfShellExec( $cmd );
265 wfProfileOut( 'djvutoxml' );
266 } else {
267 $xml = null;
268 }
269 # Text layer
270 if ( isset( $wgDjvuTxt ) ) {
271 wfProfileIn( 'djvutxt' );
272 $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename );
273 wfDebug( __METHOD__ . ": $cmd\n" );
274 $retval = '';
275 $txt = wfShellExec( $cmd, $retval, array(), array( 'memory' => self::DJVUTXT_MEMORY_LIMIT ) );
276 wfProfileOut( 'djvutxt' );
277 if ( $retval == 0 ) {
278 # Strip some control characters
279 $txt = preg_replace( "/[\013\035\037]/", "", $txt );
280 $reg = <<<EOR
281 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*"
282 ((?> # Text to match is composed of atoms of either:
283 \\\\. # - any escaped character
284 | # - any character different from " and \
285 [^"\\\\]+
286 )*?)
287 "\s*\)
288 | # Or page can be empty ; in this case, djvutxt dumps ()
289 \(\s*()\)/sx
290 EOR;
291 $txt = preg_replace_callback( $reg, array( $this, 'pageTextCallback' ), $txt );
292 $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
293 $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml, 1 );
294 $xml = $xml . $txt . '</mw-djvu>';
295 }
296 }
297 wfProfileOut( __METHOD__ );
298
299 return $xml;
300 }
301
302 function pageTextCallback( $matches ) {
303 # Get rid of invalid UTF-8, strip control characters
304 return '<PAGE value="' . htmlspecialchars( UtfNormal::cleanUp( $matches[1] ) ) . '" />';
305 }
306
307 /**
308 * Hack to temporarily work around djvutoxml bug
309 * @return bool|string
310 */
311 function convertDumpToXML( $dump ) {
312 if ( strval( $dump ) == '' ) {
313 return false;
314 }
315
316 $xml = <<<EOT
317 <?xml version="1.0" ?>
318 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
319 <DjVuXML>
320 <HEAD></HEAD>
321 <BODY>
322 EOT;
323
324 $dump = str_replace( "\r", '', $dump );
325 $line = strtok( $dump, "\n" );
326 $m = false;
327 $good = false;
328 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
329 # Single-page
330 if ( $this->parseFormDjvu( $line, $xml ) ) {
331 $good = true;
332 } else {
333 return false;
334 }
335 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
336 # Multi-page
337 $parentLevel = strlen( $m[1] );
338 # Find DIRM
339 $line = strtok( "\n" );
340 while ( $line !== false ) {
341 $childLevel = strspn( $line, ' ' );
342 if ( $childLevel <= $parentLevel ) {
343 # End of chunk
344 break;
345 }
346
347 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
348 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
349
350 return false;
351 }
352 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
353 # Found page
354 if ( $this->parseFormDjvu( $line, $xml ) ) {
355 $good = true;
356 } else {
357 return false;
358 }
359 }
360 $line = strtok( "\n" );
361 }
362 }
363 if ( !$good ) {
364 return false;
365 }
366
367 $xml .= "</BODY>\n</DjVuXML>\n";
368
369 return $xml;
370 }
371
372 function parseFormDjvu( $line, &$xml ) {
373 $parentLevel = strspn( $line, ' ' );
374 $line = strtok( "\n" );
375
376 # Find INFO
377 while ( $line !== false ) {
378 $childLevel = strspn( $line, ' ' );
379 if ( $childLevel <= $parentLevel ) {
380 # End of chunk
381 break;
382 }
383
384 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
385 $xml .= Xml::tags(
386 'OBJECT',
387 array(
388 #'data' => '',
389 #'type' => 'image/x.djvu',
390 'height' => $m[2],
391 'width' => $m[1],
392 #'usemap' => '',
393 ),
394 "\n" .
395 Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
396 Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
397 ) . "\n";
398
399 return true;
400 }
401 $line = strtok( "\n" );
402 }
403
404 # Not found
405 return false;
406 }
407 }