Merge "Unblacklist group-specific JS/CSS for the user group"
[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 = array();
41
42 /**
43 * Get a MediaHandler for a given MIME type from the instance cache
44 *
45 * @param string $type
46 *
47 * @return MediaHandler
48 */
49 static function getHandler( $type ) {
50 global $wgMediaHandlers;
51 if ( !isset( $wgMediaHandlers[$type] ) ) {
52 wfDebug( __METHOD__ . ": no handler found for $type.\n" );
53
54 return false;
55 }
56 $class = $wgMediaHandlers[$type];
57 if ( !isset( self::$handlers[$class] ) ) {
58 self::$handlers[$class] = new $class;
59 if ( !self::$handlers[$class]->isEnabled() ) {
60 self::$handlers[$class] = false;
61 }
62 }
63
64 return self::$handlers[$class];
65 }
66
67 /**
68 * Get an associative array mapping magic word IDs to parameter names.
69 * Will be used by the parser to identify parameters.
70 */
71 abstract function getParamMap();
72
73 /**
74 * Validate a thumbnail parameter at parse time.
75 * Return true to accept the parameter, and false to reject it.
76 * If you return false, the parser will do something quiet and forgiving.
77 *
78 * @param string $name
79 * @param $value
80 */
81 abstract function validateParam( $name, $value );
82
83 /**
84 * Merge a parameter array into a string appropriate for inclusion in filenames
85 *
86 * @param array $params Array of parameters that have been through normaliseParams.
87 * @return string
88 */
89 abstract function makeParamString( $params );
90
91 /**
92 * Parse a param string made with makeParamString back into an array
93 *
94 * @param string $str The parameter string without file name (e.g. 122px)
95 * @return array|bool Array of parameters or false on failure.
96 */
97 abstract function parseParamString( $str );
98
99 /**
100 * Changes the parameter array as necessary, ready for transformation.
101 * Should be idempotent.
102 * Returns false if the parameters are unacceptable and the transform should fail
103 * @param $image
104 * @param $params
105 */
106 abstract function normaliseParams( $image, &$params );
107
108 /**
109 * Get an image size array like that returned by getimagesize(), or false if it
110 * can't be determined.
111 *
112 * @param File $image The image object, or false if there isn't one
113 * @param string $path the filename
114 * @return array Follow the format of PHP getimagesize() internal function. See http://www.php.net/getimagesize
115 */
116 abstract function getImageSize( $image, $path );
117
118 /**
119 * Get handler-specific metadata which will be saved in the img_metadata field.
120 *
121 * @param File $image The image object, or false if there isn't one.
122 * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
123 * @param string $path The filename
124 * @return string
125 */
126 function getMetadata( $image, $path ) {
127 return '';
128 }
129
130 /**
131 * Get metadata version.
132 *
133 * This is not used for validating metadata, this is used for the api when returning
134 * metadata, since api content formats should stay the same over time, and so things
135 * using ForiegnApiRepo can keep backwards compatibility
136 *
137 * All core media handlers share a common version number, and extensions can
138 * use the GetMetadataVersion hook to append to the array (they should append a unique
139 * string so not to get confusing). If there was a media handler named 'foo' with metadata
140 * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
141 * version is 2, the end version string would look like '2;foo=3'.
142 *
143 * @return string Version string
144 */
145 static function getMetadataVersion() {
146 $version = array( '2' ); // core metadata version
147 wfRunHooks( 'GetMetadataVersion', array( &$version ) );
148
149 return implode( ';', $version );
150 }
151
152 /**
153 * Convert metadata version.
154 *
155 * By default just returns $metadata, but can be used to allow
156 * media handlers to convert between metadata versions.
157 *
158 * @param mixed|string|array $metadata Metadata array (serialized if string)
159 * @param int $version Target version
160 * @return array Serialized metadata in specified version, or $metadata on fail.
161 */
162 function convertMetadataVersion( $metadata, $version = 1 ) {
163 if ( !is_array( $metadata ) ) {
164
165 //unserialize to keep return parameter consistent.
166 wfSuppressWarnings();
167 $ret = unserialize( $metadata );
168 wfRestoreWarnings();
169
170 return $ret;
171 }
172
173 return $metadata;
174 }
175
176 /**
177 * Get a string describing the type of metadata, for display purposes.
178 * @param $image
179 * @return string
180 */
181 function getMetadataType( $image ) {
182 return false;
183 }
184
185 /**
186 * Check if the metadata string is valid for this handler.
187 * If it returns MediaHandler::METADATA_BAD (or false), Image
188 * will reload the metadata from the file and update the database.
189 * MediaHandler::METADATA_GOOD for if the metadata is a-ok,
190 * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards
191 * compatible (which may or may not trigger a metadata reload).
192 * @return bool
193 */
194 function isMetadataValid( $image, $metadata ) {
195 return self::METADATA_GOOD;
196 }
197
198 /**
199 * Get an array of standard (FormatMetadata type) metadata values.
200 *
201 * The returned data is largely the same as that from getMetadata(),
202 * but formatted in a standard, stable, handler-independent way.
203 * The idea being that some values like ImageDescription or Artist
204 * are universal and should be retrievable in a handler generic way.
205 *
206 * The specific properties are the type of properties that can be
207 * handled by the FormatMetadata class. These values are exposed to the
208 * user via the filemetadata parser function.
209 *
210 * Details of the response format of this function can be found at
211 * https://www.mediawiki.org/wiki/Manual:File_metadata_handling
212 * tl/dr: the response is an associative array of
213 * properties keyed by name, but the value can be complex. You probably
214 * want to call one of the FormatMetadata::flatten* functions on the
215 * property values before using them, or call
216 * FormatMetadata::getFormattedData() on the full response array, which
217 * transforms all values into prettified, human-readable text.
218 *
219 * Subclasses overriding this function must return a value which is a
220 * valid API response fragment (all associative array keys are valid
221 * XML tagnames).
222 *
223 * Note, if the file simply has no metadata, but the handler supports
224 * this interface, it should return an empty array, not false.
225 *
226 * @param File $file
227 *
228 * @return array|bool False if interface not supported
229 * @since 1.23
230 */
231 public function getCommonMetaArray( File $file ) {
232 return false;
233 }
234
235 /**
236 * Get a MediaTransformOutput object representing an alternate of the transformed
237 * output which will call an intermediary thumbnail assist script.
238 *
239 * Used when the repository has a thumbnailScriptUrl option configured.
240 *
241 * Return false to fall back to the regular getTransform().
242 * @return bool
243 */
244 function getScriptedTransform( $image, $script, $params ) {
245 return false;
246 }
247
248 /**
249 * Get a MediaTransformOutput object representing the transformed output. Does not
250 * actually do the transform.
251 *
252 * @param File $image The image object
253 * @param string $dstPath filesystem destination path
254 * @param string $dstUrl Destination URL to use in output HTML
255 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
256 * @return MediaTransformOutput
257 */
258 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
259 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
260 }
261
262 /**
263 * Get a MediaTransformOutput object representing the transformed output. Does the
264 * transform unless $flags contains self::TRANSFORM_LATER.
265 *
266 * @param File $image The image object
267 * @param string $dstPath filesystem destination path
268 * @param string $dstUrl destination URL to use in output HTML
269 * @param array $params arbitrary set of parameters validated by $this->validateParam()
270 * Note: These parameters have *not* gone through $this->normaliseParams()
271 * @param int $flags A bitfield, may contain self::TRANSFORM_LATER
272 *
273 * @return MediaTransformOutput
274 */
275 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
276
277 /**
278 * Get the thumbnail extension and MIME type for a given source MIME type
279 *
280 * @param string $ext Extension of original file
281 * @param string $mime Mime type of original file
282 * @param array $params Handler specific rendering parameters
283 * @return array thumbnail extension and MIME type
284 */
285 function getThumbType( $ext, $mime, $params = null ) {
286 $magic = MimeMagic::singleton();
287 if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
288 // The extension is not valid for this mime type and we do
289 // recognize the mime type
290 $extensions = $magic->getExtensionsForType( $mime );
291 if ( $extensions ) {
292 return array( strtok( $extensions, ' ' ), $mime );
293 }
294 }
295
296 // The extension is correct (true) or the mime type is unknown to
297 // MediaWiki (null)
298 return array( $ext, $mime );
299 }
300
301 /**
302 * Get useful response headers for GET/HEAD requests for a file with the given metadata
303 * @param mixed $metadata Result of the getMetadata() function of this handler for a file
304 * @return array
305 */
306 public function getStreamHeaders( $metadata ) {
307 return array();
308 }
309
310 /**
311 * True if the handled types can be transformed
312 * @return bool
313 */
314 function canRender( $file ) {
315 return true;
316 }
317
318 /**
319 * True if handled types cannot be displayed directly in a browser
320 * but can be rendered
321 * @return bool
322 */
323 function mustRender( $file ) {
324 return false;
325 }
326
327 /**
328 * True if the type has multi-page capabilities
329 * @return bool
330 */
331 function isMultiPage( $file ) {
332 return false;
333 }
334
335 /**
336 * Page count for a multi-page document, false if unsupported or unknown
337 * @return bool
338 */
339 function pageCount( $file ) {
340 return false;
341 }
342
343 /**
344 * The material is vectorized and thus scaling is lossless
345 * @return bool
346 */
347 function isVectorized( $file ) {
348 return false;
349 }
350
351 /**
352 * The material is an image, and is animated.
353 * In particular, video material need not return true.
354 * @note Before 1.20, this was a method of ImageHandler only
355 * @return bool
356 */
357 function isAnimatedImage( $file ) {
358 return false;
359 }
360
361 /**
362 * If the material is animated, we can animate the thumbnail
363 * @since 1.20
364 * @return bool If material is not animated, handler may return any value.
365 */
366 function canAnimateThumbnail( $file ) {
367 return true;
368 }
369
370 /**
371 * False if the handler is disabled for all files
372 * @return bool
373 */
374 function isEnabled() {
375 return true;
376 }
377
378 /**
379 * Get an associative array of page dimensions
380 * Currently "width" and "height" are understood, but this might be
381 * expanded in the future.
382 * Returns false if unknown.
383 *
384 * It is expected that handlers for paged media (e.g. DjVuHandler)
385 * will override this method so that it gives the correct results
386 * for each specific page of the file, using the $page argument.
387 *
388 * @note For non-paged media, use getImageSize.
389 *
390 * @param File $image
391 * @param int $page What page to get dimensions of
392 * @return array|bool
393 */
394 function getPageDimensions( $image, $page ) {
395 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
396 if ( $gis ) {
397 return array(
398 'width' => $gis[0],
399 'height' => $gis[1]
400 );
401 } else {
402 return false;
403 }
404 }
405
406 /**
407 * Generic getter for text layer.
408 * Currently overloaded by PDF and DjVu handlers
409 * @return bool
410 */
411 function getPageText( $image, $page ) {
412 return false;
413 }
414
415 /**
416 * Get an array structure that looks like this:
417 *
418 * array(
419 * 'visible' => array(
420 * 'Human-readable name' => 'Human readable value',
421 * ...
422 * ),
423 * 'collapsed' => array(
424 * 'Human-readable name' => 'Human readable value',
425 * ...
426 * )
427 * )
428 * The UI will format this into a table where the visible fields are always
429 * visible, and the collapsed fields are optionally visible.
430 *
431 * The function should return false if there is no metadata to display.
432 */
433
434 /**
435 * @todo FIXME: I don't really like this interface, it's not very flexible
436 * I think the media handler should generate HTML instead. It can do
437 * all the formatting according to some standard. That makes it possible
438 * to do things like visual indication of grouped and chained streams
439 * in ogg container files.
440 * @return bool
441 */
442 function formatMetadata( $image ) {
443 return false;
444 }
445
446 /** sorts the visible/invisible field.
447 * Split off from ImageHandler::formatMetadata, as used by more than
448 * one type of handler.
449 *
450 * This is used by the media handlers that use the FormatMetadata class
451 *
452 * @param array $metadataArray metadata array
453 * @return array for use displaying metadata.
454 */
455 function formatMetadataHelper( $metadataArray ) {
456 $result = array(
457 'visible' => array(),
458 'collapsed' => array()
459 );
460
461 $formatted = FormatMetadata::getFormattedData( $metadataArray );
462 // Sort fields into visible and collapsed
463 $visibleFields = $this->visibleMetadataFields();
464 foreach ( $formatted as $name => $value ) {
465 $tag = strtolower( $name );
466 self::addMeta( $result,
467 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
468 'exif',
469 $tag,
470 $value
471 );
472 }
473
474 return $result;
475 }
476
477 /**
478 * Get a list of metadata items which should be displayed when
479 * the metadata table is collapsed.
480 *
481 * @return array of strings
482 * @access protected
483 */
484 function visibleMetadataFields() {
485 return FormatMetadata::getVisibleFields();
486 }
487
488 /**
489 * This is used to generate an array element for each metadata value
490 * That array is then used to generate the table of metadata values
491 * on the image page
492 *
493 * @param &$array Array An array containing elements for each type of visibility
494 * and each of those elements being an array of metadata items. This function adds
495 * a value to that array.
496 * @param string $visibility ('visible' or 'collapsed') if this value is hidden
497 * by default.
498 * @param string $type type of metadata tag (currently always 'exif')
499 * @param string $id the name of the metadata tag (like 'artist' for example).
500 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
501 * @param string $value thingy goes into a wikitext table; it used to be escaped but
502 * that was incompatible with previous practise of customized display
503 * with wikitext formatting via messages such as 'exif-model-value'.
504 * So the escaping is taken back out, but generally this seems a confusing
505 * interface.
506 * @param string $param value to pass to the message for the name of the field
507 * as $1. Currently this parameter doesn't seem to ever be used.
508 *
509 * Note, everything here is passed through the parser later on (!)
510 */
511 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
512 $msg = wfMessage( "$type-$id", $param );
513 if ( $msg->exists() ) {
514 $name = $msg->text();
515 } else {
516 // This is for future compatibility when using instant commons.
517 // So as to not display as ugly a name if a new metadata
518 // property is defined that we don't know about
519 // (not a major issue since such a property would be collapsed
520 // by default).
521 wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
522 $name = wfEscapeWikiText( $id );
523 }
524 $array[$visibility][] = array(
525 'id' => "$type-$id",
526 'name' => $name,
527 'value' => $value
528 );
529 }
530
531 /**
532 * Used instead of getLongDesc if there is no handler registered for file.
533 *
534 * @param $file File
535 * @return string
536 */
537 function getShortDesc( $file ) {
538 global $wgLang;
539
540 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
541 }
542
543 /**
544 * Short description. Shown on Special:Search results.
545 *
546 * @param $file File
547 * @return string
548 */
549 function getLongDesc( $file ) {
550 global $wgLang;
551
552 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
553 $file->getMimeType() )->parse();
554 }
555
556 /**
557 * Long description. Shown under image on image description page surounded by ().
558 *
559 * @param $file File
560 * @return string
561 */
562 static function getGeneralShortDesc( $file ) {
563 global $wgLang;
564
565 return $wgLang->formatSize( $file->getSize() );
566 }
567
568 /**
569 * Used instead of getShortDesc if there is no handler registered for file.
570 *
571 * @param $file File
572 * @return string
573 */
574 static function getGeneralLongDesc( $file ) {
575 global $wgLang;
576
577 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
578 $file->getMimeType() )->parse();
579 }
580
581 /**
582 * Calculate the largest thumbnail width for a given original file size
583 * such that the thumbnail's height is at most $maxHeight.
584 * @param $boxWidth Integer Width of the thumbnail box.
585 * @param $boxHeight Integer Height of the thumbnail box.
586 * @param $maxHeight Integer Maximum height expected for the thumbnail.
587 * @return Integer.
588 */
589 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
590 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
591 $roundedUp = ceil( $idealWidth );
592 if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
593 return floor( $idealWidth );
594 } else {
595 return $roundedUp;
596 }
597 }
598
599 /**
600 * Shown in file history box on image description page.
601 *
602 * @param File $file
603 * @return String Dimensions
604 */
605 function getDimensionsString( $file ) {
606 return '';
607 }
608
609 /**
610 * Modify the parser object post-transform.
611 *
612 * This is often used to do $parser->addOutputHook(),
613 * in order to add some javascript to render a viewer.
614 * See TimedMediaHandler or OggHandler for an example.
615 *
616 * @param Parser $parser
617 * @param File $file
618 */
619 function parserTransformHook( $parser, $file ) {
620 }
621
622 /**
623 * File validation hook called on upload.
624 *
625 * If the file at the given local path is not valid, or its MIME type does not
626 * match the handler class, a Status object should be returned containing
627 * relevant errors.
628 *
629 * @param string $fileName The local path to the file.
630 * @return Status object
631 */
632 function verifyUpload( $fileName ) {
633 return Status::newGood();
634 }
635
636 /**
637 * Check for zero-sized thumbnails. These can be generated when
638 * no disk space is available or some other error occurs
639 *
640 * @param string $dstPath The location of the suspect file
641 * @param int $retval Return value of some shell process, file will be deleted if this is non-zero
642 * @return bool True if removed, false otherwise
643 */
644 function removeBadFile( $dstPath, $retval = 0 ) {
645 if ( file_exists( $dstPath ) ) {
646 $thumbstat = stat( $dstPath );
647 if ( $thumbstat['size'] == 0 || $retval != 0 ) {
648 $result = unlink( $dstPath );
649
650 if ( $result ) {
651 wfDebugLog( 'thumbnail',
652 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
653 $thumbstat['size'], $dstPath ) );
654 } else {
655 wfDebugLog( 'thumbnail',
656 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
657 $thumbstat['size'], $dstPath ) );
658 }
659
660 return true;
661 }
662 }
663
664 return false;
665 }
666
667 /**
668 * Remove files from the purge list.
669 *
670 * This is used by some video handlers to prevent ?action=purge
671 * from removing a transcoded video, which is expensive to
672 * regenerate.
673 *
674 * @see LocalFile::purgeThumbnails
675 *
676 * @param array $files
677 * @param array $options Purge options. Currently will always be
678 * an array with a single key 'forThumbRefresh' set to true.
679 */
680 public function filterThumbnailPurgeList( &$files, $options ) {
681 // Do nothing
682 }
683
684 /*
685 * True if the handler can rotate the media
686 * @since 1.21
687 * @return bool
688 */
689 public static function canRotate() {
690 return false;
691 }
692
693 /**
694 * On supporting image formats, try to read out the low-level orientation
695 * of the file and return the angle that the file needs to be rotated to
696 * be viewed.
697 *
698 * This information is only useful when manipulating the original file;
699 * the width and height we normally work with is logical, and will match
700 * any produced output views.
701 *
702 * For files we don't know, we return 0.
703 *
704 * @param $file File
705 * @return int 0, 90, 180 or 270
706 */
707 public function getRotation( $file ) {
708 return 0;
709 }
710
711 /**
712 * Log an error that occurred in an external process
713 *
714 * Moved from BitmapHandler to MediaHandler with MediaWiki 1.23
715 *
716 * @since 1.23
717 * @param $retval int
718 * @param $err string Error reported by command. Anything longer than
719 * MediaHandler::MAX_ERR_LOG_SIZE is stripped off.
720 * @param $cmd string
721 */
722 protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
723 # Keep error output limited (bug 57985)
724 $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
725
726 wfDebugLog( 'thumbnail',
727 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
728 wfHostname(), $retval, $errMessage, $cmd ) );
729 }
730
731 }