00001 <?php
00002
00009 class ForeignAPIFile extends File {
00010
00011 private $mExists;
00012
00013 function __construct( $title, $repo, $info, $exists = false ) {
00014 parent::__construct( $title, $repo );
00015 $this->mInfo = $info;
00016 $this->mExists = $exists;
00017 }
00018
00019 static function newFromTitle( $title, $repo ) {
00020 $info = $repo->getImageInfo( $title );
00021 if( $info ) {
00022 return new ForeignAPIFile( $title, $repo, $info, true );
00023 } else {
00024 return null;
00025 }
00026 }
00027
00028
00029 public function exists() {
00030 return $this->mExists;
00031 }
00032
00033 public function getPath() {
00034 return false;
00035 }
00036
00037 function transform( $params, $flags = 0 ) {
00038 if( !$this->canRender() ) {
00039
00040 return parent::transform( $params, $flags );
00041 }
00042 $thumbUrl = $this->repo->getThumbUrlFromCache(
00043 $this->getName(),
00044 isset( $params['width'] ) ? $params['width'] : -1,
00045 isset( $params['height'] ) ? $params['height'] : -1 );
00046 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
00047 }
00048
00049
00050 public function getWidth( $page = 1 ) {
00051 return intval( @$this->mInfo['width'] );
00052 }
00053
00054 public function getHeight( $page = 1 ) {
00055 return intval( @$this->mInfo['height'] );
00056 }
00057
00058 public function getMetadata() {
00059 if ( isset( $this->mInfo['metadata'] ) ) {
00060 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
00061 }
00062 return null;
00063 }
00064
00065 public static function parseMetadata( $metadata ) {
00066 if( !is_array( $metadata ) ) {
00067 return $metadata;
00068 }
00069 $ret = array();
00070 foreach( $metadata as $meta ) {
00071 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
00072 }
00073 return $ret;
00074 }
00075
00076 public function getSize() {
00077 return intval( @$this->mInfo['size'] );
00078 }
00079
00080 public function getUrl() {
00081 return strval( @$this->mInfo['url'] );
00082 }
00083
00084 public function getUser( $method='text' ) {
00085 return strval( @$this->mInfo['user'] );
00086 }
00087
00088 public function getDescription() {
00089 return strval( @$this->mInfo['comment'] );
00090 }
00091
00092 function getSha1() {
00093 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
00094 }
00095
00096 function getTimestamp() {
00097 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
00098 }
00099
00100 function getMimeType() {
00101 if( !isset( $this->mInfo['mime'] ) ) {
00102 $magic = MimeMagic::singleton();
00103 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
00104 }
00105 return $this->mInfo['mime'];
00106 }
00107
00109 function getMediaType() {
00110 $magic = MimeMagic::singleton();
00111 return $magic->getMediaType( null, $this->getMimeType() );
00112 }
00113
00114 function getDescriptionUrl() {
00115 return isset( $this->mInfo['descriptionurl'] )
00116 ? $this->mInfo['descriptionurl']
00117 : false;
00118 }
00119
00123 function getThumbPath( $suffix = '' ) {
00124 if ( $this->repo->canCacheThumbs() ) {
00125 global $wgUploadDirectory;
00126 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
00127 if ( $suffix ) {
00128 $path = $path . $suffix . '/';
00129 }
00130 return $path;
00131 }
00132 else {
00133 return null;
00134 }
00135 }
00136
00137 function getThumbnails() {
00138 $files = array();
00139 $dir = $this->getThumbPath( $this->getName() );
00140 if ( is_dir( $dir ) ) {
00141 $handle = opendir( $dir );
00142 if ( $handle ) {
00143 while ( false !== ( $file = readdir($handle) ) ) {
00144 if ( $file{0} != '.' ) {
00145 $files[] = $file;
00146 }
00147 }
00148 closedir( $handle );
00149 }
00150 }
00151 return $files;
00152 }
00153
00154 function purgeCache() {
00155 $this->purgeThumbnails();
00156 $this->purgeDescriptionPage();
00157 }
00158
00159 function purgeDescriptionPage() {
00160 global $wgMemc, $wgContLang;
00161 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
00162 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
00163 $wgMemc->delete( $key );
00164 }
00165
00166 function purgeThumbnails() {
00167 global $wgMemc;
00168 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
00169 $wgMemc->delete( $key );
00170 $files = $this->getThumbnails();
00171 $dir = $this->getThumbPath( $this->getName() );
00172 foreach ( $files as $file ) {
00173 unlink( $dir . $file );
00174 }
00175 if ( is_dir( $dir ) ) {
00176 rmdir( $dir );
00177 }
00178 }
00179 }