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.
35 * Max length of error logged by logErrorForExternalProcess()
37 const MAX_ERR_LOG_SIZE
= 65535;
39 /** @var MediaHandler[] Instance cache with array of MediaHandler */
40 protected static $handlers = array();
43 * Get a MediaHandler for a given MIME type from the instance cache
46 * @return MediaHandler
48 static function getHandler( $type ) {
49 global $wgMediaHandlers;
50 if ( !isset( $wgMediaHandlers[$type] ) ) {
51 wfDebug( __METHOD__
. ": no handler found for $type.\n" );
55 $class = $wgMediaHandlers[$type];
56 if ( !isset( self
::$handlers[$class] ) ) {
57 self
::$handlers[$class] = new $class;
58 if ( !self
::$handlers[$class]->isEnabled() ) {
59 wfDebug( __METHOD__
. ": $class is not enabled\n" );
60 self
::$handlers[$class] = false;
64 return self
::$handlers[$class];
68 * Resets all static caches
70 public static function resetCache() {
71 self
::$handlers = array();
75 * Get an associative array mapping magic word IDs to parameter names.
76 * Will be used by the parser to identify parameters.
78 abstract function getParamMap();
81 * Validate a thumbnail parameter at parse time.
82 * Return true to accept the parameter, and false to reject it.
83 * If you return false, the parser will do something quiet and forgiving.
88 abstract function validateParam( $name, $value );
91 * Merge a parameter array into a string appropriate for inclusion in filenames
93 * @param array $params Array of parameters that have been through normaliseParams.
96 abstract function makeParamString( $params );
99 * Parse a param string made with makeParamString back into an array
101 * @param string $str The parameter string without file name (e.g. 122px)
102 * @return array|bool Array of parameters or false on failure.
104 abstract function parseParamString( $str );
107 * Changes the parameter array as necessary, ready for transformation.
108 * Should be idempotent.
109 * Returns false if the parameters are unacceptable and the transform should fail
111 * @param array $params
113 abstract function normaliseParams( $image, &$params );
116 * Get an image size array like that returned by getimagesize(), or false if it
117 * can't be determined.
119 * This function is used for determining the width, height and bitdepth directly
120 * from an image. The results are stored in the database in the img_width,
121 * img_height, img_bits fields.
123 * @note If this is a multipage file, return the width and height of the
126 * @param File $image The image object, or false if there isn't one
127 * @param string $path The filename
128 * @return array Follow the format of PHP getimagesize() internal function.
129 * See http://www.php.net/getimagesize. MediaWiki will only ever use the
130 * first two array keys (the width and height), and the 'bits' associative
131 * key. All other array keys are ignored. Returning a 'bits' key is optional
132 * as not all formats have a notion of "bitdepth".
134 abstract function getImageSize( $image, $path );
137 * Get handler-specific metadata which will be saved in the img_metadata field.
139 * @param File $image The image object, or false if there isn't one.
140 * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
141 * @param string $path The filename
142 * @return string A string of metadata in php serialized form (Run through serialize())
144 function getMetadata( $image, $path ) {
149 * Get metadata version.
151 * This is not used for validating metadata, this is used for the api when returning
152 * metadata, since api content formats should stay the same over time, and so things
153 * using ForeignApiRepo can keep backwards compatibility
155 * All core media handlers share a common version number, and extensions can
156 * use the GetMetadataVersion hook to append to the array (they should append a unique
157 * string so not to get confusing). If there was a media handler named 'foo' with metadata
158 * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
159 * version is 2, the end version string would look like '2;foo=3'.
161 * @return string Version string
163 static function getMetadataVersion() {
164 $version = array( '2' ); // core metadata version
165 Hooks
::run( 'GetMetadataVersion', array( &$version ) );
167 return implode( ';', $version );
171 * Convert metadata version.
173 * By default just returns $metadata, but can be used to allow
174 * media handlers to convert between metadata versions.
176 * @param string|array $metadata Metadata array (serialized if string)
177 * @param int $version Target version
178 * @return array Serialized metadata in specified version, or $metadata on fail.
180 function convertMetadataVersion( $metadata, $version = 1 ) {
181 if ( !is_array( $metadata ) ) {
183 // unserialize to keep return parameter consistent.
184 MediaWiki\
suppressWarnings();
185 $ret = unserialize( $metadata );
186 MediaWiki\restoreWarnings
();
195 * Get a string describing the type of metadata, for display purposes.
197 * @note This method is currently unused.
201 function getMetadataType( $image ) {
206 * Check if the metadata string is valid for this handler.
207 * If it returns MediaHandler::METADATA_BAD (or false), Image
208 * will reload the metadata from the file and update the database.
209 * MediaHandler::METADATA_GOOD for if the metadata is a-ok,
210 * MediaHandler::METADATA_COMPATIBLE if metadata is old but backwards
211 * compatible (which may or may not trigger a metadata reload).
213 * @note Returning self::METADATA_BAD will trigger a metadata reload from
214 * file on page view. Always returning this from a broken file, or suddenly
215 * triggering as bad metadata for a large number of files can cause
216 * performance problems.
218 * @param string $metadata The metadata in serialized form
221 function isMetadataValid( $image, $metadata ) {
222 return self
::METADATA_GOOD
;
226 * Get an array of standard (FormatMetadata type) metadata values.
228 * The returned data is largely the same as that from getMetadata(),
229 * but formatted in a standard, stable, handler-independent way.
230 * The idea being that some values like ImageDescription or Artist
231 * are universal and should be retrievable in a handler generic way.
233 * The specific properties are the type of properties that can be
234 * handled by the FormatMetadata class. These values are exposed to the
235 * user via the filemetadata parser function.
237 * Details of the response format of this function can be found at
238 * https://www.mediawiki.org/wiki/Manual:File_metadata_handling
239 * tl/dr: the response is an associative array of
240 * properties keyed by name, but the value can be complex. You probably
241 * want to call one of the FormatMetadata::flatten* functions on the
242 * property values before using them, or call
243 * FormatMetadata::getFormattedData() on the full response array, which
244 * transforms all values into prettified, human-readable text.
246 * Subclasses overriding this function must return a value which is a
247 * valid API response fragment (all associative array keys are valid
250 * Note, if the file simply has no metadata, but the handler supports
251 * this interface, it should return an empty array, not false.
254 * @return array|bool False if interface not supported
257 public function getCommonMetaArray( File
$file ) {
262 * Get a MediaTransformOutput object representing an alternate of the transformed
263 * output which will call an intermediary thumbnail assist script.
265 * Used when the repository has a thumbnailScriptUrl option configured.
267 * Return false to fall back to the regular getTransform().
269 * @param string $script
270 * @param array $params
271 * @return bool|ThumbnailImage
273 function getScriptedTransform( $image, $script, $params ) {
278 * Get a MediaTransformOutput object representing the transformed output. Does not
279 * actually do the transform.
281 * @param File $image The image object
282 * @param string $dstPath Filesystem destination path
283 * @param string $dstUrl Destination URL to use in output HTML
284 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
285 * @return MediaTransformOutput
287 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
288 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
292 * Get a MediaTransformOutput object representing the transformed output. Does the
293 * transform unless $flags contains self::TRANSFORM_LATER.
295 * @param File $image The image object
296 * @param string $dstPath Filesystem destination path
297 * @param string $dstUrl Destination URL to use in output HTML
298 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
299 * Note: These parameters have *not* gone through $this->normaliseParams()
300 * @param int $flags A bitfield, may contain self::TRANSFORM_LATER
301 * @return MediaTransformOutput
303 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
306 * Get the thumbnail extension and MIME type for a given source MIME type
308 * @param string $ext Extension of original file
309 * @param string $mime MIME type of original file
310 * @param array $params Handler specific rendering parameters
311 * @return array Thumbnail extension and MIME type
313 function getThumbType( $ext, $mime, $params = null ) {
314 $magic = MimeMagic
::singleton();
315 if ( !$ext ||
$magic->isMatchingExtension( $ext, $mime ) === false ) {
316 // The extension is not valid for this MIME type and we do
317 // recognize the MIME type
318 $extensions = $magic->getExtensionsForType( $mime );
320 return array( strtok( $extensions, ' ' ), $mime );
324 // The extension is correct (true) or the MIME type is unknown to
326 return array( $ext, $mime );
330 * Get useful response headers for GET/HEAD requests for a file with the given metadata
332 * @param mixed $metadata Result of the getMetadata() function of this handler for a file
335 public function getStreamHeaders( $metadata ) {
340 * True if the handled types can be transformed
345 function canRender( $file ) {
350 * True if handled types cannot be displayed directly in a browser
351 * but can be rendered
356 function mustRender( $file ) {
361 * True if the type has multi-page capabilities
366 function isMultiPage( $file ) {
371 * Page count for a multi-page document, false if unsupported or unknown
376 function pageCount( File
$file ) {
381 * The material is vectorized and thus scaling is lossless
386 function isVectorized( $file ) {
391 * The material is an image, and is animated.
392 * In particular, video material need not return true.
393 * @note Before 1.20, this was a method of ImageHandler only
398 function isAnimatedImage( $file ) {
403 * If the material is animated, we can animate the thumbnail
407 * @return bool If material is not animated, handler may return any value.
409 function canAnimateThumbnail( $file ) {
414 * False if the handler is disabled for all files
417 function isEnabled() {
422 * Get an associative array of page dimensions
423 * Currently "width" and "height" are understood, but this might be
424 * expanded in the future.
425 * Returns false if unknown.
427 * It is expected that handlers for paged media (e.g. DjVuHandler)
428 * will override this method so that it gives the correct results
429 * for each specific page of the file, using the $page argument.
431 * @note For non-paged media, use getImageSize.
434 * @param int $page What page to get dimensions of
437 function getPageDimensions( File
$image, $page ) {
438 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
450 * Generic getter for text layer.
451 * Currently overloaded by PDF and DjVu handlers
453 * @param int $page Page number to get information for
454 * @return bool|string Page text or false when no text found or if
457 function getPageText( File
$image, $page ) {
462 * Get the text of the entire document.
464 * @return bool|string The text of the document or false if unsupported.
466 public function getEntireText( File
$file ) {
467 $numPages = $file->pageCount();
469 // Not a multipage document
470 return $this->getPageText( $file, 1 );
473 for ( $i = 1; $i <= $numPages; $i++
) {
474 $curPage = $this->getPageText( $file, $i );
475 if ( is_string( $curPage ) ) {
476 $document .= $curPage . "\n";
479 if ( $document !== '' ) {
486 * Get an array structure that looks like this:
489 * 'visible' => array(
490 * 'Human-readable name' => 'Human readable value',
493 * 'collapsed' => array(
494 * 'Human-readable name' => 'Human readable value',
498 * The UI will format this into a table where the visible fields are always
499 * visible, and the collapsed fields are optionally visible.
501 * The function should return false if there is no metadata to display.
505 * @todo FIXME: This interface is not very flexible. The media handler
506 * should generate HTML instead. It can do all the formatting according
507 * to some standard. That makes it possible to do things like visual
508 * indication of grouped and chained streams in ogg container files.
510 * @param bool|IContextSource $context Context to use (optional)
513 function formatMetadata( $image, $context = false ) {
517 /** sorts the visible/invisible field.
518 * Split off from ImageHandler::formatMetadata, as used by more than
519 * one type of handler.
521 * This is used by the media handlers that use the FormatMetadata class
523 * @param array $metadataArray Metadata array
524 * @param bool|IContextSource $context Context to use (optional)
525 * @return array Array for use displaying metadata.
527 function formatMetadataHelper( $metadataArray, $context = false ) {
529 'visible' => array(),
530 'collapsed' => array()
533 $formatted = FormatMetadata
::getFormattedData( $metadataArray, $context );
534 // Sort fields into visible and collapsed
535 $visibleFields = $this->visibleMetadataFields();
536 foreach ( $formatted as $name => $value ) {
537 $tag = strtolower( $name );
538 self
::addMeta( $result,
539 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',
550 * Get a list of metadata items which should be displayed when
551 * the metadata table is collapsed.
553 * @return array Array of strings
555 protected function visibleMetadataFields() {
556 return FormatMetadata
::getVisibleFields();
560 * This is used to generate an array element for each metadata value
561 * That array is then used to generate the table of metadata values
564 * @param array &$array An array containing elements for each type of visibility
565 * and each of those elements being an array of metadata items. This function adds
566 * a value to that array.
567 * @param string $visibility ('visible' or 'collapsed') if this value is hidden
569 * @param string $type Type of metadata tag (currently always 'exif')
570 * @param string $id The name of the metadata tag (like 'artist' for example).
571 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
572 * @param string $value Thingy goes into a wikitext table; it used to be escaped but
573 * that was incompatible with previous practise of customized display
574 * with wikitext formatting via messages such as 'exif-model-value'.
575 * So the escaping is taken back out, but generally this seems a confusing
577 * @param bool|string $param Value to pass to the message for the name of the field
578 * as $1. Currently this parameter doesn't seem to ever be used.
580 * Note, everything here is passed through the parser later on (!)
582 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
583 $msg = wfMessage( "$type-$id", $param );
584 if ( $msg->exists() ) {
585 $name = $msg->text();
587 // This is for future compatibility when using instant commons.
588 // So as to not display as ugly a name if a new metadata
589 // property is defined that we don't know about
590 // (not a major issue since such a property would be collapsed
592 wfDebug( __METHOD__
. ' Unknown metadata name: ' . $id . "\n" );
593 $name = wfEscapeWikiText( $id );
595 $array[$visibility][] = array(
603 * Short description. Shown on Special:Search results.
608 function getShortDesc( $file ) {
609 return self
::getGeneralShortDesc( $file );
613 * Long description. Shown under image on image description page surounded by ().
618 function getLongDesc( $file ) {
619 return self
::getGeneralLongDesc( $file );
623 * Used instead of getShortDesc if there is no handler registered for file.
628 static function getGeneralShortDesc( $file ) {
631 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
635 * Used instead of getLongDesc if there is no handler registered for file.
640 static function getGeneralLongDesc( $file ) {
641 return wfMessage( 'file-info' )->sizeParams( $file->getSize() )
642 ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
646 * Calculate the largest thumbnail width for a given original file size
647 * such that the thumbnail's height is at most $maxHeight.
648 * @param int $boxWidth Width of the thumbnail box.
649 * @param int $boxHeight Height of the thumbnail box.
650 * @param int $maxHeight Maximum height expected for the thumbnail.
653 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
654 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
655 $roundedUp = ceil( $idealWidth );
656 if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
657 return floor( $idealWidth );
664 * Shown in file history box on image description page.
667 * @return string Dimensions
669 function getDimensionsString( $file ) {
674 * Modify the parser object post-transform.
676 * This is often used to do $parser->addOutputHook(),
677 * in order to add some javascript to render a viewer.
678 * See TimedMediaHandler or OggHandler for an example.
680 * @param Parser $parser
683 function parserTransformHook( $parser, $file ) {
687 * File validation hook called on upload.
689 * If the file at the given local path is not valid, or its MIME type does not
690 * match the handler class, a Status object should be returned containing
693 * @param string $fileName The local path to the file.
696 function verifyUpload( $fileName ) {
697 return Status
::newGood();
701 * Check for zero-sized thumbnails. These can be generated when
702 * no disk space is available or some other error occurs
704 * @param string $dstPath The location of the suspect file
705 * @param int $retval Return value of some shell process, file will be deleted if this is non-zero
706 * @return bool True if removed, false otherwise
708 function removeBadFile( $dstPath, $retval = 0 ) {
709 if ( file_exists( $dstPath ) ) {
710 $thumbstat = stat( $dstPath );
711 if ( $thumbstat['size'] == 0 ||
$retval != 0 ) {
712 $result = unlink( $dstPath );
715 wfDebugLog( 'thumbnail',
716 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
717 $thumbstat['size'], $dstPath ) );
719 wfDebugLog( 'thumbnail',
720 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
721 $thumbstat['size'], $dstPath ) );
732 * Remove files from the purge list.
734 * This is used by some video handlers to prevent ?action=purge
735 * from removing a transcoded video, which is expensive to
738 * @see LocalFile::purgeThumbnails
740 * @param array $files
741 * @param array $options Purge options. Currently will always be
742 * an array with a single key 'forThumbRefresh' set to true.
744 public function filterThumbnailPurgeList( &$files, $options ) {
749 * True if the handler can rotate the media
750 * @since 1.24 non-static. From 1.21-1.23 was static
753 public function canRotate() {
758 * On supporting image formats, try to read out the low-level orientation
759 * of the file and return the angle that the file needs to be rotated to
762 * This information is only useful when manipulating the original file;
763 * the width and height we normally work with is logical, and will match
764 * any produced output views.
766 * For files we don't know, we return 0.
769 * @return int 0, 90, 180 or 270
771 public function getRotation( $file ) {
776 * Log an error that occurred in an external process
778 * Moved from BitmapHandler to MediaHandler with MediaWiki 1.23
782 * @param string $err Error reported by command. Anything longer than
783 * MediaHandler::MAX_ERR_LOG_SIZE is stripped off.
786 protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
787 # Keep error output limited (bug 57985)
788 $errMessage = trim( substr( $err, 0, self
::MAX_ERR_LOG_SIZE
) );
790 wfDebugLog( 'thumbnail',
791 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
792 wfHostname(), $retval, $errMessage, $cmd ) );
796 * Get list of languages file can be viewed in.
799 * @return string[] Array of language codes, or empty array if unsupported.
802 public function getAvailableLanguages( File
$file ) {
807 * On file types that support renderings in multiple languages,
808 * which language is used by default if unspecified.
810 * If getAvailableLanguages returns a non-empty array, this must return
811 * a valid language code. Otherwise can return null if files of this
812 * type do not support alternative language renderings.
815 * @return string|null Language code or null if multi-language not supported for filetype.
818 public function getDefaultRenderLanguage( File
$file ) {
823 * If its an audio file, return the length of the file. Otherwise 0.
825 * File::getLength() existed for a long time, but was calling a method
826 * that only existed in some subclasses of this class (The TMH ones).
829 * @return float Length in seconds
832 public function getLength( $file ) {
837 * True if creating thumbnails from the file is large or otherwise resource-intensive.
841 public function isExpensiveToThumbnail( $file ) {
846 * Returns whether or not this handler supports the chained generation of thumbnails according
851 public function supportsBucketing() {
856 * Returns a normalised params array for which parameters have been cleaned up for bucketing
858 * @param array $params
861 public function sanitizeParamsForBucketing( $params ) {
866 * Gets configuration for the file warning message. Return value of
867 * the following structure:
869 * // Required, module with messages loaded for the client
870 * 'module' => 'example.filewarning.messages',
871 * // Required, array of names of messages
872 * 'messages' => array(
873 * // Required, main warning message
874 * 'main' => 'example-filewarning-main',
875 * // Optional, header for warning dialog
876 * 'header' => 'example-filewarning-header',
877 * // Optional, footer for warning dialog
878 * 'footer' => 'example-filewarning-footer',
879 * // Optional, text for more-information link (see below)
880 * 'info' => 'example-filewarning-info',
882 * // Optional, link for more information
883 * 'link' => 'http://example.com',
886 * Returns null if no warning is necessary.
890 public function getWarningConfig( $file ) {