Localization: Removing right and action messages for the 'restrict' feature, which...
[mediawiki.git] / includes / media / Generic.php
bloba9c681e1464e26938fe404632824956556924f81
1 <?php
2 /**
3 * Media-handling base classes and generic functionality
4 * @file
5 * @ingroup Media
6 */
8 /**
9 * Base media handler class
11 * @ingroup Media
13 abstract class MediaHandler {
14 const TRANSFORM_LATER = 1;
16 /**
17 * Instance cache
19 static $handlers = array();
21 /**
22 * Get a MediaHandler for a given MIME type from the instance cache
24 static function getHandler( $type ) {
25 global $wgMediaHandlers;
26 if ( !isset( $wgMediaHandlers[$type] ) ) {
27 wfDebug( __METHOD__ . ": no handler found for $type.\n");
28 return false;
30 $class = $wgMediaHandlers[$type];
31 if ( !isset( self::$handlers[$class] ) ) {
32 self::$handlers[$class] = new $class;
33 if ( !self::$handlers[$class]->isEnabled() ) {
34 self::$handlers[$class] = false;
37 return self::$handlers[$class];
40 /**
41 * Get an associative array mapping magic word IDs to parameter names.
42 * Will be used by the parser to identify parameters.
44 abstract function getParamMap();
47 * Validate a thumbnail parameter at parse time.
48 * Return true to accept the parameter, and false to reject it.
49 * If you return false, the parser will do something quiet and forgiving.
51 abstract function validateParam( $name, $value );
53 /**
54 * Merge a parameter array into a string appropriate for inclusion in filenames
56 abstract function makeParamString( $params );
58 /**
59 * Parse a param string made with makeParamString back into an array
61 abstract function parseParamString( $str );
63 /**
64 * Changes the parameter array as necessary, ready for transformation.
65 * Should be idempotent.
66 * Returns false if the parameters are unacceptable and the transform should fail
68 abstract function normaliseParams( $image, &$params );
70 /**
71 * Get an image size array like that returned by getimagesize(), or false if it
72 * can't be determined.
74 * @param Image $image The image object, or false if there isn't one
75 * @param string $fileName The filename
76 * @return array
78 abstract function getImageSize( $image, $path );
80 /**
81 * Get handler-specific metadata which will be saved in the img_metadata field.
83 * @param Image $image The image object, or false if there isn't one
84 * @param string $fileName The filename
85 * @return string
87 function getMetadata( $image, $path ) { return ''; }
89 /**
90 * Get a string describing the type of metadata, for display purposes.
92 function getMetadataType( $image ) { return false; }
94 /**
95 * Check if the metadata string is valid for this handler.
96 * If it returns false, Image will reload the metadata from the file and update the database
98 function isMetadataValid( $image, $metadata ) { return true; }
102 * Get a MediaTransformOutput object representing an alternate of the transformed
103 * output which will call an intermediary thumbnail assist script.
105 * Used when the repository has a thumbnailScriptUrl option configured.
107 * Return false to fall back to the regular getTransform().
109 function getScriptedTransform( $image, $script, $params ) {
110 return false;
114 * Get a MediaTransformOutput object representing the transformed output. Does not
115 * actually do the transform.
117 * @param Image $image The image object
118 * @param string $dstPath Filesystem destination path
119 * @param string $dstUrl Destination URL to use in output HTML
120 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
122 function getTransform( $image, $dstPath, $dstUrl, $params ) {
123 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
127 * Get a MediaTransformOutput object representing the transformed output. Does the
128 * transform unless $flags contains self::TRANSFORM_LATER.
130 * @param Image $image The image object
131 * @param string $dstPath Filesystem destination path
132 * @param string $dstUrl Destination URL to use in output HTML
133 * @param array $params Arbitrary set of parameters validated by $this->validateParam()
134 * @param integer $flags A bitfield, may contain self::TRANSFORM_LATER
136 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
139 * Get the thumbnail extension and MIME type for a given source MIME type
140 * @return array thumbnail extension and MIME type
142 function getThumbType( $ext, $mime ) {
143 return array( $ext, $mime );
147 * True if the handled types can be transformed
149 function canRender( $file ) { return true; }
151 * True if handled types cannot be displayed directly in a browser
152 * but can be rendered
154 function mustRender( $file ) { return false; }
156 * True if the type has multi-page capabilities
158 function isMultiPage( $file ) { return false; }
160 * Page count for a multi-page document, false if unsupported or unknown
162 function pageCount( $file ) { return false; }
164 * False if the handler is disabled for all files
166 function isEnabled() { return true; }
169 * Get an associative array of page dimensions
170 * Currently "width" and "height" are understood, but this might be
171 * expanded in the future.
172 * Returns false if unknown or if the document is not multi-page.
174 function getPageDimensions( $image, $page ) {
175 $gis = $this->getImageSize( $image, $image->getPath() );
176 return array(
177 'width' => $gis[0],
178 'height' => $gis[1]
183 * Get an array structure that looks like this:
185 * array(
186 * 'visible' => array(
187 * 'Human-readable name' => 'Human readable value',
188 * ...
189 * ),
190 * 'collapsed' => array(
191 * 'Human-readable name' => 'Human readable value',
192 * ...
195 * The UI will format this into a table where the visible fields are always
196 * visible, and the collapsed fields are optionally visible.
198 * The function should return false if there is no metadata to display.
202 * FIXME: I don't really like this interface, it's not very flexible
203 * I think the media handler should generate HTML instead. It can do
204 * all the formatting according to some standard. That makes it possible
205 * to do things like visual indication of grouped and chained streams
206 * in ogg container files.
208 function formatMetadata( $image ) {
209 return false;
213 * @fixme document this!
214 * 'value' thingy goes into a wikitext table; it used to be escaped but
215 * that was incompatible with previous practice of customized display
216 * with wikitext formatting via messages such as 'exif-model-value'.
217 * So the escaping is taken back out, but generally this seems a confusing
218 * interface.
220 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
221 $array[$visibility][] = array(
222 'id' => "$type-$id",
223 'name' => wfMsg( "$type-$id", $param ),
224 'value' => $value
228 function getShortDesc( $file ) {
229 global $wgLang;
230 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
231 $wgLang->formatNum( $file->getSize() ) ) . ')';
232 return "$nbytes";
235 function getLongDesc( $file ) {
236 global $wgUser;
237 $sk = $wgUser->getSkin();
238 return wfMsgExt( 'file-info', 'parseinline',
239 $sk->formatSize( $file->getSize() ),
240 $file->getMimeType() );
243 static function getGeneralShortDesc( $file ) {
244 global $wgLang;
245 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
246 $wgLang->formatNum( $file->getSize() ) ) . ')';
247 return "$nbytes";
250 static function getGeneralLongDesc( $file ) {
251 global $wgUser;
252 $sk = $wgUser->getSkin();
253 return wfMsgExt( 'file-info', 'parseinline',
254 $sk->formatSize( $file->getSize() ),
255 $file->getMimeType() );
258 function getDimensionsString( $file ) {
259 return '';
263 * Modify the parser object post-transform
265 function parserTransformHook( $parser, $file ) {}
268 * Check for zero-sized thumbnails. These can be generated when
269 * no disk space is available or some other error occurs
271 * @param $dstPath The location of the suspect file
272 * @param $retval Return value of some shell process, file will be deleted if this is non-zero
273 * @return true if removed, false otherwise
275 function removeBadFile( $dstPath, $retval = 0 ) {
276 if( file_exists( $dstPath ) ) {
277 $thumbstat = stat( $dstPath );
278 if( $thumbstat['size'] == 0 || $retval != 0 ) {
279 wfDebugLog( 'thumbnail',
280 sprintf( 'Removing bad %d-byte thumbnail "%s"',
281 $thumbstat['size'], $dstPath ) );
282 unlink( $dstPath );
283 return true;
286 return false;
291 * Media handler abstract base class for images
293 * @ingroup Media
295 abstract class ImageHandler extends MediaHandler {
296 function canRender( $file ) {
297 if ( $file->getWidth() && $file->getHeight() ) {
298 return true;
299 } else {
300 return false;
304 function getParamMap() {
305 return array( 'img_width' => 'width' );
308 function validateParam( $name, $value ) {
309 if ( in_array( $name, array( 'width', 'height' ) ) ) {
310 if ( $value <= 0 ) {
311 return false;
312 } else {
313 return true;
315 } else {
316 return false;
320 function makeParamString( $params ) {
321 if ( isset( $params['physicalWidth'] ) ) {
322 $width = $params['physicalWidth'];
323 } elseif ( isset( $params['width'] ) ) {
324 $width = $params['width'];
325 } else {
326 throw new MWException( 'No width specified to '.__METHOD__ );
328 # Removed for ProofreadPage
329 #$width = intval( $width );
330 return "{$width}px";
333 function parseParamString( $str ) {
334 $m = false;
335 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
336 return array( 'width' => $m[1] );
337 } else {
338 return false;
342 function getScriptParams( $params ) {
343 return array( 'width' => $params['width'] );
346 function normaliseParams( $image, &$params ) {
347 $mimeType = $image->getMimeType();
349 if ( !isset( $params['width'] ) ) {
350 return false;
352 if ( !isset( $params['page'] ) ) {
353 $params['page'] = 1;
355 $srcWidth = $image->getWidth( $params['page'] );
356 $srcHeight = $image->getHeight( $params['page'] );
357 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
358 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
359 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
362 $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
363 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
364 return false;
366 return true;
370 * Get a transform output object without actually doing the transform
372 function getTransform( $image, $dstPath, $dstUrl, $params ) {
373 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
377 * Validate thumbnail parameters and fill in the correct height
379 * @param integer &$width Specified width (input/output)
380 * @param integer &$height Height (output only)
381 * @return false to indicate that an error should be returned to the user.
383 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
384 $width = intval( $width );
386 # Sanity check $width
387 if( $width <= 0) {
388 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
389 return false;
391 if ( $srcWidth <= 0 ) {
392 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
393 return false;
396 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
397 return true;
400 function getScriptedTransform( $image, $script, $params ) {
401 if ( !$this->normaliseParams( $image, $params ) ) {
402 return false;
404 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
405 $page = isset( $params['page'] ) ? $params['page'] : false;
407 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
408 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
412 function getImageSize( $image, $path ) {
413 wfSuppressWarnings();
414 $gis = getimagesize( $path );
415 wfRestoreWarnings();
416 return $gis;
419 function getShortDesc( $file ) {
420 global $wgLang;
421 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
422 $wgLang->formatNum( $file->getSize() ) );
423 $widthheight = wfMsgHtml( 'widthheight', $wgLang->formatNum( $file->getWidth() ) ,$wgLang->formatNum( $file->getHeight() ) );
425 return "$widthheight ($nbytes)";
428 function getLongDesc( $file ) {
429 global $wgLang;
430 return wfMsgExt('file-info-size', 'parseinline',
431 $wgLang->formatNum( $file->getWidth() ),
432 $wgLang->formatNum( $file->getHeight() ),
433 $wgLang->formatSize( $file->getSize() ),
434 $file->getMimeType() );
437 function getDimensionsString( $file ) {
438 global $wgLang;
439 $pages = $file->pageCount();
440 $width = $wgLang->formatNum( $file->getWidth() );
441 $height = $wgLang->formatNum( $file->getHeight() );
442 $pagesFmt = $wgLang->formatNum( $pages );
444 if ( $pages > 1 ) {
445 return wfMsgExt( 'widthheightpage', 'parsemag', $width, $height, $pagesFmt );
446 } else {
447 return wfMsg( 'widthheight', $width, $height );