52 $this->mFilename = $filename;
62 return $info !==
false;
72 if ( $data !==
false ) {
73 $width = $data[
'width'];
74 $height = $data[
'height'];
76 return [ $width, $height,
'DjVu',
77 "width=\"$width\" height=\"$height\"" ];
89 $file = fopen( $this->mFilename,
'rb' );
91 $arr = unpack(
'a4magic/a4chunk/NchunkLength',
$header );
92 $chunk = $arr[
'chunk'];
93 $chunkLength = $arr[
'chunkLength'];
94 echo
"$chunk $chunkLength\n";
100 $start = ftell(
$file );
101 $secondary = fread(
$file, 4 );
102 echo str_repeat(
' ', $indent * 4 ) .
"($secondary)\n";
103 while ( ftell(
$file ) - $start < $length ) {
104 $chunkHeader = fread(
$file, 8 );
105 if ( $chunkHeader ==
'' ) {
108 $arr = unpack(
'a4chunk/NchunkLength', $chunkHeader );
109 $chunk = $arr[
'chunk'];
110 $chunkLength = $arr[
'chunkLength'];
111 echo str_repeat(
' ', $indent * 4 ) .
"$chunk $chunkLength\n";
113 if ( $chunk ==
'FORM' ) {
116 fseek(
$file, $chunkLength, SEEK_CUR );
117 if ( $chunkLength & 1 ) {
119 fseek(
$file, 1, SEEK_CUR );
126 Wikimedia\suppressWarnings();
127 $file = fopen( $this->mFilename,
'rb' );
128 Wikimedia\restoreWarnings();
129 if (
$file ===
false ) {
130 wfDebug( __METHOD__ .
": missing or failed file read" );
138 if ( strlen(
$header ) < 16 ) {
139 wfDebug( __METHOD__ .
": too short file header" );
141 $arr = unpack(
'a4magic/a4form/NformLength/a4subtype',
$header );
143 $subtype = $arr[
'subtype'];
144 if ( $arr[
'magic'] !=
'AT&T' ) {
145 wfDebug( __METHOD__ .
": not a DjVu file" );
146 } elseif ( $subtype ==
'DJVU' ) {
149 } elseif ( $subtype ==
'DJVM' ) {
153 wfDebug( __METHOD__ .
": unrecognized DJVU file type '{$arr['subtype']}'" );
166 $arr = unpack(
'a4chunk/Nlength',
$header );
168 return [ $arr[
'chunk'], $arr[
'length'] ];
173 fseek(
$file, $chunkLength, SEEK_CUR );
175 if ( ( $chunkLength & 1 ) && !feof(
$file ) ) {
177 fseek(
$file, 1, SEEK_CUR );
184 $start = ftell(
$file );
191 if ( $chunk ==
'FORM' ) {
192 $subtype = fread(
$file, 4 );
193 if ( $subtype ==
'DJVU' ) {
194 wfDebug( __METHOD__ .
": found first subpage" );
200 wfDebug( __METHOD__ .
": skipping '$chunk' chunk" );
203 }
while ( $length != 0 && !feof(
$file ) && ftell(
$file ) - $start < $formLength );
205 wfDebug( __METHOD__ .
": multi-page DJVU file contained no pages" );
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 ];
260 # djvudump is faster as of version 3.5
262 $cmd = Shell::escape(
$wgDjvuDump ) .
' ' . Shell::escape( $this->mFilename );
266 $cmd = Shell::escape(
$wgDjvuToXML ) .
' --without-anno --without-text ' .
267 Shell::escape( $this->mFilename );
274 $cmd = Shell::escape(
$wgDjvuTxt ) .
' --detail=page ' . Shell::escape( $this->mFilename );
275 wfDebug( __METHOD__ .
": $cmd" );
277 $txt =
wfShellExec( $cmd, $retval, [], [
'memory' => self::DJVUTXT_MEMORY_LIMIT ] );
278 if ( $retval == 0 ) {
279 # Strip some control characters
280 $txt = preg_replace(
"/[\013\035\037]/",
"", $txt );
282 /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*
"
283 ((?> # Text to match is composed of atoms of either:
284 \\\\. # - any escaped character
285 | # - any character different from " and \
289 | # Or page can be empty ; in
this case, djvutxt dumps ()
292 $txt = preg_replace_callback( $reg, [ $this,
'pageTextCallback' ], $txt );
293 $txt =
"<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt .
"</BODY>\n</DjVuTxt>\n";
294 $xml = preg_replace(
"/<DjVuXML>/",
"<mw-djvu><DjVuXML>", $xml, 1 ) .
304 # Get rid of invalid UTF-8, strip control characters
305 $val = htmlspecialchars( UtfNormal\Validator::cleanUp( stripcslashes(
$matches[1] ) ) );
306 $val = str_replace( [
"\n",
'�' ], [
' ',
'' ], $val );
307 return '<PAGE value="' . $val .
'" />';
316 if ( strval( $dump ) ==
'' ) {
321<?xml version=
"1.0" ?>
322<!DOCTYPE DjVuXML PUBLIC
"-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
328 $dump = str_replace(
"\r",
'', $dump );
329 $line = strtok( $dump,
"\n" );
332 if ( preg_match(
'/^( *)FORM:DJVU/',
$line, $m ) ) {
339 } elseif ( preg_match(
'/^( *)FORM:DJVM/',
$line, $m ) ) {
341 $parentLevel = strlen( $m[1] );
343 $line = strtok(
"\n" );
344 while (
$line !==
false ) {
345 $childLevel = strspn(
$line,
' ' );
346 if ( $childLevel <= $parentLevel ) {
351 if ( preg_match(
'/^ *DIRM.*indirect/',
$line ) ) {
352 wfDebug(
"Indirect multi-page DjVu document, bad for server!" );
356 if ( preg_match(
'/^ *FORM:DJVU/',
$line ) ) {
364 $line = strtok(
"\n" );
371 $xml .=
"</BODY>\n</DjVuXML>\n";
377 $parentLevel = strspn(
$line,
' ' );
378 $line = strtok(
"\n" );
381 while (
$line !==
false ) {
382 $childLevel = strspn(
$line,
' ' );
383 if ( $childLevel <= $parentLevel ) {
389 '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
397 #
'type' =>
'image/x.djvu',
403 Xml::element(
'PARAM', [
'name' =>
'DPI',
'value' => $m[3] ] ) .
"\n" .
404 Xml::element(
'PARAM', [
'name' =>
'GAMMA',
'value' => $m[4] ] ) .
"\n"
409 $line = strtok(
"\n" );
$wgDjvuToXML
Path of the djvutoxml executable This works like djvudump except much, much slower as of version 3....
$wgDjvuTxt
Path of the djvutxt DJVU text extraction utility Enable this and $wgDjvuDump to enable text layer ext...
$wgDjvuDump
Path of the djvudump executable Enable this and $wgDjvuRenderer to enable djvu rendering example: $wg...
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...
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
convertDumpToXML( $dump)
Hack to temporarily work around djvutoxml bug.
parseFormDjvu( $line, &$xml)
retrieveMetaData()
Return an XML string describing the DjVu image.
const DJVUTXT_MEMORY_LIMIT
Memory limit for the DjVu description software.
skipChunk( $file, $chunkLength)
dumpForm( $file, $length, $indent)
pageTextCallback( $matches)
dump()
For debugging; dump the IFF chunk structure.
isValid()
Check if the given file is indeed a valid DjVu image file.
getMultiPageInfo( $file, $formLength)
getImageSize()
Return data in the style of getimagesize()
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.