5 * Copyright © 2006 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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.
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.
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
27 use MediaWiki\Shell\Shell
;
30 * Support for detecting/validating DjVu image files and getting
31 * some basic file metadata (resolution etc)
33 * File format docs are available in source package for DjVuLibre:
34 * http://djvulibre.djvuzone.org/
41 * Memory limit for the DjVu description software
43 const DJVUTXT_MEMORY_LIMIT
= 300000;
46 * @param string $filename The DjVu file name.
48 function __construct( $filename ) {
49 $this->mFilename
= $filename;
53 * Check if the given file is indeed a valid DjVu image file
56 public function isValid() {
57 $info = $this->getInfo();
59 return $info !== false;
63 * Return data in the style of getimagesize()
64 * @return array|bool Array or false on failure
66 public function getImageSize() {
67 $data = $this->getInfo();
69 if ( $data !== false ) {
70 $width = $data['width'];
71 $height = $data['height'];
73 return [ $width, $height, 'DjVu',
74 "width=\"$width\" height=\"$height\"" ];
83 * For debugging; dump the IFF chunk structure
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 );
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 == '' ) {
105 $arr = unpack( 'a4chunk/NchunkLength', $chunkHeader );
106 $chunk = $arr['chunk'];
107 $chunkLength = $arr['chunkLength'];
108 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
110 if ( $chunk == 'FORM' ) {
111 $this->dumpForm( $file, $chunkLength, $indent +
1 );
113 fseek( $file, $chunkLength, SEEK_CUR
);
114 if ( ( $chunkLength & 1 ) == 1 ) {
115 // Padding byte between chunks
116 fseek( $file, 1, SEEK_CUR
);
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" );
132 $header = fread( $file, 16 );
135 if ( strlen( $header ) < 16 ) {
136 wfDebug( __METHOD__
. ": too short file header\n" );
138 $arr = unpack( 'a4magic/a4form/NformLength/a4subtype', $header );
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'] );
150 wfDebug( __METHOD__
. ": unrecognized DJVU file type '{$arr['subtype']}'\n" );
158 private function readChunk( $file ) {
159 $header = fread( $file, 8 );
160 if ( strlen( $header ) < 8 ) {
163 $arr = unpack( 'a4chunk/Nlength', $header );
165 return [ $arr['chunk'], $arr['length'] ];
169 private function skipChunk( $file, $chunkLength ) {
170 fseek( $file, $chunkLength, SEEK_CUR
);
172 if ( ( $chunkLength & 0x01 ) == 1 && !feof( $file ) ) {
174 fseek( $file, 1, SEEK_CUR
);
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 );
183 list( $chunk, $length ) = $this->readChunk( $file );
188 if ( $chunk == 'FORM' ) {
189 $subtype = fread( $file, 4 );
190 if ( $subtype == 'DJVU' ) {
191 wfDebug( __METHOD__
. ": found first subpage\n" );
193 return $this->getPageInfo( $file );
195 $this->skipChunk( $file, $length - 4 );
197 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
198 $this->skipChunk( $file, $length );
200 } while ( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
202 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
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" );
216 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
220 $data = fread( $file, $length );
221 if ( strlen( $data ) < $length ) {
222 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
235 # Newer files have rotation info in byte 10, but we don't use it yet.
238 'width' => $arr['width'],
239 'height' => $arr['height'],
240 'version' => "{$arr['major']}.{$arr['minor']}",
241 'resolution' => $arr['resolution'],
242 'gamma' => $arr['gamma'] / 10.0 ];
246 * Return an XML string describing the DjVu image
247 * @return string|bool
249 function retrieveMetaData() {
250 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
252 if ( !$this->isValid() ) {
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 );
270 if ( isset( $wgDjvuTxt ) ) {
271 $cmd = Shell
::escape( $wgDjvuTxt ) . ' --detail=page ' . Shell
::escape( $this->mFilename
);
272 wfDebug( __METHOD__
. ": $cmd\n" );
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 );
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 \
286 | # Or page can be empty ; in this case, djvutxt dumps ()
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 ) .
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", '�' ], [ ' ', '' ], $val );
304 return '<PAGE value="' . $val . '" />';
308 * Hack to temporarily work around djvutoxml bug
309 * @param string $dump
312 function convertDumpToXML( $dump ) {
313 if ( strval( $dump ) == '' ) {
318 <?xml version="1.0" ?>
319 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
325 $dump = str_replace( "\r", '', $dump );
326 $line = strtok( $dump, "\n" );
329 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
331 if ( $this->parseFormDjvu( $line, $xml ) ) {
336 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
338 $parentLevel = strlen( $m[1] );
340 $line = strtok( "\n" );
341 while ( $line !== false ) {
342 $childLevel = strspn( $line, ' ' );
343 if ( $childLevel <= $parentLevel ) {
348 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
349 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
353 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
355 if ( $this->parseFormDjvu( $line, $xml ) ) {
361 $line = strtok( "\n" );
368 $xml .= "</BODY>\n</DjVuXML>\n";
373 function parseFormDjvu( $line, &$xml ) {
374 $parentLevel = strspn( $line, ' ' );
375 $line = strtok( "\n" );
378 while ( $line !== false ) {
379 $childLevel = strspn( $line, ' ' );
380 if ( $childLevel <= $parentLevel ) {
386 '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
394 # 'type' => 'image/x.djvu',
400 Xml
::element( 'PARAM', [ 'name' => 'DPI', 'value' => $m[3] ] ) . "\n" .
401 Xml
::element( 'PARAM', [ 'name' => 'GAMMA', 'value' => $m[4] ] ) . "\n"
406 $line = strtok( "\n" );