3 * Media-handling base classes and generic functionality.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Base media handler class
29 abstract class MediaHandler
{
30 const TRANSFORM_LATER
= 1;
31 const METADATA_GOOD
= true;
32 const METADATA_BAD
= false;
33 const METADATA_COMPATIBLE
= 2; // for old but backwards compatible.
37 static $handlers = array();
40 * Get a MediaHandler for a given MIME type from the instance cache
44 * @return MediaHandler
46 static function getHandler( $type ) {
47 global $wgMediaHandlers;
48 if ( !isset( $wgMediaHandlers[$type] ) ) {
49 wfDebug( __METHOD__
. ": no handler found for $type.\n" );
52 $class = $wgMediaHandlers[$type];
53 if ( !isset( self
::$handlers[$class] ) ) {
54 self
::$handlers[$class] = new $class;
55 if ( !self
::$handlers[$class]->isEnabled() ) {
56 self
::$handlers[$class] = false;
59 return self
::$handlers[$class];
63 * Get an associative array mapping magic word IDs to parameter names.
64 * Will be used by the parser to identify parameters.
66 abstract function getParamMap();
69 * Validate a thumbnail parameter at parse time.
70 * Return true to accept the parameter, and false to reject it.
71 * If you return false, the parser will do something quiet and forgiving.
76 abstract function validateParam( $name, $value );
79 * Merge a parameter array into a string appropriate for inclusion in filenames
81 * @param $params array Array of parameters that have been through normaliseParams.
84 abstract function makeParamString( $params );
87 * Parse a param string made with makeParamString back into an array
89 * @param $str string The parameter string without file name (e.g. 122px)
90 * @return Array|Boolean Array of parameters or false on failure.
92 abstract function parseParamString( $str );
95 * Changes the parameter array as necessary, ready for transformation.
96 * Should be idempotent.
97 * Returns false if the parameters are unacceptable and the transform should fail
101 abstract function normaliseParams( $image, &$params );
104 * Get an image size array like that returned by getimagesize(), or false if it
105 * can't be determined.
107 * @param $image File: the image object, or false if there isn't one
108 * @param string $path the filename
109 * @return Array Follow the format of PHP getimagesize() internal function. See http://www.php.net/getimagesize
111 abstract function getImageSize( $image, $path );
114 * Get handler-specific metadata which will be saved in the img_metadata field.
116 * @param $image File: the image object, or false if there isn't one.
117 * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
118 * @param string $path the filename
121 function getMetadata( $image, $path ) {
126 * Get metadata version.
128 * This is not used for validating metadata, this is used for the api when returning
129 * metadata, since api content formats should stay the same over time, and so things
130 * using ForiegnApiRepo can keep backwards compatibility
132 * All core media handlers share a common version number, and extensions can
133 * use the GetMetadataVersion hook to append to the array (they should append a unique
134 * string so not to get confusing). If there was a media handler named 'foo' with metadata
135 * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
136 * version is 2, the end version string would look like '2;foo=3'.
138 * @return string version string
140 static function getMetadataVersion() {
141 $version = Array( '2' ); // core metadata version
142 wfRunHooks( 'GetMetadataVersion', Array( &$version ) );
143 return implode( ';', $version );
147 * Convert metadata version.
149 * By default just returns $metadata, but can be used to allow
150 * media handlers to convert between metadata versions.
152 * @param $metadata Mixed String or Array metadata array (serialized if string)
153 * @param $version Integer target version
154 * @return Array serialized metadata in specified version, or $metadata on fail.
156 function convertMetadataVersion( $metadata, $version = 1 ) {
157 if ( !is_array( $metadata ) ) {
159 //unserialize to keep return parameter consistent.
160 wfSuppressWarnings();
161 $ret = unserialize( $metadata );
169 * Get a string describing the type of metadata, for display purposes.
173 function getMetadataType( $image ) {
178 * Check if the metadata string is valid for this handler.
179 * If it returns MediaHandler::METADATA_BAD (or false), Image
180 * will reload the metadata from the file and update the database.
181 * MediaHandler::METADATA_GOOD for if the metadata is a-ok,
182 * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards
183 * compatible (which may or may not trigger a metadata reload).
186 function isMetadataValid( $image, $metadata ) {
187 return self
::METADATA_GOOD
;
191 * Get an array of standard (FormatMetadata type) metadata values.
193 * The returned data is largely the same as that from getMetadata(),
194 * but formatted in a standard, stable, handler-independent way.
195 * The idea being that some values like ImageDescription or Artist
196 * are universal and should be retrievable in a handler generic way.
198 * The specific properties are the type of properties that can be
199 * handled by the FormatMetadata class. These values are exposed to the
200 * user via the filemetadata parser function.
202 * Details of the response format of this function can be found at
203 * https://www.mediawiki.org/wiki/Manual:File_metadata_handling
204 * tl/dr: the response is an associative array of
205 * properties keyed by name, but the value can be complex. You probably
206 * want to call one of the FormatMetadata::flatten* functions on the
207 * property values before using them, or call
208 * FormatMetadata::getFormattedData() on the full response array, which
209 * transforms all values into prettified, human-readable text.
211 * Subclasses overriding this function must return a value which is a
212 * valid API response fragment (all associative array keys are valid
215 * Note, if the file simply has no metadata, but the handler supports
216 * this interface, it should return an empty array, not false.
220 * @return Array or false if interface not supported
223 public function getCommonMetaArray( File
$file ) {
228 * Get a MediaTransformOutput object representing an alternate of the transformed
229 * output which will call an intermediary thumbnail assist script.
231 * Used when the repository has a thumbnailScriptUrl option configured.
233 * Return false to fall back to the regular getTransform().
236 function getScriptedTransform( $image, $script, $params ) {
241 * Get a MediaTransformOutput object representing the transformed output. Does not
242 * actually do the transform.
244 * @param $image File: the image object
245 * @param string $dstPath filesystem destination path
246 * @param string $dstUrl Destination URL to use in output HTML
247 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
248 * @return MediaTransformOutput
250 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
251 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
255 * Get a MediaTransformOutput object representing the transformed output. Does the
256 * transform unless $flags contains self::TRANSFORM_LATER.
258 * @param $image File: the image object
259 * @param string $dstPath filesystem destination path
260 * @param string $dstUrl destination URL to use in output HTML
261 * @param array $params arbitrary set of parameters validated by $this->validateParam()
262 * Note: These parameters have *not* gone through $this->normaliseParams()
263 * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER
265 * @return MediaTransformOutput
267 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
270 * Get the thumbnail extension and MIME type for a given source MIME type
272 * @param String $ext Extension of original file
273 * @param String $mime Mime type of original file
274 * @param Array $params Handler specific rendering parameters
275 * @return array thumbnail extension and MIME type
277 function getThumbType( $ext, $mime, $params = null ) {
278 $magic = MimeMagic
::singleton();
279 if ( !$ext ||
$magic->isMatchingExtension( $ext, $mime ) === false ) {
280 // The extension is not valid for this mime type and we do
281 // recognize the mime type
282 $extensions = $magic->getExtensionsForType( $mime );
284 return array( strtok( $extensions, ' ' ), $mime );
288 // The extension is correct (true) or the mime type is unknown to
290 return array( $ext, $mime );
294 * Get useful response headers for GET/HEAD requests for a file with the given metadata
295 * @param $metadata mixed Result of the getMetadata() function of this handler for a file
298 public function getStreamHeaders( $metadata ) {
303 * True if the handled types can be transformed
306 function canRender( $file ) {
311 * True if handled types cannot be displayed directly in a browser
312 * but can be rendered
315 function mustRender( $file ) {
320 * True if the type has multi-page capabilities
323 function isMultiPage( $file ) {
328 * Page count for a multi-page document, false if unsupported or unknown
331 function pageCount( $file ) {
336 * The material is vectorized and thus scaling is lossless
339 function isVectorized( $file ) {
344 * The material is an image, and is animated.
345 * In particular, video material need not return true.
346 * @note Before 1.20, this was a method of ImageHandler only
349 function isAnimatedImage( $file ) {
354 * If the material is animated, we can animate the thumbnail
356 * @return bool If material is not animated, handler may return any value.
358 function canAnimateThumbnail( $file ) {
363 * False if the handler is disabled for all files
366 function isEnabled() {
371 * Get an associative array of page dimensions
372 * Currently "width" and "height" are understood, but this might be
373 * expanded in the future.
374 * Returns false if unknown.
376 * It is expected that handlers for paged media (e.g. DjVuHandler)
377 * will override this method so that it gives the correct results
378 * for each specific page of the file, using the $page argument.
380 * @note For non-paged media, use getImageSize.
383 * @param $page What page to get dimensions of
386 function getPageDimensions( $image, $page ) {
387 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
399 * Generic getter for text layer.
400 * Currently overloaded by PDF and DjVu handlers
403 function getPageText( $image, $page ) {
408 * Get an array structure that looks like this:
411 * 'visible' => array(
412 * 'Human-readable name' => 'Human readable value',
415 * 'collapsed' => array(
416 * 'Human-readable name' => 'Human readable value',
420 * The UI will format this into a table where the visible fields are always
421 * visible, and the collapsed fields are optionally visible.
423 * The function should return false if there is no metadata to display.
427 * @todo FIXME: I don't really like this interface, it's not very flexible
428 * I think the media handler should generate HTML instead. It can do
429 * all the formatting according to some standard. That makes it possible
430 * to do things like visual indication of grouped and chained streams
431 * in ogg container files.
434 function formatMetadata( $image ) {
438 /** sorts the visible/invisible field.
439 * Split off from ImageHandler::formatMetadata, as used by more than
440 * one type of handler.
442 * This is used by the media handlers that use the FormatMetadata class
444 * @param array $metadataArray metadata array
445 * @return array for use displaying metadata.
447 function formatMetadataHelper( $metadataArray ) {
449 'visible' => array(),
450 'collapsed' => array()
453 $formatted = FormatMetadata
::getFormattedData( $metadataArray );
454 // Sort fields into visible and collapsed
455 $visibleFields = $this->visibleMetadataFields();
456 foreach ( $formatted as $name => $value ) {
457 $tag = strtolower( $name );
458 self
::addMeta( $result,
459 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',
469 * Get a list of metadata items which should be displayed when
470 * the metadata table is collapsed.
472 * @return array of strings
475 function visibleMetadataFields() {
476 return FormatMetadata
::getVisibleFields();
480 * This is used to generate an array element for each metadata value
481 * That array is then used to generate the table of metadata values
484 * @param &$array Array An array containing elements for each type of visibility
485 * and each of those elements being an array of metadata items. This function adds
486 * a value to that array.
487 * @param string $visibility ('visible' or 'collapsed') if this value is hidden
489 * @param string $type type of metadata tag (currently always 'exif')
490 * @param string $id the name of the metadata tag (like 'artist' for example).
491 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
492 * @param string $value thingy goes into a wikitext table; it used to be escaped but
493 * that was incompatible with previous practise of customized display
494 * with wikitext formatting via messages such as 'exif-model-value'.
495 * So the escaping is taken back out, but generally this seems a confusing
497 * @param string $param value to pass to the message for the name of the field
498 * as $1. Currently this parameter doesn't seem to ever be used.
500 * Note, everything here is passed through the parser later on (!)
502 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
503 $msg = wfMessage( "$type-$id", $param );
504 if ( $msg->exists() ) {
505 $name = $msg->text();
507 // This is for future compatibility when using instant commons.
508 // So as to not display as ugly a name if a new metadata
509 // property is defined that we don't know about
510 // (not a major issue since such a property would be collapsed
512 wfDebug( __METHOD__
. ' Unknown metadata name: ' . $id . "\n" );
513 $name = wfEscapeWikiText( $id );
515 $array[$visibility][] = array(
523 * Used instead of getLongDesc if there is no handler registered for file.
528 function getShortDesc( $file ) {
530 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
534 * Short description. Shown on Special:Search results.
539 function getLongDesc( $file ) {
541 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
542 $file->getMimeType() )->parse();
546 * Long description. Shown under image on image description page surounded by ().
551 static function getGeneralShortDesc( $file ) {
553 return $wgLang->formatSize( $file->getSize() );
557 * Used instead of getShortDesc if there is no handler registered for file.
562 static function getGeneralLongDesc( $file ) {
564 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
565 $file->getMimeType() )->parse();
569 * Calculate the largest thumbnail width for a given original file size
570 * such that the thumbnail's height is at most $maxHeight.
571 * @param $boxWidth Integer Width of the thumbnail box.
572 * @param $boxHeight Integer Height of the thumbnail box.
573 * @param $maxHeight Integer Maximum height expected for the thumbnail.
576 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
577 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
578 $roundedUp = ceil( $idealWidth );
579 if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
580 return floor( $idealWidth );
587 * Shown in file history box on image description page.
590 * @return String Dimensions
592 function getDimensionsString( $file ) {
597 * Modify the parser object post-transform.
599 * This is often used to do $parser->addOutputHook(),
600 * in order to add some javascript to render a viewer.
601 * See TimedMediaHandler or OggHandler for an example.
603 * @param Parser $parser
606 function parserTransformHook( $parser, $file ) {}
609 * File validation hook called on upload.
611 * If the file at the given local path is not valid, or its MIME type does not
612 * match the handler class, a Status object should be returned containing
615 * @param string $fileName The local path to the file.
616 * @return Status object
618 function verifyUpload( $fileName ) {
619 return Status
::newGood();
623 * Check for zero-sized thumbnails. These can be generated when
624 * no disk space is available or some other error occurs
626 * @param string $dstPath The location of the suspect file
627 * @param int $retval Return value of some shell process, file will be deleted if this is non-zero
628 * @return bool True if removed, false otherwise
630 function removeBadFile( $dstPath, $retval = 0 ) {
631 if ( file_exists( $dstPath ) ) {
632 $thumbstat = stat( $dstPath );
633 if ( $thumbstat['size'] == 0 ||
$retval != 0 ) {
634 $result = unlink( $dstPath );
637 wfDebugLog( 'thumbnail',
638 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
639 $thumbstat['size'], $dstPath ) );
641 wfDebugLog( 'thumbnail',
642 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
643 $thumbstat['size'], $dstPath ) );
652 * Remove files from the purge list.
654 * This is used by some video handlers to prevent ?action=purge
655 * from removing a transcoded video, which is expensive to
658 * @see LocalFile::purgeThumbnails
660 * @param array $files
661 * @param array $options Purge options. Currently will always be
662 * an array with a single key 'forThumbRefresh' set to true.
664 public function filterThumbnailPurgeList( &$files, $options ) {
669 * True if the handler can rotate the media
673 public static function canRotate() {
678 * On supporting image formats, try to read out the low-level orientation
679 * of the file and return the angle that the file needs to be rotated to
682 * This information is only useful when manipulating the original file;
683 * the width and height we normally work with is logical, and will match
684 * any produced output views.
686 * For files we don't know, we return 0.
689 * @return int 0, 90, 180 or 270
691 public function getRotation( $file ) {