MediaWiki
REL1_22
|
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 ) ); 00062 00063 $info = $repo->getImageInfo( $data ); 00064 00065 if ( $info ) { 00066 $lastRedirect = isset( $data['query']['redirects'] ) 00067 ? count( $data['query']['redirects'] ) - 1 00068 : -1; 00069 if ( $lastRedirect >= 0 ) { 00070 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] ); 00071 $img = new self( $newtitle, $repo, $info, true ); 00072 if ( $img ) { 00073 $img->redirectedFrom( $title->getDBkey() ); 00074 } 00075 } else { 00076 $img = new self( $title, $repo, $info, true ); 00077 } 00078 return $img; 00079 } else { 00080 return null; 00081 } 00082 } 00083 00088 static function getProps() { 00089 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype'; 00090 } 00091 00092 // Dummy functions... 00093 00097 public function exists() { 00098 return $this->mExists; 00099 } 00100 00104 public function getPath() { 00105 return false; 00106 } 00107 00113 function transform( $params, $flags = 0 ) { 00114 if ( !$this->canRender() ) { 00115 // show icon 00116 return parent::transform( $params, $flags ); 00117 } 00118 00119 // Note, the this->canRender() check above implies 00120 // that we have a handler, and it can do makeParamString. 00121 $otherParams = $this->handler->makeParamString( $params ); 00122 $width = isset( $params['width'] ) ? $params['width'] : -1; 00123 $height = isset( $params['height'] ) ? $params['height'] : -1; 00124 00125 $thumbUrl = $this->repo->getThumbUrlFromCache( 00126 $this->getName(), 00127 $width, 00128 $height, 00129 $otherParams 00130 ); 00131 if ( $thumbUrl === false ) { 00132 global $wgLang; 00133 return $this->repo->getThumbError( 00134 $this->getName(), 00135 $width, 00136 $height, 00137 $otherParams, 00138 $wgLang->getCode() 00139 ); 00140 } 00141 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params ); 00142 } 00143 00144 // Info we can get from API... 00145 00150 public function getWidth( $page = 1 ) { 00151 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0; 00152 } 00153 00158 public function getHeight( $page = 1 ) { 00159 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0; 00160 } 00161 00165 public function getMetadata() { 00166 if ( isset( $this->mInfo['metadata'] ) ) { 00167 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) ); 00168 } 00169 return null; 00170 } 00171 00176 public static function parseMetadata( $metadata ) { 00177 if ( !is_array( $metadata ) ) { 00178 return $metadata; 00179 } 00180 $ret = array(); 00181 foreach ( $metadata as $meta ) { 00182 $ret[$meta['name']] = self::parseMetadata( $meta['value'] ); 00183 } 00184 return $ret; 00185 } 00186 00190 public function getSize() { 00191 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null; 00192 } 00193 00197 public function getUrl() { 00198 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null; 00199 } 00200 00205 public function getUser( $method = 'text' ) { 00206 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null; 00207 } 00208 00212 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { 00213 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null; 00214 } 00215 00219 function getSha1() { 00220 return isset( $this->mInfo['sha1'] ) 00221 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) 00222 : null; 00223 } 00224 00228 function getTimestamp() { 00229 return wfTimestamp( TS_MW, 00230 isset( $this->mInfo['timestamp'] ) 00231 ? strval( $this->mInfo['timestamp'] ) 00232 : null 00233 ); 00234 } 00235 00239 function getMimeType() { 00240 if ( !isset( $this->mInfo['mime'] ) ) { 00241 $magic = MimeMagic::singleton(); 00242 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() ); 00243 } 00244 return $this->mInfo['mime']; 00245 } 00246 00250 function getMediaType() { 00251 if ( isset( $this->mInfo['mediatype'] ) ) { 00252 return $this->mInfo['mediatype']; 00253 } 00254 $magic = MimeMagic::singleton(); 00255 return $magic->getMediaType( null, $this->getMimeType() ); 00256 } 00257 00261 function getDescriptionUrl() { 00262 return isset( $this->mInfo['descriptionurl'] ) 00263 ? $this->mInfo['descriptionurl'] 00264 : false; 00265 } 00266 00272 function getThumbPath( $suffix = '' ) { 00273 if ( $this->repo->canCacheThumbs() ) { 00274 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() ); 00275 if ( $suffix ) { 00276 $path = $path . $suffix . '/'; 00277 } 00278 return $path; 00279 } else { 00280 return null; 00281 } 00282 } 00283 00287 function getThumbnails() { 00288 $dir = $this->getThumbPath( $this->getName() ); 00289 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) ); 00290 00291 $files = array(); 00292 foreach ( $iter as $file ) { 00293 $files[] = $file; 00294 } 00295 00296 return $files; 00297 } 00298 00302 function purgeCache( $options = array() ) { 00303 $this->purgeThumbnails( $options ); 00304 $this->purgeDescriptionPage(); 00305 } 00306 00307 function purgeDescriptionPage() { 00308 global $wgMemc, $wgContLang; 00309 00310 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() ); 00311 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) ); 00312 00313 $wgMemc->delete( $key ); 00314 } 00315 00319 function purgeThumbnails( $options = array() ) { 00320 global $wgMemc; 00321 00322 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() ); 00323 $wgMemc->delete( $key ); 00324 00325 $files = $this->getThumbnails(); 00326 // Give media handler a chance to filter the purge list 00327 $handler = $this->getHandler(); 00328 if ( $handler ) { 00329 $handler->filterThumbnailPurgeList( $files, $options ); 00330 } 00331 00332 $dir = $this->getThumbPath( $this->getName() ); 00333 $purgeList = array(); 00334 foreach ( $files as $file ) { 00335 $purgeList[] = "{$dir}{$file}"; 00336 } 00337 00338 # Delete the thumbnails 00339 $this->repo->quickPurgeBatch( $purgeList ); 00340 # Clear out the thumbnail directory if empty 00341 $this->repo->quickCleanDir( $dir ); 00342 } 00343 }