00001 <?php
00013 abstract class MediaHandler {
00014 const TRANSFORM_LATER = 1;
00015
00019 static $handlers = array();
00020
00024 static function getHandler( $type ) {
00025 global $wgMediaHandlers;
00026 if ( !isset( $wgMediaHandlers[$type] ) ) {
00027 wfDebug( __METHOD__ . ": no handler found for $type.\n");
00028 return false;
00029 }
00030 $class = $wgMediaHandlers[$type];
00031 if ( !isset( self::$handlers[$class] ) ) {
00032 self::$handlers[$class] = new $class;
00033 if ( !self::$handlers[$class]->isEnabled() ) {
00034 self::$handlers[$class] = false;
00035 }
00036 }
00037 return self::$handlers[$class];
00038 }
00039
00044 abstract function getParamMap();
00045
00046
00047
00048
00049
00050
00051 abstract function validateParam( $name, $value );
00052
00056 abstract function makeParamString( $params );
00057
00061 abstract function parseParamString( $str );
00062
00068 abstract function normaliseParams( $image, &$params );
00069
00078 abstract function getImageSize( $image, $path );
00079
00087 function getMetadata( $image, $path ) { return ''; }
00088
00092 function getMetadataType( $image ) { return false; }
00093
00098 function isMetadataValid( $image, $metadata ) { return true; }
00099
00100
00109 function getScriptedTransform( $image, $script, $params ) {
00110 return false;
00111 }
00112
00122 function getTransform( $image, $dstPath, $dstUrl, $params ) {
00123 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00124 }
00125
00136 abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
00137
00142 function getThumbType( $ext, $mime ) {
00143 return array( $ext, $mime );
00144 }
00145
00149 function canRender( $file ) { return true; }
00154 function mustRender( $file ) { return false; }
00158 function isMultiPage( $file ) { return false; }
00162 function pageCount( $file ) { return false; }
00166 function isEnabled() { return true; }
00167
00174 function getPageDimensions( $image, $page ) {
00175 $gis = $this->getImageSize( $image, $image->getPath() );
00176 return array(
00177 'width' => $gis[0],
00178 'height' => $gis[1]
00179 );
00180 }
00181
00208 function formatMetadata( $image ) {
00209 return false;
00210 }
00211
00220 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00221 $array[$visibility][] = array(
00222 'id' => "$type-$id",
00223 'name' => wfMsg( "$type-$id", $param ),
00224 'value' => $value
00225 );
00226 }
00227
00228 function getShortDesc( $file ) {
00229 global $wgLang;
00230 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
00231 $wgLang->formatNum( $file->getSize() ) ) . ')';
00232 return "$nbytes";
00233 }
00234
00235 function getLongDesc( $file ) {
00236 global $wgUser;
00237 $sk = $wgUser->getSkin();
00238 return wfMsgExt( 'file-info', 'parseinline',
00239 $sk->formatSize( $file->getSize() ),
00240 $file->getMimeType() );
00241 }
00242
00243 static function getGeneralShortDesc( $file ) {
00244 global $wgLang;
00245 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
00246 $wgLang->formatNum( $file->getSize() ) ) . ')';
00247 return "$nbytes";
00248 }
00249
00250 static function getGeneralLongDesc( $file ) {
00251 global $wgUser;
00252 $sk = $wgUser->getSkin();
00253 return wfMsgExt( 'file-info', 'parseinline',
00254 $sk->formatSize( $file->getSize() ),
00255 $file->getMimeType() );
00256 }
00257
00258 function getDimensionsString( $file ) {
00259 return '';
00260 }
00261
00265 function parserTransformHook( $parser, $file ) {}
00266
00275 function removeBadFile( $dstPath, $retval = 0 ) {
00276 if( file_exists( $dstPath ) ) {
00277 $thumbstat = stat( $dstPath );
00278 if( $thumbstat['size'] == 0 || $retval != 0 ) {
00279 wfDebugLog( 'thumbnail',
00280 sprintf( 'Removing bad %d-byte thumbnail "%s"',
00281 $thumbstat['size'], $dstPath ) );
00282 unlink( $dstPath );
00283 return true;
00284 }
00285 }
00286 return false;
00287 }
00288 }
00289
00295 abstract class ImageHandler extends MediaHandler {
00296 function canRender( $file ) {
00297 if ( $file->getWidth() && $file->getHeight() ) {
00298 return true;
00299 } else {
00300 return false;
00301 }
00302 }
00303
00304 function getParamMap() {
00305 return array( 'img_width' => 'width' );
00306 }
00307
00308 function validateParam( $name, $value ) {
00309 if ( in_array( $name, array( 'width', 'height' ) ) ) {
00310 if ( $value <= 0 ) {
00311 return false;
00312 } else {
00313 return true;
00314 }
00315 } else {
00316 return false;
00317 }
00318 }
00319
00320 function makeParamString( $params ) {
00321 if ( isset( $params['physicalWidth'] ) ) {
00322 $width = $params['physicalWidth'];
00323 } elseif ( isset( $params['width'] ) ) {
00324 $width = $params['width'];
00325 } else {
00326 throw new MWException( 'No width specified to '.__METHOD__ );
00327 }
00328 # Removed for ProofreadPage
00329 #$width = intval( $width );
00330 return "{$width}px";
00331 }
00332
00333 function parseParamString( $str ) {
00334 $m = false;
00335 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
00336 return array( 'width' => $m[1] );
00337 } else {
00338 return false;
00339 }
00340 }
00341
00342 function getScriptParams( $params ) {
00343 return array( 'width' => $params['width'] );
00344 }
00345
00346 function normaliseParams( $image, &$params ) {
00347 $mimeType = $image->getMimeType();
00348
00349 if ( !isset( $params['width'] ) ) {
00350 return false;
00351 }
00352 if ( !isset( $params['page'] ) ) {
00353 $params['page'] = 1;
00354 }
00355 $srcWidth = $image->getWidth( $params['page'] );
00356 $srcHeight = $image->getHeight( $params['page'] );
00357 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
00358 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
00359 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
00360 }
00361 }
00362 $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
00363 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
00364 return false;
00365 }
00366 return true;
00367 }
00368
00372 function getTransform( $image, $dstPath, $dstUrl, $params ) {
00373 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00374 }
00375
00383 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
00384 $width = intval( $width );
00385
00386 # Sanity check $width
00387 if( $width <= 0) {
00388 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
00389 return false;
00390 }
00391 if ( $srcWidth <= 0 ) {
00392 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
00393 return false;
00394 }
00395
00396 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
00397 return true;
00398 }
00399
00400 function getScriptedTransform( $image, $script, $params ) {
00401 if ( !$this->normaliseParams( $image, $params ) ) {
00402 return false;
00403 }
00404 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
00405 $page = isset( $params['page'] ) ? $params['page'] : false;
00406
00407 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
00408 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
00409 }
00410 }
00411
00412 function getImageSize( $image, $path ) {
00413 wfSuppressWarnings();
00414 $gis = getimagesize( $path );
00415 wfRestoreWarnings();
00416 return $gis;
00417 }
00418
00419 function getShortDesc( $file ) {
00420 global $wgLang;
00421 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
00422 $wgLang->formatNum( $file->getSize() ) );
00423 $widthheight = wfMsgHtml( 'widthheight', $wgLang->formatNum( $file->getWidth() ) ,$wgLang->formatNum( $file->getHeight() ) );
00424
00425 return "$widthheight ($nbytes)";
00426 }
00427
00428 function getLongDesc( $file ) {
00429 global $wgLang;
00430 return wfMsgExt('file-info-size', 'parseinline',
00431 $wgLang->formatNum( $file->getWidth() ),
00432 $wgLang->formatNum( $file->getHeight() ),
00433 $wgLang->formatSize( $file->getSize() ),
00434 $file->getMimeType() );
00435 }
00436
00437 function getDimensionsString( $file ) {
00438 global $wgLang;
00439 $pages = $file->pageCount();
00440 $width = $wgLang->formatNum( $file->getWidth() );
00441 $height = $wgLang->formatNum( $file->getHeight() );
00442 $pagesFmt = $wgLang->formatNum( $pages );
00443
00444 if ( $pages > 1 ) {
00445 return wfMsgExt( 'widthheightpage', 'parsemag', $width, $height, $pagesFmt );
00446 } else {
00447 return wfMsg( 'widthheight', $width, $height );
00448 }
00449 }
00450 }