(bug 19441) Updated date formatting for Lithuanian
[mediawiki.git] / includes / DjVuImage.php
blob1b8c4e4c10510eeb663cd2c73137bb4896601331
1 <?php
3 /**
5 * Copyright (C) 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
25 /**
26 * Support for detecting/validating DjVu image files and getting
27 * some basic file metadata (resolution etc)
29 * File format docs are available in source package for DjVuLibre:
30 * http://djvulibre.djvuzone.org/
32 * @ingroup Media
34 class DjVuImage {
35 function __construct( $filename ) {
36 $this->mFilename = $filename;
39 /**
40 * Check if the given file is indeed a valid DjVu image file
41 * @return bool
43 public function isValid() {
44 $info = $this->getInfo();
45 return $info !== false;
49 /**
50 * Return data in the style of getimagesize()
51 * @return array or false on failure
53 public function getImageSize() {
54 $data = $this->getInfo();
56 if( $data !== false ) {
57 $width = $data['width'];
58 $height = $data['height'];
60 return array( $width, $height, 'DjVu',
61 "width=\"$width\" height=\"$height\"" );
63 return false;
66 // ---------
68 /**
69 * For debugging; dump the IFF chunk structure
71 function dump() {
72 $file = fopen( $this->mFilename, 'rb' );
73 $header = fread( $file, 12 );
74 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
75 extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
76 echo "$chunk $chunkLength\n";
77 $this->dumpForm( $file, $chunkLength, 1 );
78 fclose( $file );
81 private function dumpForm( $file, $length, $indent ) {
82 $start = ftell( $file );
83 $secondary = fread( $file, 4 );
84 echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
85 while( ftell( $file ) - $start < $length ) {
86 $chunkHeader = fread( $file, 8 );
87 if( $chunkHeader == '' ) {
88 break;
90 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
91 extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
92 echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
94 if( $chunk == 'FORM' ) {
95 $this->dumpForm( $file, $chunkLength, $indent + 1 );
96 } else {
97 fseek( $file, $chunkLength, SEEK_CUR );
98 if( $chunkLength & 1 == 1 ) {
99 // Padding byte between chunks
100 fseek( $file, 1, SEEK_CUR );
106 function getInfo() {
107 wfSuppressWarnings();
108 $file = fopen( $this->mFilename, 'rb' );
109 wfRestoreWarnings();
110 if( $file === false ) {
111 wfDebug( __METHOD__ . ": missing or failed file read\n" );
112 return false;
115 $header = fread( $file, 16 );
116 $info = false;
118 if( strlen( $header ) < 16 ) {
119 wfDebug( __METHOD__ . ": too short file header\n" );
120 } else {
121 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
122 extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
124 if( $magic != 'AT&T' ) {
125 wfDebug( __METHOD__ . ": not a DjVu file\n" );
126 } elseif( $subtype == 'DJVU' ) {
127 // Single-page document
128 $info = $this->getPageInfo( $file, $formLength );
129 } elseif( $subtype == 'DJVM' ) {
130 // Multi-page document
131 $info = $this->getMultiPageInfo( $file, $formLength );
132 } else {
133 wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
136 fclose( $file );
137 return $info;
140 private function readChunk( $file ) {
141 $header = fread( $file, 8 );
142 if( strlen( $header ) < 8 ) {
143 return array( false, 0 );
144 } else {
145 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
146 extract( unpack( 'a4chunk/Nlength', $header ) );
147 return array( $chunk, $length );
151 private function skipChunk( $file, $chunkLength ) {
152 fseek( $file, $chunkLength, SEEK_CUR );
154 if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
155 // padding byte
156 fseek( $file, 1, SEEK_CUR );
160 private function getMultiPageInfo( $file, $formLength ) {
161 // For now, we'll just look for the first page in the file
162 // and report its information, hoping others are the same size.
163 $start = ftell( $file );
164 do {
165 list( $chunk, $length ) = $this->readChunk( $file );
166 if( !$chunk ) {
167 break;
170 if( $chunk == 'FORM' ) {
171 $subtype = fread( $file, 4 );
172 if( $subtype == 'DJVU' ) {
173 wfDebug( __METHOD__ . ": found first subpage\n" );
174 return $this->getPageInfo( $file, $length );
176 $this->skipChunk( $file, $length - 4 );
177 } else {
178 wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
179 $this->skipChunk( $file, $length );
181 } while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
183 wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
184 return false;
187 private function getPageInfo( $file, $formLength ) {
188 list( $chunk, $length ) = $this->readChunk( $file );
189 if( $chunk != 'INFO' ) {
190 wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
191 return false;
194 if( $length < 9 ) {
195 wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
196 return false;
198 $data = fread( $file, $length );
199 if( strlen( $data ) < $length ) {
200 wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
201 return false;
204 // FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
205 extract( unpack(
206 'nwidth/' .
207 'nheight/' .
208 'Cminor/' .
209 'Cmajor/' .
210 'vresolution/' .
211 'Cgamma', $data ) );
212 # Newer files have rotation info in byte 10, but we don't use it yet.
214 return array(
215 'width' => $width,
216 'height' => $height,
217 'version' => "$major.$minor",
218 'resolution' => $resolution,
219 'gamma' => $gamma / 10.0 );
223 * Return an XML string describing the DjVu image
224 * @return string
226 function retrieveMetaData() {
227 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
228 if ( isset( $wgDjvuDump ) ) {
229 # djvudump is faster as of version 3.5
230 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
231 wfProfileIn( 'djvudump' );
232 $cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename );
233 $dump = wfShellExec( $cmd );
234 $xml = $this->convertDumpToXML( $dump );
235 wfProfileOut( 'djvudump' );
236 } elseif ( isset( $wgDjvuToXML ) ) {
237 wfProfileIn( 'djvutoxml' );
238 $cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
239 wfEscapeShellArg( $this->mFilename );
240 $xml = wfShellExec( $cmd );
241 wfProfileOut( 'djvutoxml' );
242 } else {
243 $xml = null;
245 # Text layer
246 if ( isset( $wgDjvuTxt ) ) {
247 wfProfileIn( 'djvutxt' );
248 $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename ) ;
249 wfDebug( __METHOD__.": $cmd\n" );
250 $txt = wfShellExec( $cmd, $retval );
251 wfProfileOut( 'djvutxt' );
252 if( $retval == 0) {
253 $txt = utf8_encode($txt);
254 $txt = htmlspecialchars($txt);
255 $txt = preg_replace( "/\((page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*\&quot;([^<]*?)\&quot;\s*|)\)/s", "<PAGE value=\"$2\" />", $txt );
256 $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
257 $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml );
258 $xml = $xml . $txt. '</mw-djvu>' ;
261 return $xml;
265 * Hack to temporarily work around djvutoxml bug
267 function convertDumpToXML( $dump ) {
268 if ( strval( $dump ) == '' ) {
269 return false;
272 $xml = <<<EOT
273 <?xml version="1.0" ?>
274 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
275 <DjVuXML>
276 <HEAD></HEAD>
277 <BODY>
278 EOT;
280 $dump = str_replace( "\r", '', $dump );
281 $line = strtok( $dump, "\n" );
282 $m = false;
283 $good = false;
284 if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
285 # Single-page
286 if ( $this->parseFormDjvu( $line, $xml ) ) {
287 $good = true;
288 } else {
289 return false;
291 } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
292 # Multi-page
293 $parentLevel = strlen( $m[1] );
294 # Find DIRM
295 $line = strtok( "\n" );
296 while ( $line !== false ) {
297 $childLevel = strspn( $line, ' ' );
298 if ( $childLevel <= $parentLevel ) {
299 # End of chunk
300 break;
303 if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
304 wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
305 return false;
307 if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
308 # Found page
309 if ( $this->parseFormDjvu( $line, $xml ) ) {
310 $good = true;
311 } else {
312 return false;
315 $line = strtok( "\n" );
318 if ( !$good ) {
319 return false;
322 $xml .= "</BODY>\n</DjVuXML>\n";
323 return $xml;
326 function parseFormDjvu( $line, &$xml ) {
327 $parentLevel = strspn( $line, ' ' );
328 $line = strtok( "\n" );
330 # Find INFO
331 while ( $line !== false ) {
332 $childLevel = strspn( $line, ' ' );
333 if ( $childLevel <= $parentLevel ) {
334 # End of chunk
335 break;
338 if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
339 $xml .= Xml::tags( 'OBJECT',
340 array(
341 #'data' => '',
342 #'type' => 'image/x.djvu',
343 'height' => $m[2],
344 'width' => $m[1],
345 #'usemap' => '',
347 "\n" .
348 Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
349 Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
350 ) . "\n";
351 return true;
353 $line = strtok( "\n" );
355 # Not found
356 return false;