MediaWiki  master
DjVuImage.php
Go to the documentation of this file.
1 <?php
28 
38 class DjVuImage {
39 
43  const DJVUTXT_MEMORY_LIMIT = 300000;
44 
48  function __construct( $filename ) {
49  $this->mFilename = $filename;
50  }
51 
56  public function isValid() {
57  $info = $this->getInfo();
58 
59  return $info !== false;
60  }
61 
66  public function getImageSize() {
67  $data = $this->getInfo();
68 
69  if ( $data !== false ) {
70  $width = $data['width'];
71  $height = $data['height'];
72 
73  return [ $width, $height, 'DjVu',
74  "width=\"$width\" height=\"$height\"" ];
75  }
76 
77  return false;
78  }
79 
80  // ---------
81 
85  function dump() {
86  $file = fopen( $this->mFilename, 'rb' );
87  $header = fread( $file, 12 );
88  $arr = unpack( 'a4magic/a4chunk/NchunkLength', $header );
89  $chunk = $arr['chunk'];
90  $chunkLength = $arr['chunkLength'];
91  echo "$chunk $chunkLength\n";
92  $this->dumpForm( $file, $chunkLength, 1 );
93  fclose( $file );
94  }
95 
96  private function dumpForm( $file, $length, $indent ) {
97  $start = ftell( $file );
98  $secondary = fread( $file, 4 );
99  echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
100  while ( ftell( $file ) - $start < $length ) {
101  $chunkHeader = fread( $file, 8 );
102  if ( $chunkHeader == '' ) {
103  break;
104  }
105  $arr = unpack( 'a4chunk/NchunkLength', $chunkHeader );
106  $chunk = $arr['chunk'];
107  $chunkLength = $arr['chunkLength'];
108  echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
109 
110  if ( $chunk == 'FORM' ) {
111  $this->dumpForm( $file, $chunkLength, $indent + 1 );
112  } else {
113  fseek( $file, $chunkLength, SEEK_CUR );
114  if ( ( $chunkLength & 1 ) == 1 ) {
115  // Padding byte between chunks
116  fseek( $file, 1, SEEK_CUR );
117  }
118  }
119  }
120  }
121 
122  function getInfo() {
123  Wikimedia\suppressWarnings();
124  $file = fopen( $this->mFilename, 'rb' );
125  Wikimedia\restoreWarnings();
126  if ( $file === false ) {
127  wfDebug( __METHOD__ . ": missing or failed file read\n" );
128 
129  return false;
130  }
131 
132  $header = fread( $file, 16 );
133  $info = false;
134 
135  if ( strlen( $header ) < 16 ) {
136  wfDebug( __METHOD__ . ": too short file header\n" );
137  } else {
138  $arr = unpack( 'a4magic/a4form/NformLength/a4subtype', $header );
139 
140  $subtype = $arr['subtype'];
141  if ( $arr['magic'] != 'AT&T' ) {
142  wfDebug( __METHOD__ . ": not a DjVu file\n" );
143  } elseif ( $subtype == 'DJVU' ) {
144  // Single-page document
145  $info = $this->getPageInfo( $file );
146  } elseif ( $subtype == 'DJVM' ) {
147  // Multi-page document
148  $info = $this->getMultiPageInfo( $file, $arr['formLength'] );
149  } else {
150  wfDebug( __METHOD__ . ": unrecognized DJVU file type '{$arr['subtype']}'\n" );
151  }
152  }
153  fclose( $file );
154 
155  return $info;
156  }
157 
158  private function readChunk( $file ) {
159  $header = fread( $file, 8 );
160  if ( strlen( $header ) < 8 ) {
161  return [ false, 0 ];
162  } else {
163  $arr = unpack( 'a4chunk/Nlength', $header );
164 
165  return [ $arr['chunk'], $arr['length'] ];
166  }
167  }
168 
169  private function skipChunk( $file, $chunkLength ) {
170  fseek( $file, $chunkLength, SEEK_CUR );
171 
172  if ( ( $chunkLength & 0x01 ) == 1 && !feof( $file ) ) {
173  // padding byte
174  fseek( $file, 1, SEEK_CUR );
175  }
176  }
177 
178  private function getMultiPageInfo( $file, $formLength ) {
179  // For now, we'll just look for the first page in the file
180  // and report its information, hoping others are the same size.
181  $start = ftell( $file );
182  do {
183  list( $chunk, $length ) = $this->readChunk( $file );
184  if ( !$chunk ) {
185  break;
186  }
187 
188  if ( $chunk == 'FORM' ) {
189  $subtype = fread( $file, 4 );
190  if ( $subtype == 'DJVU' ) {
191  wfDebug( __METHOD__ . ": found first subpage\n" );
192 
193  return $this->getPageInfo( $file );
194  }
195  $this->skipChunk( $file, $length - 4 );
196  } else {
197  wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
198  $this->skipChunk( $file, $length );
199  }
200  } while ( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
201 
202  wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
203 
204  return false;
205  }
206 
207  private function getPageInfo( $file ) {
208  list( $chunk, $length ) = $this->readChunk( $file );
209  if ( $chunk != 'INFO' ) {
210  wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
211 
212  return false;
213  }
214 
215  if ( $length < 9 ) {
216  wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
217 
218  return false;
219  }
220  $data = fread( $file, $length );
221  if ( strlen( $data ) < $length ) {
222  wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
223 
224  return false;
225  }
226 
227  $arr = unpack(
228  'nwidth/' .
229  'nheight/' .
230  'Cminor/' .
231  'Cmajor/' .
232  'vresolution/' .
233  'Cgamma', $data );
234 
235  # Newer files have rotation info in byte 10, but we don't use it yet.
236 
237  return [
238  'width' => $arr['width'],
239  'height' => $arr['height'],
240  'version' => "{$arr['major']}.{$arr['minor']}",
241  'resolution' => $arr['resolution'],
242  'gamma' => $arr['gamma'] / 10.0 ];
243  }
244 
249  function retrieveMetaData() {
251 
252  if ( !$this->isValid() ) {
253  return false;
254  }
255 
256  if ( isset( $wgDjvuDump ) ) {
257  # djvudump is faster as of version 3.5
258  # https://sourceforge.net/p/djvu/bugs/71/
259  $cmd = Shell::escape( $wgDjvuDump ) . ' ' . Shell::escape( $this->mFilename );
260  $dump = wfShellExec( $cmd );
261  $xml = $this->convertDumpToXML( $dump );
262  } elseif ( isset( $wgDjvuToXML ) ) {
263  $cmd = Shell::escape( $wgDjvuToXML ) . ' --without-anno --without-text ' .
264  Shell::escape( $this->mFilename );
265  $xml = wfShellExec( $cmd );
266  } else {
267  $xml = null;
268  }
269  # Text layer
270  if ( isset( $wgDjvuTxt ) ) {
271  $cmd = Shell::escape( $wgDjvuTxt ) . ' --detail=page ' . Shell::escape( $this->mFilename );
272  wfDebug( __METHOD__ . ": $cmd\n" );
273  $retval = '';
274  $txt = wfShellExec( $cmd, $retval, [], [ 'memory' => self::DJVUTXT_MEMORY_LIMIT ] );
275  if ( $retval == 0 ) {
276  # Strip some control characters
277  $txt = preg_replace( "/[\013\035\037]/", "", $txt );
278  $reg = <<<EOR
279  /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*"
280  ((?> # Text to match is composed of atoms of either:
281  \\\\. # - any escaped character
282  | # - any character different from " and \
283  [^"\\\\]+
284  )*?)
285  "\s*\)
286  | # Or page can be empty ; in this case, djvutxt dumps ()
287  \(\s*()\)/sx
288 EOR;
289  $txt = preg_replace_callback( $reg, [ $this, 'pageTextCallback' ], $txt );
290  $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
291  $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml, 1 ) .
292  $txt .
293  '</mw-djvu>';
294  }
295  }
296 
297  return $xml;
298  }
299 
301  # Get rid of invalid UTF-8, strip control characters
302  $val = htmlspecialchars( UtfNormal\Validator::cleanUp( stripcslashes( $matches[1] ) ) );
303  $val = str_replace( [ "\n", '�' ], [ '&#10;', '' ], $val );
304  return '<PAGE value="' . $val . '" />';
305  }
306 
312  function convertDumpToXML( $dump ) {
313  if ( strval( $dump ) == '' ) {
314  return false;
315  }
316 
317  $xml = <<<EOT
318 <?xml version="1.0" ?>
319 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
320 <DjVuXML>
321 <HEAD></HEAD>
322 <BODY>
323 EOT;
324 
325  $dump = str_replace( "\r", '', $dump );
326  $line = strtok( $dump, "\n" );
327  $m = false;
328  $good = false;
329  if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
330  # Single-page
331  if ( $this->parseFormDjvu( $line, $xml ) ) {
332  $good = true;
333  } else {
334  return false;
335  }
336  } elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
337  # Multi-page
338  $parentLevel = strlen( $m[1] );
339  # Find DIRM
340  $line = strtok( "\n" );
341  while ( $line !== false ) {
342  $childLevel = strspn( $line, ' ' );
343  if ( $childLevel <= $parentLevel ) {
344  # End of chunk
345  break;
346  }
347 
348  if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
349  wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
350 
351  return false;
352  }
353  if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
354  # Found page
355  if ( $this->parseFormDjvu( $line, $xml ) ) {
356  $good = true;
357  } else {
358  return false;
359  }
360  }
361  $line = strtok( "\n" );
362  }
363  }
364  if ( !$good ) {
365  return false;
366  }
367 
368  $xml .= "</BODY>\n</DjVuXML>\n";
369 
370  return $xml;
371  }
372 
373  function parseFormDjvu( $line, &$xml ) {
374  $parentLevel = strspn( $line, ' ' );
375  $line = strtok( "\n" );
376 
377  # Find INFO
378  while ( $line !== false ) {
379  $childLevel = strspn( $line, ' ' );
380  if ( $childLevel <= $parentLevel ) {
381  # End of chunk
382  break;
383  }
384 
385  if ( preg_match(
386  '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
387  $line,
388  $m
389  ) ) {
390  $xml .= Xml::tags(
391  'OBJECT',
392  [
393  # 'data' => '',
394  # 'type' => 'image/x.djvu',
395  'height' => $m[2],
396  'width' => $m[1],
397  # 'usemap' => '',
398  ],
399  "\n" .
400  Xml::element( 'PARAM', [ 'name' => 'DPI', 'value' => $m[3] ] ) . "\n" .
401  Xml::element( 'PARAM', [ 'name' => 'GAMMA', 'value' => $m[4] ] ) . "\n"
402  ) . "\n";
403 
404  return true;
405  }
406  $line = strtok( "\n" );
407  }
408 
409  # Not found
410  return false;
411  }
412 }
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
pageTextCallback( $matches)
Definition: DjVuImage.php:300
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
parseFormDjvu( $line, &$xml)
Definition: DjVuImage.php:373
null for the local wiki Added in
Definition: hooks.txt:1585
$wgDjvuTxt
Path of the djvutxt DJVU text extraction utility Enable this and $wgDjvuDump to enable text layer ext...
dumpForm( $file, $length, $indent)
Definition: DjVuImage.php:96
getMultiPageInfo( $file, $formLength)
Definition: DjVuImage.php:178
__construct( $filename)
Definition: DjVuImage.php:48
target page
getImageSize()
Return data in the style of getimagesize()
Definition: DjVuImage.php:66
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
Prior to version
Definition: maintenance.txt:1
Support for detecting/validating DjVu image files and getting some basic file metadata (resolution et...
Definition: DjVuImage.php:38
dump()
For debugging; dump the IFF chunk structure.
Definition: DjVuImage.php:85
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
readChunk( $file)
Definition: DjVuImage.php:158
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:130
and(b) You must cause any modified files to carry prominent notices stating that You changed the files
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
$header
const DJVUTXT_MEMORY_LIMIT
Memory limit for the DjVu description software.
Definition: DjVuImage.php:43
skipChunk( $file, $chunkLength)
Definition: DjVuImage.php:169
$wgDjvuDump
Path of the djvudump executable Enable this and $wgDjvuRenderer to enable djvu rendering example: $wg...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
isValid()
Check if the given file is indeed a valid DjVu image file.
Definition: DjVuImage.php:56
$line
Definition: cdb.php:59
getPageInfo( $file)
Definition: DjVuImage.php:207
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
$wgDjvuToXML
Path of the djvutoxml executable This works like djvudump except much, much slower as of version 3...
retrieveMetaData()
Return an XML string describing the DjVu image.
Definition: DjVuImage.php:249
convertDumpToXML( $dump)
Hack to temporarily work around djvutoxml bug.
Definition: DjVuImage.php:312
$matches