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 if( $thumbUrl ) {
00047 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
00048 }
00049 return false;
00050 }
00051
00052
00053 public function getWidth( $page = 1 ) {
00054 return intval( @$this->mInfo['width'] );
00055 }
00056
00057 public function getHeight( $page = 1 ) {
00058 return intval( @$this->mInfo['height'] );
00059 }
00060
00061 public function getMetadata() {
00062 if ( isset( $this->mInfo['metadata'] ) ) {
00063 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
00064 }
00065 return null;
00066 }
00067
00068 public static function parseMetadata( $metadata ) {
00069 if( !is_array( $metadata ) ) {
00070 return $metadata;
00071 }
00072 $ret = array();
00073 foreach( $metadata as $meta ) {
00074 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
00075 }
00076 return $ret;
00077 }
00078
00079 public function getSize() {
00080 return intval( @$this->mInfo['size'] );
00081 }
00082
00083 public function getUrl() {
00084 return strval( @$this->mInfo['url'] );
00085 }
00086
00087 public function getUser( $method='text' ) {
00088 return strval( @$this->mInfo['user'] );
00089 }
00090
00091 public function getDescription() {
00092 return strval( @$this->mInfo['comment'] );
00093 }
00094
00095 function getSha1() {
00096 return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
00097 }
00098
00099 function getTimestamp() {
00100 return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
00101 }
00102
00103 function getMimeType() {
00104 if( !isset( $this->mInfo['mime'] ) ) {
00105 $magic = MimeMagic::singleton();
00106 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
00107 }
00108 return $this->mInfo['mime'];
00109 }
00110
00112 function getMediaType() {
00113 $magic = MimeMagic::singleton();
00114 return $magic->getMediaType( null, $this->getMimeType() );
00115 }
00116
00117 function getDescriptionUrl() {
00118 return isset( $this->mInfo['descriptionurl'] )
00119 ? $this->mInfo['descriptionurl']
00120 : false;
00121 }
00122
00126 function getThumbPath( $suffix = '' ) {
00127 if ( $this->repo->canCacheThumbs() ) {
00128 global $wgUploadDirectory;
00129 $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
00130 if ( $suffix ) {
00131 $path = $path . $suffix . '/';
00132 }
00133 return $path;
00134 }
00135 else {
00136 return null;
00137 }
00138 }
00139
00140 function getThumbnails() {
00141 $files = array();
00142 $dir = $this->getThumbPath( $this->getName() );
00143 if ( is_dir( $dir ) ) {
00144 $handle = opendir( $dir );
00145 if ( $handle ) {
00146 while ( false !== ( $file = readdir($handle) ) ) {
00147 if ( $file{0} != '.' ) {
00148 $files[] = $file;
00149 }
00150 }
00151 closedir( $handle );
00152 }
00153 }
00154 return $files;
00155 }
00156
00157 function purgeCache() {
00158 $this->purgeThumbnails();
00159 $this->purgeDescriptionPage();
00160 }
00161
00162 function purgeDescriptionPage() {
00163 global $wgMemc, $wgContLang;
00164 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
00165 $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
00166 $wgMemc->delete( $key );
00167 }
00168
00169 function purgeThumbnails() {
00170 global $wgMemc;
00171 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
00172 $wgMemc->delete( $key );
00173 $files = $this->getThumbnails();
00174 $dir = $this->getThumbPath( $this->getName() );
00175 foreach ( $files as $file ) {
00176 unlink( $dir . $file );
00177 }
00178 if ( is_dir( $dir ) ) {
00179 rmdir( $dir );
00180 }
00181 }
00182 }