56dcae05b8ef65eb469d6342f7997ce351f8627a
[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 * Instance cache
36 */
37 static $handlers = array();
38
39 /**
40 * Get a MediaHandler for a given MIME type from the instance cache
41 *
42 * @param string $type
43 *
44 * @return MediaHandler
45 */
46 static function getHandler( $type ) {
47 global $wgMediaHandlers;
48 if ( !isset( $wgMediaHandlers[$type] ) ) {
49 wfDebug( __METHOD__ . ": no handler found for $type.\n" );
50 return false;
51 }
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;
57 }
58 }
59 return self::$handlers[$class];
60 }
61
62 /**
63 * Get an associative array mapping magic word IDs to parameter names.
64 * Will be used by the parser to identify parameters.
65 */
66 abstract function getParamMap();
67
68 /**
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.
72 *
73 * @param string $name
74 * @param $value
75 */
76 abstract function validateParam( $name, $value );
77
78 /**
79 * Merge a parameter array into a string appropriate for inclusion in filenames
80 *
81 * @param array $params Array of parameters that have been through normaliseParams.
82 * @return string
83 */
84 abstract function makeParamString( $params );
85
86 /**
87 * Parse a param string made with makeParamString back into an array
88 *
89 * @param string $str The parameter string without file name (e.g. 122px)
90 * @return array|bool Array of parameters or false on failure.
91 */
92 abstract function parseParamString( $str );
93
94 /**
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
98 * @param $image
99 * @param $params
100 */
101 abstract function normaliseParams( $image, &$params );
102
103 /**
104 * Get an image size array like that returned by getimagesize(), or false if it
105 * can't be determined.
106 *
107 * @param File $image 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
110 */
111 abstract function getImageSize( $image, $path );
112
113 /**
114 * Get handler-specific metadata which will be saved in the img_metadata field.
115 *
116 * @param File $image 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
119 * @return string
120 */
121 function getMetadata( $image, $path ) {
122 return '';
123 }
124
125 /**
126 * Get metadata version.
127 *
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
131 *
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'.
137 *
138 * @return string Version string
139 */
140 static function getMetadataVersion() {
141 $version = array( '2' ); // core metadata version
142 wfRunHooks( 'GetMetadataVersion', array( &$version ) );
143 return implode( ';', $version );
144 }
145
146 /**
147 * Convert metadata version.
148 *
149 * By default just returns $metadata, but can be used to allow
150 * media handlers to convert between metadata versions.
151 *
152 * @param mixed|string|array $metadata Metadata array (serialized if string)
153 * @param int $version Target version
154 * @return array Serialized metadata in specified version, or $metadata on fail.
155 */
156 function convertMetadataVersion( $metadata, $version = 1 ) {
157 if ( !is_array( $metadata ) ) {
158
159 //unserialize to keep return parameter consistent.
160 wfSuppressWarnings();
161 $ret = unserialize( $metadata );
162 wfRestoreWarnings();
163 return $ret;
164 }
165 return $metadata;
166 }
167
168 /**
169 * Get a string describing the type of metadata, for display purposes.
170 * @param $image
171 * @return string
172 */
173 function getMetadataType( $image ) {
174 return false;
175 }
176
177 /**
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).
184 * @return bool
185 */
186 function isMetadataValid( $image, $metadata ) {
187 return self::METADATA_GOOD;
188 }
189
190 /**
191 * Get an array of standard (FormatMetadata type) metadata values.
192 *
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.
197 *
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.
201 *
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.
210 *
211 * Subclasses overriding this function must return a value which is a
212 * valid API response fragment (all associative array keys are valid
213 * XML tagnames).
214 *
215 * Note, if the file simply has no metadata, but the handler supports
216 * this interface, it should return an empty array, not false.
217 *
218 * @param File $file
219 *
220 * @return array|bool False if interface not supported
221 * @since 1.23
222 */
223 public function getCommonMetaArray( File $file ) {
224 return false;
225 }
226
227 /**
228 * Get a MediaTransformOutput object representing an alternate of the transformed
229 * output which will call an intermediary thumbnail assist script.
230 *
231 * Used when the repository has a thumbnailScriptUrl option configured.
232 *
233 * Return false to fall back to the regular getTransform().
234 * @return bool
235 */
236 function getScriptedTransform( $image, $script, $params ) {
237 return false;
238 }
239
240 /**
241 * Get a MediaTransformOutput object representing the transformed output. Does not
242 * actually do the transform.
243 *
244 * @param File $image 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
249 */
250 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
251 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
252 }
253
254 /**
255 * Get a MediaTransformOutput object representing the transformed output. Does the
256 * transform unless $flags contains self::TRANSFORM_LATER.
257 *
258 * @param File $image 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 int $flags A bitfield, may contain self::TRANSFORM_LATER
264 *
265 * @return MediaTransformOutput
266 */
267 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
268
269 /**
270 * Get the thumbnail extension and MIME type for a given source MIME type
271 *
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
276 */
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 );
283 if ( $extensions ) {
284 return array( strtok( $extensions, ' ' ), $mime );
285 }
286 }
287
288 // The extension is correct (true) or the mime type is unknown to
289 // MediaWiki (null)
290 return array( $ext, $mime );
291 }
292
293 /**
294 * Get useful response headers for GET/HEAD requests for a file with the given metadata
295 * @param mixed $metadata Result of the getMetadata() function of this handler for a file
296 * @return array
297 */
298 public function getStreamHeaders( $metadata ) {
299 return array();
300 }
301
302 /**
303 * True if the handled types can be transformed
304 * @return bool
305 */
306 function canRender( $file ) {
307 return true;
308 }
309
310 /**
311 * True if handled types cannot be displayed directly in a browser
312 * but can be rendered
313 * @return bool
314 */
315 function mustRender( $file ) {
316 return false;
317 }
318
319 /**
320 * True if the type has multi-page capabilities
321 * @return bool
322 */
323 function isMultiPage( $file ) {
324 return false;
325 }
326
327 /**
328 * Page count for a multi-page document, false if unsupported or unknown
329 * @return bool
330 */
331 function pageCount( $file ) {
332 return false;
333 }
334
335 /**
336 * The material is vectorized and thus scaling is lossless
337 * @return bool
338 */
339 function isVectorized( $file ) {
340 return false;
341 }
342
343 /**
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
347 * @return bool
348 */
349 function isAnimatedImage( $file ) {
350 return false;
351 }
352
353 /**
354 * If the material is animated, we can animate the thumbnail
355 * @since 1.20
356 * @return bool If material is not animated, handler may return any value.
357 */
358 function canAnimateThumbnail( $file ) {
359 return true;
360 }
361
362 /**
363 * False if the handler is disabled for all files
364 * @return bool
365 */
366 function isEnabled() {
367 return true;
368 }
369
370 /**
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.
375 *
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.
379 *
380 * @note For non-paged media, use getImageSize.
381 *
382 * @param $image File
383 * @param $page What page to get dimensions of
384 * @return array|bool
385 */
386 function getPageDimensions( $image, $page ) {
387 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
388 if ( $gis ) {
389 return array(
390 'width' => $gis[0],
391 'height' => $gis[1]
392 );
393 } else {
394 return false;
395 }
396 }
397
398 /**
399 * Generic getter for text layer.
400 * Currently overloaded by PDF and DjVu handlers
401 * @return bool
402 */
403 function getPageText( $image, $page ) {
404 return false;
405 }
406
407 /**
408 * Get an array structure that looks like this:
409 *
410 * array(
411 * 'visible' => array(
412 * 'Human-readable name' => 'Human readable value',
413 * ...
414 * ),
415 * 'collapsed' => array(
416 * 'Human-readable name' => 'Human readable value',
417 * ...
418 * )
419 * )
420 * The UI will format this into a table where the visible fields are always
421 * visible, and the collapsed fields are optionally visible.
422 *
423 * The function should return false if there is no metadata to display.
424 */
425
426 /**
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.
432 * @return bool
433 */
434 function formatMetadata( $image ) {
435 return false;
436 }
437
438 /** sorts the visible/invisible field.
439 * Split off from ImageHandler::formatMetadata, as used by more than
440 * one type of handler.
441 *
442 * This is used by the media handlers that use the FormatMetadata class
443 *
444 * @param array $metadataArray metadata array
445 * @return array for use displaying metadata.
446 */
447 function formatMetadataHelper( $metadataArray ) {
448 $result = array(
449 'visible' => array(),
450 'collapsed' => array()
451 );
452
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',
460 'exif',
461 $tag,
462 $value
463 );
464 }
465 return $result;
466 }
467
468 /**
469 * Get a list of metadata items which should be displayed when
470 * the metadata table is collapsed.
471 *
472 * @return array of strings
473 * @access protected
474 */
475 function visibleMetadataFields() {
476 return FormatMetadata::getVisibleFields();
477 }
478
479 /**
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
482 * on the image page
483 *
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
488 * by default.
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
496 * interface.
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.
499 *
500 * Note, everything here is passed through the parser later on (!)
501 */
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();
506 } else {
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
511 // by default).
512 wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
513 $name = wfEscapeWikiText( $id );
514 }
515 $array[$visibility][] = array(
516 'id' => "$type-$id",
517 'name' => $name,
518 'value' => $value
519 );
520 }
521
522 /**
523 * Used instead of getLongDesc if there is no handler registered for file.
524 *
525 * @param $file File
526 * @return string
527 */
528 function getShortDesc( $file ) {
529 global $wgLang;
530 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
531 }
532
533 /**
534 * Short description. Shown on Special:Search results.
535 *
536 * @param $file File
537 * @return string
538 */
539 function getLongDesc( $file ) {
540 global $wgLang;
541 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
542 $file->getMimeType() )->parse();
543 }
544
545 /**
546 * Long description. Shown under image on image description page surounded by ().
547 *
548 * @param $file File
549 * @return string
550 */
551 static function getGeneralShortDesc( $file ) {
552 global $wgLang;
553 return $wgLang->formatSize( $file->getSize() );
554 }
555
556 /**
557 * Used instead of getShortDesc if there is no handler registered for file.
558 *
559 * @param $file File
560 * @return string
561 */
562 static function getGeneralLongDesc( $file ) {
563 global $wgLang;
564 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
565 $file->getMimeType() )->parse();
566 }
567
568 /**
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.
574 * @return Integer.
575 */
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 );
581 } else {
582 return $roundedUp;
583 }
584 }
585
586 /**
587 * Shown in file history box on image description page.
588 *
589 * @param File $file
590 * @return String Dimensions
591 */
592 function getDimensionsString( $file ) {
593 return '';
594 }
595
596 /**
597 * Modify the parser object post-transform.
598 *
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.
602 *
603 * @param Parser $parser
604 * @param File $file
605 */
606 function parserTransformHook( $parser, $file ) {}
607
608 /**
609 * File validation hook called on upload.
610 *
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
613 * relevant errors.
614 *
615 * @param string $fileName The local path to the file.
616 * @return Status object
617 */
618 function verifyUpload( $fileName ) {
619 return Status::newGood();
620 }
621
622 /**
623 * Check for zero-sized thumbnails. These can be generated when
624 * no disk space is available or some other error occurs
625 *
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
629 */
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 );
635
636 if ( $result ) {
637 wfDebugLog( 'thumbnail',
638 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
639 $thumbstat['size'], $dstPath ) );
640 } else {
641 wfDebugLog( 'thumbnail',
642 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
643 $thumbstat['size'], $dstPath ) );
644 }
645 return true;
646 }
647 }
648 return false;
649 }
650
651 /**
652 * Remove files from the purge list.
653 *
654 * This is used by some video handlers to prevent ?action=purge
655 * from removing a transcoded video, which is expensive to
656 * regenerate.
657 *
658 * @see LocalFile::purgeThumbnails
659 *
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.
663 */
664 public function filterThumbnailPurgeList( &$files, $options ) {
665 // Do nothing
666 }
667
668 /*
669 * True if the handler can rotate the media
670 * @since 1.21
671 * @return bool
672 */
673 public static function canRotate() {
674 return false;
675 }
676
677 /**
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
680 * be viewed.
681 *
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.
685 *
686 * For files we don't know, we return 0.
687 *
688 * @param $file File
689 * @return int 0, 90, 180 or 270
690 */
691 public function getRotation( $file ) {
692 return 0;
693 }
694
695 }