MediaWiki  REL1_23
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                 self::$handlers[$class] = false;
00060             }
00061         }
00062 
00063         return self::$handlers[$class];
00064     }
00065 
00070     abstract function getParamMap();
00071 
00080     abstract function validateParam( $name, $value );
00081 
00088     abstract function makeParamString( $params );
00089 
00096     abstract function parseParamString( $str );
00097 
00105     abstract function normaliseParams( $image, &$params );
00106 
00116     abstract function getImageSize( $image, $path );
00117 
00126     function getMetadata( $image, $path ) {
00127         return '';
00128     }
00129 
00145     static function getMetadataVersion() {
00146         $version = array( '2' ); // core metadata version
00147         wfRunHooks( 'GetMetadataVersion', array( &$version ) );
00148 
00149         return implode( ';', $version );
00150     }
00151 
00162     function convertMetadataVersion( $metadata, $version = 1 ) {
00163         if ( !is_array( $metadata ) ) {
00164 
00165             //unserialize to keep return parameter consistent.
00166             wfSuppressWarnings();
00167             $ret = unserialize( $metadata );
00168             wfRestoreWarnings();
00169 
00170             return $ret;
00171         }
00172 
00173         return $metadata;
00174     }
00175 
00181     function getMetadataType( $image ) {
00182         return false;
00183     }
00184 
00196     function isMetadataValid( $image, $metadata ) {
00197         return self::METADATA_GOOD;
00198     }
00199 
00232     public function getCommonMetaArray( File $file ) {
00233         return false;
00234     }
00235 
00248     function getScriptedTransform( $image, $script, $params ) {
00249         return false;
00250     }
00251 
00262     final function getTransform( $image, $dstPath, $dstUrl, $params ) {
00263         return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER );
00264     }
00265 
00278     abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 );
00279 
00288     function getThumbType( $ext, $mime, $params = null ) {
00289         $magic = MimeMagic::singleton();
00290         if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) {
00291             // The extension is not valid for this mime type and we do
00292             // recognize the mime type
00293             $extensions = $magic->getExtensionsForType( $mime );
00294             if ( $extensions ) {
00295                 return array( strtok( $extensions, ' ' ), $mime );
00296             }
00297         }
00298 
00299         // The extension is correct (true) or the mime type is unknown to
00300         // MediaWiki (null)
00301         return array( $ext, $mime );
00302     }
00303 
00310     public function getStreamHeaders( $metadata ) {
00311         return array();
00312     }
00313 
00320     function canRender( $file ) {
00321         return true;
00322     }
00323 
00331     function mustRender( $file ) {
00332         return false;
00333     }
00334 
00341     function isMultiPage( $file ) {
00342         return false;
00343     }
00344 
00351     function pageCount( $file ) {
00352         return false;
00353     }
00354 
00361     function isVectorized( $file ) {
00362         return false;
00363     }
00364 
00373     function isAnimatedImage( $file ) {
00374         return false;
00375     }
00376 
00384     function canAnimateThumbnail( $file ) {
00385         return true;
00386     }
00387 
00392     function isEnabled() {
00393         return true;
00394     }
00395 
00412     function getPageDimensions( $image, $page ) {
00413         $gis = $this->getImageSize( $image, $image->getLocalRefPath() );
00414         if ( $gis ) {
00415             return array(
00416                 'width' => $gis[0],
00417                 'height' => $gis[1]
00418             );
00419         } else {
00420             return false;
00421         }
00422     }
00423 
00432     function getPageText( $image, $page ) {
00433         return false;
00434     }
00435 
00441     public function getEntireText( File $file ) {
00442         $numPages = $file->pageCount();
00443         if ( !$numPages ) {
00444             // Not a multipage document
00445             return $this->getPageText( $file, 1 );
00446         }
00447         $document = '';
00448         for ( $i = 1; $i <= $numPages; $i++ ) {
00449             $curPage = $this->getPageText( $file, $i );
00450             if ( is_string( $curPage ) ) {
00451                 $document .= $curPage . "\n";
00452             }
00453         }
00454         if ( $document !== '' ) {
00455             return $document;
00456         }
00457         return false;
00458     }
00459 
00487     function formatMetadata( $image ) {
00488         return false;
00489     }
00490 
00500     function formatMetadataHelper( $metadataArray ) {
00501         $result = array(
00502             'visible' => array(),
00503             'collapsed' => array()
00504         );
00505 
00506         $formatted = FormatMetadata::getFormattedData( $metadataArray );
00507         // Sort fields into visible and collapsed
00508         $visibleFields = $this->visibleMetadataFields();
00509         foreach ( $formatted as $name => $value ) {
00510             $tag = strtolower( $name );
00511             self::addMeta( $result,
00512                 in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
00513                 'exif',
00514                 $tag,
00515                 $value
00516             );
00517         }
00518 
00519         return $result;
00520     }
00521 
00528     protected function visibleMetadataFields() {
00529         return FormatMetadata::getVisibleFields();
00530     }
00531 
00555     protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
00556         $msg = wfMessage( "$type-$id", $param );
00557         if ( $msg->exists() ) {
00558             $name = $msg->text();
00559         } else {
00560             // This is for future compatibility when using instant commons.
00561             // So as to not display as ugly a name if a new metadata
00562             // property is defined that we don't know about
00563             // (not a major issue since such a property would be collapsed
00564             // by default).
00565             wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" );
00566             $name = wfEscapeWikiText( $id );
00567         }
00568         $array[$visibility][] = array(
00569             'id' => "$type-$id",
00570             'name' => $name,
00571             'value' => $value
00572         );
00573     }
00574 
00581     function getShortDesc( $file ) {
00582         global $wgLang;
00583 
00584         return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
00585     }
00586 
00593     function getLongDesc( $file ) {
00594         global $wgLang;
00595 
00596         return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
00597             $file->getMimeType() )->parse();
00598     }
00599 
00606     static function getGeneralShortDesc( $file ) {
00607         global $wgLang;
00608 
00609         return $wgLang->formatSize( $file->getSize() );
00610     }
00611 
00618     static function getGeneralLongDesc( $file ) {
00619         global $wgLang;
00620 
00621         return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
00622             $file->getMimeType() )->parse();
00623     }
00624 
00633     public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) {
00634         $idealWidth = $boxWidth * $maxHeight / $boxHeight;
00635         $roundedUp = ceil( $idealWidth );
00636         if ( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) {
00637             return floor( $idealWidth );
00638         } else {
00639             return $roundedUp;
00640         }
00641     }
00642 
00649     function getDimensionsString( $file ) {
00650         return '';
00651     }
00652 
00663     function parserTransformHook( $parser, $file ) {
00664     }
00665 
00676     function verifyUpload( $fileName ) {
00677         return Status::newGood();
00678     }
00679 
00688     function removeBadFile( $dstPath, $retval = 0 ) {
00689         if ( file_exists( $dstPath ) ) {
00690             $thumbstat = stat( $dstPath );
00691             if ( $thumbstat['size'] == 0 || $retval != 0 ) {
00692                 $result = unlink( $dstPath );
00693 
00694                 if ( $result ) {
00695                     wfDebugLog( 'thumbnail',
00696                         sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded',
00697                             $thumbstat['size'], $dstPath ) );
00698                 } else {
00699                     wfDebugLog( 'thumbnail',
00700                         sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
00701                             $thumbstat['size'], $dstPath ) );
00702                 }
00703 
00704                 return true;
00705             }
00706         }
00707 
00708         return false;
00709     }
00710 
00724     public function filterThumbnailPurgeList( &$files, $options ) {
00725         // Do nothing
00726     }
00727 
00728     /*
00729      * True if the handler can rotate the media
00730      * @since 1.21
00731      * @return bool
00732      */
00733     public static function canRotate() {
00734         return false;
00735     }
00736 
00751     public function getRotation( $file ) {
00752         return 0;
00753     }
00754 
00766     protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
00767         # Keep error output limited (bug 57985)
00768         $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
00769 
00770         wfDebugLog( 'thumbnail',
00771             sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
00772                     wfHostname(), $retval, $errMessage, $cmd ) );
00773     }
00774 
00782     public function getAvailableLanguages( File $file ) {
00783         return array();
00784     }
00785 
00798     public function getDefaultRenderLanguage( File $file ) {
00799         return null;
00800     }
00801 
00812     public function getLength( $file ) {
00813         return 0.0;
00814     }
00815 }