MediaWiki  REL1_20
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 
00184 
00194         function getScriptedTransform( $image, $script, $params ) {
00195                 return false;
00196         }
00197 
00208         final function getTransform( $image, $dstPath, $dstUrl, $params ) {
00209                 return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00210         }
00211 
00224         abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
00225 
00230         function getThumbType( $ext, $mime, $params = null ) {
00231                 $magic = MimeMagic::singleton();
00232                 if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
00233                         // The extension is not valid for this mime type and we do
00234                         // recognize the mime type
00235                         $extensions = $magic->getExtensionsForType( $mime );
00236                         if ( $extensions ) {
00237                                 return array( strtok( $extensions, ' ' ), $mime );
00238                         }
00239                 }
00240 
00241                 // The extension is correct (true) or the mime type is unknown to
00242                 // MediaWiki (null)
00243                 return array( $ext, $mime );
00244         }
00245 
00250         function canRender( $file ) { return true; }
00256         function mustRender( $file ) { return false; }
00261         function isMultiPage( $file ) { return false; }
00266         function pageCount( $file ) { return false; }
00271         function isVectorized( $file ) { return false; }
00278         function isAnimatedImage( $file ) { return false; }
00284         function canAnimateThumbnail( $file ) { return true; }
00289         function isEnabled() { return true; }
00290 
00301         function getPageDimensions( $image, $page ) {
00302                 $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
00303                 return array(
00304                         'width' => $gis[0],
00305                         'height' => $gis[1]
00306                 );
00307         }
00308 
00314         function getPageText( $image, $page ) {
00315                 return false;
00316         }
00317 
00345         function formatMetadata( $image ) {
00346                 return false;
00347         }
00348 
00358         function formatMetadataHelper( $metadataArray ) {
00359                  $result = array(
00360                         'visible' => array(),
00361                         'collapsed' => array()
00362                 );
00363 
00364                 $formatted = FormatMetadata::getFormattedData( $metadataArray );
00365                 // Sort fields into visible and collapsed
00366                 $visibleFields = $this->visibleMetadataFields();
00367                 foreach ( $formatted as $name => $value ) {
00368                         $tag = strtolower( $name );
00369                         self::addMeta( $result,
00370                                 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
00371                                 'exif',
00372                                 $tag,
00373                                 $value
00374                         );
00375                 }
00376                 return $result;
00377         }
00378 
00386         function visibleMetadataFields() {
00387                 $fields = array();
00388                 $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() );
00389                 foreach( $lines as $line ) {
00390                         $matches = array();
00391                         if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
00392                                 $fields[] = $matches[1];
00393                         }
00394                 }
00395                 $fields = array_map( 'strtolower', $fields );
00396                 return $fields;
00397         }
00398 
00399 
00423         protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00424                 $msg = wfMessage( "$type-$id", $param );
00425                 if ( $msg->exists() ) {
00426                         $name = $msg->text();
00427                 } else {
00428                         // This is for future compatibility when using instant commons.
00429                         // So as to not display as ugly a name if a new metadata
00430                         // property is defined that we don't know about
00431                         // (not a major issue since such a property would be collapsed
00432                         // by default).
00433                         wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
00434                         $name = wfEscapeWikiText( $id );
00435                 }
00436                 $array[$visibility][] = array(
00437                         'id' => "$type-$id",
00438                         'name' => $name,
00439                         'value' => $value
00440                 );
00441         }
00442 
00447         function getShortDesc( $file ) {
00448                 global $wgLang;
00449                 return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00450         }
00451 
00456         function getLongDesc( $file ) {
00457                 global $wgLang;
00458                 return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
00459                         $file->getMimeType() )->parse();
00460         }
00461 
00466         static function getGeneralShortDesc( $file ) {
00467                 global $wgLang;
00468                 return $wgLang->formatSize( $file->getSize() );
00469         }
00470 
00475         static function getGeneralLongDesc( $file ) {
00476                 global $wgLang;
00477                 return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
00478                         $file->getMimeType() )->parse();
00479         }
00480 
00489         public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
00490                 $idealWidth = $boxWidth * $maxHeight / $boxHeight;
00491                 $roundedUp = ceil( $idealWidth );
00492                 if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
00493                         return floor( $idealWidth );
00494                 } else {
00495                         return $roundedUp;
00496                 }
00497         }
00498 
00499         function getDimensionsString( $file ) {
00500                 return '';
00501         }
00502 
00506         function parserTransformHook( $parser, $file ) {}
00507 
00518         function verifyUpload( $fileName ) {
00519                 return Status::newGood();
00520         }
00521 
00530         function removeBadFile( $dstPath, $retval = 0 ) {
00531                 if( file_exists( $dstPath ) ) {
00532                         $thumbstat = stat( $dstPath );
00533                         if( $thumbstat['size'] == 0 || $retval != 0 ) {
00534                                 $result = unlink( $dstPath );
00535 
00536                                 if ( $result ) {
00537                                         wfDebugLog( 'thumbnail',
00538                                                 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
00539                                                         $thumbstat['size'], $dstPath ) );
00540                                 } else {
00541                                         wfDebugLog( 'thumbnail',
00542                                                 sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
00543                                                         $thumbstat['size'], $dstPath ) );
00544                                 }
00545                                 return true;
00546                         }
00547                 }
00548                 return false;
00549         }
00550 
00557         public function filterThumbnailPurgeList( &$files, $options ) {
00558                 // Do nothing
00559         }
00560 }