95e856817ebbd857ca7928939675c9549a69fc77
4 * Media-handling base classes and generic functionality
8 * Base media handler class
12 abstract class MediaHandler
{
13 const TRANSFORM_LATER
= 1;
18 static $handlers = array();
21 * Get a MediaHandler for a given MIME type from the instance cache
23 static function getHandler( $type ) {
24 global $wgMediaHandlers;
25 if ( !isset( $wgMediaHandlers[$type] ) ) {
26 wfDebug( __METHOD__
. ": no handler found for $type.\n");
29 $class = $wgMediaHandlers[$type];
30 if ( !isset( self
::$handlers[$class] ) ) {
31 self
::$handlers[$class] = new $class;
32 if ( !self
::$handlers[$class]->isEnabled() ) {
33 self
::$handlers[$class] = false;
36 return self
::$handlers[$class];
40 * Validate a thumbnail parameter at parse time.
41 * Return true to accept the parameter, and false to reject it.
42 * If you return false, the parser will do something quiet and forgiving.
44 abstract function validateParam( $name, $value );
47 * Merge a parameter array into a string appropriate for inclusion in filenames
49 abstract function makeParamString( $params );
52 * Parse a param string made with makeParamString back into an array
54 abstract function parseParamString( $str );
57 * Changes the parameter array as necessary, ready for transformation.
58 * Should be idempotent.
59 * Returns false if the parameters are unacceptable and the transform should fail
61 abstract function normaliseParams( $image, &$params );
64 * Get an image size array like that returned by getimagesize(), or false if it
65 * can't be determined.
67 * @param Image $image The image object, or false if there isn't one
68 * @param string $fileName The filename
71 abstract function getImageSize( $image, $path );
74 * Get handler-specific metadata which will be saved in the img_metadata field.
76 * @param Image $image The image object, or false if there isn't one
77 * @param string $fileName The filename
80 function getMetadata( $image, $path ) { return ''; }
83 * Get a string describing the type of metadata, for display purposes.
85 function getMetadataType( $image ) { return false; }
88 * Check if the metadata string is valid for this handler.
89 * If it returns false, Image will reload the metadata from the file and update the database
91 function isMetadataValid( $image, $metadata ) { return true; }
94 * Get a MediaTransformOutput object representing the transformed output. Does not
95 * actually do the transform.
97 * @param Image $image The image object
98 * @param string $dstPath Filesystem destination path
99 * @param string $dstUrl Destination URL to use in output HTML
100 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
102 function getTransform( $image, $dstPath, $dstUrl, $params ) {
103 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
107 * Get a MediaTransformOutput object representing the transformed output. Does the
108 * transform unless $flags contains self::TRANSFORM_LATER.
110 * @param Image $image The image object
111 * @param string $dstPath Filesystem destination path
112 * @param string $dstUrl Destination URL to use in output HTML
113 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
114 * @param integer $flags A bitfield, may contain self::TRANSFORM_LATER
116 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
119 * Get the thumbnail extension and MIME type for a given source MIME type
120 * @return array thumbnail extension and MIME type
122 function getThumbType( $ext, $mime ) {
123 return array( $ext, $mime );
127 * True if the handled types can be transformed
129 function canRender() { return true; }
131 * True if handled types cannot be displayed directly in a browser
132 * but can be rendered
134 function mustRender() { return false; }
136 * True if the type has multi-page capabilities
138 function isMultiPage() { return false; }
140 * Page count for a multi-page document, false if unsupported or unknown
142 function pageCount() { return false; }
144 * False if the handler is disabled for all files
146 function isEnabled() { return true; }
149 * Get an associative array of page dimensions
150 * Currently "width" and "height" are understood, but this might be
151 * expanded in the future.
152 * Returns false if unknown or if the document is not multi-page.
154 function getPageDimensions( $image, $page ) {
155 $gis = $this->getImageSize( $image, $image->getPath() );
163 * Get an array structure that looks like this:
166 * 'visible' => array(
167 * 'Human-readable name' => 'Human readable value',
170 * 'collapsed' => array(
171 * 'Human-readable name' => 'Human readable value',
175 * The UI will format this into a table where the visible fields are always
176 * visible, and the collapsed fields are optionally visible.
178 * The function should return false if there is no metadata to display.
180 function formatMetadata( $image, $metadata ) {
184 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
185 $array[$visibility][] = array(
187 'name' => wfMsg( "$type-$id", $param ),
188 'value' => wfEscapeWikiText( $value )
195 * Media handler abstract base class for images
199 abstract class ImageHandler
extends MediaHandler
{
200 function validateParam( $name, $value ) {
201 if ( in_array( $name, array( 'width', 'height' ) ) ) {
212 function makeParamString( $params ) {
213 if ( isset( $params['physicalWidth'] ) ) {
214 $width = $params['physicalWidth'];
215 } elseif ( isset( $params['width'] ) ) {
216 $width = $params['width'];
218 throw new MWException( 'No width specified to '.__METHOD__
);
220 # Removed for ProofreadPage
221 #$width = intval( $width );
225 function parseParamString( $str ) {
227 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
228 return array( 'width' => $m[1] );
234 function getScriptParams( $params ) {
235 return array( 'width' => $params['width'] );
238 function normaliseParams( $image, &$params ) {
239 $mimeType = $image->getMimeType();
241 if ( !isset( $params['width'] ) ) {
244 if ( !isset( $params['page'] ) ) {
247 $srcWidth = $image->getWidth( $params['page'] );
248 $srcHeight = $image->getHeight( $params['page'] );
249 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
250 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
251 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
254 $params['height'] = File
::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
255 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
262 * Get a transform output object without actually doing the transform
264 function getTransform( $image, $dstPath, $dstUrl, $params ) {
265 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
269 * Validate thumbnail parameters and fill in the correct height
271 * @param integer &$width Specified width (input/output)
272 * @param integer &$height Height (output only)
273 * @return false to indicate that an error should be returned to the user.
275 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
276 $width = intval( $width );
278 # Sanity check $width
280 wfDebug( __METHOD__
.": Invalid destination width: $width\n" );
283 if ( $srcWidth <= 0 ) {
284 wfDebug( __METHOD__
.": Invalid source width: $srcWidth\n" );
288 $height = File
::scaleHeight( $srcWidth, $srcHeight, $width );
292 function getScriptedTransform( $image, $script, $params ) {
293 if ( !$this->normaliseParams( $image, $params ) ) {
296 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
297 return new ThumbnailImage( $url, $params['width'], $params['height'] );
301 * Check for zero-sized thumbnails. These can be generated when
302 * no disk space is available or some other error occurs
304 * @param $dstPath The location of the suspect file
305 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
306 * @return true if removed, false otherwise
308 function removeBadFile( $dstPath, $retval = 0 ) {
309 if( file_exists( $dstPath ) ) {
310 $thumbstat = stat( $dstPath );
311 if( $thumbstat['size'] == 0 ||
$retval != 0 ) {
312 wfDebugLog( 'thumbnail',
313 sprintf( 'Removing bad %d-byte thumbnail "%s"',
314 $thumbstat['size'], $dstPath ) );
322 function getImageSize( $image, $path ) {
323 wfSuppressWarnings();
324 $gis = getimagesize( $path );