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