MediaWiki  1.23.2
DjVu.php
Go to the documentation of this file.
1 <?php
29 class DjVuHandler extends ImageHandler {
33  function isEnabled() {
34  global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
35  if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
36  wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
37 
38  return false;
39  } else {
40  return true;
41  }
42  }
43 
48  function mustRender( $file ) {
49  return true;
50  }
51 
56  function isMultiPage( $file ) {
57  return true;
58  }
59 
63  function getParamMap() {
64  return array(
65  'img_width' => 'width',
66  'img_page' => 'page',
67  );
68  }
69 
75  function validateParam( $name, $value ) {
76  if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
77  if ( $value <= 0 ) {
78  return false;
79  } else {
80  return true;
81  }
82  } else {
83  return false;
84  }
85  }
86 
91  function makeParamString( $params ) {
92  $page = isset( $params['page'] ) ? $params['page'] : 1;
93  if ( !isset( $params['width'] ) ) {
94  return false;
95  }
96 
97  return "page{$page}-{$params['width']}px";
98  }
99 
104  function parseParamString( $str ) {
105  $m = false;
106  if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
107  return array( 'width' => $m[2], 'page' => $m[1] );
108  } else {
109  return false;
110  }
111  }
112 
117  function getScriptParams( $params ) {
118  return array(
119  'width' => $params['width'],
120  'page' => $params['page'],
121  );
122  }
123 
132  function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
133  global $wgDjvuRenderer, $wgDjvuPostProcessor;
134 
135  // Fetch XML and check it, to give a more informative error message than the one which
136  // normaliseParams will inevitably give.
137  $xml = $image->getMetadata();
138  if ( !$xml ) {
139  $width = isset( $params['width'] ) ? $params['width'] : 0;
140  $height = isset( $params['height'] ) ? $params['height'] : 0;
141 
142  return new MediaTransformError( 'thumbnail_error', $width, $height,
143  wfMessage( 'djvu_no_xml' )->text() );
144  }
145 
146  if ( !$this->normaliseParams( $image, $params ) ) {
147  return new TransformParameterError( $params );
148  }
149  $width = $params['width'];
150  $height = $params['height'];
151  $page = $params['page'];
152  if ( $page > $this->pageCount( $image ) ) {
153  return new MediaTransformError(
154  'thumbnail_error',
155  $width,
156  $height,
157  wfMessage( 'djvu_page_error' )->text()
158  );
159  }
160 
161  if ( $flags & self::TRANSFORM_LATER ) {
162  $params = array(
163  'width' => $width,
164  'height' => $height,
165  'page' => $page
166  );
167 
168  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
169  }
170 
171  if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
172  return new MediaTransformError(
173  'thumbnail_error',
174  $width,
175  $height,
176  wfMessage( 'thumbnail_dest_directory' )->text()
177  );
178  }
179 
180  // Get local copy source for shell scripts
181  // Thumbnail extraction is very inefficient for large files.
182  // Provide a way to pool count limit the number of downloaders.
183  if ( $image->getSize() >= 1e7 ) { // 10MB
184  $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
185  array(
186  'doWork' => function() use ( $image ) {
187  return $image->getLocalRefPath();
188  }
189  )
190  );
191  $srcPath = $work->execute();
192  } else {
193  $srcPath = $image->getLocalRefPath();
194  }
195 
196  if ( $srcPath === false ) { // Failed to get local copy
197  wfDebugLog( 'thumbnail',
198  sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
199  wfHostname(), $image->getName() ) );
200 
201  return new MediaTransformError( 'thumbnail_error',
202  $params['width'], $params['height'],
203  wfMessage( 'filemissing' )->text()
204  );
205  }
206 
207  # Use a subshell (brackets) to aggregate stderr from both pipeline commands
208  # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
209  $cmd = '(' . wfEscapeShellArg(
210  $wgDjvuRenderer,
211  "-format=ppm",
212  "-page={$page}",
213  "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
214  $srcPath );
215  if ( $wgDjvuPostProcessor ) {
216  $cmd .= " | {$wgDjvuPostProcessor}";
217  }
218  $cmd .= ' > ' . wfEscapeShellArg( $dstPath ) . ') 2>&1';
219  wfProfileIn( 'ddjvu' );
220  wfDebug( __METHOD__ . ": $cmd\n" );
221  $retval = '';
222  $err = wfShellExec( $cmd, $retval );
223  wfProfileOut( 'ddjvu' );
224 
225  $removed = $this->removeBadFile( $dstPath, $retval );
226  if ( $retval != 0 || $removed ) {
227  $this->logErrorForExternalProcess( $retval, $err, $cmd );
228  return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
229  } else {
230  $params = array(
231  'width' => $width,
232  'height' => $height,
233  'page' => $page
234  );
235 
236  return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
237  }
238  }
239 
247  function getDjVuImage( $image, $path ) {
248  if ( !$image ) {
249  $deja = new DjVuImage( $path );
250  } elseif ( !isset( $image->dejaImage ) ) {
251  $deja = $image->dejaImage = new DjVuImage( $path );
252  } else {
253  $deja = $image->dejaImage;
254  }
255 
256  return $deja;
257  }
258 
265  private function getUnserializedMetadata( File $file ) {
266  $metadata = $file->getMetadata();
267  if ( substr( $metadata, 0, 3 ) === '<?xml' ) {
268  // Old style. Not serialized but instead just a raw string of XML.
269  return $metadata;
270  }
271 
273  $unser = unserialize( $metadata );
275  if ( is_array( $unser ) ) {
276  return $unser['xml'];
277  }
278 
279  // unserialize failed. Guess it wasn't really serialized after all,
280  return $metadata;
281  }
282 
289  function getMetaTree( $image, $gettext = false ) {
290  if ( $gettext && isset( $image->djvuTextTree ) ) {
291  return $image->djvuTextTree;
292  }
293  if ( !$gettext && isset( $image->dejaMetaTree ) ) {
294  return $image->dejaMetaTree;
295  }
296 
297  $metadata = $this->getUnserializedMetadata( $image );
298  if ( !$this->isMetadataValid( $image, $metadata ) ) {
299  wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
300 
301  return false;
302  }
303  wfProfileIn( __METHOD__ );
304 
306  try {
307  // Set to false rather than null to avoid further attempts
308  $image->dejaMetaTree = false;
309  $image->djvuTextTree = false;
310  $tree = new SimpleXMLElement( $metadata );
311  if ( $tree->getName() == 'mw-djvu' ) {
313  foreach ( $tree->children() as $b ) {
314  if ( $b->getName() == 'DjVuTxt' ) {
315  // @todo File::djvuTextTree and File::dejaMetaTree are declared
316  // dynamically. Add a public File::$data to facilitate this?
317  $image->djvuTextTree = $b;
318  } elseif ( $b->getName() == 'DjVuXML' ) {
319  $image->dejaMetaTree = $b;
320  }
321  }
322  } else {
323  $image->dejaMetaTree = $tree;
324  }
325  } catch ( Exception $e ) {
326  wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
327  }
329  wfProfileOut( __METHOD__ );
330  if ( $gettext ) {
331  return $image->djvuTextTree;
332  } else {
333  return $image->dejaMetaTree;
334  }
335  }
336 
342  function getImageSize( $image, $path ) {
343  return $this->getDjVuImage( $image, $path )->getImageSize();
344  }
345 
346  function getThumbType( $ext, $mime, $params = null ) {
347  global $wgDjvuOutputExtension;
348  static $mime;
349  if ( !isset( $mime ) ) {
350  $magic = MimeMagic::singleton();
351  $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
352  }
353 
354  return array( $wgDjvuOutputExtension, $mime );
355  }
356 
357  function getMetadata( $image, $path ) {
358  wfDebug( "Getting DjVu metadata for $path\n" );
359 
360  $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
361  if ( $xml === false ) {
362  return false;
363  } else {
364  return serialize( array( 'xml' => $xml ) );
365  }
366  }
367 
368  function getMetadataType( $image ) {
369  return 'djvuxml';
370  }
371 
372  function isMetadataValid( $image, $metadata ) {
373  return !empty( $metadata ) && $metadata != serialize( array() );
374  }
375 
376  function pageCount( $image ) {
377  $tree = $this->getMetaTree( $image );
378  if ( !$tree ) {
379  return false;
380  }
381 
382  return count( $tree->xpath( '//OBJECT' ) );
383  }
384 
385  function getPageDimensions( $image, $page ) {
386  $tree = $this->getMetaTree( $image );
387  if ( !$tree ) {
388  return false;
389  }
390 
391  $o = $tree->BODY[0]->OBJECT[$page - 1];
392  if ( $o ) {
393  return array(
394  'width' => intval( $o['width'] ),
395  'height' => intval( $o['height'] )
396  );
397  } else {
398  return false;
399  }
400  }
401 
407  function getPageText( $image, $page ) {
408  $tree = $this->getMetaTree( $image, true );
409  if ( !$tree ) {
410  return false;
411  }
412 
413  $o = $tree->BODY[0]->PAGE[$page - 1];
414  if ( $o ) {
415  $txt = $o['value'];
416 
417  return $txt;
418  } else {
419  return false;
420  }
421  }
422 }
MediaHandler\removeBadFile
removeBadFile( $dstPath, $retval=0)
Check for zero-sized thumbnails.
Definition: MediaHandler.php:688
DjVuHandler\isMetadataValid
isMetadataValid( $image, $metadata)
Check if the metadata string is valid for this handler.
Definition: DjVu.php:372
MediaTransformError
Basic media transform error class.
Definition: MediaTransformOutput.php:409
ThumbnailImage
Media transform output for images.
Definition: MediaTransformOutput.php:250
wfShellExec
wfShellExec( $cmd, &$retval=null, $environ=array(), $limits=array(), $options=array())
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
Definition: GlobalFunctions.php:2804
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
DjVuHandler\getParamMap
getParamMap()
Definition: DjVu.php:63
$mime
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string $mime
Definition: hooks.txt:2573
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:2590
DjVuHandler\pageCount
pageCount( $image)
Page count for a multi-page document, false if unsupported or unknown.
Definition: DjVu.php:376
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
DjVuHandler\isMultiPage
isMultiPage( $file)
Definition: DjVu.php:56
DjVuHandler\parseParamString
parseParamString( $str)
Definition: DjVu.php:104
DjVuHandler\getUnserializedMetadata
getUnserializedMetadata(File $file)
Get metadata, unserializing it if neccessary.
Definition: DjVu.php:265
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1040
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
DjVuHandler\isEnabled
isEnabled()
Definition: DjVu.php:33
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2387
$params
$params
Definition: styleTest.css.php:40
wfHostname
wfHostname()
Fetch server name for use in error reporting etc.
Definition: GlobalFunctions.php:1786
PoolCounterWorkViaCallback
Convenience class for dealing with PoolCounters using callbacks.
Definition: PoolCounterWork.php:155
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2113
DjVuHandler\makeParamString
makeParamString( $params)
Definition: DjVu.php:91
DjVuHandler\getMetadata
getMetadata( $image, $path)
Get handler-specific metadata which will be saved in the img_metadata field.
Definition: DjVu.php:357
DjVuHandler\getDjVuImage
getDjVuImage( $image, $path)
Cache an instance of DjVuImage in an Image object, return that instance.
Definition: DjVu.php:247
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2417
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
ImageHandler
Media handler abstract base class for images.
Definition: ImageHandler.php:29
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
DjVuHandler\validateParam
validateParam( $name, $value)
Definition: DjVu.php:75
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ImageHandler\normaliseParams
normaliseParams( $image, &$params)
Definition: ImageHandler.php:86
DjVuHandler\getImageSize
getImageSize( $image, $path)
Definition: DjVu.php:342
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
$value
$value
Definition: styleTest.css.php:45
DjVuHandler\getPageText
getPageText( $image, $page)
Definition: DjVu.php:407
TransformParameterError
Shortcut class for parameter validation errors.
Definition: MediaTransformOutput.php:452
wfEscapeShellArg
wfEscapeShellArg()
Windows-compatible version of escapeshellarg() Windows doesn't recognise single-quotes in the shell,...
Definition: GlobalFunctions.php:2705
DjVuHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: DjVu.php:368
DjVuHandler\getScriptParams
getScriptParams( $params)
Definition: DjVu.php:117
DjVuHandler
Handler for DjVu images.
Definition: DjVu.php:29
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
$ext
$ext
Definition: NoLocalSettings.php:34
DjVuHandler\getThumbType
getThumbType( $ext, $mime, $params=null)
Get the thumbnail extension and MIME type for a given source MIME type.
Definition: DjVu.php:346
MediaHandler\logErrorForExternalProcess
logErrorForExternalProcess( $retval, $err, $cmd)
Log an error that occurred in an external process.
Definition: MediaHandler.php:766
DjVuHandler\doTransform
doTransform( $image, $dstPath, $dstUrl, $params, $flags=0)
Definition: DjVu.php:132
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
DjVuHandler\getMetaTree
getMetaTree( $image, $gettext=false)
Cache a document tree for the DjVu XML metadata.
Definition: DjVu.php:289
DjVuHandler\getPageDimensions
getPageDimensions( $image, $page)
Get an associative array of page dimensions Currently "width" and "height" are understood,...
Definition: DjVu.php:385
DjVuHandler\mustRender
mustRender( $file)
Definition: DjVu.php:48
$e
if( $useReadline) $e
Definition: eval.php:66
$retval
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account incomplete not yet checked for validity & $retval
Definition: hooks.txt:237