46 private const DJVUTXT_MEMORY_LIMIT = 300000000;
55 $this->mFilename = $filename;
63 $info = $this->getInfo();
65 return $info !==
false;
73 $data = $this->getInfo();
75 if ( $data !==
false ) {
77 'width' => $data[
'width'],
78 'height' => $data[
'height']
90 $file = fopen( $this->mFilename,
'rb' );
92 $arr = unpack(
'a4magic/a4chunk/NchunkLength',
$header );
93 $chunk = $arr[
'chunk'];
94 $chunkLength = $arr[
'chunkLength'];
95 echo
"$chunk $chunkLength\n";
96 $this->dumpForm(
$file, $chunkLength, 1 );
100 private function dumpForm(
$file, $length, $indent ) {
101 $start = ftell(
$file );
102 $secondary = fread(
$file, 4 );
103 echo str_repeat(
' ', $indent * 4 ) .
"($secondary)\n";
104 while ( ftell(
$file ) - $start < $length ) {
105 $chunkHeader = fread(
$file, 8 );
106 if ( $chunkHeader ==
'' ) {
109 $arr = unpack(
'a4chunk/NchunkLength', $chunkHeader );
110 $chunk = $arr[
'chunk'];
111 $chunkLength = $arr[
'chunkLength'];
112 echo str_repeat(
' ', $indent * 4 ) .
"$chunk $chunkLength\n";
114 if ( $chunk ===
'FORM' ) {
115 $this->dumpForm(
$file, $chunkLength, $indent + 1 );
117 fseek(
$file, $chunkLength, SEEK_CUR );
118 if ( $chunkLength & 1 ) {
120 fseek(
$file, 1, SEEK_CUR );
126 private function getInfo() {
127 AtEase::suppressWarnings();
128 $file = fopen( $this->mFilename,
'rb' );
129 AtEase::restoreWarnings();
130 if (
$file ===
false ) {
131 wfDebug( __METHOD__ .
": missing or failed file read" );
139 if ( strlen(
$header ) < 16 ) {
140 wfDebug( __METHOD__ .
": too short file header" );
142 $arr = unpack(
'a4magic/a4form/NformLength/a4subtype',
$header );
144 $subtype = $arr[
'subtype'];
145 if ( $arr[
'magic'] !==
'AT&T' ) {
146 wfDebug( __METHOD__ .
": not a DjVu file" );
147 } elseif ( $subtype ===
'DJVU' ) {
149 $info = $this->getPageInfo(
$file );
150 } elseif ( $subtype ===
'DJVM' ) {
152 $info = $this->getMultiPageInfo(
$file, $arr[
'formLength'] );
154 wfDebug( __METHOD__ .
": unrecognized DJVU file type '{$arr['subtype']}'" );
162 private function readChunk(
$file ) {
167 $arr = unpack(
'a4chunk/Nlength',
$header );
169 return [ $arr[
'chunk'], $arr[
'length'] ];
172 private function skipChunk(
$file, $chunkLength ) {
173 fseek(
$file, $chunkLength, SEEK_CUR );
175 if ( ( $chunkLength & 1 ) && !feof(
$file ) ) {
177 fseek(
$file, 1, SEEK_CUR );
181 private function getMultiPageInfo(
$file, $formLength ) {
184 $start = ftell(
$file );
186 [ $chunk, $length ] = $this->readChunk(
$file );
191 if ( $chunk ===
'FORM' ) {
192 $subtype = fread(
$file, 4 );
193 if ( $subtype ===
'DJVU' ) {
194 wfDebug( __METHOD__ .
": found first subpage" );
196 return $this->getPageInfo(
$file );
198 $this->skipChunk(
$file, $length - 4 );
200 wfDebug( __METHOD__ .
": skipping '$chunk' chunk" );
201 $this->skipChunk(
$file, $length );
203 }
while ( $length != 0 && !feof(
$file ) && ftell(
$file ) - $start < $formLength );
205 wfDebug( __METHOD__ .
": multi-page DJVU file contained no pages" );
210 private function getPageInfo(
$file ) {
211 [ $chunk, $length ] = $this->readChunk(
$file );
212 if ( $chunk !==
'INFO' ) {
213 wfDebug( __METHOD__ .
": expected INFO chunk, got '$chunk'" );
219 wfDebug( __METHOD__ .
": INFO should be 9 or 10 bytes, found $length" );
223 $data = fread(
$file, $length );
224 if ( strlen( $data ) < $length ) {
225 wfDebug( __METHOD__ .
": INFO chunk cut off" );
238 # Newer files have rotation info in byte 10, but we don't use it yet.
241 'width' => $arr[
'width'],
242 'height' => $arr[
'height'],
243 'version' =>
"{$arr['major']}.{$arr['minor']}",
244 'resolution' => $arr[
'resolution'],
245 'gamma' => $arr[
'gamma'] / 10.0 ];
253 $djvuDump = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuDump );
254 $djvuTxt = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuTxt );
259 if ( isset( $djvuDump ) ) {
260 # djvudump is faster than djvutoxml (now abandoned) as of version 3.5
262 $cmd = Shell::escape( $djvuDump ) .
' ' . Shell::escape( $this->mFilename );
264 $json = [
'data' => $this->convertDumpToJSON( $dump ) ];
269 if ( isset( $djvuTxt ) ) {
270 $cmd = Shell::escape( $djvuTxt ) .
' --detail=page ' . Shell::escape( $this->mFilename );
271 wfDebug( __METHOD__ .
": $cmd" );
273 $txt =
wfShellExec( $cmd, $retval, [], [
'memory' => self::DJVUTXT_MEMORY_LIMIT ] );
275 if ( $retval === 0 ) {
276 # Strip some control characters
277 # Ignore carriage returns
278 $txt = preg_replace(
"/\\\\013/",
"", $txt );
279 # Replace runs of OCR region separators with a single extra line break
280 $txt = preg_replace(
"/(?:\\\\(035|037))+/",
"\n", $txt );
283 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*
"
284 ((?> # Text to match is composed of atoms of either:
285 \\\\. # - any escaped character
286 | # - any character different from " and \
290 | # Or page can be empty ; in
this case, djvutxt dumps ()
294 preg_match_all( $reg, $txt,
$matches );
295 $json[
'text'] = array_map( [ $this,
'pageTextCallback' ],
$matches[1] );
302 private function pageTextCallback(
string $match ) {
303 # Get rid of invalid UTF-8
304 $val = UtfNormal\Validator::cleanUp( stripcslashes( $match ) );
305 return str_replace(
'�',
'', $val );
312 private function convertDumpToJSON( $dump ) {
313 if ( strval( $dump ) ==
'' ) {
317 $dump = str_replace(
"\r",
'', $dump );
318 $line = strtok( $dump,
"\n" );
322 if ( preg_match(
'/^( *)FORM:DJVU/', $line, $m ) ) {
324 $parsed = $this->parseFormDjvu( $line );
330 $result[
'pages'] = [ $parsed ];
331 } elseif ( preg_match(
'/^( *)FORM:DJVM/', $line, $m ) ) {
333 $parentLevel = strlen( $m[1] );
335 $line = strtok(
"\n" );
336 $result[
'pages'] = [];
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!" );
350 if ( preg_match(
'/^ *FORM:DJVU/', $line ) ) {
352 $parsed = $this->parseFormDjvu( $line );
358 $result[
'pages'][] = $parsed;
360 $line = strtok(
"\n" );
370 private function parseFormDjvu( $line ) {
371 $parentLevel = strspn( $line,
' ' );
372 $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.-]+)/',
387 'height' => (int)$m[2],
388 'width' => (
int)$m[1],
389 'dpi' => (float)$m[3],
390 'gamma' => (
float)$m[4],
393 $line = strtok(
"\n" );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...