3 * Media-handling base classes and generic functionality
10 * Base media handler class
14 abstract class MediaHandler
{
15 const TRANSFORM_LATER
= 1;
20 static $handlers = array();
23 * Get a MediaHandler for a given MIME type from the instance cache
25 static function getHandler( $type ) {
26 global $wgMediaHandlers;
27 if ( !isset( $wgMediaHandlers[$type] ) ) {
28 wfDebug( __METHOD__
. ": no handler found for $type.\n");
31 $class = $wgMediaHandlers[$type];
32 if ( !isset( self
::$handlers[$class] ) ) {
33 self
::$handlers[$class] = new $class;
34 if ( !self
::$handlers[$class]->isEnabled() ) {
35 self
::$handlers[$class] = false;
38 return self
::$handlers[$class];
42 * Get an associative array mapping magic word IDs to parameter names.
43 * Will be used by the parser to identify parameters.
45 abstract function getParamMap();
48 * Validate a thumbnail parameter at parse time.
49 * Return true to accept the parameter, and false to reject it.
50 * If you return false, the parser will do something quiet and forgiving.
52 abstract function validateParam( $name, $value );
55 * Merge a parameter array into a string appropriate for inclusion in filenames
57 abstract function makeParamString( $params );
60 * Parse a param string made with makeParamString back into an array
62 abstract function parseParamString( $str );
65 * Changes the parameter array as necessary, ready for transformation.
66 * Should be idempotent.
67 * Returns false if the parameters are unacceptable and the transform should fail
69 abstract function normaliseParams( $image, &$params );
72 * Get an image size array like that returned by getimagesize(), or false if it
73 * can't be determined.
75 * @param $image File: the image object, or false if there isn't one
76 * @param $path String: the filename
79 abstract function getImageSize( $image, $path );
82 * Get handler-specific metadata which will be saved in the img_metadata field.
84 * @param $image File: the image object, or false if there isn't one
85 * @param $path String: the filename
88 function getMetadata( $image, $path ) { return ''; }
91 * Get a string describing the type of metadata, for display purposes.
93 function getMetadataType( $image ) { return false; }
96 * Check if the metadata string is valid for this handler.
97 * If it returns false, Image will reload the metadata from the file and update the database
99 function isMetadataValid( $image, $metadata ) { return true; }
103 * Get a MediaTransformOutput object representing an alternate of the transformed
104 * output which will call an intermediary thumbnail assist script.
106 * Used when the repository has a thumbnailScriptUrl option configured.
108 * Return false to fall back to the regular getTransform().
110 function getScriptedTransform( $image, $script, $params ) {
115 * Get a MediaTransformOutput object representing the transformed output. Does not
116 * actually do the transform.
118 * @param $image File: the image object
119 * @param $dstPath String: filesystem destination path
120 * @param $dstUrl String: Destination URL to use in output HTML
121 * @param $params Array: Arbitrary set of parameters validated by $this->validateParam()
123 function getTransform( $image, $dstPath, $dstUrl, $params ) {
124 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
128 * Get a MediaTransformOutput object representing the transformed output. Does the
129 * transform unless $flags contains self::TRANSFORM_LATER.
131 * @param $image File: the image object
132 * @param $dstPath String: filesystem destination path
133 * @param $dstUrl String: destination URL to use in output HTML
134 * @param $params Array: arbitrary set of parameters validated by $this->validateParam()
135 * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER
137 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
140 * Get the thumbnail extension and MIME type for a given source MIME type
141 * @return array thumbnail extension and MIME type
143 function getThumbType( $ext, $mime, $params = null ) {
144 return array( $ext, $mime );
148 * True if the handled types can be transformed
150 function canRender( $file ) { return true; }
152 * True if handled types cannot be displayed directly in a browser
153 * but can be rendered
155 function mustRender( $file ) { return false; }
157 * True if the type has multi-page capabilities
159 function isMultiPage( $file ) { return false; }
161 * Page count for a multi-page document, false if unsupported or unknown
163 function pageCount( $file ) { return false; }
165 * False if the handler is disabled for all files
167 function isEnabled() { return true; }
170 * Get an associative array of page dimensions
171 * Currently "width" and "height" are understood, but this might be
172 * expanded in the future.
173 * Returns false if unknown or if the document is not multi-page.
175 function getPageDimensions( $image, $page ) {
176 $gis = $this->getImageSize( $image, $image->getPath() );
184 * Generic getter for text layer.
185 * Currently overloaded by PDF and DjVu handlers
187 function getPageText( $image, $page ) {
192 * Get an array structure that looks like this:
195 * 'visible' => array(
196 * 'Human-readable name' => 'Human readable value',
199 * 'collapsed' => array(
200 * 'Human-readable name' => 'Human readable value',
204 * The UI will format this into a table where the visible fields are always
205 * visible, and the collapsed fields are optionally visible.
207 * The function should return false if there is no metadata to display.
211 * FIXME: I don't really like this interface, it's not very flexible
212 * I think the media handler should generate HTML instead. It can do
213 * all the formatting according to some standard. That makes it possible
214 * to do things like visual indication of grouped and chained streams
215 * in ogg container files.
217 function formatMetadata( $image ) {
222 * @todo Fixme: document this!
223 * 'value' thingy goes into a wikitext table; it used to be escaped but
224 * that was incompatible with previous practice of customized display
225 * with wikitext formatting via messages such as 'exif-model-value'.
226 * So the escaping is taken back out, but generally this seems a confusing
229 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
230 $array[$visibility][] = array(
232 'name' => wfMsg( "$type-$id", $param ),
237 function getShortDesc( $file ) {
239 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
240 $wgLang->formatNum( $file->getSize() ) ) . ')';
244 function getLongDesc( $file ) {
246 $sk = $wgUser->getSkin();
247 return wfMsgExt( 'file-info', 'parseinline',
248 $sk->formatSize( $file->getSize() ),
249 $file->getMimeType() );
252 static function getGeneralShortDesc( $file ) {
254 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
255 $wgLang->formatNum( $file->getSize() ) ) . ')';
259 static function getGeneralLongDesc( $file ) {
261 $sk = $wgUser->getSkin();
262 return wfMsgExt( 'file-info', 'parseinline',
263 $sk->formatSize( $file->getSize() ),
264 $file->getMimeType() );
267 function getDimensionsString( $file ) {
272 * Modify the parser object post-transform
274 function parserTransformHook( $parser, $file ) {}
277 * File validation hook called on upload.
279 * If the file at the given local path is not valid, or its MIME type does not
280 * match the handler class, a Status object should be returned containing
283 * @param $fileName The local path to the file.
284 * @return Status object
286 function verifyUpload( $fileName ) {
287 return Status
::newGood();
291 * Check for zero-sized thumbnails. These can be generated when
292 * no disk space is available or some other error occurs
294 * @param $dstPath The location of the suspect file
295 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
296 * @return true if removed, false otherwise
298 function removeBadFile( $dstPath, $retval = 0 ) {
299 if( file_exists( $dstPath ) ) {
300 $thumbstat = stat( $dstPath );
301 if( $thumbstat['size'] == 0 ||
$retval != 0 ) {
302 wfDebugLog( 'thumbnail',
303 sprintf( 'Removing bad %d-byte thumbnail "%s"',
304 $thumbstat['size'], $dstPath ) );
314 * Media handler abstract base class for images
318 abstract class ImageHandler
extends MediaHandler
{
319 function canRender( $file ) {
320 if ( $file->getWidth() && $file->getHeight() ) {
327 function getParamMap() {
328 return array( 'img_width' => 'width' );
331 function validateParam( $name, $value ) {
332 if ( in_array( $name, array( 'width', 'height' ) ) ) {
343 function makeParamString( $params ) {
344 if ( isset( $params['physicalWidth'] ) ) {
345 $width = $params['physicalWidth'];
346 } elseif ( isset( $params['width'] ) ) {
347 $width = $params['width'];
349 throw new MWException( 'No width specified to '.__METHOD__
);
351 # Removed for ProofreadPage
352 #$width = intval( $width );
356 function parseParamString( $str ) {
358 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
359 return array( 'width' => $m[1] );
365 function getScriptParams( $params ) {
366 return array( 'width' => $params['width'] );
369 function normaliseParams( $image, &$params ) {
370 $mimeType = $image->getMimeType();
372 if ( !isset( $params['width'] ) ) {
376 if ( !isset( $params['page'] ) ) {
379 if ( $params['page'] > $image->pageCount() ) {
380 $params['page'] = $image->pageCount();
383 if ( $params['page'] < 1 ) {
388 $srcWidth = $image->getWidth( $params['page'] );
389 $srcHeight = $image->getHeight( $params['page'] );
390 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
391 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
392 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
395 $params['height'] = File
::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
396 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
403 * Get a transform output object without actually doing the transform
405 function getTransform( $image, $dstPath, $dstUrl, $params ) {
406 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
410 * Validate thumbnail parameters and fill in the correct height
412 * @param $width Integer: specified width (input/output)
413 * @param $height Integer: height (output only)
414 * @param $srcWidth Integer: width of the source image
415 * @param $srcHeight Integer: height of the source image
416 * @param $mimeType Unused
417 * @return false to indicate that an error should be returned to the user.
419 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
420 $width = intval( $width );
422 # Sanity check $width
424 wfDebug( __METHOD__
.": Invalid destination width: $width\n" );
427 if ( $srcWidth <= 0 ) {
428 wfDebug( __METHOD__
.": Invalid source width: $srcWidth\n" );
432 $height = File
::scaleHeight( $srcWidth, $srcHeight, $width );
436 function getScriptedTransform( $image, $script, $params ) {
437 if ( !$this->normaliseParams( $image, $params ) ) {
440 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
441 $page = isset( $params['page'] ) ?
$params['page'] : false;
443 if( $image->mustRender() ||
$params['width'] < $image->getWidth() ) {
444 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
448 function getImageSize( $image, $path ) {
449 wfSuppressWarnings();
450 $gis = getimagesize( $path );
455 function isAnimatedImage( $image ) {
459 function getShortDesc( $file ) {
461 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
462 $wgLang->formatNum( $file->getSize() ) );
463 $widthheight = wfMsgHtml( 'widthheight', $wgLang->formatNum( $file->getWidth() ) ,$wgLang->formatNum( $file->getHeight() ) );
465 return "$widthheight ($nbytes)";
468 function getLongDesc( $file ) {
470 return wfMsgExt('file-info-size', 'parseinline',
471 $wgLang->formatNum( $file->getWidth() ),
472 $wgLang->formatNum( $file->getHeight() ),
473 $wgLang->formatSize( $file->getSize() ),
474 $file->getMimeType() );
477 function getDimensionsString( $file ) {
479 $pages = $file->pageCount();
480 $width = $wgLang->formatNum( $file->getWidth() );
481 $height = $wgLang->formatNum( $file->getHeight() );
482 $pagesFmt = $wgLang->formatNum( $pages );
485 return wfMsgExt( 'widthheightpage', 'parsemag', $width, $height, $pagesFmt );
487 return wfMsg( 'widthheight', $width, $height );