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 
100  // Dummy functions...
101 
105  public function exists() {
106  return $this->mExists;
107  }
108 
112  public function getPath() {
113  return false;
114  }
115 
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 
160  public function getWidth( $page = 1 ) {
161  return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
162  }
163 
168  public function getHeight( $page = 1 ) {
169  return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
170  }
171 
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 
187  public function getExtendedMetadata() {
188  return $this->mInfo['extmetadata'] ?? null;
189  }
190 
195  public static function parseMetadata( $metadata ) {
196  if ( !is_array( $metadata ) ) {
197  return $metadata;
198  }
199  '@phan-var array[] $metadata';
200  $ret = [];
201  foreach ( $metadata as $meta ) {
202  $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
203  }
204 
205  return $ret;
206  }
207 
211  public function getSize() {
212  return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
213  }
214 
218  public function getUrl() {
219  return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
220  }
221 
229  public function getDescriptionShortUrl() {
230  if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
231  return $this->mInfo['descriptionshorturl'];
232  } elseif ( isset( $this->mInfo['pageid'] ) ) {
233  $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
234  if ( $url !== false ) {
235  return $url;
236  }
237  }
238  return null;
239  }
240 
245  public function getUser( $type = 'text' ) {
246  if ( $type == 'text' ) {
247  return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
248  } else {
249  return 0; // What makes sense here, for a remote user?
250  }
251  }
252 
258  public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
259  return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
260  }
261 
265  function getSha1() {
266  return isset( $this->mInfo['sha1'] )
267  ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
268  : null;
269  }
270 
274  function getTimestamp() {
275  return wfTimestamp( TS_MW,
276  isset( $this->mInfo['timestamp'] )
277  ? strval( $this->mInfo['timestamp'] )
278  : null
279  );
280  }
281 
285  function getMimeType() {
286  if ( !isset( $this->mInfo['mime'] ) ) {
287  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
288  $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
289  }
290 
291  return $this->mInfo['mime'];
292  }
293 
297  function getMediaType() {
298  if ( isset( $this->mInfo['mediatype'] ) ) {
299  return $this->mInfo['mediatype'];
300  }
301  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
302 
303  return $magic->getMediaType( null, $this->getMimeType() );
304  }
305 
309  function getDescriptionUrl() {
310  return $this->mInfo['descriptionurl'] ?? false;
311  }
312 
318  function getThumbPath( $suffix = '' ) {
319  if ( !$this->repo->canCacheThumbs() ) {
320  return null;
321  }
322 
323  $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
324  if ( $suffix ) {
325  $path .= $suffix . '/';
326  }
327  return $path;
328  }
329 
333  function getThumbnails() {
334  $dir = $this->getThumbPath( $this->getName() );
335  $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
336 
337  $files = [];
338  if ( $iter ) {
339  foreach ( $iter as $file ) {
340  $files[] = $file;
341  }
342  }
343 
344  return $files;
345  }
346 
347  function purgeCache( $options = [] ) {
348  $this->purgeThumbnails( $options );
349  $this->purgeDescriptionPage();
350  }
351 
352  function purgeDescriptionPage() {
353  $services = MediaWikiServices::getInstance();
354  $url = $this->repo->getDescriptionRenderUrl(
355  $this->getName(), $services->getContentLanguage()->getCode() );
356  $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
357 
358  $services->getMainWANObjectCache()->delete( $key );
359  }
360 
364  function purgeThumbnails( $options = [] ) {
365  $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
366  MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
367 
368  $files = $this->getThumbnails();
369  // Give media handler a chance to filter the purge list
370  $handler = $this->getHandler();
371  if ( $handler ) {
372  $handler->filterThumbnailPurgeList( $files, $options );
373  }
374 
375  $dir = $this->getThumbPath( $this->getName() );
376  $purgeList = [];
377  foreach ( $files as $file ) {
378  $purgeList[] = "{$dir}{$file}";
379  }
380 
381  # Delete the thumbnails
382  $this->repo->quickPurgeBatch( $purgeList );
383  # Clear out the thumbnail directory if empty
384  $this->repo->quickCleanDir( $dir );
385  }
386 
392  public function isTransformedLocally() {
393  return false;
394  }
395 }
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
purgeThumbnails( $options=[])
MediaHandler $handler
Definition: File.php:124
getDescription( $audience=self::FOR_PUBLIC, User $user=null)
serialize()
static getMetadataVersion()
Get metadata version.
getDescriptionShortUrl()
Get short description URL for a file based on the foreign API response, or if unavailable, the short URL is constructed from the foreign page ID.
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition: File.php:758
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2284
getName()
Return the name of this file.
Definition: File.php:307
static getInstance()
Returns the global default instance of the top level service locator.
Title string bool $title
Definition: File.php:109
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1526
getHeight( $page=1)
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Foreign file accessible through api.php requests.
static parseMetadata( $metadata)
$wgLang
Definition: Setup.php:858
getThumbPath( $suffix='')
Only useful if we&#39;re locally caching thumbs anyway...
getDBkey()
Get the main part with underscores.
Definition: Title.php:1014
isTransformedLocally()
The thumbnail is created on the foreign server and fetched over internet.
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1390
static newFromTitle(Title $title, $repo)
static getProps()
Get the property string for iiprop and aiprop.
getUser( $type='text')
getWidth( $page=1)
getExtension()
Get the file extension, e.g.
Definition: File.php:321
purgeCache( $options=[])
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:106
string $url
The URL corresponding to one of the four basic zones.
Definition: File.php:127
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
__construct( $title, $repo, $info, $exists=false)
string $path
The storage path corresponding to one of the zones.
Definition: File.php:136
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
transform( $params, $flags=0)
return true
Definition: router.php:92
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317