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() );
164 * Media handler abstract base class for images
168 abstract class ImageHandler
extends MediaHandler
{
169 function validateParam( $name, $value ) {
170 if ( in_array( $name, array( 'width', 'height' ) ) ) {
181 function makeParamString( $params ) {
182 if ( isset( $params['physicalWidth'] ) ) {
183 $width = $params['physicalWidth'];
184 } elseif ( isset( $params['width'] ) ) {
185 $width = $params['width'];
187 throw new MWException( 'No width specified to '.__METHOD__
);
189 # Removed for ProofreadPage
190 #$width = intval( $width );
194 function parseParamString( $str ) {
196 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
197 return array( 'width' => $m[1] );
203 function getScriptParams( $params ) {
204 return array( 'width' => $params['width'] );
207 function normaliseParams( $image, &$params ) {
208 $mimeType = $image->getMimeType();
210 if ( !isset( $params['width'] ) ) {
213 if ( !isset( $params['page'] ) ) {
216 $srcWidth = $image->getWidth( $params['page'] );
217 $srcHeight = $image->getHeight( $params['page'] );
218 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
219 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
220 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
223 $params['height'] = File
::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
224 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
231 * Get a transform output object without actually doing the transform
233 function getTransform( $image, $dstPath, $dstUrl, $params ) {
234 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self
::TRANSFORM_LATER
);
238 * Validate thumbnail parameters and fill in the correct height
240 * @param integer &$width Specified width (input/output)
241 * @param integer &$height Height (output only)
242 * @return false to indicate that an error should be returned to the user.
244 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
245 $width = intval( $width );
247 # Sanity check $width
249 wfDebug( __METHOD__
.": Invalid destination width: $width\n" );
252 if ( $srcWidth <= 0 ) {
253 wfDebug( __METHOD__
.": Invalid source width: $srcWidth\n" );
257 $height = File
::scaleHeight( $srcWidth, $srcHeight, $width );
261 function getScriptedTransform( $image, $script, $params ) {
262 if ( !$this->normaliseParams( $image, $params ) ) {
265 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
266 return new ThumbnailImage( $url, $params['width'], $params['height'] );
270 * Check for zero-sized thumbnails. These can be generated when
271 * no disk space is available or some other error occurs
273 * @param $dstPath The location of the suspect file
274 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
275 * @return true if removed, false otherwise
277 function removeBadFile( $dstPath, $retval = 0 ) {
278 if( file_exists( $dstPath ) ) {
279 $thumbstat = stat( $dstPath );
280 if( $thumbstat['size'] == 0 ||
$retval != 0 ) {
281 wfDebugLog( 'thumbnail',
282 sprintf( 'Removing bad %d-byte thumbnail "%s"',
283 $thumbstat['size'], $dstPath ) );
291 function getImageSize( $image, $path ) {
292 wfSuppressWarnings();
293 $gis = getimagesize( $path );