MediaWiki  REL1_24
ForeignAPIFile.php
Go to the documentation of this file.
00001 <?php
00030 class ForeignAPIFile extends File {
00031     private $mExists;
00032 
00033     protected $repoClass = 'ForeignApiRepo';
00034 
00041     function __construct( $title, $repo, $info, $exists = false ) {
00042         parent::__construct( $title, $repo );
00043 
00044         $this->mInfo = $info;
00045         $this->mExists = $exists;
00046 
00047         $this->assertRepoDefined();
00048     }
00049 
00055     static function newFromTitle( Title $title, $repo ) {
00056         $data = $repo->fetchImageQuery( array(
00057             'titles' => 'File:' . $title->getDBkey(),
00058             'iiprop' => self::getProps(),
00059             'prop' => 'imageinfo',
00060             'iimetadataversion' => MediaHandler::getMetadataVersion(),
00061             // extmetadata is language-dependant, accessing the current language here
00062             // would be problematic, so we just get them all
00063             'iiextmetadatamultilang' => 1,
00064         ) );
00065 
00066         $info = $repo->getImageInfo( $data );
00067 
00068         if ( $info ) {
00069             $lastRedirect = isset( $data['query']['redirects'] )
00070                 ? count( $data['query']['redirects'] ) - 1
00071                 : -1;
00072             if ( $lastRedirect >= 0 ) {
00073                 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
00074                 $img = new self( $newtitle, $repo, $info, true );
00075                 if ( $img ) {
00076                     $img->redirectedFrom( $title->getDBkey() );
00077                 }
00078             } else {
00079                 $img = new self( $title, $repo, $info, true );
00080             }
00081 
00082             return $img;
00083         } else {
00084             return null;
00085         }
00086     }
00087 
00092     static function getProps() {
00093         return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
00094     }
00095 
00096     // Dummy functions...
00097 
00101     public function exists() {
00102         return $this->mExists;
00103     }
00104 
00108     public function getPath() {
00109         return false;
00110     }
00111 
00117     function transform( $params, $flags = 0 ) {
00118         if ( !$this->canRender() ) {
00119             // show icon
00120             return parent::transform( $params, $flags );
00121         }
00122 
00123         // Note, the this->canRender() check above implies
00124         // that we have a handler, and it can do makeParamString.
00125         $otherParams = $this->handler->makeParamString( $params );
00126         $width = isset( $params['width'] ) ? $params['width'] : -1;
00127         $height = isset( $params['height'] ) ? $params['height'] : -1;
00128 
00129         $thumbUrl = $this->repo->getThumbUrlFromCache(
00130             $this->getName(),
00131             $width,
00132             $height,
00133             $otherParams
00134         );
00135         if ( $thumbUrl === false ) {
00136             global $wgLang;
00137 
00138             return $this->repo->getThumbError(
00139                 $this->getName(),
00140                 $width,
00141                 $height,
00142                 $otherParams,
00143                 $wgLang->getCode()
00144             );
00145         }
00146 
00147         return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
00148     }
00149 
00150     // Info we can get from API...
00151 
00156     public function getWidth( $page = 1 ) {
00157         return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
00158     }
00159 
00164     public function getHeight( $page = 1 ) {
00165         return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
00166     }
00167 
00171     public function getMetadata() {
00172         if ( isset( $this->mInfo['metadata'] ) ) {
00173             return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
00174         }
00175 
00176         return null;
00177     }
00178 
00183     public function getExtendedMetadata() {
00184         if ( isset( $this->mInfo['extmetadata'] ) ) {
00185             return $this->mInfo['extmetadata'];
00186         }
00187 
00188         return null;
00189     }
00190 
00195     public static function parseMetadata( $metadata ) {
00196         if ( !is_array( $metadata ) ) {
00197             return $metadata;
00198         }
00199         $ret = array();
00200         foreach ( $metadata as $meta ) {
00201             $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
00202         }
00203 
00204         return $ret;
00205     }
00206 
00210     public function getSize() {
00211         return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
00212     }
00213 
00217     public function getUrl() {
00218         return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
00219     }
00220 
00225     public function getUser( $method = 'text' ) {
00226         return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
00227     }
00228 
00234     public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
00235         return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
00236     }
00237 
00241     function getSha1() {
00242         return isset( $this->mInfo['sha1'] )
00243             ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
00244             : null;
00245     }
00246 
00250     function getTimestamp() {
00251         return wfTimestamp( TS_MW,
00252             isset( $this->mInfo['timestamp'] )
00253                 ? strval( $this->mInfo['timestamp'] )
00254                 : null
00255         );
00256     }
00257 
00261     function getMimeType() {
00262         if ( !isset( $this->mInfo['mime'] ) ) {
00263             $magic = MimeMagic::singleton();
00264             $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
00265         }
00266 
00267         return $this->mInfo['mime'];
00268     }
00269 
00273     function getMediaType() {
00274         if ( isset( $this->mInfo['mediatype'] ) ) {
00275             return $this->mInfo['mediatype'];
00276         }
00277         $magic = MimeMagic::singleton();
00278 
00279         return $magic->getMediaType( null, $this->getMimeType() );
00280     }
00281 
00285     function getDescriptionUrl() {
00286         return isset( $this->mInfo['descriptionurl'] )
00287             ? $this->mInfo['descriptionurl']
00288             : false;
00289     }
00290 
00296     function getThumbPath( $suffix = '' ) {
00297         if ( $this->repo->canCacheThumbs() ) {
00298             $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
00299             if ( $suffix ) {
00300                 $path = $path . $suffix . '/';
00301             }
00302 
00303             return $path;
00304         } else {
00305             return null;
00306         }
00307     }
00308 
00312     function getThumbnails() {
00313         $dir = $this->getThumbPath( $this->getName() );
00314         $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
00315 
00316         $files = array();
00317         foreach ( $iter as $file ) {
00318             $files[] = $file;
00319         }
00320 
00321         return $files;
00322     }
00323 
00327     function purgeCache( $options = array() ) {
00328         $this->purgeThumbnails( $options );
00329         $this->purgeDescriptionPage();
00330     }
00331 
00332     function purgeDescriptionPage() {
00333         global $wgMemc, $wgContLang;
00334 
00335         $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
00336         $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
00337 
00338         $wgMemc->delete( $key );
00339     }
00340 
00344     function purgeThumbnails( $options = array() ) {
00345         global $wgMemc;
00346 
00347         $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
00348         $wgMemc->delete( $key );
00349 
00350         $files = $this->getThumbnails();
00351         // Give media handler a chance to filter the purge list
00352         $handler = $this->getHandler();
00353         if ( $handler ) {
00354             $handler->filterThumbnailPurgeList( $files, $options );
00355         }
00356 
00357         $dir = $this->getThumbPath( $this->getName() );
00358         $purgeList = array();
00359         foreach ( $files as $file ) {
00360             $purgeList[] = "{$dir}{$file}";
00361         }
00362 
00363         # Delete the thumbnails
00364         $this->repo->quickPurgeBatch( $purgeList );
00365         # Clear out the thumbnail directory if empty
00366         $this->repo->quickCleanDir( $dir );
00367     }
00368 }