MediaWiki  master
ForeignAPIFile.php
Go to the documentation of this file.
1 <?php
25 
32 class ForeignAPIFile extends File {
34  private $mExists;
36  private $mInfo = [];
37 
38  protected $repoClass = ForeignAPIRepo::class;
39 
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 
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 
96  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  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  function getSha1() {
273  return isset( $this->mInfo['sha1'] )
274  ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
275  : null;
276  }
277 
281  function getTimestamp() {
282  return wfTimestamp( TS_MW,
283  isset( $this->mInfo['timestamp'] )
284  ? strval( $this->mInfo['timestamp'] )
285  : null
286  );
287  }
288 
292  function getMimeType() {
293  if ( !isset( $this->mInfo['mime'] ) ) {
294  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
295  $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
296  }
297 
298  return $this->mInfo['mime'];
299  }
300 
304  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  function getDescriptionUrl() {
317  return $this->mInfo['descriptionurl'] ?? false;
318  }
319 
325  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  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  function purgeCache( $options = [] ) {
355  $this->purgeThumbnails( $options );
356  $this->purgeDescriptionPage();
357  }
358 
359  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  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 }
File\getExtension
getExtension()
Get the file extension, e.g.
Definition: File.php:321
ForeignAPIFile\getMimeType
getMimeType()
Definition: ForeignAPIFile.php:292
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:332
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:106
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:137
ForeignAPIFile\getThumbPath
getThumbPath( $suffix='')
Only useful if we're locally caching thumbs anyway...
Definition: ForeignAPIFile.php:325
MediaHandler\getMetadataVersion
static getMetadataVersion()
Get metadata version.
Definition: MediaHandler.php:146
MediaHandler\filterThumbnailPurgeList
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
Definition: MediaHandler.php:716
true
return true
Definition: router.php:92
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:1809
ForeignAPIFile\exists
exists()
Definition: ForeignAPIFile.php:112
$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:399
serialize
serialize()
Definition: ApiMessageTrait.php:138
ForeignAPIFile\getUser
getUser( $type='text')
Definition: ForeignAPIFile.php:252
ForeignAPIFile\getRepo
getRepo()
Definition: ForeignAPIFile.php:103
ForeignAPIFile\getMetadata
getMetadata()
Definition: ForeignAPIFile.php:182
File\$path
string $path
The storage path corresponding to one of the zones.
Definition: File.php:136
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:168
ForeignAPIFile\$mInfo
array $mInfo
Definition: ForeignAPIFile.php:36
ForeignAPIFile\getExtendedMetadata
getExtendedMetadata()
Definition: ForeignAPIFile.php:194
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1025
ForeignAPIFile\purgeCache
purgeCache( $options=[])
Purge shared caches such as thumbnails and DB data caching STUB Overridden by LocalFile.
Definition: ForeignAPIFile.php:354
File\$url
string $url
The URL corresponding to one of the four basic zones.
Definition: File.php:127
ForeignAPIFile\getThumbnails
getThumbnails()
Definition: ForeignAPIFile.php:340
ForeignAPIFile\getDescriptionShortUrl
getDescriptionShortUrl()
Get short description URL for a file based on the foreign API response, or if unavailable,...
Definition: ForeignAPIFile.php:236
$wgLang
$wgLang
Definition: Setup.php:776
ForeignAPIFile\__construct
__construct( $title, $repo, $info, $exists=false)
Definition: ForeignAPIFile.php:46
ForeignAPIFile\getWidth
getWidth( $page=1)
Definition: ForeignAPIFile.php:167
ForeignAPIFile\getUrl
getUrl()
Definition: ForeignAPIFile.php:225
ForeignAPIFile\getDescriptionUrl
getDescriptionUrl()
Definition: ForeignAPIFile.php:316
ForeignAPIFile\getSize
getSize()
Definition: ForeignAPIFile.php:218
ForeignAPIFile\getDescription
getDescription( $audience=self::FOR_PUBLIC, User $user=null)
Definition: ForeignAPIFile.php:265
File\canRender
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition: File.php:798
ForeignAPIFile\getPath
getPath()
Definition: ForeignAPIFile.php:119
ForeignAPIFile\getTimestamp
getTimestamp()
Definition: ForeignAPIFile.php:281
File\$handler
MediaHandler $handler
Definition: File.php:124
File\$title
Title string bool $title
Definition: File.php:109
ForeignAPIFile\getMediaType
getMediaType()
Definition: ForeignAPIFile.php:304
ForeignAPIFile\getHeight
getHeight( $page=1)
Definition: ForeignAPIFile.php:175
File\getName
getName()
Return the name of this file.
Definition: File.php:307
ForeignAPIFile\transform
transform( $params, $flags=0)
Definition: ForeignAPIFile.php:128
ForeignAPIFile\purgeThumbnails
purgeThumbnails( $options=[])
Definition: ForeignAPIFile.php:371
Title
Represents a title within MediaWiki.
Definition: Title.php:42
File\assertRepoDefined
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2348
ForeignAPIFile\getSha1
getSha1()
Definition: ForeignAPIFile.php:272
ForeignAPIFile\parseMetadata
static parseMetadata( $metadata)
Definition: ForeignAPIFile.php:202
ForeignAPIFile\getProps
static getProps()
Get the property string for iiprop and aiprop.
Definition: ForeignAPIFile.php:96
ForeignAPIFile\purgeDescriptionPage
purgeDescriptionPage()
Definition: ForeignAPIFile.php:359
File\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1430
ForeignAPIFile
Foreign file accessible through api.php requests.
Definition: ForeignAPIFile.php:32
ForeignAPIFile\$repoClass
$repoClass
Definition: ForeignAPIFile.php:38
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
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:1566
$type
$type
Definition: testCompression.php:52