Merge "[FileBackend] Added some basic directory functions."
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * @file
6 * @ingroup FileAbstraction
7 */
8
9 /**
10 * Foreign file accessible through api.php requests.
11 * Very hacky and inefficient, do not use :D
12 *
13 * @ingroup FileAbstraction
14 */
15 class ForeignAPIFile extends File {
16 private $mExists;
17
18 protected $repoClass = 'ForeignApiRepo';
19
20 /**
21 * @param $title
22 * @param $repo ForeignApiRepo
23 * @param $info
24 * @param bool $exists
25 */
26 function __construct( $title, $repo, $info, $exists = false ) {
27 parent::__construct( $title, $repo );
28
29 $this->mInfo = $info;
30 $this->mExists = $exists;
31
32 $this->assertRepoDefined();
33 }
34
35 /**
36 * @param $title Title
37 * @param $repo ForeignApiRepo
38 * @return ForeignAPIFile|null
39 */
40 static function newFromTitle( Title $title, $repo ) {
41 $data = $repo->fetchImageQuery( array(
42 'titles' => 'File:' . $title->getDBKey(),
43 'iiprop' => self::getProps(),
44 'prop' => 'imageinfo',
45 'iimetadataversion' => MediaHandler::getMetadataVersion()
46 ) );
47
48 $info = $repo->getImageInfo( $data );
49
50 if( $info ) {
51 $lastRedirect = isset( $data['query']['redirects'] )
52 ? count( $data['query']['redirects'] ) - 1
53 : -1;
54 if( $lastRedirect >= 0 ) {
55 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to']);
56 $img = new self( $newtitle, $repo, $info, true );
57 if( $img ) {
58 $img->redirectedFrom( $title->getDBkey() );
59 }
60 } else {
61 $img = new self( $title, $repo, $info, true );
62 }
63 return $img;
64 } else {
65 return null;
66 }
67 }
68
69 /**
70 * Get the property string for iiprop and aiprop
71 * @return string
72 */
73 static function getProps() {
74 return 'timestamp|user|comment|url|size|sha1|metadata|mime';
75 }
76
77 // Dummy functions...
78 public function exists() {
79 return $this->mExists;
80 }
81
82 public function getPath() {
83 return false;
84 }
85
86 function transform( $params, $flags = 0 ) {
87 if( !$this->canRender() ) {
88 // show icon
89 return parent::transform( $params, $flags );
90 }
91
92 // Note, the this->canRender() check above implies
93 // that we have a handler, and it can do makeParamString.
94 $otherParams = $this->handler->makeParamString( $params );
95
96 $thumbUrl = $this->repo->getThumbUrlFromCache(
97 $this->getName(),
98 isset( $params['width'] ) ? $params['width'] : -1,
99 isset( $params['height'] ) ? $params['height'] : -1,
100 $otherParams );
101 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
102 }
103
104 // Info we can get from API...
105 public function getWidth( $page = 1 ) {
106 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
107 }
108
109 /**
110 * @param $page int
111 * @return int
112 */
113 public function getHeight( $page = 1 ) {
114 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
115 }
116
117 public function getMetadata() {
118 if ( isset( $this->mInfo['metadata'] ) ) {
119 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
120 }
121 return null;
122 }
123
124 public static function parseMetadata( $metadata ) {
125 if( !is_array( $metadata ) ) {
126 return $metadata;
127 }
128 $ret = array();
129 foreach( $metadata as $meta ) {
130 $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
131 }
132 return $ret;
133 }
134
135 public function getSize() {
136 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
137 }
138
139 public function getUrl() {
140 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
141 }
142
143 public function getUser( $method='text' ) {
144 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
145 }
146
147 public function getDescription() {
148 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
149 }
150
151 function getSha1() {
152 return isset( $this->mInfo['sha1'] )
153 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
154 : null;
155 }
156
157 function getTimestamp() {
158 return wfTimestamp( TS_MW,
159 isset( $this->mInfo['timestamp'] )
160 ? strval( $this->mInfo['timestamp'] )
161 : null
162 );
163 }
164
165 function getMimeType() {
166 if( !isset( $this->mInfo['mime'] ) ) {
167 $magic = MimeMagic::singleton();
168 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
169 }
170 return $this->mInfo['mime'];
171 }
172
173 /// @todo FIXME: May guess wrong on file types that can be eg audio or video
174 function getMediaType() {
175 $magic = MimeMagic::singleton();
176 return $magic->getMediaType( null, $this->getMimeType() );
177 }
178
179 function getDescriptionUrl() {
180 return isset( $this->mInfo['descriptionurl'] )
181 ? $this->mInfo['descriptionurl']
182 : false;
183 }
184
185 /**
186 * Only useful if we're locally caching thumbs anyway...
187 * @return null|string
188 */
189 function getThumbPath( $suffix = '' ) {
190 if ( $this->repo->canCacheThumbs() ) {
191 $path = $this->repo->getZonePath('thumb') . '/' . $this->getHashPath( $this->getName() );
192 if ( $suffix ) {
193 $path = $path . $suffix . '/';
194 }
195 return $path;
196 } else {
197 return null;
198 }
199 }
200
201 function getThumbnails() {
202 $dir = $this->getThumbPath( $this->getName() );
203 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
204
205 $files = array();
206 foreach ( $iter as $file ) {
207 $files[] = $file;
208 }
209
210 return $files;
211 }
212
213 /**
214 * @see File::purgeCache()
215 */
216 function purgeCache( $options = array() ) {
217 $this->purgeThumbnails( $options );
218 $this->purgeDescriptionPage();
219 }
220
221 function purgeDescriptionPage() {
222 global $wgMemc, $wgContLang;
223
224 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
225 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5($url) );
226
227 $wgMemc->delete( $key );
228 }
229
230 function purgeThumbnails( $options = array() ) {
231 global $wgMemc;
232
233 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
234 $wgMemc->delete( $key );
235
236 $files = $this->getThumbnails();
237 // Give media handler a chance to filter the purge list
238 $handler = $this->getHandler();
239 if ( $handler ) {
240 $handler->filterThumbnailPurgeList( $files, $options );
241 }
242
243 $dir = $this->getThumbPath( $this->getName() );
244 $purgeList = array();
245 foreach ( $files as $file ) {
246 $purgeList[] = "{$dir}{$file}";
247 }
248
249 # Delete the thumbnails
250 $this->repo->quickPurgeBatch( $purgeList );
251 # Clear out the thumbnail directory if empty
252 $this->repo->quickCleanDir( $dir );
253 }
254 }