Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Foreign file accessible through api.php requests.
28 * Very hacky and inefficient, do not use :D
29 *
30 * @ingroup FileAbstraction
31 */
32 class ForeignAPIFile extends File {
33 /** @var bool */
34 private $mExists;
35 /** @var array */
36 private $mInfo = [];
37
38 protected $repoClass = ForeignAPIRepo::class;
39
40 /**
41 * @param Title|string|bool $title
42 * @param ForeignApiRepo $repo
43 * @param array $info
44 * @param bool $exists
45 */
46 function __construct( $title, $repo, $info, $exists = false ) {
47 parent::__construct( $title, $repo );
48
49 $this->mInfo = $info;
50 $this->mExists = $exists;
51
52 $this->assertRepoDefined();
53 }
54
55 /**
56 * @param Title $title
57 * @param ForeignApiRepo $repo
58 * @return ForeignAPIFile|null
59 */
60 static function newFromTitle( Title $title, $repo ) {
61 $data = $repo->fetchImageQuery( [
62 'titles' => 'File:' . $title->getDBkey(),
63 'iiprop' => self::getProps(),
64 'prop' => 'imageinfo',
65 'iimetadataversion' => MediaHandler::getMetadataVersion(),
66 // extmetadata is language-dependant, accessing the current language here
67 // would be problematic, so we just get them all
68 'iiextmetadatamultilang' => 1,
69 ] );
70
71 $info = $repo->getImageInfo( $data );
72
73 if ( $info ) {
74 $lastRedirect = isset( $data['query']['redirects'] )
75 ? count( $data['query']['redirects'] ) - 1
76 : -1;
77 if ( $lastRedirect >= 0 ) {
78 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
79 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
80 $img = new self( $newtitle, $repo, $info, true );
81 $img->redirectedFrom( $title->getDBkey() );
82 } else {
83 $img = new self( $title, $repo, $info, true );
84 }
85
86 return $img;
87 } else {
88 return null;
89 }
90 }
91
92 /**
93 * Get the property string for iiprop and aiprop
94 * @return string
95 */
96 static function getProps() {
97 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
98 }
99
100 // Dummy functions...
101
102 /**
103 * @return bool
104 */
105 public function exists() {
106 return $this->mExists;
107 }
108
109 /**
110 * @return bool
111 */
112 public function getPath() {
113 return false;
114 }
115
116 /**
117 * @param array $params
118 * @param int $flags
119 * @return bool|MediaTransformOutput
120 */
121 function transform( $params, $flags = 0 ) {
122 if ( !$this->canRender() ) {
123 // show icon
124 return parent::transform( $params, $flags );
125 }
126
127 // Note, the this->canRender() check above implies
128 // that we have a handler, and it can do makeParamString.
129 $otherParams = $this->handler->makeParamString( $params );
130 $width = $params['width'] ?? -1;
131 $height = $params['height'] ?? -1;
132
133 $thumbUrl = $this->repo->getThumbUrlFromCache(
134 $this->getName(),
135 $width,
136 $height,
137 $otherParams
138 );
139 if ( $thumbUrl === false ) {
140 global $wgLang;
141
142 return $this->repo->getThumbError(
143 $this->getName(),
144 $width,
145 $height,
146 $otherParams,
147 $wgLang->getCode()
148 );
149 }
150
151 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
152 }
153
154 // Info we can get from API...
155
156 /**
157 * @param int $page
158 * @return int
159 */
160 public function getWidth( $page = 1 ) {
161 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
162 }
163
164 /**
165 * @param int $page
166 * @return int
167 */
168 public function getHeight( $page = 1 ) {
169 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
170 }
171
172 /**
173 * @return bool|null|string
174 */
175 public function getMetadata() {
176 if ( isset( $this->mInfo['metadata'] ) ) {
177 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
178 }
179
180 return null;
181 }
182
183 /**
184 * @return array|null Extended metadata (see imageinfo API for format) or
185 * null on error
186 */
187 public function getExtendedMetadata() {
188 return $this->mInfo['extmetadata'] ?? null;
189 }
190
191 /**
192 * @param mixed $metadata
193 * @return mixed
194 */
195 public static function parseMetadata( $metadata ) {
196 if ( !is_array( $metadata ) ) {
197 return $metadata;
198 }
199 $ret = [];
200 foreach ( $metadata as $meta ) {
201 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
202 }
203
204 return $ret;
205 }
206
207 /**
208 * @return bool|int|null
209 */
210 public function getSize() {
211 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
212 }
213
214 /**
215 * @return null|string
216 */
217 public function getUrl() {
218 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
219 }
220
221 /**
222 * Get short description URL for a file based on the foreign API response,
223 * or if unavailable, the short URL is constructed from the foreign page ID.
224 *
225 * @return null|string
226 * @since 1.27
227 */
228 public function getDescriptionShortUrl() {
229 if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
230 return $this->mInfo['descriptionshorturl'];
231 } elseif ( isset( $this->mInfo['pageid'] ) ) {
232 $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
233 if ( $url !== false ) {
234 return $url;
235 }
236 }
237 return null;
238 }
239
240 /**
241 * @param string $type
242 * @return int|null|string
243 */
244 public function getUser( $type = 'text' ) {
245 if ( $type == 'text' ) {
246 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
247 } else {
248 return 0; // What makes sense here, for a remote user?
249 }
250 }
251
252 /**
253 * @param int $audience
254 * @param User|null $user
255 * @return null|string
256 */
257 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
258 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
259 }
260
261 /**
262 * @return null|string
263 */
264 function getSha1() {
265 return isset( $this->mInfo['sha1'] )
266 ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
267 : null;
268 }
269
270 /**
271 * @return bool|string
272 */
273 function getTimestamp() {
274 return wfTimestamp( TS_MW,
275 isset( $this->mInfo['timestamp'] )
276 ? strval( $this->mInfo['timestamp'] )
277 : null
278 );
279 }
280
281 /**
282 * @return string
283 */
284 function getMimeType() {
285 if ( !isset( $this->mInfo['mime'] ) ) {
286 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
287 $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
288 }
289
290 return $this->mInfo['mime'];
291 }
292
293 /**
294 * @return int|string
295 */
296 function getMediaType() {
297 if ( isset( $this->mInfo['mediatype'] ) ) {
298 return $this->mInfo['mediatype'];
299 }
300 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
301
302 return $magic->getMediaType( null, $this->getMimeType() );
303 }
304
305 /**
306 * @return bool|string
307 */
308 function getDescriptionUrl() {
309 return $this->mInfo['descriptionurl'] ?? false;
310 }
311
312 /**
313 * Only useful if we're locally caching thumbs anyway...
314 * @param string $suffix
315 * @return null|string
316 */
317 function getThumbPath( $suffix = '' ) {
318 if ( !$this->repo->canCacheThumbs() ) {
319 return null;
320 }
321
322 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
323 if ( $suffix ) {
324 $path .= $suffix . '/';
325 }
326 return $path;
327 }
328
329 /**
330 * @return string[]
331 */
332 function getThumbnails() {
333 $dir = $this->getThumbPath( $this->getName() );
334 $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
335
336 $files = [];
337 if ( $iter ) {
338 foreach ( $iter as $file ) {
339 $files[] = $file;
340 }
341 }
342
343 return $files;
344 }
345
346 function purgeCache( $options = [] ) {
347 $this->purgeThumbnails( $options );
348 $this->purgeDescriptionPage();
349 }
350
351 function purgeDescriptionPage() {
352 $services = MediaWikiServices::getInstance();
353 $url = $this->repo->getDescriptionRenderUrl(
354 $this->getName(), $services->getContentLanguage()->getCode() );
355 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
356
357 $services->getMainWANObjectCache()->delete( $key );
358 }
359
360 /**
361 * @param array $options
362 */
363 function purgeThumbnails( $options = [] ) {
364 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
365 MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
366
367 $files = $this->getThumbnails();
368 // Give media handler a chance to filter the purge list
369 $handler = $this->getHandler();
370 if ( $handler ) {
371 $handler->filterThumbnailPurgeList( $files, $options );
372 }
373
374 $dir = $this->getThumbPath( $this->getName() );
375 $purgeList = [];
376 foreach ( $files as $file ) {
377 $purgeList[] = "{$dir}{$file}";
378 }
379
380 # Delete the thumbnails
381 $this->repo->quickPurgeBatch( $purgeList );
382 # Clear out the thumbnail directory if empty
383 $this->repo->quickCleanDir( $dir );
384 }
385
386 /**
387 * The thumbnail is created on the foreign server and fetched over internet
388 * @since 1.25
389 * @return bool
390 */
391 public function isTransformedLocally() {
392 return false;
393 }
394 }