MediaWiki master
ForeignAPIFile.php
Go to the documentation of this file.
1<?php
26
32class ForeignAPIFile extends File {
34 private $mExists;
36 private $mInfo;
37
39 protected $repoClass = ForeignAPIRepo::class;
40
47 public function __construct( $title, $repo, $info, $exists = false ) {
48 parent::__construct( $title, $repo );
49
50 $this->mInfo = $info;
51 $this->mExists = $exists;
52
53 $this->assertRepoDefined();
54 }
55
61 public static function newFromTitle( Title $title, $repo ) {
62 $data = $repo->fetchImageQuery( [
63 'titles' => 'File:' . $title->getDBkey(),
64 'iiprop' => self::getProps(),
65 'prop' => 'imageinfo',
66 'iimetadataversion' => MediaHandler::getMetadataVersion(),
67 // extmetadata is language-dependent, accessing the current language here
68 // would be problematic, so we just get them all
69 'iiextmetadatamultilang' => 1,
70 ] );
71
72 $info = $repo->getImageInfo( $data );
73
74 if ( $info ) {
75 $lastRedirect = count( $data['query']['redirects'] ?? [] ) - 1;
76 if ( $lastRedirect >= 0 ) {
77 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
78 $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
79 $img = new self( $newtitle, $repo, $info, true );
80 $img->redirectedFrom( $title->getDBkey() );
81 } else {
82 $img = new self( $title, $repo, $info, true );
83 }
84
85 return $img;
86 } else {
87 return null;
88 }
89 }
90
95 public static function getProps() {
96 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
97 }
98
102 public function getRepo() {
103 return $this->repo;
104 }
105
106 // Dummy functions...
107
111 public function exists() {
112 return $this->mExists;
113 }
114
118 public function getPath() {
119 return false;
120 }
121
127 public function transform( $params, $flags = 0 ) {
128 if ( !$this->canRender() ) {
129 // show icon
130 return parent::transform( $params, $flags );
131 }
132
133 // Note, the this->canRender() check above implies
134 // that we have a handler, and it can do makeParamString.
135 $otherParams = $this->handler->makeParamString( $params );
136 $width = $params['width'] ?? -1;
137 $height = $params['height'] ?? -1;
138 $thumbUrl = false;
139
140 if ( $width > 0 || $height > 0 ) {
141 // Only query the remote if there are dimensions
142 $thumbUrl = $this->repo->getThumbUrlFromCache(
143 $this->getName(),
144 $width,
145 $height,
146 $otherParams
147 );
148 } elseif ( $this->getMediaType() === MEDIATYPE_AUDIO ) {
149 // This has no dimensions, but we still need to pass a value to getTransform()
150 $thumbUrl = '/';
151 }
152 if ( $thumbUrl === false ) {
153 global $wgLang;
154
155 return $this->repo->getThumbError(
156 $this->getName(),
157 $width,
158 $height,
159 $otherParams,
160 $wgLang->getCode()
161 );
162 }
163
164 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
165 }
166
167 // Info we can get from API...
168
173 public function getWidth( $page = 1 ) {
174 return (int)( $this->mInfo['width'] ?? 0 );
175 }
176
181 public function getHeight( $page = 1 ) {
182 return (int)( $this->mInfo['height'] ?? 0 );
183 }
184
188 public function getMetadata() {
189 if ( isset( $this->mInfo['metadata'] ) ) {
190 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
191 }
192
193 return false;
194 }
195
199 public function getMetadataArray(): array {
200 if ( isset( $this->mInfo['metadata'] ) ) {
201 return self::parseMetadata( $this->mInfo['metadata'] );
202 }
203
204 return [];
205 }
206
211 public function getExtendedMetadata() {
212 return $this->mInfo['extmetadata'] ?? null;
213 }
214
219 public static function parseMetadata( $metadata ) {
220 if ( !is_array( $metadata ) ) {
221 return [ '_error' => $metadata ];
222 }
223 '@phan-var array[] $metadata';
224 $ret = [];
225 foreach ( $metadata as $meta ) {
226 $ret[$meta['name']] = self::parseMetadataValue( $meta['value'] );
227 }
228
229 return $ret;
230 }
231
236 private static function parseMetadataValue( $metadata ) {
237 if ( !is_array( $metadata ) ) {
238 return $metadata;
239 }
240 '@phan-var array[] $metadata';
241 $ret = [];
242 foreach ( $metadata as $meta ) {
243 $ret[$meta['name']] = self::parseMetadataValue( $meta['value'] );
244 }
245
246 return $ret;
247 }
248
252 public function getSize() {
253 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
254 }
255
259 public function getUrl() {
260 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
261 }
262
270 public function getDescriptionShortUrl() {
271 if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
272 return $this->mInfo['descriptionshorturl'];
273 } elseif ( isset( $this->mInfo['pageid'] ) ) {
274 $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
275 if ( $url !== false ) {
276 return $url;
277 }
278 }
279 return null;
280 }
281
282 public function getUploader( int $audience = self::FOR_PUBLIC, ?Authority $performer = null ): ?UserIdentity {
283 if ( isset( $this->mInfo['user'] ) ) {
284 return UserIdentityValue::newExternal( $this->getRepoName(), $this->mInfo['user'] );
285 }
286 return null;
287 }
288
294 public function getDescription( $audience = self::FOR_PUBLIC, ?Authority $performer = null ) {
295 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
296 }
297
301 public function getSha1() {
302 return isset( $this->mInfo['sha1'] )
303 ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
304 : null;
305 }
306
310 public function getTimestamp() {
311 return wfTimestamp( TS_MW,
312 isset( $this->mInfo['timestamp'] )
313 ? strval( $this->mInfo['timestamp'] )
314 : null
315 );
316 }
317
321 public function getMimeType() {
322 if ( !isset( $this->mInfo['mime'] ) ) {
323 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
324 $this->mInfo['mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() );
325 }
326
327 return $this->mInfo['mime'];
328 }
329
333 public function getMediaType() {
334 if ( isset( $this->mInfo['mediatype'] ) ) {
335 return $this->mInfo['mediatype'];
336 }
337 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
338
339 return $magic->getMediaType( null, $this->getMimeType() );
340 }
341
345 public function getDescriptionUrl() {
346 return $this->mInfo['descriptionurl'] ?? false;
347 }
348
354 public function getThumbPath( $suffix = '' ) {
355 if ( !$this->repo->canCacheThumbs() ) {
356 return null;
357 }
358
359 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
360 if ( $suffix ) {
361 $path .= $suffix . '/';
362 }
363 return $path;
364 }
365
369 protected function getThumbnails() {
370 $dir = $this->getThumbPath( $this->getName() );
371 $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
372
373 $files = [];
374 if ( $iter ) {
375 foreach ( $iter as $file ) {
376 $files[] = $file;
377 }
378 }
379
380 return $files;
381 }
382
383 public function purgeCache( $options = [] ) {
384 $this->purgeThumbnails( $options );
385 $this->purgeDescriptionPage();
386 }
387
388 private function purgeDescriptionPage() {
389 $services = MediaWikiServices::getInstance();
390 $langCode = $services->getContentLanguageCode()->toString();
391
392 // Key must match File::getDescriptionText
393 $key = $this->repo->getLocalCacheKey( 'file-remote-description', $langCode, md5( $this->getName() ) );
394 $services->getMainWANObjectCache()->delete( $key );
395 }
396
400 public function purgeThumbnails( $options = [] ) {
401 $key = $this->repo->getLocalCacheKey( 'file-thumb-url', sha1( $this->getName() ) );
402 MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
403
404 $files = $this->getThumbnails();
405 // Give media handler a chance to filter the purge list
406 $handler = $this->getHandler();
407 if ( $handler ) {
408 $handler->filterThumbnailPurgeList( $files, $options );
409 }
410
411 $dir = $this->getThumbPath( $this->getName() );
412 $purgeList = [];
413 foreach ( $files as $file ) {
414 $purgeList[] = "{$dir}{$file}";
415 }
416
417 # Delete the thumbnails
418 $this->repo->quickPurgeBatch( $purgeList );
419 # Clear out the thumbnail directory if empty
420 $this->repo->quickCleanDir( $dir );
421 }
422
428 public function isTransformedLocally() {
429 return false;
430 }
431}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:541
array $params
The job parameters.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition File.php:2486
getName()
Return the name of this file.
Definition File.php:347
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:126
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition File.php:889
Title string false $title
Definition File.php:129
Foreign file accessible through api.php requests.
getThumbPath( $suffix='')
Only useful if we're locally caching thumbs anyway...
__construct( $title, $repo, $info, $exists=false)
purgeCache( $options=[])
Purge shared caches such as thumbnails and DB data caching STUB Overridden by LocalFile.
isTransformedLocally()
The thumbnail is created on the foreign server and fetched over internet.
static parseMetadata( $metadata)
static newFromTitle(Title $title, $repo)
purgeThumbnails( $options=[])
getUploader(int $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Get the identity of the file uploader.
transform( $params, $flags=0)
getDescription( $audience=self::FOR_PUBLIC, ?Authority $performer=null)
getDescriptionShortUrl()
Get short description URL for a file based on the foreign API response, or if unavailable,...
static getProps()
Get the property string for iiprop and aiprop.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:78
getDBkey()
Get the main part with underscores.
Definition Title.php:1034
Value object representing a user's identity.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
Interface for objects representing user identity.
const MEDIATYPE_AUDIO
Definition defines.php:33