Implement extension registration from an extension.json file
[mediawiki.git] / includes / media / MediaHandler.php
blobb88a1b12a0ca9384ab4126374e20f909dc747c20
1 <?php
2 /**
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
20 * @file
21 * @ingroup Media
24 /**
25 * Base media handler class
27 * @ingroup Media
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.
34 /**
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();
42 /**
43 * Get a MediaHandler for a given MIME type from the instance cache
45 * @param string $type
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" );
53 return false;
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];
67 /**
68 * Resets all static caches
70 public static function resetCache() {
71 self::$handlers = array();
74 /**
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();
80 /**
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.
85 * @param string $name
86 * @param mixed $value
88 abstract function validateParam( $name, $value );
90 /**
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.
94 * @return string
96 abstract function makeParamString( $params );
98 /**
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
110 * @param File $image
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
124 * first page.
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 ) {
145 return '';
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 wfSuppressWarnings();
185 $ret = unserialize( $metadata );
186 wfRestoreWarnings();
188 return $ret;
191 return $metadata;
195 * Get a string describing the type of metadata, for display purposes.
197 * @note This method is currently unused.
198 * @param File $image
199 * @return string
201 function getMetadataType( $image ) {
202 return false;
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.
217 * @param File $image
218 * @param string $metadata The metadata in serialized form
219 * @return bool
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
248 * XML tagnames).
250 * Note, if the file simply has no metadata, but the handler supports
251 * this interface, it should return an empty array, not false.
253 * @param File $file
254 * @return array|bool False if interface not supported
255 * @since 1.23
257 public function getCommonMetaArray( File $file ) {
258 return false;
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().
268 * @param File $image
269 * @param string $script
270 * @param array $params
271 * @return bool|ThumbnailImage
273 function getScriptedTransform( $image, $script, $params ) {
274 return false;
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 );
319 if ( $extensions ) {
320 return array( strtok( $extensions, ' ' ), $mime );
324 // The extension is correct (true) or the MIME type is unknown to
325 // MediaWiki (null)
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
333 * @return array
335 public function getStreamHeaders( $metadata ) {
336 return array();
340 * True if the handled types can be transformed
342 * @param File $file
343 * @return bool
345 function canRender( $file ) {
346 return true;
350 * True if handled types cannot be displayed directly in a browser
351 * but can be rendered
353 * @param File $file
354 * @return bool
356 function mustRender( $file ) {
357 return false;
361 * True if the type has multi-page capabilities
363 * @param File $file
364 * @return bool
366 function isMultiPage( $file ) {
367 return false;
371 * Page count for a multi-page document, false if unsupported or unknown
373 * @param File $file
374 * @return bool
376 function pageCount( $file ) {
377 return false;
381 * The material is vectorized and thus scaling is lossless
383 * @param File $file
384 * @return bool
386 function isVectorized( $file ) {
387 return false;
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
395 * @param File $file
396 * @return bool
398 function isAnimatedImage( $file ) {
399 return false;
403 * If the material is animated, we can animate the thumbnail
404 * @since 1.20
406 * @param File $file
407 * @return bool If material is not animated, handler may return any value.
409 function canAnimateThumbnail( $file ) {
410 return true;
414 * False if the handler is disabled for all files
415 * @return bool
417 function isEnabled() {
418 return true;
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.
433 * @param File $image
434 * @param int $page What page to get dimensions of
435 * @return array|bool
437 function getPageDimensions( $image, $page ) {
438 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
439 if ( $gis ) {
440 return array(
441 'width' => $gis[0],
442 'height' => $gis[1]
444 } else {
445 return false;
450 * Generic getter for text layer.
451 * Currently overloaded by PDF and DjVu handlers
452 * @param File $image
453 * @param int $page Page number to get information for
454 * @return bool|string Page text or false when no text found or if
455 * unsupported.
457 function getPageText( $image, $page ) {
458 return false;
462 * Get the text of the entire document.
463 * @param File $file
464 * @return bool|string The text of the document or false if unsupported.
466 public function getEntireText( File $file ) {
467 $numPages = $file->pageCount();
468 if ( !$numPages ) {
469 // Not a multipage document
470 return $this->getPageText( $file, 1 );
472 $document = '';
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 !== '' ) {
480 return $document;
482 return false;
486 * Get an array structure that looks like this:
488 * array(
489 * 'visible' => array(
490 * 'Human-readable name' => 'Human readable value',
491 * ...
492 * ),
493 * 'collapsed' => array(
494 * 'Human-readable name' => 'Human readable value',
495 * ...
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.
509 * @param File $image
510 * @return array|bool
512 function formatMetadata( $image ) {
513 return false;
516 /** sorts the visible/invisible field.
517 * Split off from ImageHandler::formatMetadata, as used by more than
518 * one type of handler.
520 * This is used by the media handlers that use the FormatMetadata class
522 * @param array $metadataArray Metadata array
523 * @return array Array for use displaying metadata.
525 function formatMetadataHelper( $metadataArray ) {
526 $result = array(
527 'visible' => array(),
528 'collapsed' => array()
531 $formatted = FormatMetadata::getFormattedData( $metadataArray );
532 // Sort fields into visible and collapsed
533 $visibleFields = $this->visibleMetadataFields();
534 foreach ( $formatted as $name => $value ) {
535 $tag = strtolower( $name );
536 self::addMeta( $result,
537 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
538 'exif',
539 $tag,
540 $value
544 return $result;
548 * Get a list of metadata items which should be displayed when
549 * the metadata table is collapsed.
551 * @return array Array of strings
553 protected function visibleMetadataFields() {
554 return FormatMetadata::getVisibleFields();
558 * This is used to generate an array element for each metadata value
559 * That array is then used to generate the table of metadata values
560 * on the image page
562 * @param array &$array An array containing elements for each type of visibility
563 * and each of those elements being an array of metadata items. This function adds
564 * a value to that array.
565 * @param string $visibility ('visible' or 'collapsed') if this value is hidden
566 * by default.
567 * @param string $type Type of metadata tag (currently always 'exif')
568 * @param string $id The name of the metadata tag (like 'artist' for example).
569 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
570 * @param string $value Thingy goes into a wikitext table; it used to be escaped but
571 * that was incompatible with previous practise of customized display
572 * with wikitext formatting via messages such as 'exif-model-value'.
573 * So the escaping is taken back out, but generally this seems a confusing
574 * interface.
575 * @param bool|string $param Value to pass to the message for the name of the field
576 * as $1. Currently this parameter doesn't seem to ever be used.
578 * Note, everything here is passed through the parser later on (!)
580 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
581 $msg = wfMessage( "$type-$id", $param );
582 if ( $msg->exists() ) {
583 $name = $msg->text();
584 } else {
585 // This is for future compatibility when using instant commons.
586 // So as to not display as ugly a name if a new metadata
587 // property is defined that we don't know about
588 // (not a major issue since such a property would be collapsed
589 // by default).
590 wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
591 $name = wfEscapeWikiText( $id );
593 $array[$visibility][] = array(
594 'id' => "$type-$id",
595 'name' => $name,
596 'value' => $value
601 * Short description. Shown on Special:Search results.
603 * @param File $file
604 * @return string
606 function getShortDesc( $file ) {
607 return self::getGeneralShortDesc( $file );
611 * Long description. Shown under image on image description page surounded by ().
613 * @param File $file
614 * @return string
616 function getLongDesc( $file ) {
617 return self::getGeneralLongDesc( $file );
621 * Used instead of getShortDesc if there is no handler registered for file.
623 * @param File $file
624 * @return string
626 static function getGeneralShortDesc( $file ) {
627 global $wgLang;
629 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
633 * Used instead of getLongDesc if there is no handler registered for file.
635 * @param File $file
636 * @return string
638 static function getGeneralLongDesc( $file ) {
639 return wfMessage( 'file-info' )->sizeParams( $file->getSize() )
640 ->params( $file->getMimeType() )->parse();
644 * Calculate the largest thumbnail width for a given original file size
645 * such that the thumbnail's height is at most $maxHeight.
646 * @param int $boxWidth Width of the thumbnail box.
647 * @param int $boxHeight Height of the thumbnail box.
648 * @param int $maxHeight Maximum height expected for the thumbnail.
649 * @return int
651 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
652 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
653 $roundedUp = ceil( $idealWidth );
654 if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
655 return floor( $idealWidth );
656 } else {
657 return $roundedUp;
662 * Shown in file history box on image description page.
664 * @param File $file
665 * @return string Dimensions
667 function getDimensionsString( $file ) {
668 return '';
672 * Modify the parser object post-transform.
674 * This is often used to do $parser->addOutputHook(),
675 * in order to add some javascript to render a viewer.
676 * See TimedMediaHandler or OggHandler for an example.
678 * @param Parser $parser
679 * @param File $file
681 function parserTransformHook( $parser, $file ) {
685 * File validation hook called on upload.
687 * If the file at the given local path is not valid, or its MIME type does not
688 * match the handler class, a Status object should be returned containing
689 * relevant errors.
691 * @param string $fileName The local path to the file.
692 * @return Status
694 function verifyUpload( $fileName ) {
695 return Status::newGood();
699 * Check for zero-sized thumbnails. These can be generated when
700 * no disk space is available or some other error occurs
702 * @param string $dstPath The location of the suspect file
703 * @param int $retval Return value of some shell process, file will be deleted if this is non-zero
704 * @return bool True if removed, false otherwise
706 function removeBadFile( $dstPath, $retval = 0 ) {
707 if ( file_exists( $dstPath ) ) {
708 $thumbstat = stat( $dstPath );
709 if ( $thumbstat['size'] == 0 || $retval != 0 ) {
710 $result = unlink( $dstPath );
712 if ( $result ) {
713 wfDebugLog( 'thumbnail',
714 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
715 $thumbstat['size'], $dstPath ) );
716 } else {
717 wfDebugLog( 'thumbnail',
718 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
719 $thumbstat['size'], $dstPath ) );
722 return true;
726 return false;
730 * Remove files from the purge list.
732 * This is used by some video handlers to prevent ?action=purge
733 * from removing a transcoded video, which is expensive to
734 * regenerate.
736 * @see LocalFile::purgeThumbnails
738 * @param array $files
739 * @param array $options Purge options. Currently will always be
740 * an array with a single key 'forThumbRefresh' set to true.
742 public function filterThumbnailPurgeList( &$files, $options ) {
743 // Do nothing
747 * True if the handler can rotate the media
748 * @since 1.24 non-static. From 1.21-1.23 was static
749 * @return bool
751 public function canRotate() {
752 return false;
756 * On supporting image formats, try to read out the low-level orientation
757 * of the file and return the angle that the file needs to be rotated to
758 * be viewed.
760 * This information is only useful when manipulating the original file;
761 * the width and height we normally work with is logical, and will match
762 * any produced output views.
764 * For files we don't know, we return 0.
766 * @param File $file
767 * @return int 0, 90, 180 or 270
769 public function getRotation( $file ) {
770 return 0;
774 * Log an error that occurred in an external process
776 * Moved from BitmapHandler to MediaHandler with MediaWiki 1.23
778 * @since 1.23
779 * @param int $retval
780 * @param string $err Error reported by command. Anything longer than
781 * MediaHandler::MAX_ERR_LOG_SIZE is stripped off.
782 * @param string $cmd
784 protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
785 # Keep error output limited (bug 57985)
786 $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
788 wfDebugLog( 'thumbnail',
789 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
790 wfHostname(), $retval, $errMessage, $cmd ) );
794 * Get list of languages file can be viewed in.
796 * @param File $file
797 * @return string[] Array of language codes, or empty array if unsupported.
798 * @since 1.23
800 public function getAvailableLanguages( File $file ) {
801 return array();
805 * On file types that support renderings in multiple languages,
806 * which language is used by default if unspecified.
808 * If getAvailableLanguages returns a non-empty array, this must return
809 * a valid language code. Otherwise can return null if files of this
810 * type do not support alternative language renderings.
812 * @param File $file
813 * @return string|null Language code or null if multi-language not supported for filetype.
814 * @since 1.23
816 public function getDefaultRenderLanguage( File $file ) {
817 return null;
821 * If its an audio file, return the length of the file. Otherwise 0.
823 * File::getLength() existed for a long time, but was calling a method
824 * that only existed in some subclasses of this class (The TMH ones).
826 * @param File $file
827 * @return float Length in seconds
828 * @since 1.23
830 public function getLength( $file ) {
831 return 0.0;
835 * True if creating thumbnails from the file is large or otherwise resource-intensive.
836 * @param File $file
837 * @return bool
839 public function isExpensiveToThumbnail( $file ) {
840 return false;
844 * Returns whether or not this handler supports the chained generation of thumbnails according
845 * to buckets
846 * @return bool
847 * @since 1.24
849 public function supportsBucketing() {
850 return false;
854 * Returns a normalised params array for which parameters have been cleaned up for bucketing
855 * purposes
856 * @param array $params
857 * @return array
859 public function sanitizeParamsForBucketing( $params ) {
860 return $params;