MediaWiki  REL1_22
MediaHandler.php
Go to the documentation of this file.
00001 <?php
00029 abstract class MediaHandler {
00030     const TRANSFORM_LATER = 1;
00031     const METADATA_GOOD = true;
00032     const METADATA_BAD = false;
00033     const METADATA_COMPATIBLE = 2; // for old but backwards compatible.
00037     static $handlers = array();
00038 
00046     static function getHandler( $type ) {
00047         global $wgMediaHandlers;
00048         if ( !isset( $wgMediaHandlers[$type] ) ) {
00049             wfDebug( __METHOD__ . ": no handler found for $type.\n" );
00050             return false;
00051         }
00052         $class = $wgMediaHandlers[$type];
00053         if ( !isset( self::$handlers[$class] ) ) {
00054             self::$handlers[$class] = new $class;
00055             if ( !self::$handlers[$class]->isEnabled() ) {
00056                 self::$handlers[$class] = false;
00057             }
00058         }
00059         return self::$handlers[$class];
00060     }
00061 
00066     abstract function getParamMap();
00067 
00076     abstract function validateParam( $name, $value );
00077 
00084     abstract function makeParamString( $params );
00085 
00092     abstract function parseParamString( $str );
00093 
00101     abstract function normaliseParams( $image, &$params );
00102 
00111     abstract function getImageSize( $image, $path );
00112 
00121     function getMetadata( $image, $path ) {
00122         return '';
00123     }
00124 
00140     static function getMetadataVersion() {
00141         $version = Array( '2' ); // core metadata version
00142         wfRunHooks( 'GetMetadataVersion', Array( &$version ) );
00143         return implode( ';', $version );
00144     }
00145 
00156     function convertMetadataVersion( $metadata, $version = 1 ) {
00157         if ( !is_array( $metadata ) ) {
00158 
00159             //unserialize to keep return parameter consistent.
00160             wfSuppressWarnings();
00161             $ret = unserialize( $metadata );
00162             wfRestoreWarnings();
00163             return $ret;
00164         }
00165         return $metadata;
00166     }
00167 
00173     function getMetadataType( $image ) {
00174         return false;
00175     }
00176 
00186     function isMetadataValid( $image, $metadata ) {
00187         return self::METADATA_GOOD;
00188     }
00189 
00199     function getScriptedTransform( $image, $script, $params ) {
00200         return false;
00201     }
00202 
00213     final function getTransform( $image, $dstPath, $dstUrl, $params ) {
00214         return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00215     }
00216 
00230     abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
00231 
00240     function getThumbType( $ext, $mime, $params = null ) {
00241         $magic = MimeMagic::singleton();
00242         if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
00243             // The extension is not valid for this mime type and we do
00244             // recognize the mime type
00245             $extensions = $magic->getExtensionsForType( $mime );
00246             if ( $extensions ) {
00247                 return array( strtok( $extensions, ' ' ), $mime );
00248             }
00249         }
00250 
00251         // The extension is correct (true) or the mime type is unknown to
00252         // MediaWiki (null)
00253         return array( $ext, $mime );
00254     }
00255 
00261     public function getStreamHeaders( $metadata ) {
00262         return array();
00263     }
00264 
00269     function canRender( $file ) {
00270         return true;
00271     }
00272 
00278     function mustRender( $file ) {
00279         return false;
00280     }
00281 
00286     function isMultiPage( $file ) {
00287         return false;
00288     }
00289 
00294     function pageCount( $file ) {
00295         return false;
00296     }
00297 
00302     function isVectorized( $file ) {
00303         return false;
00304     }
00305 
00312     function isAnimatedImage( $file ) {
00313         return false;
00314     }
00315 
00321     function canAnimateThumbnail( $file ) {
00322         return true;
00323     }
00324 
00329     function isEnabled() {
00330         return true;
00331     }
00332 
00349     function getPageDimensions( $image, $page ) {
00350         $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
00351         if ( $gis ) {
00352             return array(
00353                 'width' => $gis[0],
00354                 'height' => $gis[1]
00355             );
00356         } else {
00357             return false;
00358         }
00359     }
00360 
00366     function getPageText( $image, $page ) {
00367         return false;
00368     }
00369 
00397     function formatMetadata( $image ) {
00398         return false;
00399     }
00400 
00410     function formatMetadataHelper( $metadataArray ) {
00411         $result = array(
00412             'visible' => array(),
00413             'collapsed' => array()
00414         );
00415 
00416         $formatted = FormatMetadata::getFormattedData( $metadataArray );
00417         // Sort fields into visible and collapsed
00418         $visibleFields = $this->visibleMetadataFields();
00419         foreach ( $formatted as $name => $value ) {
00420             $tag = strtolower( $name );
00421             self::addMeta( $result,
00422                 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
00423                 'exif',
00424                 $tag,
00425                 $value
00426             );
00427         }
00428         return $result;
00429     }
00430 
00438     function visibleMetadataFields() {
00439         $fields = array();
00440         $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
00441         foreach ( $lines as $line ) {
00442             $matches = array();
00443             if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
00444                 $fields[] = $matches[1];
00445             }
00446         }
00447         $fields = array_map( 'strtolower', $fields );
00448         return $fields;
00449     }
00450 
00474     protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00475         $msg = wfMessage( "$type-$id", $param );
00476         if ( $msg->exists() ) {
00477             $name = $msg->text();
00478         } else {
00479             // This is for future compatibility when using instant commons.
00480             // So as to not display as ugly a name if a new metadata
00481             // property is defined that we don't know about
00482             // (not a major issue since such a property would be collapsed
00483             // by default).
00484             wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
00485             $name = wfEscapeWikiText( $id );
00486         }
00487         $array[$visibility][] = array(
00488             'id' => "$type-$id",
00489             'name' => $name,
00490             'value' => $value
00491         );
00492     }
00493 
00500     function getShortDesc( $file ) {
00501         global $wgLang;
00502         return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00503     }
00504 
00511     function getLongDesc( $file ) {
00512         global $wgLang;
00513         return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
00514             $file->getMimeType() )->parse();
00515     }
00516 
00523     static function getGeneralShortDesc( $file ) {
00524         global $wgLang;
00525         return $wgLang->formatSize( $file->getSize() );
00526     }
00527 
00534     static function getGeneralLongDesc( $file ) {
00535         global $wgLang;
00536         return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
00537             $file->getMimeType() )->parse();
00538     }
00539 
00548     public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
00549         $idealWidth = $boxWidth * $maxHeight / $boxHeight;
00550         $roundedUp = ceil( $idealWidth );
00551         if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
00552             return floor( $idealWidth );
00553         } else {
00554             return $roundedUp;
00555         }
00556     }
00557 
00564     function getDimensionsString( $file ) {
00565         return '';
00566     }
00567 
00578     function parserTransformHook( $parser, $file ) {}
00579 
00590     function verifyUpload( $fileName ) {
00591         return Status::newGood();
00592     }
00593 
00602     function removeBadFile( $dstPath, $retval = 0 ) {
00603         if ( file_exists( $dstPath ) ) {
00604             $thumbstat = stat( $dstPath );
00605             if ( $thumbstat['size'] == 0 || $retval != 0 ) {
00606                 $result = unlink( $dstPath );
00607 
00608                 if ( $result ) {
00609                     wfDebugLog( 'thumbnail',
00610                         sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
00611                             $thumbstat['size'], $dstPath ) );
00612                 } else {
00613                     wfDebugLog( 'thumbnail',
00614                         sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
00615                             $thumbstat['size'], $dstPath ) );
00616                 }
00617                 return true;
00618             }
00619         }
00620         return false;
00621     }
00622 
00636     public function filterThumbnailPurgeList( &$files, $options ) {
00637         // Do nothing
00638     }
00639 
00640     /*
00641      * True if the handler can rotate the media
00642      * @since 1.21
00643      * @return bool
00644      */
00645     public static function canRotate() {
00646         return false;
00647     }
00648 
00663     public function getRotation( $file ) {
00664         return 0;
00665     }
00666 
00667 }