MediaWiki
REL1_19
|
00001 <?php 00015 class ForeignAPIFile extends File { 00016 private $mExists; 00017 00018 protected $repoClass = 'ForeignApiRepo'; 00019 00026 function __construct( $title, $repo, $info, $exists = false ) { 00027 parent::__construct( $title, $repo ); 00028 00029 $this->mInfo = $info; 00030 $this->mExists = $exists; 00031 00032 $this->assertRepoDefined(); 00033 } 00034 00040 static function newFromTitle( Title $title, $repo ) { 00041 $data = $repo->fetchImageQuery( array( 00042 'titles' => 'File:' . $title->getDBKey(), 00043 'iiprop' => self::getProps(), 00044 'prop' => 'imageinfo', 00045 'iimetadataversion' => MediaHandler::getMetadataVersion() 00046 ) ); 00047 00048 $info = $repo->getImageInfo( $data ); 00049 00050 if( $info ) { 00051 $lastRedirect = isset( $data['query']['redirects'] ) 00052 ? count( $data['query']['redirects'] ) - 1 00053 : -1; 00054 if( $lastRedirect >= 0 ) { 00055 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']); 00056 $img = new self( $newtitle, $repo, $info, true ); 00057 if( $img ) { 00058 $img->redirectedFrom( $title->getDBkey() ); 00059 } 00060 } else { 00061 $img = new self( $title, $repo, $info, true ); 00062 } 00063 return $img; 00064 } else { 00065 return null; 00066 } 00067 } 00068 00072 static function getProps() { 00073 return 'timestamp|user|comment|url|size|sha1|metadata|mime'; 00074 } 00075 00076 // Dummy functions... 00077 public function exists() { 00078 return $this->mExists; 00079 } 00080 00081 public function getPath() { 00082 return false; 00083 } 00084 00085 function transform( $params, $flags = 0 ) { 00086 if( !$this->canRender() ) { 00087 // show icon 00088 return parent::transform( $params, $flags ); 00089 } 00090 00091 // Note, the this->canRender() check above implies 00092 // that we have a handler, and it can do makeParamString. 00093 $otherParams = $this->handler->makeParamString( $params ); 00094 00095 $thumbUrl = $this->repo->getThumbUrlFromCache( 00096 $this->getName(), 00097 isset( $params['width'] ) ? $params['width'] : -1, 00098 isset( $params['height'] ) ? $params['height'] : -1, 00099 $otherParams ); 00100 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params ); 00101 } 00102 00103 // Info we can get from API... 00104 public function getWidth( $page = 1 ) { 00105 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0; 00106 } 00107 00112 public function getHeight( $page = 1 ) { 00113 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0; 00114 } 00115 00116 public function getMetadata() { 00117 if ( isset( $this->mInfo['metadata'] ) ) { 00118 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) ); 00119 } 00120 return null; 00121 } 00122 00123 public static function parseMetadata( $metadata ) { 00124 if( !is_array( $metadata ) ) { 00125 return $metadata; 00126 } 00127 $ret = array(); 00128 foreach( $metadata as $meta ) { 00129 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] ); 00130 } 00131 return $ret; 00132 } 00133 00134 public function getSize() { 00135 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null; 00136 } 00137 00138 public function getUrl() { 00139 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null; 00140 } 00141 00142 public function getUser( $method='text' ) { 00143 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null; 00144 } 00145 00146 public function getDescription() { 00147 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null; 00148 } 00149 00150 function getSha1() { 00151 return isset( $this->mInfo['sha1'] ) 00152 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) 00153 : null; 00154 } 00155 00156 function getTimestamp() { 00157 return wfTimestamp( TS_MW, 00158 isset( $this->mInfo['timestamp'] ) 00159 ? strval( $this->mInfo['timestamp'] ) 00160 : null 00161 ); 00162 } 00163 00164 function getMimeType() { 00165 if( !isset( $this->mInfo['mime'] ) ) { 00166 $magic = MimeMagic::singleton(); 00167 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() ); 00168 } 00169 return $this->mInfo['mime']; 00170 } 00171 00173 function getMediaType() { 00174 $magic = MimeMagic::singleton(); 00175 return $magic->getMediaType( null, $this->getMimeType() ); 00176 } 00177 00178 function getDescriptionUrl() { 00179 return isset( $this->mInfo['descriptionurl'] ) 00180 ? $this->mInfo['descriptionurl'] 00181 : false; 00182 } 00183 00187 function getThumbPath( $suffix = '' ) { 00188 if ( $this->repo->canCacheThumbs() ) { 00189 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() ); 00190 if ( $suffix ) { 00191 $path = $path . $suffix . '/'; 00192 } 00193 return $path; 00194 } else { 00195 return null; 00196 } 00197 } 00198 00199 function getThumbnails() { 00200 $dir = $this->getThumbPath( $this->getName() ); 00201 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) ); 00202 00203 $files = array(); 00204 foreach ( $iter as $file ) { 00205 $files[] = $file; 00206 } 00207 00208 return $files; 00209 } 00210 00214 function purgeCache( $options = array() ) { 00215 $this->purgeThumbnails( $options ); 00216 $this->purgeDescriptionPage(); 00217 } 00218 00219 function purgeDescriptionPage() { 00220 global $wgMemc, $wgContLang; 00221 00222 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() ); 00223 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) ); 00224 00225 $wgMemc->delete( $key ); 00226 } 00227 00228 function purgeThumbnails( $options = array() ) { 00229 global $wgMemc; 00230 00231 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() ); 00232 $wgMemc->delete( $key ); 00233 00234 $files = $this->getThumbnails(); 00235 // Give media handler a chance to filter the purge list 00236 $handler = $this->getHandler(); 00237 if ( $handler ) { 00238 $handler->filterThumbnailPurgeList( $files, $options ); 00239 } 00240 00241 $dir = $this->getThumbPath( $this->getName() ); 00242 $purgeList = array(); 00243 foreach ( $files as $file ) { 00244 $purgeList[] = "{$dir}{$file}"; 00245 } 00246 00247 # Delete the thumbnails 00248 $this->repo->cleanupBatch( $purgeList, FileRepo::SKIP_LOCKING ); 00249 # Clear out the thumbnail directory if empty 00250 $this->repo->getBackend()->clean( array( 'dir' => $dir ) ); 00251 } 00252 }