Merge "Added "maxPartitionsTry" option to JobQueueFederated"
[lhc/web/wiklou.git] / includes / filerepo / file / ForeignAPIFile.php
1 <?php
2 /**
3 * Foreign file accessible through api.php requests.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Foreign file accessible through api.php requests.
26 * Very hacky and inefficient, do not use :D
27 *
28 * @ingroup FileAbstraction
29 */
30 class ForeignAPIFile extends File {
31 private $mExists;
32
33 protected $repoClass = 'ForeignApiRepo';
34
35 /**
36 * @param $title
37 * @param $repo ForeignApiRepo
38 * @param $info
39 * @param bool $exists
40 */
41 function __construct( $title, $repo, $info, $exists = false ) {
42 parent::__construct( $title, $repo );
43
44 $this->mInfo = $info;
45 $this->mExists = $exists;
46
47 $this->assertRepoDefined();
48 }
49
50 /**
51 * @param $title Title
52 * @param $repo ForeignApiRepo
53 * @return ForeignAPIFile|null
54 */
55 static function newFromTitle( Title $title, $repo ) {
56 $data = $repo->fetchImageQuery( array(
57 'titles' => 'File:' . $title->getDBkey(),
58 'iiprop' => self::getProps(),
59 'prop' => 'imageinfo',
60 'iimetadataversion' => MediaHandler::getMetadataVersion(),
61 // extmetadata is language-dependant, accessing the current language here
62 // would be problematic, so we just get them all
63 'iiextmetadatamultilang' => 1,
64 ) );
65
66 $info = $repo->getImageInfo( $data );
67
68 if ( $info ) {
69 $lastRedirect = isset( $data['query']['redirects'] )
70 ? count( $data['query']['redirects'] ) - 1
71 : -1;
72 if ( $lastRedirect >= 0 ) {
73 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
74 $img = new self( $newtitle, $repo, $info, true );
75 if ( $img ) {
76 $img->redirectedFrom( $title->getDBkey() );
77 }
78 } else {
79 $img = new self( $title, $repo, $info, true );
80 }
81 return $img;
82 } else {
83 return null;
84 }
85 }
86
87 /**
88 * Get the property string for iiprop and aiprop
89 * @return string
90 */
91 static function getProps() {
92 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
93 }
94
95 // Dummy functions...
96
97 /**
98 * @return bool
99 */
100 public function exists() {
101 return $this->mExists;
102 }
103
104 /**
105 * @return bool
106 */
107 public function getPath() {
108 return false;
109 }
110
111 /**
112 * @param array $params
113 * @param int $flags
114 * @return bool|MediaTransformOutput
115 */
116 function transform( $params, $flags = 0 ) {
117 if ( !$this->canRender() ) {
118 // show icon
119 return parent::transform( $params, $flags );
120 }
121
122 // Note, the this->canRender() check above implies
123 // that we have a handler, and it can do makeParamString.
124 $otherParams = $this->handler->makeParamString( $params );
125 $width = isset( $params['width'] ) ? $params['width'] : -1;
126 $height = isset( $params['height'] ) ? $params['height'] : -1;
127
128 $thumbUrl = $this->repo->getThumbUrlFromCache(
129 $this->getName(),
130 $width,
131 $height,
132 $otherParams
133 );
134 if ( $thumbUrl === false ) {
135 global $wgLang;
136 return $this->repo->getThumbError(
137 $this->getName(),
138 $width,
139 $height,
140 $otherParams,
141 $wgLang->getCode()
142 );
143 }
144 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
145 }
146
147 // Info we can get from API...
148
149 /**
150 * @param $page int
151 * @return int|number
152 */
153 public function getWidth( $page = 1 ) {
154 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
155 }
156
157 /**
158 * @param $page int
159 * @return int
160 */
161 public function getHeight( $page = 1 ) {
162 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
163 }
164
165 /**
166 * @return bool|null|string
167 */
168 public function getMetadata() {
169 if ( isset( $this->mInfo['metadata'] ) ) {
170 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
171 }
172 return null;
173 }
174
175 /**
176 * @return array|null extended metadata (see imageinfo API for format) or null on error
177 */
178 public function getExtendedMetadata() {
179 if ( isset( $this->mInfo['extmetadata'] ) ) {
180 return $this->mInfo['extmetadata'];
181 }
182 return null;
183 }
184
185 /**
186 * @param $metadata array
187 * @return array
188 */
189 public static function parseMetadata( $metadata ) {
190 if ( !is_array( $metadata ) ) {
191 return $metadata;
192 }
193 $ret = array();
194 foreach ( $metadata as $meta ) {
195 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
196 }
197 return $ret;
198 }
199
200 /**
201 * @return bool|int|null
202 */
203 public function getSize() {
204 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
205 }
206
207 /**
208 * @return null|string
209 */
210 public function getUrl() {
211 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
212 }
213
214 /**
215 * @param string $method
216 * @return int|null|string
217 */
218 public function getUser( $method = 'text' ) {
219 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
220 }
221
222 /**
223 * @return null|string
224 */
225 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
226 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
227 }
228
229 /**
230 * @return null|String
231 */
232 function getSha1() {
233 return isset( $this->mInfo['sha1'] )
234 ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
235 : null;
236 }
237
238 /**
239 * @return bool|Mixed|string
240 */
241 function getTimestamp() {
242 return wfTimestamp( TS_MW,
243 isset( $this->mInfo['timestamp'] )
244 ? strval( $this->mInfo['timestamp'] )
245 : null
246 );
247 }
248
249 /**
250 * @return string
251 */
252 function getMimeType() {
253 if ( !isset( $this->mInfo['mime'] ) ) {
254 $magic = MimeMagic::singleton();
255 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
256 }
257 return $this->mInfo['mime'];
258 }
259
260 /**
261 * @return int|string
262 */
263 function getMediaType() {
264 if ( isset( $this->mInfo['mediatype'] ) ) {
265 return $this->mInfo['mediatype'];
266 }
267 $magic = MimeMagic::singleton();
268 return $magic->getMediaType( null, $this->getMimeType() );
269 }
270
271 /**
272 * @return bool|string
273 */
274 function getDescriptionUrl() {
275 return isset( $this->mInfo['descriptionurl'] )
276 ? $this->mInfo['descriptionurl']
277 : false;
278 }
279
280 /**
281 * Only useful if we're locally caching thumbs anyway...
282 * @param $suffix string
283 * @return null|string
284 */
285 function getThumbPath( $suffix = '' ) {
286 if ( $this->repo->canCacheThumbs() ) {
287 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
288 if ( $suffix ) {
289 $path = $path . $suffix . '/';
290 }
291 return $path;
292 } else {
293 return null;
294 }
295 }
296
297 /**
298 * @return array
299 */
300 function getThumbnails() {
301 $dir = $this->getThumbPath( $this->getName() );
302 $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
303
304 $files = array();
305 foreach ( $iter as $file ) {
306 $files[] = $file;
307 }
308
309 return $files;
310 }
311
312 /**
313 * @see File::purgeCache()
314 */
315 function purgeCache( $options = array() ) {
316 $this->purgeThumbnails( $options );
317 $this->purgeDescriptionPage();
318 }
319
320 function purgeDescriptionPage() {
321 global $wgMemc, $wgContLang;
322
323 $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
324 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
325
326 $wgMemc->delete( $key );
327 }
328
329 /**
330 * @param $options array
331 */
332 function purgeThumbnails( $options = array() ) {
333 global $wgMemc;
334
335 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
336 $wgMemc->delete( $key );
337
338 $files = $this->getThumbnails();
339 // Give media handler a chance to filter the purge list
340 $handler = $this->getHandler();
341 if ( $handler ) {
342 $handler->filterThumbnailPurgeList( $files, $options );
343 }
344
345 $dir = $this->getThumbPath( $this->getName() );
346 $purgeList = array();
347 foreach ( $files as $file ) {
348 $purgeList[] = "{$dir}{$file}";
349 }
350
351 # Delete the thumbnails
352 $this->repo->quickPurgeBatch( $purgeList );
353 # Clear out the thumbnail directory if empty
354 $this->repo->quickCleanDir( $dir );
355 }
356 }