MediaWiki  REL1_24
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     const MAX_ERR_LOG_SIZE = 65535;
00038 
00040     protected static $handlers = array();
00041 
00048     static function getHandler( $type ) {
00049         global $wgMediaHandlers;
00050         if ( !isset( $wgMediaHandlers[$type] ) ) {
00051             wfDebug( __METHOD__ . ": no handler found for $type.\n" );
00052 
00053             return false;
00054         }
00055         $class = $wgMediaHandlers[$type];
00056         if ( !isset( self::$handlers[$class] ) ) {
00057             self::$handlers[$class] = new $class;
00058             if ( !self::$handlers[$class]->isEnabled() ) {
00059                 wfDebug( __METHOD__ . ": $class is not enabled\n" );
00060                 self::$handlers[$class] = false;
00061             }
00062         }
00063 
00064         return self::$handlers[$class];
00065     }
00066 
00070     public static function resetCache() {
00071         self::$handlers = array();
00072     }
00073 
00078     abstract function getParamMap();
00079 
00088     abstract function validateParam( $name, $value );
00089 
00096     abstract function makeParamString( $params );
00097 
00104     abstract function parseParamString( $str );
00105 
00113     abstract function normaliseParams( $image, &$params );
00114 
00134     abstract function getImageSize( $image, $path );
00135 
00144     function getMetadata( $image, $path ) {
00145         return '';
00146     }
00147 
00163     static function getMetadataVersion() {
00164         $version = array( '2' ); // core metadata version
00165         wfRunHooks( 'GetMetadataVersion', array( &$version ) );
00166 
00167         return implode( ';', $version );
00168     }
00169 
00180     function convertMetadataVersion( $metadata, $version = 1 ) {
00181         if ( !is_array( $metadata ) ) {
00182 
00183             //unserialize to keep return parameter consistent.
00184             wfSuppressWarnings();
00185             $ret = unserialize( $metadata );
00186             wfRestoreWarnings();
00187 
00188             return $ret;
00189         }
00190 
00191         return $metadata;
00192     }
00193 
00201     function getMetadataType( $image ) {
00202         return false;
00203     }
00204 
00221     function isMetadataValid( $image, $metadata ) {
00222         return self::METADATA_GOOD;
00223     }
00224 
00257     public function getCommonMetaArray( File $file ) {
00258         return false;
00259     }
00260 
00273     function getScriptedTransform( $image, $script, $params ) {
00274         return false;
00275     }
00276 
00287     final function getTransform( $image, $dstPath, $dstUrl, $params ) {
00288         return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00289     }
00290 
00303     abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
00304 
00313     function getThumbType( $ext, $mime, $params = null ) {
00314         $magic = MimeMagic::singleton();
00315         if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
00316             // The extension is not valid for this MIME type and we do
00317             // recognize the MIME type
00318             $extensions = $magic->getExtensionsForType( $mime );
00319             if ( $extensions ) {
00320                 return array( strtok( $extensions, ' ' ), $mime );
00321             }
00322         }
00323 
00324         // The extension is correct (true) or the MIME type is unknown to
00325         // MediaWiki (null)
00326         return array( $ext, $mime );
00327     }
00328 
00335     public function getStreamHeaders( $metadata ) {
00336         return array();
00337     }
00338 
00345     function canRender( $file ) {
00346         return true;
00347     }
00348 
00356     function mustRender( $file ) {
00357         return false;
00358     }
00359 
00366     function isMultiPage( $file ) {
00367         return false;
00368     }
00369 
00376     function pageCount( $file ) {
00377         return false;
00378     }
00379 
00386     function isVectorized( $file ) {
00387         return false;
00388     }
00389 
00398     function isAnimatedImage( $file ) {
00399         return false;
00400     }
00401 
00409     function canAnimateThumbnail( $file ) {
00410         return true;
00411     }
00412 
00417     function isEnabled() {
00418         return true;
00419     }
00420 
00437     function getPageDimensions( $image, $page ) {
00438         $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
00439         if ( $gis ) {
00440             return array(
00441                 'width' => $gis[0],
00442                 'height' => $gis[1]
00443             );
00444         } else {
00445             return false;
00446         }
00447     }
00448 
00457     function getPageText( $image, $page ) {
00458         return false;
00459     }
00460 
00466     public function getEntireText( File $file ) {
00467         $numPages = $file->pageCount();
00468         if ( !$numPages ) {
00469             // Not a multipage document
00470             return $this->getPageText( $file, 1 );
00471         }
00472         $document = '';
00473         for ( $i = 1; $i <= $numPages; $i++ ) {
00474             $curPage = $this->getPageText( $file, $i );
00475             if ( is_string( $curPage ) ) {
00476                 $document .= $curPage . "\n";
00477             }
00478         }
00479         if ( $document !== '' ) {
00480             return $document;
00481         }
00482         return false;
00483     }
00484 
00512     function formatMetadata( $image ) {
00513         return false;
00514     }
00515 
00525     function formatMetadataHelper( $metadataArray ) {
00526         $result = array(
00527             'visible' => array(),
00528             'collapsed' => array()
00529         );
00530 
00531         $formatted = FormatMetadata::getFormattedData( $metadataArray );
00532         // Sort fields into visible and collapsed
00533         $visibleFields = $this->visibleMetadataFields();
00534         foreach ( $formatted as $name => $value ) {
00535             $tag = strtolower( $name );
00536             self::addMeta( $result,
00537                 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
00538                 'exif',
00539                 $tag,
00540                 $value
00541             );
00542         }
00543 
00544         return $result;
00545     }
00546 
00553     protected function visibleMetadataFields() {
00554         return FormatMetadata::getVisibleFields();
00555     }
00556 
00580     protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00581         $msg = wfMessage( "$type-$id", $param );
00582         if ( $msg->exists() ) {
00583             $name = $msg->text();
00584         } else {
00585             // This is for future compatibility when using instant commons.
00586             // So as to not display as ugly a name if a new metadata
00587             // property is defined that we don't know about
00588             // (not a major issue since such a property would be collapsed
00589             // by default).
00590             wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
00591             $name = wfEscapeWikiText( $id );
00592         }
00593         $array[$visibility][] = array(
00594             'id' => "$type-$id",
00595             'name' => $name,
00596             'value' => $value
00597         );
00598     }
00599 
00606     function getShortDesc( $file ) {
00607         return self::getGeneralShortDesc( $file );
00608     }
00609 
00616     function getLongDesc( $file ) {
00617         return self::getGeneralLongDesc( $file );
00618     }
00619 
00626     static function getGeneralShortDesc( $file ) {
00627         global $wgLang;
00628 
00629         return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00630     }
00631 
00638     static function getGeneralLongDesc( $file ) {
00639         return wfMessage( 'file-info' )->sizeParams( $file->getSize() )
00640             ->params( $file->getMimeType() )->parse();
00641     }
00642 
00651     public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
00652         $idealWidth = $boxWidth * $maxHeight / $boxHeight;
00653         $roundedUp = ceil( $idealWidth );
00654         if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
00655             return floor( $idealWidth );
00656         } else {
00657             return $roundedUp;
00658         }
00659     }
00660 
00667     function getDimensionsString( $file ) {
00668         return '';
00669     }
00670 
00681     function parserTransformHook( $parser, $file ) {
00682     }
00683 
00694     function verifyUpload( $fileName ) {
00695         return Status::newGood();
00696     }
00697 
00706     function removeBadFile( $dstPath, $retval = 0 ) {
00707         if ( file_exists( $dstPath ) ) {
00708             $thumbstat = stat( $dstPath );
00709             if ( $thumbstat['size'] == 0 || $retval != 0 ) {
00710                 $result = unlink( $dstPath );
00711 
00712                 if ( $result ) {
00713                     wfDebugLog( 'thumbnail',
00714                         sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
00715                             $thumbstat['size'], $dstPath ) );
00716                 } else {
00717                     wfDebugLog( 'thumbnail',
00718                         sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
00719                             $thumbstat['size'], $dstPath ) );
00720                 }
00721 
00722                 return true;
00723             }
00724         }
00725 
00726         return false;
00727     }
00728 
00742     public function filterThumbnailPurgeList( &$files, $options ) {
00743         // Do nothing
00744     }
00745 
00751     public function canRotate() {
00752         return false;
00753     }
00754 
00769     public function getRotation( $file ) {
00770         return 0;
00771     }
00772 
00784     protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
00785         # Keep error output limited (bug 57985)
00786         $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
00787 
00788         wfDebugLog( 'thumbnail',
00789             sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
00790                     wfHostname(), $retval, $errMessage, $cmd ) );
00791     }
00792 
00800     public function getAvailableLanguages( File $file ) {
00801         return array();
00802     }
00803 
00816     public function getDefaultRenderLanguage( File $file ) {
00817         return null;
00818     }
00819 
00830     public function getLength( $file ) {
00831         return 0.0;
00832     }
00833 
00839     public function isExpensiveToThumbnail( $file ) {
00840         return false;
00841     }
00842 
00849     public function supportsBucketing() {
00850         return false;
00851     }
00852 
00859     public function sanitizeParamsForBucketing( $params ) {
00860         return $params;
00861     }
00862 }