40 const DJVUTXT_MEMORY_LIMIT = 300000;
47 function __construct( $filename ) {
48 $this->mFilename = $filename;
55 public function isValid() {
56 $info = $this->getInfo();
58 return $info !==
false;
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\"" );
85 $file = fopen( $this->mFilename,
'rb' );
86 $header = fread(
$file, 12 );
89 extract( unpack(
'a4magic/a4chunk/NchunkLength', $header ) );
92 echo
"$chunk $chunkLength\n";
93 $this->dumpForm(
$file, $chunkLength, 1 );
97 private function dumpForm(
$file, $length, $indent ) {
98 $start = ftell(
$file );
99 $secondary = fread(
$file, 4 );
100 echo str_repeat(
' ', $indent * 4 ) .
"($secondary)\n";
101 while ( ftell(
$file ) - $start < $length ) {
102 $chunkHeader = fread(
$file, 8 );
103 if ( $chunkHeader ==
'' ) {
108 extract( unpack(
'a4chunk/NchunkLength', $chunkHeader ) );
111 echo str_repeat(
' ', $indent * 4 ) .
"$chunk $chunkLength\n";
113 if ( $chunk ==
'FORM' ) {
114 $this->dumpForm(
$file, $chunkLength, $indent + 1 );
116 fseek(
$file, $chunkLength, SEEK_CUR );
117 if ( $chunkLength & 1 == 1 ) {
119 fseek(
$file, 1, SEEK_CUR );
127 $file = fopen( $this->mFilename,
'rb' );
129 if (
$file ===
false ) {
130 wfDebug( __METHOD__ .
": missing or failed file read\n" );
135 $header = fread(
$file, 16 );
138 if ( strlen( $header ) < 16 ) {
139 wfDebug( __METHOD__ .
": too short file header\n" );
143 extract( unpack(
'a4magic/a4form/NformLength/a4subtype', $header ) );
149 if ( $magic !=
'AT&T' ) {
150 wfDebug( __METHOD__ .
": not a DjVu file\n" );
151 } elseif ( $subtype ==
'DJVU' ) {
153 $info = $this->getPageInfo(
$file, $formLength );
154 } elseif ( $subtype ==
'DJVM' ) {
156 $info = $this->getMultiPageInfo(
$file, $formLength );
158 wfDebug( __METHOD__ .
": unrecognized DJVU file type '$formType'\n" );
166 private function readChunk(
$file ) {
167 $header = fread(
$file, 8 );
168 if ( strlen( $header ) < 8 ) {
169 return array(
false, 0 );
173 extract( unpack(
'a4chunk/Nlength', $header ) );
177 return array( $chunk, $length );
181 private function skipChunk(
$file, $chunkLength ) {
182 fseek(
$file, $chunkLength, SEEK_CUR );
184 if ( $chunkLength & 0x01 == 1 && !feof(
$file ) ) {
186 fseek(
$file, 1, SEEK_CUR );
190 private function getMultiPageInfo(
$file, $formLength ) {
193 $start = ftell(
$file );
195 list( $chunk, $length ) = $this->readChunk(
$file );
200 if ( $chunk ==
'FORM' ) {
201 $subtype = fread(
$file, 4 );
202 if ( $subtype ==
'DJVU' ) {
203 wfDebug( __METHOD__ .
": found first subpage\n" );
205 return $this->getPageInfo(
$file, $length );
207 $this->skipChunk(
$file, $length - 4 );
209 wfDebug( __METHOD__ .
": skipping '$chunk' chunk\n" );
210 $this->skipChunk(
$file, $length );
212 }
while ( $length != 0 && !feof(
$file ) && ftell(
$file ) - $start < $formLength );
214 wfDebug( __METHOD__ .
": multi-page DJVU file contained no pages\n" );
219 private function getPageInfo(
$file, $formLength ) {
220 list( $chunk, $length ) = $this->readChunk(
$file );
221 if ( $chunk !=
'INFO' ) {
222 wfDebug( __METHOD__ .
": expected INFO chunk, got '$chunk'\n" );
228 wfDebug( __METHOD__ .
": INFO should be 9 or 10 bytes, found $length\n" );
232 $data = fread(
$file, $length );
233 if ( strlen( $data ) < $length ) {
234 wfDebug( __METHOD__ .
": INFO chunk cut off\n" );
249 # Newer files have rotation info in byte 10, but we don't use it yet.
261 'version' =>
"$major.$minor",
262 'resolution' => $resolution,
263 'gamma' => $gamma / 10.0 );
270 function retrieveMetaData() {
271 global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
274 if ( isset( $wgDjvuDump ) ) {
275 # djvudump is faster as of version 3.5
276 # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
280 $xml = $this->convertDumpToXML( $dump );
282 } elseif ( isset( $wgDjvuToXML ) ) {
284 $cmd =
wfEscapeShellArg( $wgDjvuToXML ) .
' --without-anno --without-text ' .
292 if ( isset( $wgDjvuTxt ) ) {
295 wfDebug( __METHOD__ .
": $cmd\n" );
300 # Strip some control characters
301 $txt = preg_replace(
"/[\013\035\037]/",
"", $txt );
303 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*
"
304 ((?> # Text to match is composed of atoms of either:
305 \\\\. # - any escaped character
306 | # - any character different from " and \
310 | # Or
page can be empty ;
in this case, djvutxt dumps ()
313 $txt = preg_replace_callback( $reg,
array( $this,
'pageTextCallback' ), $txt );
314 $txt =
"<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt .
"</BODY>\n</DjVuTxt>\n";
315 $xml = preg_replace(
"/<DjVuXML>/",
"<mw-djvu><DjVuXML>", $xml, 1 );
316 $xml = $xml . $txt .
'</mw-djvu>';
324 function pageTextCallback(
$matches ) {
325 # Get rid of invalid UTF-8, strip control characters
327 $val = str_replace(
array(
"\n",
'�' ),
array(
' ',
'' ), $val );
328 return '<PAGE value="' . $val .
'" />';
336 function convertDumpToXML( $dump ) {
337 if ( strval( $dump ) ==
'' ) {
343 <!DOCTYPE DjVuXML PUBLIC
"-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
349 $dump = str_replace(
"\r",
'', $dump );
350 $line = strtok( $dump,
"\n" );
353 if ( preg_match(
'/^( *)FORM:DJVU/',
$line, $m ) ) {
355 if ( $this->parseFormDjvu(
$line, $xml ) ) {
360 } elseif ( preg_match(
'/^( *)FORM:DJVM/',
$line, $m ) ) {
362 $parentLevel = strlen( $m[1] );
364 $line = strtok(
"\n" );
365 while (
$line !==
false ) {
366 $childLevel = strspn(
$line,
' ' );
367 if ( $childLevel <= $parentLevel ) {
372 if ( preg_match(
'/^ *DIRM.*indirect/',
$line ) ) {
373 wfDebug(
"Indirect multi-page DjVu document, bad for server!\n" );
377 if ( preg_match(
'/^ *FORM:DJVU/',
$line ) ) {
379 if ( $this->parseFormDjvu(
$line, $xml ) ) {
385 $line = strtok(
"\n" );
392 $xml .=
"</BODY>\n</DjVuXML>\n";
397 function parseFormDjvu(
$line, &$xml ) {
398 $parentLevel = strspn(
$line,
' ' );
399 $line = strtok(
"\n" );
402 while (
$line !==
false ) {
403 $childLevel = strspn(
$line,
' ' );
404 if ( $childLevel <= $parentLevel ) {
410 '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
418 #
'type' =>
'image/x.djvu',
430 $line = strtok(
"\n" );