MediaWiki  REL1_21
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 
00083         abstract function makeParamString( $params );
00084 
00090         abstract function parseParamString( $str );
00091 
00099         abstract function normaliseParams( $image, &$params );
00100 
00109         abstract function getImageSize( $image, $path );
00110 
00119         function getMetadata( $image, $path ) { return ''; }
00120 
00136         static function getMetadataVersion () {
00137                 $version = Array( '2' ); // core metadata version
00138                 wfRunHooks( 'GetMetadataVersion', Array( &$version ) );
00139                 return implode( ';', $version);
00140         }
00141 
00152         function convertMetadataVersion( $metadata, $version = 1 ) {
00153                 if ( !is_array( $metadata ) ) {
00154 
00155                         //unserialize to keep return parameter consistent.
00156                         wfSuppressWarnings();
00157                         $ret = unserialize( $metadata );
00158                         wfRestoreWarnings();
00159                         return $ret;
00160                 }
00161                 return $metadata;
00162         }
00163 
00169         function getMetadataType( $image ) { return false; }
00170 
00180         function isMetadataValid( $image, $metadata ) {
00181                 return self::METADATA_GOOD;
00182         }
00183 
00193         function getScriptedTransform( $image, $script, $params ) {
00194                 return false;
00195         }
00196 
00207         final function getTransform( $image, $dstPath, $dstUrl, $params ) {
00208                 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00209         }
00210 
00223         abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
00224 
00229         function getThumbType( $ext, $mime, $params = null ) {
00230                 $magic = MimeMagic::singleton();
00231                 if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
00232                         // The extension is not valid for this mime type and we do
00233                         // recognize the mime type
00234                         $extensions = $magic->getExtensionsForType( $mime );
00235                         if ( $extensions ) {
00236                                 return array( strtok( $extensions, ' ' ), $mime );
00237                         }
00238                 }
00239 
00240                 // The extension is correct (true) or the mime type is unknown to
00241                 // MediaWiki (null)
00242                 return array( $ext, $mime );
00243         }
00244 
00250         public function getStreamHeaders( $metadata ) {
00251                 return array();
00252         }
00253 
00258         function canRender( $file ) { return true; }
00264         function mustRender( $file ) { return false; }
00269         function isMultiPage( $file ) { return false; }
00274         function pageCount( $file ) { return false; }
00279         function isVectorized( $file ) { return false; }
00286         function isAnimatedImage( $file ) { return false; }
00292         function canAnimateThumbnail( $file ) { return true; }
00297         function isEnabled() { return true; }
00298 
00309         function getPageDimensions( $image, $page ) {
00310                 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
00311                 return array(
00312                         'width' => $gis[0],
00313                         'height' => $gis[1]
00314                 );
00315         }
00316 
00322         function getPageText( $image, $page ) {
00323                 return false;
00324         }
00325 
00353         function formatMetadata( $image ) {
00354                 return false;
00355         }
00356 
00366         function formatMetadataHelper( $metadataArray ) {
00367                 $result = array(
00368                         'visible' => array(),
00369                         'collapsed' => array()
00370                 );
00371 
00372                 $formatted = FormatMetadata::getFormattedData( $metadataArray );
00373                 // Sort fields into visible and collapsed
00374                 $visibleFields = $this->visibleMetadataFields();
00375                 foreach ( $formatted as $name => $value ) {
00376                         $tag = strtolower( $name );
00377                         self::addMeta( $result,
00378                                 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
00379                                 'exif',
00380                                 $tag,
00381                                 $value
00382                         );
00383                 }
00384                 return $result;
00385         }
00386 
00394         function visibleMetadataFields() {
00395                 $fields = array();
00396                 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
00397                 foreach( $lines as $line ) {
00398                         $matches = array();
00399                         if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
00400                                 $fields[] = $matches[1];
00401                         }
00402                 }
00403                 $fields = array_map( 'strtolower', $fields );
00404                 return $fields;
00405         }
00406 
00430         protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00431                 $msg = wfMessage( "$type-$id", $param );
00432                 if ( $msg->exists() ) {
00433                         $name = $msg->text();
00434                 } else {
00435                         // This is for future compatibility when using instant commons.
00436                         // So as to not display as ugly a name if a new metadata
00437                         // property is defined that we don't know about
00438                         // (not a major issue since such a property would be collapsed
00439                         // by default).
00440                         wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
00441                         $name = wfEscapeWikiText( $id );
00442                 }
00443                 $array[$visibility][] = array(
00444                         'id' => "$type-$id",
00445                         'name' => $name,
00446                         'value' => $value
00447                 );
00448         }
00449 
00454         function getShortDesc( $file ) {
00455                 global $wgLang;
00456                 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00457         }
00458 
00463         function getLongDesc( $file ) {
00464                 global $wgLang;
00465                 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
00466                         $file->getMimeType() )->parse();
00467         }
00468 
00473         static function getGeneralShortDesc( $file ) {
00474                 global $wgLang;
00475                 return $wgLang->formatSize( $file->getSize() );
00476         }
00477 
00482         static function getGeneralLongDesc( $file ) {
00483                 global $wgLang;
00484                 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
00485                         $file->getMimeType() )->parse();
00486         }
00487 
00496         public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
00497                 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
00498                 $roundedUp = ceil( $idealWidth );
00499                 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
00500                         return floor( $idealWidth );
00501                 } else {
00502                         return $roundedUp;
00503                 }
00504         }
00505 
00506         function getDimensionsString( $file ) {
00507                 return '';
00508         }
00509 
00513         function parserTransformHook( $parser, $file ) {}
00514 
00525         function verifyUpload( $fileName ) {
00526                 return Status::newGood();
00527         }
00528 
00537         function removeBadFile( $dstPath, $retval = 0 ) {
00538                 if( file_exists( $dstPath ) ) {
00539                         $thumbstat = stat( $dstPath );
00540                         if( $thumbstat['size'] == 0 || $retval != 0 ) {
00541                                 $result = unlink( $dstPath );
00542 
00543                                 if ( $result ) {
00544                                         wfDebugLog( 'thumbnail',
00545                                                 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
00546                                                         $thumbstat['size'], $dstPath ) );
00547                                 } else {
00548                                         wfDebugLog( 'thumbnail',
00549                                                 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
00550                                                         $thumbstat['size'], $dstPath ) );
00551                                 }
00552                                 return true;
00553                         }
00554                 }
00555                 return false;
00556         }
00557 
00564         public function filterThumbnailPurgeList( &$files, $options ) {
00565                 // Do nothing
00566         }
00567 
00568         /*
00569          * True if the handler can rotate the media
00570          * @since 1.21
00571          * @return bool
00572          */
00573         public static function canRotate() {
00574                 return false;
00575         }
00576 }