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
27 * Support for detecting/validating DjVu image files and getting
28 * some basic file metadata (resolution etc)
30 * File format docs are available in source package for DjVuLibre:
31 * http://djvulibre.djvuzone.org/
36 function __construct( $filename ) {
37 $this->mFilename
= $filename;
41 * Check if the given file is indeed a valid DjVu image file
44 public function isValid() {
45 $info = $this->getInfo();
46 return $info !== false;
51 * Return data in the style of getimagesize()
52 * @return array or false on failure
54 public function getImageSize() {
55 $data = $this->getInfo();
57 if( $data !== false ) {
58 $width = $data['width'];
59 $height = $data['height'];
61 return array( $width, $height, 'DjVu',
62 "width=\"$width\" height=\"$height\"" );
70 * For debugging; dump the IFF chunk structure
73 $file = fopen( $this->mFilename
, 'rb' );
74 $header = fread( $file, 12 );
75 // @todo 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 );
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 == '' ) {
91 // @todo 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";
95 if( $chunk == 'FORM' ) {
96 $this->dumpForm( $file, $chunkLength, $indent +
1 );
98 fseek( $file, $chunkLength, SEEK_CUR
);
99 if( $chunkLength & 1 == 1 ) {
100 // Padding byte between chunks
101 fseek( $file, 1, SEEK_CUR
);
108 wfSuppressWarnings();
109 $file = fopen( $this->mFilename
, 'rb' );
111 if( $file === false ) {
112 wfDebug( __METHOD__
. ": missing or failed file read\n" );
116 $header = fread( $file, 16 );
119 if( strlen( $header ) < 16 ) {
120 wfDebug( __METHOD__
. ": too short file header\n" );
122 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
123 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
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 );
134 wfDebug( __METHOD__
. ": unrecognized DJVU file type '$formType'\n" );
141 private function readChunk( $file ) {
142 $header = fread( $file, 8 );
143 if( strlen( $header ) < 8 ) {
144 return array( false, 0 );
146 // @todo 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 );
152 private function skipChunk( $file, $chunkLength ) {
153 fseek( $file, $chunkLength, SEEK_CUR
);
155 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
157 fseek( $file, 1, SEEK_CUR
);
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 );
166 list( $chunk, $length ) = $this->readChunk( $file );
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 );
177 $this->skipChunk( $file, $length - 4 );
179 wfDebug( __METHOD__
. ": skipping '$chunk' chunk\n" );
180 $this->skipChunk( $file, $length );
182 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
184 wfDebug( __METHOD__
. ": multi-page DJVU file contained no pages\n" );
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" );
196 wfDebug( __METHOD__
. ": INFO should be 9 or 10 bytes, found $length\n" );
199 $data = fread( $file, $length );
200 if( strlen( $data ) < $length ) {
201 wfDebug( __METHOD__
. ": INFO chunk cut off\n" );
205 // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
213 # Newer files have rotation info in byte 10, but we don't use it yet.
218 'version' => "$major.$minor",
219 'resolution' => $resolution,
220 'gamma' => $gamma / 10.0 );
224 * Return an XML string describing the DjVu image
227 function retrieveMetaData() {
228 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
229 wfProfileIn( __METHOD__
);
231 if ( isset( $wgDjvuDump ) ) {
232 # djvudump is faster as of version 3.5
233 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
234 wfProfileIn( 'djvudump' );
235 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename
);
236 $dump = wfShellExec( $cmd );
237 $xml = $this->convertDumpToXML( $dump );
238 wfProfileOut( 'djvudump' );
239 } elseif ( isset( $wgDjvuToXML ) ) {
240 wfProfileIn( 'djvutoxml' );
241 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
242 wfEscapeShellArg( $this->mFilename
);
243 $xml = wfShellExec( $cmd );
244 wfProfileOut( 'djvutoxml' );
249 if ( isset( $wgDjvuTxt ) ) {
250 wfProfileIn( 'djvutxt' );
251 $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename
) ;
252 wfDebug( __METHOD__
.": $cmd\n" );
254 $txt = wfShellExec( $cmd, $retval );
255 wfProfileOut( 'djvutxt' );
257 # Strip some control characters
258 $txt = preg_replace( "/[\013\035\037]/", "", $txt );
260 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*"
261 ((?> # Text to match is composed of atoms of either:
262 \\\\. # - any escaped character
263 | # - any character different from " and \
267 | # Or page can be empty ; in this case, djvutxt dumps ()
270 $txt = preg_replace_callback( $reg, array( $this, 'pageTextCallback' ), $txt );
271 $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
272 $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml, 1 );
273 $xml = $xml . $txt. '</mw-djvu>' ;
276 wfProfileOut( __METHOD__
);
280 function pageTextCallback( $matches ) {
281 # Get rid of invalid UTF-8, strip control characters
282 return '<PAGE value="' . htmlspecialchars( UtfNormal
::cleanUp( $matches[1] ) ) . '" />';
286 * Hack to temporarily work around djvutoxml bug
287 * @return bool|string
289 function convertDumpToXML( $dump ) {
290 if ( strval( $dump ) == '' ) {
295 <?xml version="1.0" ?>
296 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
302 $dump = str_replace( "\r", '', $dump );
303 $line = strtok( $dump, "\n" );
306 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
308 if ( $this->parseFormDjvu( $line, $xml ) ) {
313 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
315 $parentLevel = strlen( $m[1] );
317 $line = strtok( "\n" );
318 while ( $line !== false ) {
319 $childLevel = strspn( $line, ' ' );
320 if ( $childLevel <= $parentLevel ) {
325 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
326 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
329 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
331 if ( $this->parseFormDjvu( $line, $xml ) ) {
337 $line = strtok( "\n" );
344 $xml .= "</BODY>\n</DjVuXML>\n";
348 function parseFormDjvu( $line, &$xml ) {
349 $parentLevel = strspn( $line, ' ' );
350 $line = strtok( "\n" );
353 while ( $line !== false ) {
354 $childLevel = strspn( $line, ' ' );
355 if ( $childLevel <= $parentLevel ) {
360 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
361 $xml .= Xml
::tags( 'OBJECT',
364 #'type' => 'image/x.djvu',
370 Xml
::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
371 Xml
::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
375 $line = strtok( "\n" );