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/
37 function __construct( $filename ) {
38 $this->mFilename
= $filename;
42 * Check if the given file is indeed a valid DjVu image file
45 public function isValid() {
46 $info = $this->getInfo();
47 return $info !== false;
52 * Return data in the style of getimagesize()
53 * @return array or false on failure
55 public function getImageSize() {
56 $data = $this->getInfo();
58 if( $data !== false ) {
59 $width = $data['width'];
60 $height = $data['height'];
62 return array( $width, $height, 'DjVu',
63 "width=\"$width\" height=\"$height\"" );
71 * For debugging; dump the IFF chunk structure
74 $file = fopen( $this->mFilename
, 'rb' );
75 $header = fread( $file, 12 );
76 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
77 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
78 echo "$chunk $chunkLength\n";
79 $this->dumpForm( $file, $chunkLength, 1 );
83 private function dumpForm( $file, $length, $indent ) {
84 $start = ftell( $file );
85 $secondary = fread( $file, 4 );
86 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
87 while( ftell( $file ) - $start < $length ) {
88 $chunkHeader = fread( $file, 8 );
89 if( $chunkHeader == '' ) {
92 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
93 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
94 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
96 if( $chunk == 'FORM' ) {
97 $this->dumpForm( $file, $chunkLength, $indent +
1 );
99 fseek( $file, $chunkLength, SEEK_CUR
);
100 if( $chunkLength & 1 == 1 ) {
101 // Padding byte between chunks
102 fseek( $file, 1, SEEK_CUR
);
109 wfSuppressWarnings();
110 $file = fopen( $this->mFilename
, 'rb' );
112 if( $file === false ) {
113 wfDebug( __METHOD__
. ": missing or failed file read\n" );
117 $header = fread( $file, 16 );
120 if( strlen( $header ) < 16 ) {
121 wfDebug( __METHOD__
. ": too short file header\n" );
123 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
124 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
126 if( $magic != 'AT&T' ) {
127 wfDebug( __METHOD__
. ": not a DjVu file\n" );
128 } elseif( $subtype == 'DJVU' ) {
129 // Single-page document
130 $info = $this->getPageInfo( $file, $formLength );
131 } elseif( $subtype == 'DJVM' ) {
132 // Multi-page document
133 $info = $this->getMultiPageInfo( $file, $formLength );
135 wfDebug( __METHOD__
. ": unrecognized DJVU file type '$formType'\n" );
142 private function readChunk( $file ) {
143 $header = fread( $file, 8 );
144 if( strlen( $header ) < 8 ) {
145 return array( false, 0 );
147 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
148 extract( unpack( 'a4chunk/Nlength', $header ) );
149 return array( $chunk, $length );
153 private function skipChunk( $file, $chunkLength ) {
154 fseek( $file, $chunkLength, SEEK_CUR
);
156 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
158 fseek( $file, 1, SEEK_CUR
);
162 private function getMultiPageInfo( $file, $formLength ) {
163 // For now, we'll just look for the first page in the file
164 // and report its information, hoping others are the same size.
165 $start = ftell( $file );
167 list( $chunk, $length ) = $this->readChunk( $file );
172 if( $chunk == 'FORM' ) {
173 $subtype = fread( $file, 4 );
174 if( $subtype == 'DJVU' ) {
175 wfDebug( __METHOD__
. ": found first subpage\n" );
176 return $this->getPageInfo( $file, $length );
178 $this->skipChunk( $file, $length - 4 );
180 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
181 $this->skipChunk( $file, $length );
183 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
185 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
189 private function getPageInfo( $file, $formLength ) {
190 list( $chunk, $length ) = $this->readChunk( $file );
191 if( $chunk != 'INFO' ) {
192 wfDebug( __METHOD__
. ": expected INFO chunk, got '$chunk'\n" );
197 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
200 $data = fread( $file, $length );
201 if( strlen( $data ) < $length ) {
202 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
206 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
214 # Newer files have rotation info in byte 10, but we don't use it yet.
219 'version' => "$major.$minor",
220 'resolution' => $resolution,
221 'gamma' => $gamma / 10.0 );
225 * Return an XML string describing the DjVu image
228 function retrieveMetaData() {
229 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
230 wfProfileIn( __METHOD__
);
232 if ( isset( $wgDjvuDump ) ) {
233 # djvudump is faster as of version 3.5
234 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
235 wfProfileIn( 'djvudump' );
236 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename
);
237 $dump = wfShellExec( $cmd );
238 $xml = $this->convertDumpToXML( $dump );
239 wfProfileOut( 'djvudump' );
240 } elseif ( isset( $wgDjvuToXML ) ) {
241 wfProfileIn( 'djvutoxml' );
242 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
243 wfEscapeShellArg( $this->mFilename
);
244 $xml = wfShellExec( $cmd );
245 wfProfileOut( 'djvutoxml' );
250 if ( isset( $wgDjvuTxt ) ) {
251 wfProfileIn( 'djvutxt' );
252 $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename
) ;
253 wfDebug( __METHOD__
.": $cmd\n" );
255 $txt = wfShellExec( $cmd, $retval );
256 wfProfileOut( 'djvutxt' );
258 # Strip some control characters
259 $txt = preg_replace( "/[\013\035\037]/", "", $txt );
261 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*"
262 ((?> # Text to match is composed of atoms of either:
263 \\\\. # - any escaped character
264 | # - any character different from " and \
268 | # Or page can be empty ; in this case, djvutxt dumps ()
271 $txt = preg_replace_callback( $reg, array( $this, 'pageTextCallback' ), $txt );
272 $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
273 $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml, 1 );
274 $xml = $xml . $txt. '</mw-djvu>' ;
277 wfProfileOut( __METHOD__
);
281 function pageTextCallback( $matches ) {
282 # Get rid of invalid UTF-8, strip control characters
283 return '<PAGE value="' . htmlspecialchars( UtfNormal
::cleanUp( $matches[1] ) ) . '" />';
287 * Hack to temporarily work around djvutoxml bug
288 * @return bool|string
290 function convertDumpToXML( $dump ) {
291 if ( strval( $dump ) == '' ) {
296 <?xml version="1.0" ?>
297 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
303 $dump = str_replace( "\r", '', $dump );
304 $line = strtok( $dump, "\n" );
307 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
309 if ( $this->parseFormDjvu( $line, $xml ) ) {
314 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
316 $parentLevel = strlen( $m[1] );
318 $line = strtok( "\n" );
319 while ( $line !== false ) {
320 $childLevel = strspn( $line, ' ' );
321 if ( $childLevel <= $parentLevel ) {
326 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
327 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
330 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
332 if ( $this->parseFormDjvu( $line, $xml ) ) {
338 $line = strtok( "\n" );
345 $xml .= "</BODY>\n</DjVuXML>\n";
349 function parseFormDjvu( $line, &$xml ) {
350 $parentLevel = strspn( $line, ' ' );
351 $line = strtok( "\n" );
354 while ( $line !== false ) {
355 $childLevel = strspn( $line, ' ' );
356 if ( $childLevel <= $parentLevel ) {
361 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
362 $xml .= Xml
::tags( 'OBJECT',
365 #'type' => 'image/x.djvu',
371 Xml
::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
372 Xml
::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
376 $line = strtok( "\n" );