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 [ $width, $height,
'DjVu',
73 "width=\"$width\" height=\"$height\"" ];
85 $file = fopen( $this->mFilename,
'rb' );
87 $arr = unpack(
'a4magic/a4chunk/NchunkLength',
$header );
88 $chunk = $arr[
'chunk'];
89 $chunkLength = $arr[
'chunkLength'];
90 echo
"$chunk $chunkLength\n";
91 $this->dumpForm( $file, $chunkLength, 1 );
95 private function dumpForm( $file, $length, $indent ) {
96 $start = ftell( $file );
97 $secondary = fread( $file, 4 );
98 echo str_repeat(
' ', $indent * 4 ) .
"($secondary)\n";
99 while ( ftell( $file ) - $start < $length ) {
100 $chunkHeader = fread( $file, 8 );
101 if ( $chunkHeader ==
'' ) {
104 $arr = unpack(
'a4chunk/NchunkLength', $chunkHeader );
105 $chunk = $arr[
'chunk'];
106 $chunkLength = $arr[
'chunkLength'];
107 echo str_repeat(
' ', $indent * 4 ) .
"$chunk $chunkLength\n";
109 if ( $chunk ==
'FORM' ) {
110 $this->dumpForm( $file, $chunkLength, $indent + 1 );
112 fseek( $file, $chunkLength, SEEK_CUR );
113 if ( $chunkLength & 1 == 1 ) {
115 fseek( $file, 1, SEEK_CUR );
122 MediaWiki\suppressWarnings();
123 $file = fopen( $this->mFilename,
'rb' );
124 MediaWiki\restoreWarnings();
125 if ( $file ===
false ) {
126 wfDebug( __METHOD__ .
": missing or failed file read\n" );
134 if ( strlen(
$header ) < 16 ) {
135 wfDebug( __METHOD__ .
": too short file header\n" );
137 $arr = unpack(
'a4magic/a4form/NformLength/a4subtype',
$header );
139 $subtype = $arr[
'subtype'];
140 if ( $arr[
'magic'] !=
'AT&T' ) {
141 wfDebug( __METHOD__ .
": not a DjVu file\n" );
142 } elseif ( $subtype ==
'DJVU' ) {
144 $info = $this->getPageInfo( $file );
145 } elseif ( $subtype ==
'DJVM' ) {
147 $info = $this->getMultiPageInfo( $file, $arr[
'formLength'] );
149 wfDebug( __METHOD__ .
": unrecognized DJVU file type '{$arr['subtype']}'\n" );
157 private function readChunk( $file ) {
162 $arr = unpack(
'a4chunk/Nlength',
$header );
164 return [ $arr[
'chunk'], $arr[
'length'] ];
168 private function skipChunk( $file, $chunkLength ) {
169 fseek( $file, $chunkLength, SEEK_CUR );
171 if ( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
173 fseek( $file, 1, SEEK_CUR );
177 private function getMultiPageInfo( $file, $formLength ) {
180 $start = ftell( $file );
182 list( $chunk, $length ) = $this->readChunk( $file );
187 if ( $chunk ==
'FORM' ) {
188 $subtype = fread( $file, 4 );
189 if ( $subtype ==
'DJVU' ) {
190 wfDebug( __METHOD__ .
": found first subpage\n" );
192 return $this->getPageInfo( $file );
194 $this->skipChunk( $file, $length - 4 );
196 wfDebug( __METHOD__ .
": skipping '$chunk' chunk\n" );
197 $this->skipChunk( $file, $length );
199 }
while ( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
201 wfDebug( __METHOD__ .
": multi-page DJVU file contained no pages\n" );
206 private function getPageInfo( $file ) {
207 list( $chunk, $length ) = $this->readChunk( $file );
208 if ( $chunk !=
'INFO' ) {
209 wfDebug( __METHOD__ .
": expected INFO chunk, got '$chunk'\n" );
215 wfDebug( __METHOD__ .
": INFO should be 9 or 10 bytes, found $length\n" );
219 $data = fread( $file, $length );
220 if ( strlen( $data ) < $length ) {
221 wfDebug( __METHOD__ .
": INFO chunk cut off\n" );
234 # Newer files have rotation info in byte 10, but we don't use it yet.
237 'width' => $arr[
'width'],
238 'height' => $arr[
'height'],
239 'version' =>
"{$arr['major']}.{$arr['minor']}",
240 'resolution' => $arr[
'resolution'],
241 'gamma' => $arr[
'gamma'] / 10.0 ];
248 function retrieveMetaData() {
251 if ( !$this->isValid() ) {
256 # djvudump is faster as of version 3.5
257 # https://sourceforge.net/p/djvu/bugs/71/
260 $xml = $this->convertDumpToXML( $dump );
271 wfDebug( __METHOD__ .
": $cmd\n" );
275 # Strip some control characters
276 $txt = preg_replace(
"/[\013\035\037]/",
"", $txt );
278 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*
"
279 ((?> # Text to match is composed of atoms of either:
280 \\\\. # - any escaped character
281 | # - any character different from " and \
285 | # Or
page can be empty ;
in this case, djvutxt dumps ()
288 $txt = preg_replace_callback( $reg, [ $this,
'pageTextCallback' ], $txt );
289 $txt =
"<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt .
"</BODY>\n</DjVuTxt>\n";
290 $xml = preg_replace(
"/<DjVuXML>/",
"<mw-djvu><DjVuXML>", $xml, 1 );
291 $xml = $xml . $txt .
'</mw-djvu>';
298 function pageTextCallback(
$matches ) {
299 # Get rid of invalid UTF-8, strip control characters
300 $val = htmlspecialchars(
UtfNormal\Validator::cleanUp( stripcslashes(
$matches[1] ) ) );
301 $val = str_replace( [
"\n",
'�' ], [
' ',
'' ], $val );
302 return '<PAGE value="' . $val .
'" />';
310 function convertDumpToXML( $dump ) {
311 if ( strval( $dump ) ==
'' ) {
317 <!DOCTYPE DjVuXML PUBLIC
"-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
323 $dump = str_replace(
"\r",
'', $dump );
324 $line = strtok( $dump,
"\n" );
327 if ( preg_match(
'/^( *)FORM:DJVU/',
$line, $m ) ) {
329 if ( $this->parseFormDjvu(
$line, $xml ) ) {
334 } elseif ( preg_match(
'/^( *)FORM:DJVM/',
$line, $m ) ) {
336 $parentLevel = strlen( $m[1] );
338 $line = strtok(
"\n" );
339 while (
$line !==
false ) {
340 $childLevel = strspn(
$line,
' ' );
341 if ( $childLevel <= $parentLevel ) {
346 if ( preg_match(
'/^ *DIRM.*indirect/',
$line ) ) {
347 wfDebug(
"Indirect multi-page DjVu document, bad for server!\n" );
351 if ( preg_match(
'/^ *FORM:DJVU/',
$line ) ) {
353 if ( $this->parseFormDjvu(
$line, $xml ) ) {
359 $line = strtok(
"\n" );
366 $xml .=
"</BODY>\n</DjVuXML>\n";
371 function parseFormDjvu(
$line, &$xml ) {
372 $parentLevel = strspn(
$line,
' ' );
373 $line = strtok(
"\n" );
376 while (
$line !==
false ) {
377 $childLevel = strspn(
$line,
' ' );
378 if ( $childLevel <= $parentLevel ) {
384 '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
392 #
'type' =>
'image/x.djvu',
398 Xml::element(
'PARAM', [
'name' =>
'DPI',
'value' => $m[3] ] ) .
"\n" .
399 Xml::element(
'PARAM', [
'name' =>
'GAMMA',
'value' => $m[4] ] ) .
"\n"
404 $line = strtok(
"\n" );