mediaHandler可以理解为处理media文件的
/includes/filerepo/file/File.php /** * Get a MediaHandler instance for this file * * @return MediaHandler|bool Registered MediaHandler for file's MIME type * or false if none found */ function getHandler() { if ( !isset( $this->handler ) ) { $this->handler = MediaHandler::getHandler( $this->getMimeType() ); } return $this->handler; }
getHandler()代码:
includes/media/MediaHandler.php /** * Get a MediaHandler for a given MIME type from the instance cache * * @param string $type * @return MediaHandler */ static function getHandler( $type ) { global $wgMediaHandlers; if ( !isset( $wgMediaHandlers[$type] ) ) { wfDebug( __METHOD__ . ": no handler found for $type. " ); return false; } $class = $wgMediaHandlers[$type]; if ( !isset( self::$handlers[$class] ) ) { self::$handlers[$class] = new $class; if ( !self::$handlers[$class]->isEnabled() ) { self::$handlers[$class] = false; } } return self::$handlers[$class]; }
$class = $wgMediaHandlers[$type];
创建一个MIME-MediaHandler 的类的临时对象。
请看全局变量wgMediaHandlers:
/** * Plugins for media file type handling. * Each entry in the array maps a MIME type to a class name */ $wgMediaHandlers = array( 'image/jpeg' => 'JpegHandler', 'image/png' => 'PNGHandler', 'image/gif' => 'GIFHandler', 'image/tiff' => 'TiffHandler', 'image/x-ms-bmp' => 'BmpHandler', 'image/x-bmp' => 'BmpHandler', 'image/x-xcf' => 'XCFHandler', 'image/svg+xml' => 'SvgHandler', // official 'image/svg' => 'SvgHandler', // compat 'image/vnd.djvu' => 'DjVuHandler', // official 'image/x.djvu' => 'DjVuHandler', // compat 'image/x-djvu' => 'DjVuHandler', // compat );
比如JpegHandler:
includes/media/Jpeg.php class JpegHandler extends ExifBitmapHandler { function getMetadata( $image, $filename ) { } }
请看UML 图:
再看更详细的JpegHandler:
关于thumb 对象的创建,handler 对象在渲染完成thumb 后,执
行dotransform,创建一个thumb 文件对象。以下代码是生成指
令。
Transform()方法的部分代码:
/includes/filerepo/file/File.php // Actually render the thumbnail... wfProfileIn( __METHOD__ . '-doTransform' ); $thumb = $handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params ); wfProfileOut( __METHOD__ . '-doTransform' ); $tmpFile->bind( $thumb ); // keep alive with $thumb
透过UML 图可以猜测到thumb 对象类是一个
mediatransformoutput 的类型,相比其MIME 类型有关。我们在
MediaHandler 的抽象类中只会看到一个抽象方法而无方法体。
/** * Get a MediaTransformOutput object representing the transformed output. Does the * transform unless $flags contains self::TRANSFORM_LATER. * * @param File $image The image object * @param string $dstPath Filesystem destination path * @param string $dstUrl Destination URL to use in output HTML * @param array $params Arbitrary set of parameters validated by $this->validateParam() * Note: These parameters have *not* gone through $this->normaliseParams() * @param int $flags A bitfield, may contain self::TRANSFORM_LATER * @return MediaTransformOutput */ abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
再来看JpegHandler UML 图就一目了然。