MediaWiki  master
ForeignAPIFile.php
Go to the documentation of this file.
1 <?php
26 
32 class 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-dependent, 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 = count( $data['query']['redirects'] ?? [] ) - 1;
75  if ( $lastRedirect >= 0 ) {
76  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
77  $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
78  $img = new self( $newtitle, $repo, $info, true );
79  $img->redirectedFrom( $title->getDBkey() );
80  } else {
81  $img = new self( $title, $repo, $info, true );
82  }
83 
84  return $img;
85  } else {
86  return null;
87  }
88  }
89 
94  public static function getProps() {
95  return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
96  }
97 
101  public function getRepo() {
102  return $this->repo;
103  }
104 
105  // Dummy functions...
106 
110  public function exists() {
111  return $this->mExists;
112  }
113 
117  public function getPath() {
118  return false;
119  }
120 
126  public function transform( $params, $flags = 0 ) {
127  if ( !$this->canRender() ) {
128  // show icon
129  return parent::transform( $params, $flags );
130  }
131 
132  // Note, the this->canRender() check above implies
133  // that we have a handler, and it can do makeParamString.
134  $otherParams = $this->handler->makeParamString( $params );
135  $width = $params['width'] ?? -1;
136  $height = $params['height'] ?? -1;
137 
138  $thumbUrl = $this->repo->getThumbUrlFromCache(
139  $this->getName(),
140  $width,
141  $height,
142  $otherParams
143  );
144  if ( $thumbUrl === false ) {
145  global $wgLang;
146 
147  return $this->repo->getThumbError(
148  $this->getName(),
149  $width,
150  $height,
151  $otherParams,
152  $wgLang->getCode()
153  );
154  }
155 
156  return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
157  }
158 
159  // Info we can get from API...
160 
165  public function getWidth( $page = 1 ) {
166  return (int)( $this->mInfo['width'] ?? 0 );
167  }
168 
173  public function getHeight( $page = 1 ) {
174  return (int)( $this->mInfo['height'] ?? 0 );
175  }
176 
180  public function getMetadata() {
181  if ( isset( $this->mInfo['metadata'] ) ) {
182  return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
183  }
184 
185  return false;
186  }
187 
191  public function getMetadataArray(): array {
192  if ( isset( $this->mInfo['metadata'] ) ) {
193  return self::parseMetadata( $this->mInfo['metadata'] );
194  }
195 
196  return [];
197  }
198 
203  public function getExtendedMetadata() {
204  return $this->mInfo['extmetadata'] ?? null;
205  }
206 
211  public static function parseMetadata( $metadata ) {
212  if ( !is_array( $metadata ) ) {
213  return [ '_error' => $metadata ];
214  }
215  '@phan-var array[] $metadata';
216  $ret = [];
217  foreach ( $metadata as $meta ) {
218  $ret[$meta['name']] = self::parseMetadataValue( $meta['value'] );
219  }
220 
221  return $ret;
222  }
223 
228  private static function parseMetadataValue( $metadata ) {
229  if ( !is_array( $metadata ) ) {
230  return $metadata;
231  }
232  '@phan-var array[] $metadata';
233  $ret = [];
234  foreach ( $metadata as $meta ) {
235  $ret[$meta['name']] = self::parseMetadataValue( $meta['value'] );
236  }
237 
238  return $ret;
239  }
240 
244  public function getSize() {
245  return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
246  }
247 
251  public function getUrl() {
252  return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
253  }
254 
262  public function getDescriptionShortUrl() {
263  if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
264  return $this->mInfo['descriptionshorturl'];
265  } elseif ( isset( $this->mInfo['pageid'] ) ) {
266  $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
267  if ( $url !== false ) {
268  return $url;
269  }
270  }
271  return null;
272  }
273 
274  public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
275  if ( isset( $this->mInfo['user'] ) ) {
276  // We don't know if the foreign repo will have a real interwiki prefix,
277  // treat this user as a foreign imported user. Maybe we can do better?
278  return UserIdentityValue::newExternal( $this->getRepoName(), $this->mInfo['user'] );
279  }
280  return null;
281  }
282 
288  public function getDescription( $audience = self::FOR_PUBLIC, Authority $performer = null ) {
289  return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
290  }
291 
295  public function getSha1() {
296  return isset( $this->mInfo['sha1'] )
297  ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
298  : null;
299  }
300 
304  public function getTimestamp() {
305  return wfTimestamp( TS_MW,
306  isset( $this->mInfo['timestamp'] )
307  ? strval( $this->mInfo['timestamp'] )
308  : null
309  );
310  }
311 
315  public function getMimeType() {
316  if ( !isset( $this->mInfo['mime'] ) ) {
317  $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
318  $this->mInfo['mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() );
319  }
320 
321  return $this->mInfo['mime'];
322  }
323 
327  public function getMediaType() {
328  if ( isset( $this->mInfo['mediatype'] ) ) {
329  return $this->mInfo['mediatype'];
330  }
331  $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
332 
333  return $magic->getMediaType( null, $this->getMimeType() );
334  }
335 
339  public function getDescriptionUrl() {
340  return $this->mInfo['descriptionurl'] ?? false;
341  }
342 
348  public function getThumbPath( $suffix = '' ) {
349  if ( !$this->repo->canCacheThumbs() ) {
350  return null;
351  }
352 
353  $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
354  if ( $suffix ) {
355  $path .= $suffix . '/';
356  }
357  return $path;
358  }
359 
363  protected function getThumbnails() {
364  $dir = $this->getThumbPath( $this->getName() );
365  $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
366 
367  $files = [];
368  if ( $iter ) {
369  foreach ( $iter as $file ) {
370  $files[] = $file;
371  }
372  }
373 
374  return $files;
375  }
376 
377  public function purgeCache( $options = [] ) {
378  $this->purgeThumbnails( $options );
379  $this->purgeDescriptionPage();
380  }
381 
382  private function purgeDescriptionPage() {
383  $services = MediaWikiServices::getInstance();
384  $url = $this->repo->getDescriptionRenderUrl(
385  $this->getName(),
386  $services->getContentLanguage()->getCode()
387  );
388 
389  $key = $this->repo->getLocalCacheKey( 'file-remote-description', md5( $url ) );
390  $services->getMainWANObjectCache()->delete( $key );
391  }
392 
396  public function purgeThumbnails( $options = [] ) {
397  $key = $this->repo->getLocalCacheKey( 'file-thumb-url', sha1( $this->getName() ) );
398  MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
399 
400  $files = $this->getThumbnails();
401  // Give media handler a chance to filter the purge list
402  $handler = $this->getHandler();
403  if ( $handler ) {
404  $handler->filterThumbnailPurgeList( $files, $options );
405  }
406 
407  $dir = $this->getThumbPath( $this->getName() );
408  $purgeList = [];
409  foreach ( $files as $file ) {
410  $purgeList[] = "{$dir}{$file}";
411  }
412 
413  # Delete the thumbnails
414  $this->repo->quickPurgeBatch( $purgeList );
415  # Clear out the thumbnail directory if empty
416  $this->repo->quickCleanDir( $dir );
417  }
418 
424  public function isTransformedLocally() {
425  return false;
426  }
427 }
File\getExtension
getExtension()
Get the file extension, e.g.
Definition: File.php:346
MediaWiki\User\UserIdentityValue
Value object representing a user's identity.
Definition: UserIdentityValue.php:35
ForeignAPIFile\getUploader
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Get the identity of the file uploader.
Definition: ForeignAPIFile.php:274
ForeignAPIFile\getMimeType
getMimeType()
Definition: ForeignAPIFile.php:315
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:377
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:114
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
ForeignAPIFile\getThumbPath
getThumbPath( $suffix='')
Only useful if we're locally caching thumbs anyway...
Definition: ForeignAPIFile.php:348
MediaHandler\getMetadataVersion
static getMetadataVersion()
Get metadata version.
Definition: MediaHandler.php:291
MediaHandler\filterThumbnailPurgeList
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
Definition: MediaHandler.php:962
true
return true
Definition: router.php:90
ForeignAPIFile\getDescription
getDescription( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: ForeignAPIFile.php:288
ForeignAPIFile\newFromTitle
static newFromTitle(Title $title, $repo)
Definition: ForeignAPIFile.php:60
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1649
ForeignAPIFile\exists
exists()
Definition: ForeignAPIFile.php:110
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ForeignAPIFile\isTransformedLocally
isTransformedLocally()
The thumbnail is created on the foreign server and fetched over internet.
Definition: ForeignAPIFile.php:424
serialize
serialize()
Definition: ApiMessageTrait.php:138
$wgLang
$wgLang
Definition: Setup.php:861
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
ForeignAPIFile\getRepo
getRepo()
Definition: ForeignAPIFile.php:101
ForeignAPIFile\getMetadata
getMetadata()
Definition: ForeignAPIFile.php:180
File\$path
string $path
The storage path corresponding to one of the zones.
Definition: File.php:144
ForeignAPIFile\$mInfo
array $mInfo
Definition: ForeignAPIFile.php:36
ForeignAPIFile\getExtendedMetadata
getExtendedMetadata()
Definition: ForeignAPIFile.php:203
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1057
ForeignAPIFile\purgeCache
purgeCache( $options=[])
Purge shared caches such as thumbnails and DB data caching STUB Overridden by LocalFile.
Definition: ForeignAPIFile.php:377
File\$url
string $url
The URL corresponding to one of the four basic zones.
Definition: File.php:135
ForeignAPIFile\getThumbnails
getThumbnails()
Definition: ForeignAPIFile.php:363
ForeignAPIFile\getDescriptionShortUrl
getDescriptionShortUrl()
Get short description URL for a file based on the foreign API response, or if unavailable,...
Definition: ForeignAPIFile.php:262
ForeignAPIFile\__construct
__construct( $title, $repo, $info, $exists=false)
Definition: ForeignAPIFile.php:46
ForeignAPIFile\getWidth
getWidth( $page=1)
Definition: ForeignAPIFile.php:165
ForeignAPIFile\getUrl
getUrl()
Definition: ForeignAPIFile.php:251
ForeignAPIFile\getDescriptionUrl
getDescriptionUrl()
Definition: ForeignAPIFile.php:339
ForeignAPIFile\getSize
getSize()
Definition: ForeignAPIFile.php:244
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
File\canRender
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition: File.php:865
ForeignAPIFile\getPath
getPath()
Definition: ForeignAPIFile.php:117
ForeignAPIFile\getTimestamp
getTimestamp()
Definition: ForeignAPIFile.php:304
File\$handler
MediaHandler $handler
Definition: File.php:132
File\$title
Title string bool $title
Definition: File.php:117
ForeignAPIFile\parseMetadataValue
static parseMetadataValue( $metadata)
Definition: ForeignAPIFile.php:228
ForeignAPIFile\getMediaType
getMediaType()
Definition: ForeignAPIFile.php:327
ForeignAPIFile\getHeight
getHeight( $page=1)
Definition: ForeignAPIFile.php:173
File\getName
getName()
Return the name of this file.
Definition: File.php:331
ForeignAPIFile\transform
transform( $params, $flags=0)
Definition: ForeignAPIFile.php:126
File\getRepoName
getRepoName()
Returns the name of the repository.
Definition: File.php:2006
ForeignAPIFile\purgeThumbnails
purgeThumbnails( $options=[])
Definition: ForeignAPIFile.php:396
Title
Represents a title within MediaWiki.
Definition: Title.php:47
File\assertRepoDefined
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2432
ForeignAPIFile\getSha1
getSha1()
Definition: ForeignAPIFile.php:295
ForeignAPIFile\parseMetadata
static parseMetadata( $metadata)
Definition: ForeignAPIFile.php:211
ForeignAPIFile\getProps
static getProps()
Get the property string for iiprop and aiprop.
Definition: ForeignAPIFile.php:94
ForeignAPIFile\purgeDescriptionPage
purgeDescriptionPage()
Definition: ForeignAPIFile.php:382
ForeignAPIFile\getMetadataArray
getMetadataArray()
Definition: ForeignAPIFile.php:191
File\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1527
ForeignAPIFile
Foreign file accessible through api.php requests.
Definition: ForeignAPIFile.php:32
ForeignAPIFile\$repoClass
$repoClass
Definition: ForeignAPIFile.php:38
ForeignAPIFile\$mExists
bool $mExists
Definition: ForeignAPIFile.php:34
File\getHashPath
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1670