5 * Copyright © 2006 Brion Vibber <brion@pobox.com>
6 * http://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
28 * Support for detecting/validating DjVu image files and getting
29 * some basic file metadata (resolution etc)
31 * File format docs are available in source package for DjVuLibre:
32 * http://djvulibre.djvuzone.org/
40 * @param string $filename The DjVu file name.
42 function __construct( $filename ) {
43 $this->mFilename
= $filename;
47 * @const DJVUTXT_MEMORY_LIMIT Memory limit for the DjVu description software
49 const DJVUTXT_MEMORY_LIMIT
= 300000;
52 * Check if the given file is indeed a valid DjVu image file
55 public function isValid() {
56 $info = $this->getInfo();
58 return $info !== false;
62 * Return data in the style of getimagesize()
63 * @return array or false on failure
65 public function getImageSize() {
66 $data = $this->getInfo();
68 if ( $data !== false ) {
69 $width = $data['width'];
70 $height = $data['height'];
72 return array( $width, $height, 'DjVu',
73 "width=\"$width\" height=\"$height\"" );
82 * For debugging; dump the IFF chunk structure
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 );
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 == '' ) {
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";
107 if ( $chunk == 'FORM' ) {
108 $this->dumpForm( $file, $chunkLength, $indent +
1 );
110 fseek( $file, $chunkLength, SEEK_CUR
);
111 if ( $chunkLength & 1 == 1 ) {
112 // Padding byte between chunks
113 fseek( $file, 1, SEEK_CUR
);
120 wfSuppressWarnings();
121 $file = fopen( $this->mFilename
, 'rb' );
123 if ( $file === false ) {
124 wfDebug( __METHOD__
. ": missing or failed file read\n" );
129 $header = fread( $file, 16 );
132 if ( strlen( $header ) < 16 ) {
133 wfDebug( __METHOD__
. ": too short file header\n" );
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 ) );
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 );
147 wfDebug( __METHOD__
. ": unrecognized DJVU file type '$formType'\n" );
155 private function readChunk( $file ) {
156 $header = fread( $file, 8 );
157 if ( strlen( $header ) < 8 ) {
158 return array( false, 0 );
160 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
161 extract( unpack( 'a4chunk/Nlength', $header ) );
163 return array( $chunk, $length );
167 private function skipChunk( $file, $chunkLength ) {
168 fseek( $file, $chunkLength, SEEK_CUR
);
170 if ( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
172 fseek( $file, 1, SEEK_CUR
);
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 );
181 list( $chunk, $length ) = $this->readChunk( $file );
186 if ( $chunk == 'FORM' ) {
187 $subtype = fread( $file, 4 );
188 if ( $subtype == 'DJVU' ) {
189 wfDebug( __METHOD__
. ": found first subpage\n" );
191 return $this->getPageInfo( $file, $length );
193 $this->skipChunk( $file, $length - 4 );
195 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
196 $this->skipChunk( $file, $length );
198 } while ( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
200 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
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" );
214 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
218 $data = fread( $file, $length );
219 if ( strlen( $data ) < $length ) {
220 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
225 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
234 # Newer files have rotation info in byte 10, but we don't use it yet.
239 'version' => "$major.$minor",
240 'resolution' => $resolution,
241 'gamma' => $gamma / 10.0 );
245 * Return an XML string describing the DjVu image
248 function retrieveMetaData() {
249 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
250 wfProfileIn( __METHOD__
);
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' );
270 if ( isset( $wgDjvuTxt ) ) {
271 wfProfileIn( 'djvutxt' );
272 $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename
);
273 wfDebug( __METHOD__
. ": $cmd\n" );
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 );
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 \
288 | # Or page can be empty ; in this case, djvutxt dumps ()
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>';
297 wfProfileOut( __METHOD__
);
302 function pageTextCallback( $matches ) {
303 # Get rid of invalid UTF-8, strip control characters
304 return '<PAGE value="' . htmlspecialchars( UtfNormal
::cleanUp( $matches[1] ) ) . '" />';
308 * Hack to temporarily work around djvutoxml bug
309 * @return bool|string
311 function convertDumpToXML( $dump ) {
312 if ( strval( $dump ) == '' ) {
317 <?xml version="1.0" ?>
318 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
324 $dump = str_replace( "\r", '', $dump );
325 $line = strtok( $dump, "\n" );
328 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
330 if ( $this->parseFormDjvu( $line, $xml ) ) {
335 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
337 $parentLevel = strlen( $m[1] );
339 $line = strtok( "\n" );
340 while ( $line !== false ) {
341 $childLevel = strspn( $line, ' ' );
342 if ( $childLevel <= $parentLevel ) {
347 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
348 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
352 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
354 if ( $this->parseFormDjvu( $line, $xml ) ) {
360 $line = strtok( "\n" );
367 $xml .= "</BODY>\n</DjVuXML>\n";
372 function parseFormDjvu( $line, &$xml ) {
373 $parentLevel = strspn( $line, ' ' );
374 $line = strtok( "\n" );
377 while ( $line !== false ) {
378 $childLevel = strspn( $line, ' ' );
379 if ( $childLevel <= $parentLevel ) {
384 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
389 #'type' => 'image/x.djvu',
395 Xml
::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
396 Xml
::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
401 $line = strtok( "\n" );