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
00186 function getPageText( $image, $page ) {
00187 return false;
00188 }
00189
00216 function formatMetadata( $image ) {
00217 return false;
00218 }
00219
00228 protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00229 $array[$visibility][] = array(
00230 'id' => "$type-$id",
00231 'name' => wfMsg( "$type-$id", $param ),
00232 'value' => $value
00233 );
00234 }
00235
00236 function getShortDesc( $file ) {
00237 global $wgLang;
00238 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
00239 $wgLang->formatNum( $file->getSize() ) ) . ')';
00240 return "$nbytes";
00241 }
00242
00243 function getLongDesc( $file ) {
00244 global $wgUser;
00245 $sk = $wgUser->getSkin();
00246 return wfMsgExt( 'file-info', 'parseinline',
00247 $sk->formatSize( $file->getSize() ),
00248 $file->getMimeType() );
00249 }
00250
00251 static function getGeneralShortDesc( $file ) {
00252 global $wgLang;
00253 $nbytes = '(' . wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
00254 $wgLang->formatNum( $file->getSize() ) ) . ')';
00255 return "$nbytes";
00256 }
00257
00258 static function getGeneralLongDesc( $file ) {
00259 global $wgUser;
00260 $sk = $wgUser->getSkin();
00261 return wfMsgExt( 'file-info', 'parseinline',
00262 $sk->formatSize( $file->getSize() ),
00263 $file->getMimeType() );
00264 }
00265
00266 function getDimensionsString( $file ) {
00267 return '';
00268 }
00269
00273 function parserTransformHook( $parser, $file ) {}
00274
00283 function removeBadFile( $dstPath, $retval = 0 ) {
00284 if( file_exists( $dstPath ) ) {
00285 $thumbstat = stat( $dstPath );
00286 if( $thumbstat['size'] == 0 || $retval != 0 ) {
00287 wfDebugLog( 'thumbnail',
00288 sprintf( 'Removing bad %d-byte thumbnail "%s"',
00289 $thumbstat['size'], $dstPath ) );
00290 unlink( $dstPath );
00291 return true;
00292 }
00293 }
00294 return false;
00295 }
00296 }
00297
00303 abstract class ImageHandler extends MediaHandler {
00304 function canRender( $file ) {
00305 if ( $file->getWidth() && $file->getHeight() ) {
00306 return true;
00307 } else {
00308 return false;
00309 }
00310 }
00311
00312 function getParamMap() {
00313 return array( 'img_width' => 'width' );
00314 }
00315
00316 function validateParam( $name, $value ) {
00317 if ( in_array( $name, array( 'width', 'height' ) ) ) {
00318 if ( $value <= 0 ) {
00319 return false;
00320 } else {
00321 return true;
00322 }
00323 } else {
00324 return false;
00325 }
00326 }
00327
00328 function makeParamString( $params ) {
00329 if ( isset( $params['physicalWidth'] ) ) {
00330 $width = $params['physicalWidth'];
00331 } elseif ( isset( $params['width'] ) ) {
00332 $width = $params['width'];
00333 } else {
00334 throw new MWException( 'No width specified to '.__METHOD__ );
00335 }
00336 # Removed for ProofreadPage
00337 #$width = intval( $width );
00338 return "{$width}px";
00339 }
00340
00341 function parseParamString( $str ) {
00342 $m = false;
00343 if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
00344 return array( 'width' => $m[1] );
00345 } else {
00346 return false;
00347 }
00348 }
00349
00350 function getScriptParams( $params ) {
00351 return array( 'width' => $params['width'] );
00352 }
00353
00354 function normaliseParams( $image, &$params ) {
00355 $mimeType = $image->getMimeType();
00356
00357 if ( !isset( $params['width'] ) ) {
00358 return false;
00359 }
00360 if ( !isset( $params['page'] ) ) {
00361 $params['page'] = 1;
00362 }
00363 $srcWidth = $image->getWidth( $params['page'] );
00364 $srcHeight = $image->getHeight( $params['page'] );
00365 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
00366 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
00367 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
00368 }
00369 }
00370 $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
00371 if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
00372 return false;
00373 }
00374 return true;
00375 }
00376
00380 function getTransform( $image, $dstPath, $dstUrl, $params ) {
00381 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00382 }
00383
00391 function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
00392 $width = intval( $width );
00393
00394 # Sanity check $width
00395 if( $width <= 0) {
00396 wfDebug( __METHOD__.": Invalid destination width: $width\n" );
00397 return false;
00398 }
00399 if ( $srcWidth <= 0 ) {
00400 wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" );
00401 return false;
00402 }
00403
00404 $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
00405 return true;
00406 }
00407
00408 function getScriptedTransform( $image, $script, $params ) {
00409 if ( !$this->normaliseParams( $image, $params ) ) {
00410 return false;
00411 }
00412 $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) );
00413 $page = isset( $params['page'] ) ? $params['page'] : false;
00414
00415 if( $image->mustRender() || $params['width'] < $image->getWidth() ) {
00416 return new ThumbnailImage( $image, $url, $params['width'], $params['height'], $page );
00417 }
00418 }
00419
00420 function getImageSize( $image, $path ) {
00421 wfSuppressWarnings();
00422 $gis = getimagesize( $path );
00423 wfRestoreWarnings();
00424 return $gis;
00425 }
00426
00427 function getShortDesc( $file ) {
00428 global $wgLang;
00429 $nbytes = wfMsgExt( 'nbytes', array( 'parsemag', 'escape' ),
00430 $wgLang->formatNum( $file->getSize() ) );
00431 $widthheight = wfMsgHtml( 'widthheight', $wgLang->formatNum( $file->getWidth() ) ,$wgLang->formatNum( $file->getHeight() ) );
00432
00433 return "$widthheight ($nbytes)";
00434 }
00435
00436 function getLongDesc( $file ) {
00437 global $wgLang;
00438 return wfMsgExt('file-info-size', 'parseinline',
00439 $wgLang->formatNum( $file->getWidth() ),
00440 $wgLang->formatNum( $file->getHeight() ),
00441 $wgLang->formatSize( $file->getSize() ),
00442 $file->getMimeType() );
00443 }
00444
00445 function getDimensionsString( $file ) {
00446 global $wgLang;
00447 $pages = $file->pageCount();
00448 $width = $wgLang->formatNum( $file->getWidth() );
00449 $height = $wgLang->formatNum( $file->getHeight() );
00450 $pagesFmt = $wgLang->formatNum( $pages );
00451
00452 if ( $pages > 1 ) {
00453 return wfMsgExt( 'widthheightpage', 'parsemag', $width, $height, $pagesFmt );
00454 } else {
00455 return wfMsg( 'widthheight', $width, $height );
00456 }
00457 }
00458 }