MediaWiki
REL1_21
|
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'; 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 00123 $thumbUrl = $this->repo->getThumbUrlFromCache( 00124 $this->getName(), 00125 isset( $params['width'] ) ? $params['width'] : -1, 00126 isset( $params['height'] ) ? $params['height'] : -1, 00127 $otherParams ); 00128 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params ); 00129 } 00130 00131 // Info we can get from API... 00132 00137 public function getWidth( $page = 1 ) { 00138 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0; 00139 } 00140 00145 public function getHeight( $page = 1 ) { 00146 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0; 00147 } 00148 00152 public function getMetadata() { 00153 if ( isset( $this->mInfo['metadata'] ) ) { 00154 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) ); 00155 } 00156 return null; 00157 } 00158 00163 public static function parseMetadata( $metadata ) { 00164 if( !is_array( $metadata ) ) { 00165 return $metadata; 00166 } 00167 $ret = array(); 00168 foreach( $metadata as $meta ) { 00169 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] ); 00170 } 00171 return $ret; 00172 } 00173 00177 public function getSize() { 00178 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null; 00179 } 00180 00184 public function getUrl() { 00185 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null; 00186 } 00187 00192 public function getUser( $method = 'text' ) { 00193 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null; 00194 } 00195 00199 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { 00200 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null; 00201 } 00202 00206 function getSha1() { 00207 return isset( $this->mInfo['sha1'] ) 00208 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) 00209 : null; 00210 } 00211 00215 function getTimestamp() { 00216 return wfTimestamp( TS_MW, 00217 isset( $this->mInfo['timestamp'] ) 00218 ? strval( $this->mInfo['timestamp'] ) 00219 : null 00220 ); 00221 } 00222 00226 function getMimeType() { 00227 if( !isset( $this->mInfo['mime'] ) ) { 00228 $magic = MimeMagic::singleton(); 00229 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() ); 00230 } 00231 return $this->mInfo['mime']; 00232 } 00233 00238 function getMediaType() { 00239 $magic = MimeMagic::singleton(); 00240 return $magic->getMediaType( null, $this->getMimeType() ); 00241 } 00242 00246 function getDescriptionUrl() { 00247 return isset( $this->mInfo['descriptionurl'] ) 00248 ? $this->mInfo['descriptionurl'] 00249 : false; 00250 } 00251 00257 function getThumbPath( $suffix = '' ) { 00258 if ( $this->repo->canCacheThumbs() ) { 00259 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() ); 00260 if ( $suffix ) { 00261 $path = $path . $suffix . '/'; 00262 } 00263 return $path; 00264 } else { 00265 return null; 00266 } 00267 } 00268 00272 function getThumbnails() { 00273 $dir = $this->getThumbPath( $this->getName() ); 00274 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) ); 00275 00276 $files = array(); 00277 foreach ( $iter as $file ) { 00278 $files[] = $file; 00279 } 00280 00281 return $files; 00282 } 00283 00287 function purgeCache( $options = array() ) { 00288 $this->purgeThumbnails( $options ); 00289 $this->purgeDescriptionPage(); 00290 } 00291 00292 function purgeDescriptionPage() { 00293 global $wgMemc, $wgContLang; 00294 00295 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() ); 00296 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) ); 00297 00298 $wgMemc->delete( $key ); 00299 } 00300 00304 function purgeThumbnails( $options = array() ) { 00305 global $wgMemc; 00306 00307 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() ); 00308 $wgMemc->delete( $key ); 00309 00310 $files = $this->getThumbnails(); 00311 // Give media handler a chance to filter the purge list 00312 $handler = $this->getHandler(); 00313 if ( $handler ) { 00314 $handler->filterThumbnailPurgeList( $files, $options ); 00315 } 00316 00317 $dir = $this->getThumbPath( $this->getName() ); 00318 $purgeList = array(); 00319 foreach ( $files as $file ) { 00320 $purgeList[] = "{$dir}{$file}"; 00321 } 00322 00323 # Delete the thumbnails 00324 $this->repo->quickPurgeBatch( $purgeList ); 00325 # Clear out the thumbnail directory if empty 00326 $this->repo->quickCleanDir( $dir ); 00327 } 00328 }