Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / media / MediaHandler.php
1 <?php
2 /**
3 * Media-handling base classes and generic functionality.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Base media handler class
26 *
27 * @ingroup Media
28 */
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()
36 */
37 const MAX_ERR_LOG_SIZE = 65535;
38
39 /** @var MediaHandler[] Instance cache with array of MediaHandler */
40 protected static $handlers = [];
41
42 /**
43 * Get a MediaHandler for a given MIME type from the instance cache
44 *
45 * @param string $type
46 * @return MediaHandler
47 */
48 static function getHandler( $type ) {
49 global $wgMediaHandlers;
50 if ( !isset( $wgMediaHandlers[$type] ) ) {
51 wfDebug( __METHOD__ . ": no handler found for $type.\n" );
52
53 return false;
54 }
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;
61 }
62 }
63
64 return self::$handlers[$class];
65 }
66
67 /**
68 * Resets all static caches
69 */
70 public static function resetCache() {
71 self::$handlers = [];
72 }
73
74 /**
75 * Get an associative array mapping magic word IDs to parameter names.
76 * Will be used by the parser to identify parameters.
77 */
78 abstract public function getParamMap();
79
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.
84 *
85 * @param string $name
86 * @param mixed $value
87 */
88 abstract public function validateParam( $name, $value );
89
90 /**
91 * Merge a parameter array into a string appropriate for inclusion in filenames
92 *
93 * @param array $params Array of parameters that have been through normaliseParams.
94 * @return string
95 */
96 abstract public function makeParamString( $params );
97
98 /**
99 * Parse a param string made with makeParamString back into an array
100 *
101 * @param string $str The parameter string without file name (e.g. 122px)
102 * @return array|bool Array of parameters or false on failure.
103 */
104 abstract public function parseParamString( $str );
105
106 /**
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
112 */
113 abstract function normaliseParams( $image, &$params );
114
115 /**
116 * Get an image size array like that returned by getimagesize(), or false if it
117 * can't be determined.
118 *
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.
122 *
123 * @note If this is a multipage file, return the width and height of the
124 * first page.
125 *
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".
133 */
134 abstract function getImageSize( $image, $path );
135
136 /**
137 * Get handler-specific metadata which will be saved in the img_metadata field.
138 *
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())
143 */
144 function getMetadata( $image, $path ) {
145 return '';
146 }
147
148 /**
149 * Get metadata version.
150 *
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
154 *
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'.
160 *
161 * @return string Version string
162 */
163 static function getMetadataVersion() {
164 $version = [ '2' ]; // core metadata version
165 Hooks::run( 'GetMetadataVersion', [ &$version ] );
166
167 return implode( ';', $version );
168 }
169
170 /**
171 * Convert metadata version.
172 *
173 * By default just returns $metadata, but can be used to allow
174 * media handlers to convert between metadata versions.
175 *
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.
179 */
180 function convertMetadataVersion( $metadata, $version = 1 ) {
181 if ( !is_array( $metadata ) ) {
182
183 // unserialize to keep return parameter consistent.
184 MediaWiki\suppressWarnings();
185 $ret = unserialize( $metadata );
186 MediaWiki\restoreWarnings();
187
188 return $ret;
189 }
190
191 return $metadata;
192 }
193
194 /**
195 * Get a string describing the type of metadata, for display purposes.
196 *
197 * @note This method is currently unused.
198 * @param File $image
199 * @return string
200 */
201 function getMetadataType( $image ) {
202 return false;
203 }
204
205 /**
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).
212 *
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
220 */
221 function isMetadataValid( $image, $metadata ) {
222 return self::METADATA_GOOD;
223 }
224
225 /**
226 * Get an array of standard (FormatMetadata type) metadata values.
227 *
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.
232 *
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.
236 *
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.
245 *
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).
249 *
250 * Note, if the file simply has no metadata, but the handler supports
251 * this interface, it should return an empty array, not false.
252 *
253 * @param File $file
254 * @return array|bool False if interface not supported
255 * @since 1.23
256 */
257 public function getCommonMetaArray( File $file ) {
258 return false;
259 }
260
261 /**
262 * Get a MediaTransformOutput object representing an alternate of the transformed
263 * output which will call an intermediary thumbnail assist script.
264 *
265 * Used when the repository has a thumbnailScriptUrl option configured.
266 *
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
272 */
273 function getScriptedTransform( $image, $script, $params ) {
274 return false;
275 }
276
277 /**
278 * Get a MediaTransformOutput object representing the transformed output. Does not
279 * actually do the transform.
280 *
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
286 */
287 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
288 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
289 }
290
291 /**
292 * Get a MediaTransformOutput object representing the transformed output. Does the
293 * transform unless $flags contains self::TRANSFORM_LATER.
294 *
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
302 */
303 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
304
305 /**
306 * Get the thumbnail extension and MIME type for a given source MIME type
307 *
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
312 */
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 [ strtok( $extensions, ' ' ), $mime ];
321 }
322 }
323
324 // The extension is correct (true) or the MIME type is unknown to
325 // MediaWiki (null)
326 return [ $ext, $mime ];
327 }
328
329 /**
330 * Get useful response headers for GET/HEAD requests for a file with the given metadata
331 *
332 * @param mixed $metadata Result of the getMetadata() function of this handler for a file
333 * @return array
334 */
335 public function getStreamHeaders( $metadata ) {
336 return [];
337 }
338
339 /**
340 * True if the handled types can be transformed
341 *
342 * @param File $file
343 * @return bool
344 */
345 public function canRender( $file ) {
346 return true;
347 }
348
349 /**
350 * True if handled types cannot be displayed directly in a browser
351 * but can be rendered
352 *
353 * @param File $file
354 * @return bool
355 */
356 public function mustRender( $file ) {
357 return false;
358 }
359
360 /**
361 * True if the type has multi-page capabilities
362 *
363 * @param File $file
364 * @return bool
365 */
366 public function isMultiPage( $file ) {
367 return false;
368 }
369
370 /**
371 * Page count for a multi-page document, false if unsupported or unknown
372 *
373 * @param File $file
374 * @return bool
375 */
376 function pageCount( File $file ) {
377 return false;
378 }
379
380 /**
381 * The material is vectorized and thus scaling is lossless
382 *
383 * @param File $file
384 * @return bool
385 */
386 function isVectorized( $file ) {
387 return false;
388 }
389
390 /**
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
394 *
395 * @param File $file
396 * @return bool
397 */
398 function isAnimatedImage( $file ) {
399 return false;
400 }
401
402 /**
403 * If the material is animated, we can animate the thumbnail
404 * @since 1.20
405 *
406 * @param File $file
407 * @return bool If material is not animated, handler may return any value.
408 */
409 function canAnimateThumbnail( $file ) {
410 return true;
411 }
412
413 /**
414 * False if the handler is disabled for all files
415 * @return bool
416 */
417 function isEnabled() {
418 return true;
419 }
420
421 /**
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.
426 *
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.
430 *
431 * @note For non-paged media, use getImageSize.
432 *
433 * @param File $image
434 * @param int $page What page to get dimensions of
435 * @return array|bool
436 */
437 function getPageDimensions( File $image, $page ) {
438 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
439 if ( $gis ) {
440 return [
441 'width' => $gis[0],
442 'height' => $gis[1]
443 ];
444 } else {
445 return false;
446 }
447 }
448
449 /**
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.
456 */
457 function getPageText( File $image, $page ) {
458 return false;
459 }
460
461 /**
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.
465 */
466 public function getEntireText( File $file ) {
467 $numPages = $file->pageCount();
468 if ( !$numPages ) {
469 // Not a multipage document
470 return $this->getPageText( $file, 1 );
471 }
472 $document = '';
473 for ( $i = 1; $i <= $numPages; $i++ ) {
474 $curPage = $this->getPageText( $file, $i );
475 if ( is_string( $curPage ) ) {
476 $document .= $curPage . "\n";
477 }
478 }
479 if ( $document !== '' ) {
480 return $document;
481 }
482 return false;
483 }
484
485 /**
486 * Get an array structure that looks like this:
487 *
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 * ...
496 * )
497 * )
498 * The UI will format this into a table where the visible fields are always
499 * visible, and the collapsed fields are optionally visible.
500 *
501 * The function should return false if there is no metadata to display.
502 */
503
504 /**
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 * @param bool|IContextSource $context Context to use (optional)
511 * @return array|bool
512 */
513 function formatMetadata( $image, $context = false ) {
514 return false;
515 }
516
517 /** sorts the visible/invisible field.
518 * Split off from ImageHandler::formatMetadata, as used by more than
519 * one type of handler.
520 *
521 * This is used by the media handlers that use the FormatMetadata class
522 *
523 * @param array $metadataArray Metadata array
524 * @param bool|IContextSource $context Context to use (optional)
525 * @return array Array for use displaying metadata.
526 */
527 function formatMetadataHelper( $metadataArray, $context = false ) {
528 $result = [
529 'visible' => [],
530 'collapsed' => []
531 ];
532
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',
540 'exif',
541 $tag,
542 $value
543 );
544 }
545
546 return $result;
547 }
548
549 /**
550 * Get a list of metadata items which should be displayed when
551 * the metadata table is collapsed.
552 *
553 * @return array Array of strings
554 */
555 protected function visibleMetadataFields() {
556 return FormatMetadata::getVisibleFields();
557 }
558
559 /**
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
562 * on the image page
563 *
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
568 * by default.
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
576 * interface.
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.
579 *
580 * Note, everything here is passed through the parser later on (!)
581 */
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();
586 } else {
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
591 // by default).
592 wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
593 $name = wfEscapeWikiText( $id );
594 }
595 $array[$visibility][] = [
596 'id' => "$type-$id",
597 'name' => $name,
598 'value' => $value
599 ];
600 }
601
602 /**
603 * Short description. Shown on Special:Search results.
604 *
605 * @param File $file
606 * @return string
607 */
608 function getShortDesc( $file ) {
609 return self::getGeneralShortDesc( $file );
610 }
611
612 /**
613 * Long description. Shown under image on image description page surounded by ().
614 *
615 * @param File $file
616 * @return string
617 */
618 function getLongDesc( $file ) {
619 return self::getGeneralLongDesc( $file );
620 }
621
622 /**
623 * Used instead of getShortDesc if there is no handler registered for file.
624 *
625 * @param File $file
626 * @return string
627 */
628 static function getGeneralShortDesc( $file ) {
629 global $wgLang;
630
631 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
632 }
633
634 /**
635 * Used instead of getLongDesc if there is no handler registered for file.
636 *
637 * @param File $file
638 * @return string
639 */
640 static function getGeneralLongDesc( $file ) {
641 return wfMessage( 'file-info' )->sizeParams( $file->getSize() )
642 ->params( '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
643 }
644
645 /**
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.
651 * @return int
652 */
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 );
658 } else {
659 return $roundedUp;
660 }
661 }
662
663 /**
664 * Shown in file history box on image description page.
665 *
666 * @param File $file
667 * @return string Dimensions
668 */
669 function getDimensionsString( $file ) {
670 return '';
671 }
672
673 /**
674 * Modify the parser object post-transform.
675 *
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.
679 *
680 * @param Parser $parser
681 * @param File $file
682 */
683 function parserTransformHook( $parser, $file ) {
684 }
685
686 /**
687 * File validation hook called on upload.
688 *
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
691 * relevant errors.
692 *
693 * @param string $fileName The local path to the file.
694 * @return Status
695 */
696 function verifyUpload( $fileName ) {
697 return Status::newGood();
698 }
699
700 /**
701 * Check for zero-sized thumbnails. These can be generated when
702 * no disk space is available or some other error occurs
703 *
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
707 */
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 );
713
714 if ( $result ) {
715 wfDebugLog( 'thumbnail',
716 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
717 $thumbstat['size'], $dstPath ) );
718 } else {
719 wfDebugLog( 'thumbnail',
720 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
721 $thumbstat['size'], $dstPath ) );
722 }
723
724 return true;
725 }
726 }
727
728 return false;
729 }
730
731 /**
732 * Remove files from the purge list.
733 *
734 * This is used by some video handlers to prevent ?action=purge
735 * from removing a transcoded video, which is expensive to
736 * regenerate.
737 *
738 * @see LocalFile::purgeThumbnails
739 *
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.
743 */
744 public function filterThumbnailPurgeList( &$files, $options ) {
745 // Do nothing
746 }
747
748 /**
749 * True if the handler can rotate the media
750 * @since 1.24 non-static. From 1.21-1.23 was static
751 * @return bool
752 */
753 public function canRotate() {
754 return false;
755 }
756
757 /**
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
760 * be viewed.
761 *
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.
765 *
766 * For files we don't know, we return 0.
767 *
768 * @param File $file
769 * @return int 0, 90, 180 or 270
770 */
771 public function getRotation( $file ) {
772 return 0;
773 }
774
775 /**
776 * Log an error that occurred in an external process
777 *
778 * Moved from BitmapHandler to MediaHandler with MediaWiki 1.23
779 *
780 * @since 1.23
781 * @param int $retval
782 * @param string $err Error reported by command. Anything longer than
783 * MediaHandler::MAX_ERR_LOG_SIZE is stripped off.
784 * @param string $cmd
785 */
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 ) );
789
790 wfDebugLog( 'thumbnail',
791 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
792 wfHostname(), $retval, $errMessage, $cmd ) );
793 }
794
795 /**
796 * Get list of languages file can be viewed in.
797 *
798 * @param File $file
799 * @return string[] Array of language codes, or empty array if unsupported.
800 * @since 1.23
801 */
802 public function getAvailableLanguages( File $file ) {
803 return [];
804 }
805
806 /**
807 * On file types that support renderings in multiple languages,
808 * which language is used by default if unspecified.
809 *
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.
813 *
814 * @param File $file
815 * @return string|null Language code or null if multi-language not supported for filetype.
816 * @since 1.23
817 */
818 public function getDefaultRenderLanguage( File $file ) {
819 return null;
820 }
821
822 /**
823 * If its an audio file, return the length of the file. Otherwise 0.
824 *
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).
827 *
828 * @param File $file
829 * @return float Length in seconds
830 * @since 1.23
831 */
832 public function getLength( $file ) {
833 return 0.0;
834 }
835
836 /**
837 * True if creating thumbnails from the file is large or otherwise resource-intensive.
838 * @param File $file
839 * @return bool
840 */
841 public function isExpensiveToThumbnail( $file ) {
842 return false;
843 }
844
845 /**
846 * Returns whether or not this handler supports the chained generation of thumbnails according
847 * to buckets
848 * @return bool
849 * @since 1.24
850 */
851 public function supportsBucketing() {
852 return false;
853 }
854
855 /**
856 * Returns a normalised params array for which parameters have been cleaned up for bucketing
857 * purposes
858 * @param array $params
859 * @return array
860 */
861 public function sanitizeParamsForBucketing( $params ) {
862 return $params;
863 }
864
865 /**
866 * Gets configuration for the file warning message. Return value of
867 * the following structure:
868 * array(
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',
881 * ),
882 * // Optional, link for more information
883 * 'link' => 'http://example.com',
884 * )
885 *
886 * Returns null if no warning is necessary.
887 * @param File $file
888 * @return array|null
889 */
890 public function getWarningConfig( $file ) {
891 return null;
892 }
893 }