40 const DJVUTXT_MEMORY_LIMIT = 300000;
45 function __construct( $filename ) {
46 $this->mFilename = $filename;
53 public function isValid() {
54 $info = $this->getInfo();
56 return $info !==
false;
63 public function getImageSize() {
64 $data = $this->getInfo();
66 if ( $data !==
false ) {
67 $width = $data[
'width'];
68 $height = $data[
'height'];
70 return [ $width, $height,
'DjVu',
71 "width=\"$width\" height=\"$height\"" ];
83 $file = fopen( $this->mFilename,
'rb' );
85 $arr = unpack(
'a4magic/a4chunk/NchunkLength',
$header );
86 $chunk = $arr[
'chunk'];
87 $chunkLength = $arr[
'chunkLength'];
88 echo
"$chunk $chunkLength\n";
89 $this->dumpForm( $file, $chunkLength, 1 );
93 private function dumpForm( $file, $length, $indent ) {
94 $start = ftell( $file );
95 $secondary = fread( $file, 4 );
96 echo str_repeat(
' ', $indent * 4 ) .
"($secondary)\n";
97 while ( ftell( $file ) - $start < $length ) {
98 $chunkHeader = fread( $file, 8 );
99 if ( $chunkHeader ==
'' ) {
102 $arr = unpack(
'a4chunk/NchunkLength', $chunkHeader );
103 $chunk = $arr[
'chunk'];
104 $chunkLength = $arr[
'chunkLength'];
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 ) {
113 fseek( $file, 1, SEEK_CUR );
120 MediaWiki\suppressWarnings();
121 $file = fopen( $this->mFilename,
'rb' );
122 MediaWiki\restoreWarnings();
123 if ( $file ===
false ) {
124 wfDebug( __METHOD__ .
": missing or failed file read\n" );
132 if ( strlen(
$header ) < 16 ) {
133 wfDebug( __METHOD__ .
": too short file header\n" );
135 $arr = unpack(
'a4magic/a4form/NformLength/a4subtype',
$header );
137 $subtype = $arr[
'subtype'];
138 if ( $arr[
'magic'] !=
'AT&T' ) {
139 wfDebug( __METHOD__ .
": not a DjVu file\n" );
140 } elseif ( $subtype ==
'DJVU' ) {
142 $info = $this->getPageInfo( $file );
143 } elseif ( $subtype ==
'DJVM' ) {
145 $info = $this->getMultiPageInfo( $file, $arr[
'formLength'] );
147 wfDebug( __METHOD__ .
": unrecognized DJVU file type '{$arr['subtype']}'\n" );
155 private function readChunk( $file ) {
160 $arr = unpack(
'a4chunk/Nlength',
$header );
162 return [ $arr[
'chunk'], $arr[
'length'] ];
166 private function skipChunk( $file, $chunkLength ) {
167 fseek( $file, $chunkLength, SEEK_CUR );
169 if ( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
171 fseek( $file, 1, SEEK_CUR );
175 private function getMultiPageInfo( $file, $formLength ) {
178 $start = ftell( $file );
180 list( $chunk, $length ) = $this->readChunk( $file );
185 if ( $chunk ==
'FORM' ) {
186 $subtype = fread( $file, 4 );
187 if ( $subtype ==
'DJVU' ) {
188 wfDebug( __METHOD__ .
": found first subpage\n" );
190 return $this->getPageInfo( $file );
192 $this->skipChunk( $file, $length - 4 );
194 wfDebug( __METHOD__ .
": skipping '$chunk' chunk\n" );
195 $this->skipChunk( $file, $length );
197 }
while ( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
199 wfDebug( __METHOD__ .
": multi-page DJVU file contained no pages\n" );
204 private function getPageInfo( $file ) {
205 list( $chunk, $length ) = $this->readChunk( $file );
206 if ( $chunk !=
'INFO' ) {
207 wfDebug( __METHOD__ .
": expected INFO chunk, got '$chunk'\n" );
213 wfDebug( __METHOD__ .
": INFO should be 9 or 10 bytes, found $length\n" );
217 $data = fread( $file, $length );
218 if ( strlen( $data ) < $length ) {
219 wfDebug( __METHOD__ .
": INFO chunk cut off\n" );
232 # Newer files have rotation info in byte 10, but we don't use it yet.
235 'width' => $arr[
'width'],
236 'height' => $arr[
'height'],
237 'version' =>
"{$arr['major']}.{$arr['minor']}",
238 'resolution' => $arr[
'resolution'],
239 'gamma' => $arr[
'gamma'] / 10.0 ];
246 function retrieveMetaData() {
249 if ( !$this->isValid() ) {
254 # djvudump is faster as of version 3.5
255 # https://sourceforge.net/p/djvu/bugs/71/
258 $xml = $this->convertDumpToXML( $dump );
269 wfDebug( __METHOD__ .
": $cmd\n" );
273 # Strip some control characters
274 $txt = preg_replace(
"/[\013\035\037]/",
"", $txt );
276 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*
"
277 ((?> # Text to match is composed of atoms of either:
278 \\\\. # - any escaped character
279 | # - any character different from " and \
283 | # Or
page can be empty ;
in this case, djvutxt dumps ()
286 $txt = preg_replace_callback( $reg, [ $this,
'pageTextCallback' ], $txt );
287 $txt =
"<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt .
"</BODY>\n</DjVuTxt>\n";
288 $xml = preg_replace(
"/<DjVuXML>/",
"<mw-djvu><DjVuXML>", $xml, 1 );
289 $xml = $xml . $txt .
'</mw-djvu>';
296 function pageTextCallback(
$matches ) {
297 # Get rid of invalid UTF-8, strip control characters
298 $val = htmlspecialchars(
UtfNormal\Validator::cleanUp( stripcslashes(
$matches[1] ) ) );
299 $val = str_replace( [
"\n",
'�' ], [
' ',
'' ], $val );
300 return '<PAGE value="' . $val .
'" />';
308 function convertDumpToXML( $dump ) {
309 if ( strval( $dump ) ==
'' ) {
315 <!DOCTYPE DjVuXML PUBLIC
"-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
321 $dump = str_replace(
"\r",
'', $dump );
322 $line = strtok( $dump,
"\n" );
325 if ( preg_match(
'/^( *)FORM:DJVU/',
$line, $m ) ) {
327 if ( $this->parseFormDjvu(
$line, $xml ) ) {
332 } elseif ( preg_match(
'/^( *)FORM:DJVM/',
$line, $m ) ) {
334 $parentLevel = strlen( $m[1] );
336 $line = strtok(
"\n" );
337 while (
$line !==
false ) {
338 $childLevel = strspn(
$line,
' ' );
339 if ( $childLevel <= $parentLevel ) {
344 if ( preg_match(
'/^ *DIRM.*indirect/',
$line ) ) {
345 wfDebug(
"Indirect multi-page DjVu document, bad for server!\n" );
349 if ( preg_match(
'/^ *FORM:DJVU/',
$line ) ) {
351 if ( $this->parseFormDjvu(
$line, $xml ) ) {
357 $line = strtok(
"\n" );
364 $xml .=
"</BODY>\n</DjVuXML>\n";
369 function parseFormDjvu(
$line, &$xml ) {
370 $parentLevel = strspn(
$line,
' ' );
371 $line = strtok(
"\n" );
374 while (
$line !==
false ) {
375 $childLevel = strspn(
$line,
' ' );
376 if ( $childLevel <= $parentLevel ) {
382 '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
390 #
'type' =>
'image/x.djvu',
396 Xml::element(
'PARAM', [
'name' =>
'DPI',
'value' => $m[3] ] ) .
"\n" .
397 Xml::element(
'PARAM', [
'name' =>
'GAMMA',
'value' => $m[4] ] ) .
"\n"
402 $line = strtok(
"\n" );