MediaWiki REL1_35
ForeignAPIFile.php
Go to the documentation of this file.
1<?php
25
32class ForeignAPIFile extends File {
34 private $mExists;
36 private $mInfo = [];
37
38 protected $repoClass = ForeignAPIRepo::class;
39
46 public 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
60 public 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
96 public static function getProps() {
97 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
98 }
99
103 public function getRepo() {
104 return $this->repo;
105 }
106
107 // Dummy functions...
108
112 public function exists() {
113 return $this->mExists;
114 }
115
119 public function getPath() {
120 return false;
121 }
122
128 public function transform( $params, $flags = 0 ) {
129 if ( !$this->canRender() ) {
130 // show icon
131 return parent::transform( $params, $flags );
132 }
133
134 // Note, the this->canRender() check above implies
135 // that we have a handler, and it can do makeParamString.
136 $otherParams = $this->handler->makeParamString( $params );
137 $width = $params['width'] ?? -1;
138 $height = $params['height'] ?? -1;
139
140 $thumbUrl = $this->repo->getThumbUrlFromCache(
141 $this->getName(),
142 $width,
143 $height,
144 $otherParams
145 );
146 if ( $thumbUrl === false ) {
147 global $wgLang;
148
149 return $this->repo->getThumbError(
150 $this->getName(),
151 $width,
152 $height,
153 $otherParams,
154 $wgLang->getCode()
155 );
156 }
157
158 return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
159 }
160
161 // Info we can get from API...
162
167 public function getWidth( $page = 1 ) {
168 return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
169 }
170
175 public function getHeight( $page = 1 ) {
176 return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
177 }
178
182 public function getMetadata() {
183 if ( isset( $this->mInfo['metadata'] ) ) {
184 return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
185 }
186
187 return null;
188 }
189
194 public function getExtendedMetadata() {
195 return $this->mInfo['extmetadata'] ?? null;
196 }
197
202 public static function parseMetadata( $metadata ) {
203 if ( !is_array( $metadata ) ) {
204 return $metadata;
205 }
206 '@phan-var array[] $metadata';
207 $ret = [];
208 foreach ( $metadata as $meta ) {
209 $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
210 }
211
212 return $ret;
213 }
214
218 public function getSize() {
219 return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
220 }
221
225 public function getUrl() {
226 return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
227 }
228
236 public function getDescriptionShortUrl() {
237 if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
238 return $this->mInfo['descriptionshorturl'];
239 } elseif ( isset( $this->mInfo['pageid'] ) ) {
240 $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
241 if ( $url !== false ) {
242 return $url;
243 }
244 }
245 return null;
246 }
247
252 public function getUser( $type = 'text' ) {
253 if ( $type == 'text' ) {
254 return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
255 } else {
256 return 0; // What makes sense here, for a remote user?
257 }
258 }
259
265 public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
266 return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
267 }
268
272 public function getSha1() {
273 return isset( $this->mInfo['sha1'] )
274 ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
275 : null;
276 }
277
281 public function getTimestamp() {
282 return wfTimestamp( TS_MW,
283 isset( $this->mInfo['timestamp'] )
284 ? strval( $this->mInfo['timestamp'] )
285 : null
286 );
287 }
288
292 public function getMimeType() {
293 if ( !isset( $this->mInfo['mime'] ) ) {
294 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
295 $this->mInfo['mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() );
296 }
297
298 return $this->mInfo['mime'];
299 }
300
304 public function getMediaType() {
305 if ( isset( $this->mInfo['mediatype'] ) ) {
306 return $this->mInfo['mediatype'];
307 }
308 $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
309
310 return $magic->getMediaType( null, $this->getMimeType() );
311 }
312
316 public function getDescriptionUrl() {
317 return $this->mInfo['descriptionurl'] ?? false;
318 }
319
325 public function getThumbPath( $suffix = '' ) {
326 if ( !$this->repo->canCacheThumbs() ) {
327 return null;
328 }
329
330 $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
331 if ( $suffix ) {
332 $path .= $suffix . '/';
333 }
334 return $path;
335 }
336
340 protected function getThumbnails() {
341 $dir = $this->getThumbPath( $this->getName() );
342 $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
343
344 $files = [];
345 if ( $iter ) {
346 foreach ( $iter as $file ) {
347 $files[] = $file;
348 }
349 }
350
351 return $files;
352 }
353
354 public function purgeCache( $options = [] ) {
355 $this->purgeThumbnails( $options );
356 $this->purgeDescriptionPage();
357 }
358
359 private function purgeDescriptionPage() {
360 $services = MediaWikiServices::getInstance();
361 $url = $this->repo->getDescriptionRenderUrl(
362 $this->getName(), $services->getContentLanguage()->getCode() );
363 $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
364
365 $services->getMainWANObjectCache()->delete( $key );
366 }
367
371 public function purgeThumbnails( $options = [] ) {
372 $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
373 MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
374
375 $files = $this->getThumbnails();
376 // Give media handler a chance to filter the purge list
377 $handler = $this->getHandler();
378 if ( $handler ) {
379 $handler->filterThumbnailPurgeList( $files, $options );
380 }
381
382 $dir = $this->getThumbPath( $this->getName() );
383 $purgeList = [];
384 foreach ( $files as $file ) {
385 $purgeList[] = "{$dir}{$file}";
386 }
387
388 # Delete the thumbnails
389 $this->repo->quickPurgeBatch( $purgeList );
390 # Clear out the thumbnail directory if empty
391 $this->repo->quickCleanDir( $dir );
392 }
393
399 public function isTransformedLocally() {
400 return false;
401 }
402}
serialize()
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
$wgLang
Definition Setup.php:781
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:63
string $url
The URL corresponding to one of the four basic zones.
Definition File.php:131
MediaHandler $handler
Definition File.php:128
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition File.php:2421
getName()
Return the name of this file.
Definition File.php:315
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition File.php:821
getExtension()
Get the file extension, e.g.
Definition File.php:330
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition File.php:110
string $path
The storage path corresponding to one of the zones.
Definition File.php:140
Title string bool $title
Definition File.php:113
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1459
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition File.php:1601
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 Stable to ove...
isTransformedLocally()
The thumbnail is created on the foreign server and fetched over internet.
static parseMetadata( $metadata)
static newFromTitle(Title $title, $repo)
getUser( $type='text')
purgeThumbnails( $options=[])
getDescription( $audience=self::FOR_PUBLIC, User $user=null)
transform( $params, $flags=0)
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.
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
static getMetadataVersion()
Get metadata version.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:42
getDBkey()
Get the main part with underscores.
Definition Title.php:1032
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
return true
Definition router.php:92
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42