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