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