3 * Media-handling base classes and generic functionality.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Base media handler class
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.
37 static $handlers = array();
40 * Get a MediaHandler for a given MIME type from the instance cache
44 * @return MediaHandler
46 static function getHandler( $type ) {
47 global $wgMediaHandlers;
48 if ( !isset( $wgMediaHandlers[$type] ) ) {
49 wfDebug( __METHOD__
. ": no handler found for $type.\n");
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;
59 return self
::$handlers[$class];
63 * Get an associative array mapping magic word IDs to parameter names.
64 * Will be used by the parser to identify parameters.
66 abstract function getParamMap();
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.
76 abstract function validateParam( $name, $value );
79 * Merge a parameter array into a string appropriate for inclusion in filenames
81 * @param $params array
83 abstract function makeParamString( $params );
86 * Parse a param string made with makeParamString back into an array
90 abstract function parseParamString( $str );
93 * Changes the parameter array as necessary, ready for transformation.
94 * Should be idempotent.
95 * Returns false if the parameters are unacceptable and the transform should fail
99 abstract function normaliseParams( $image, &$params );
102 * Get an image size array like that returned by getimagesize(), or false if it
103 * can't be determined.
105 * @param $image File: the image object, or false if there isn't one
106 * @param $path String: the filename
107 * @return Array Follow the format of PHP getimagesize() internal function. See http://www.php.net/getimagesize
109 abstract function getImageSize( $image, $path );
112 * Get handler-specific metadata which will be saved in the img_metadata field.
114 * @param $image File: the image object, or false if there isn't one.
115 * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
116 * @param $path String: the filename
119 function getMetadata( $image, $path ) { return ''; }
122 * Get metadata version.
124 * This is not used for validating metadata, this is used for the api when returning
125 * metadata, since api content formats should stay the same over time, and so things
126 * using ForiegnApiRepo can keep backwards compatibility
128 * All core media handlers share a common version number, and extensions can
129 * use the GetMetadataVersion hook to append to the array (they should append a unique
130 * string so not to get confusing). If there was a media handler named 'foo' with metadata
131 * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
132 * version is 2, the end version string would look like '2;foo=3'.
134 * @return string version string
136 static function getMetadataVersion () {
137 $version = Array( '2' ); // core metadata version
138 wfRunHooks('GetMetadataVersion', Array(&$version));
139 return implode( ';', $version);
143 * Convert metadata version.
145 * By default just returns $metadata, but can be used to allow
146 * media handlers to convert between metadata versions.
148 * @param $metadata Mixed String or Array metadata array (serialized if string)
149 * @param $version Integer target version
150 * @return Array serialized metadata in specified version, or $metadata on fail.
152 function convertMetadataVersion( $metadata, $version = 1 ) {
153 if ( !is_array( $metadata ) ) {
155 //unserialize to keep return parameter consistent.
156 wfSuppressWarnings();
157 $ret = unserialize( $metadata );
165 * Get a string describing the type of metadata, for display purposes.
169 function getMetadataType( $image ) { return false; }
172 * Check if the metadata string is valid for this handler.
173 * If it returns MediaHandler::METADATA_BAD (or false), Image
174 * will reload the metadata from the file and update the database.
175 * MediaHandler::METADATA_GOOD for if the metadata is a-ok,
176 * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards
177 * compatible (which may or may not trigger a metadata reload).
180 function isMetadataValid( $image, $metadata ) {
181 return self
::METADATA_GOOD
;
186 * Get a MediaTransformOutput object representing an alternate of the transformed
187 * output which will call an intermediary thumbnail assist script.
189 * Used when the repository has a thumbnailScriptUrl option configured.
191 * Return false to fall back to the regular getTransform().
194 function getScriptedTransform( $image, $script, $params ) {
199 * Get a MediaTransformOutput object representing the transformed output. Does not
200 * actually do the transform.
202 * @param $image File: the image object
203 * @param $dstPath String: filesystem destination path
204 * @param $dstUrl String: Destination URL to use in output HTML
205 * @param $params Array: Arbitrary set of parameters validated by $this->validateParam()
206 * @return MediaTransformOutput
208 final function getTransform( $image, $dstPath, $dstUrl, $params ) {
209 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
213 * Get a MediaTransformOutput object representing the transformed output. Does the
214 * transform unless $flags contains self::TRANSFORM_LATER.
216 * @param $image File: the image object
217 * @param $dstPath String: filesystem destination path
218 * @param $dstUrl String: destination URL to use in output HTML
219 * @param $params Array: arbitrary set of parameters validated by $this->validateParam()
220 * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER
222 * @return MediaTransformOutput
224 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
227 * Get the thumbnail extension and MIME type for a given source MIME type
228 * @return array thumbnail extension and MIME type
230 function getThumbType( $ext, $mime, $params = null ) {
231 $magic = MimeMagic
::singleton();
232 if ( !$ext ||
$magic->isMatchingExtension( $ext, $mime ) === false ) {
233 // The extension is not valid for this mime type and we do
234 // recognize the mime type
235 $extensions = $magic->getExtensionsForType( $mime );
237 return array( strtok( $extensions, ' ' ), $mime );
241 // The extension is correct (true) or the mime type is unknown to
243 return array( $ext, $mime );
247 * True if the handled types can be transformed
250 function canRender( $file ) { return true; }
252 * True if handled types cannot be displayed directly in a browser
253 * but can be rendered
256 function mustRender( $file ) { return false; }
258 * True if the type has multi-page capabilities
261 function isMultiPage( $file ) { return false; }
263 * Page count for a multi-page document, false if unsupported or unknown
266 function pageCount( $file ) { return false; }
268 * The material is vectorized and thus scaling is lossless
271 function isVectorized( $file ) { return false; }
273 * False if the handler is disabled for all files
276 function isEnabled() { return true; }
279 * Get an associative array of page dimensions
280 * Currently "width" and "height" are understood, but this might be
281 * expanded in the future.
282 * Returns false if unknown or if the document is not multi-page.
285 * @param $page Unused, left for backcompatibility?
288 function getPageDimensions( $image, $page ) {
289 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
297 * Generic getter for text layer.
298 * Currently overloaded by PDF and DjVu handlers
301 function getPageText( $image, $page ) {
306 * Get an array structure that looks like this:
309 * 'visible' => array(
310 * 'Human-readable name' => 'Human readable value',
313 * 'collapsed' => array(
314 * 'Human-readable name' => 'Human readable value',
318 * The UI will format this into a table where the visible fields are always
319 * visible, and the collapsed fields are optionally visible.
321 * The function should return false if there is no metadata to display.
325 * @todo FIXME: I don't really like this interface, it's not very flexible
326 * I think the media handler should generate HTML instead. It can do
327 * all the formatting according to some standard. That makes it possible
328 * to do things like visual indication of grouped and chained streams
329 * in ogg container files.
332 function formatMetadata( $image ) {
336 /** sorts the visible/invisible field.
337 * Split off from ImageHandler::formatMetadata, as used by more than
338 * one type of handler.
340 * This is used by the media handlers that use the FormatMetadata class
342 * @param $metadataArray Array metadata array
343 * @return array for use displaying metadata.
345 function formatMetadataHelper( $metadataArray ) {
347 'visible' => array(),
348 'collapsed' => array()
351 $formatted = FormatMetadata
::getFormattedData( $metadataArray );
352 // Sort fields into visible and collapsed
353 $visibleFields = $this->visibleMetadataFields();
354 foreach ( $formatted as $name => $value ) {
355 $tag = strtolower( $name );
356 self
::addMeta( $result,
357 in_array( $tag, $visibleFields ) ?
'visible' : 'collapsed',
367 * Get a list of metadata items which should be displayed when
368 * the metadata table is collapsed.
370 * @return array of strings
373 function visibleMetadataFields() {
375 $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) );
376 foreach( $lines as $line ) {
378 if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
379 $fields[] = $matches[1];
382 $fields = array_map( 'strtolower', $fields );
388 * This is used to generate an array element for each metadata value
389 * That array is then used to generate the table of metadata values
392 * @param &$array Array An array containing elements for each type of visibility
393 * and each of those elements being an array of metadata items. This function adds
394 * a value to that array.
395 * @param $visibility string ('visible' or 'collapsed') if this value is hidden
397 * @param $type String type of metadata tag (currently always 'exif')
398 * @param $id String the name of the metadata tag (like 'artist' for example).
399 * its name in the table displayed is the message "$type-$id" (Ex exif-artist ).
400 * @param $value String thingy goes into a wikitext table; it used to be escaped but
401 * that was incompatible with previous practise of customized display
402 * with wikitext formatting via messages such as 'exif-model-value'.
403 * So the escaping is taken back out, but generally this seems a confusing
405 * @param $param String value to pass to the message for the name of the field
406 * as $1. Currently this parameter doesn't seem to ever be used.
408 * Note, everything here is passed through the parser later on (!)
410 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
411 $msg = wfMessage( "$type-$id", $param );
412 if ( $msg->exists() ) {
413 $name = $msg->text();
415 // This is for future compatibility when using instant commons.
416 // So as to not display as ugly a name if a new metadata
417 // property is defined that we don't know about
418 // (not a major issue since such a property would be collapsed
420 wfDebug( __METHOD__
. ' Unknown metadata name: ' . $id . "\n" );
421 $name = wfEscapeWikiText( $id );
423 $array[$visibility][] = array(
434 function getShortDesc( $file ) {
436 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
443 function getLongDesc( $file ) {
445 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
446 $file->getMimeType() )->parse();
453 static function getGeneralShortDesc( $file ) {
455 return $wgLang->formatSize( $file->getSize() );
462 static function getGeneralLongDesc( $file ) {
464 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
465 $file->getMimeType() )->parse();
469 * Calculate the largest thumbnail width for a given original file size
470 * such that the thumbnail's height is at most $maxHeight.
471 * @param $boxWidth Integer Width of the thumbnail box.
472 * @param $boxHeight Integer Height of the thumbnail box.
473 * @param $maxHeight Integer Maximum height expected for the thumbnail.
476 public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
477 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
478 $roundedUp = ceil( $idealWidth );
479 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
480 return floor( $idealWidth );
486 function getDimensionsString( $file ) {
491 * Modify the parser object post-transform
493 function parserTransformHook( $parser, $file ) {}
496 * File validation hook called on upload.
498 * If the file at the given local path is not valid, or its MIME type does not
499 * match the handler class, a Status object should be returned containing
502 * @param $fileName string The local path to the file.
503 * @return Status object
505 function verifyUpload( $fileName ) {
506 return Status
::newGood();
510 * Check for zero-sized thumbnails. These can be generated when
511 * no disk space is available or some other error occurs
513 * @param $dstPath string The location of the suspect file
514 * @param $retval int Return value of some shell process, file will be deleted if this is non-zero
515 * @return bool True if removed, false otherwise
517 function removeBadFile( $dstPath, $retval = 0 ) {
518 if( file_exists( $dstPath ) ) {
519 $thumbstat = stat( $dstPath );
520 if( $thumbstat['size'] == 0 ||
$retval != 0 ) {
521 $result = unlink( $dstPath );
524 wfDebugLog( 'thumbnail',
525 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
526 $thumbstat['size'], $dstPath ) );
528 wfDebugLog( 'thumbnail',
529 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
530 $thumbstat['size'], $dstPath ) );
539 * Remove files from the purge list
541 * @param array $files
542 * @param array $options
544 public function filterThumbnailPurgeList( &$files, $options ) {
550 * Media handler abstract base class for images
554 abstract class ImageHandler
extends MediaHandler
{
560 function canRender( $file ) {
561 return ( $file->getWidth() && $file->getHeight() );
564 function getParamMap() {
565 return array( 'img_width' => 'width' );
568 function validateParam( $name, $value ) {
569 if ( in_array( $name, array( 'width', 'height' ) ) ) {
580 function makeParamString( $params ) {
581 if ( isset( $params['physicalWidth'] ) ) {
582 $width = $params['physicalWidth'];
583 } elseif ( isset( $params['width'] ) ) {
584 $width = $params['width'];
586 throw new MWException( 'No width specified to '.__METHOD__
);
588 # Removed for ProofreadPage
589 #$width = intval( $width );
593 function parseParamString( $str ) {
595 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
596 return array( 'width' => $m[1] );
602 function getScriptParams( $params ) {
603 return array( 'width' => $params['width'] );
611 function normaliseParams( $image, &$params ) {
612 $mimeType = $image->getMimeType();
614 if ( !isset( $params['width'] ) ) {
618 if ( !isset( $params['page'] ) ) {
621 if ( $params['page'] > $image->pageCount() ) {
622 $params['page'] = $image->pageCount();
625 if ( $params['page'] < 1 ) {
630 $srcWidth = $image->getWidth( $params['page'] );
631 $srcHeight = $image->getHeight( $params['page'] );
633 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
634 # Height & width were both set
635 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
636 # Height is the relative smaller dimension, so scale width accordingly
637 $params['width'] = self
::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
639 if ( $params['width'] == 0 ) {
640 # Very small image, so we need to rely on client side scaling :(
641 $params['width'] = 1;
644 $params['physicalWidth'] = $params['width'];
646 # Height was crap, unset it so that it will be calculated later
647 unset( $params['height'] );
651 if ( !isset( $params['physicalWidth'] ) ) {
652 # Passed all validations, so set the physicalWidth
653 $params['physicalWidth'] = $params['width'];
656 # Because thumbs are only referred to by width, the height always needs
657 # to be scaled by the width to keep the thumbnail sizes consistent,
658 # even if it was set inside the if block above
659 $params['physicalHeight'] = File
::scaleHeight( $srcWidth, $srcHeight,
660 $params['physicalWidth'] );
662 # Set the height if it was not validated in the if block higher up
663 if ( !isset( $params['height'] ) ||
$params['height'] == -1 ) {
664 $params['height'] = $params['physicalHeight'];
668 if ( !$this->validateThumbParams( $params['physicalWidth'],
669 $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) {
676 * Validate thumbnail parameters and fill in the correct height
678 * @param $width Integer: specified width (input/output)
679 * @param $height Integer: height (output only)
680 * @param $srcWidth Integer: width of the source image
681 * @param $srcHeight Integer: height of the source image
683 * @return bool False to indicate that an error should be returned to the user.
685 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
686 $width = intval( $width );
688 # Sanity check $width
690 wfDebug( __METHOD__
.": Invalid destination width: $width\n" );
693 if ( $srcWidth <= 0 ) {
694 wfDebug( __METHOD__
.": Invalid source width: $srcWidth\n" );
698 $height = File
::scaleHeight( $srcWidth, $srcHeight, $width );
699 if ( $height == 0 ) {
700 # Force height to be at least 1 pixel
710 * @return bool|ThumbnailImage
712 function getScriptedTransform( $image, $script, $params ) {
713 if ( !$this->normaliseParams( $image, $params ) ) {
716 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
717 $page = isset( $params['page'] ) ?
$params['page'] : false;
719 if( $image->mustRender() ||
$params['width'] < $image->getWidth() ) {
720 return new ThumbnailImage( $image,
721 $url, $params['width'], $params['height'], false, $page );
725 function getImageSize( $image, $path ) {
726 wfSuppressWarnings();
727 $gis = getimagesize( $path );
732 function isAnimatedImage( $image ) {
740 function getShortDesc( $file ) {
742 $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
743 $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped();
745 return "$widthheight ($nbytes)";
752 function getLongDesc( $file ) {
754 $pages = $file->pageCount();
755 $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
756 if ( $pages === false ||
$pages <= 1 ) {
757 $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
758 $file->getHeight() )->params( $size,
759 $file->getMimeType() )->parse();
761 $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
762 $file->getHeight() )->params( $size,
763 $file->getMimeType() )->numParams( $pages )->parse();
772 function getDimensionsString( $file ) {
773 $pages = $file->pageCount();
775 return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
777 return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text();