MediaWiki  1.33.0
ForeignAPIFile.php
Go to the documentation of this file.
1 <?php
25 
32 class ForeignAPIFile extends File {
34  private $mExists;
36  private $mInfo = [];
37 
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  $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  static function getProps() {
96  return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
97  }
98 
99  // Dummy functions...
100 
104  public function exists() {
105  return $this->mExists;
106  }
107 
111  public function getPath() {
112  return false;
113  }
114 
120  function transform( $params, $flags = 0 ) {
121  if ( !$this->canRender() ) {
122  // show icon
123  return parent::transform( $params, $flags );
124  }
125 
126  // Note, the this->canRender() check above implies
127  // that we have a handler, and it can do makeParamString.
128  $otherParams = $this->handler->makeParamString( $params );
129  $width = $params['width'] ?? -1;
130  $height = $params['height'] ?? -1;
131 
132  $thumbUrl = $this->repo->getThumbUrlFromCache(
133  $this->getName(),
134  $width,
135  $height,
136  $otherParams
137  );
138  if ( $thumbUrl === false ) {
139  global $wgLang;
140 
141  return $this->repo->getThumbError(
142  $this->getName(),
143  $width,
144  $height,
145  $otherParams,
146  $wgLang->getCode()
147  );
148  }
149 
150  return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
151  }
152 
153  // Info we can get from API...
154 
159  public function getWidth( $page = 1 ) {
160  return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
161  }
162 
167  public function getHeight( $page = 1 ) {
168  return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
169  }
170 
174  public function getMetadata() {
175  if ( isset( $this->mInfo['metadata'] ) ) {
176  return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
177  }
178 
179  return null;
180  }
181 
186  public function getExtendedMetadata() {
187  return $this->mInfo['extmetadata'] ?? null;
188  }
189 
194  public static function parseMetadata( $metadata ) {
195  if ( !is_array( $metadata ) ) {
196  return $metadata;
197  }
198  $ret = [];
199  foreach ( $metadata as $meta ) {
200  $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
201  }
202 
203  return $ret;
204  }
205 
209  public function getSize() {
210  return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
211  }
212 
216  public function getUrl() {
217  return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
218  }
219 
227  public function getDescriptionShortUrl() {
228  if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
229  return $this->mInfo['descriptionshorturl'];
230  } elseif ( isset( $this->mInfo['pageid'] ) ) {
231  $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
232  if ( $url !== false ) {
233  return $url;
234  }
235  }
236  return null;
237  }
238 
243  public function getUser( $type = 'text' ) {
244  if ( $type == 'text' ) {
245  return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
246  } else {
247  return 0; // What makes sense here, for a remote user?
248  }
249  }
250 
256  public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
257  return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
258  }
259 
263  function getSha1() {
264  return isset( $this->mInfo['sha1'] )
265  ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
266  : null;
267  }
268 
272  function getTimestamp() {
273  return wfTimestamp( TS_MW,
274  isset( $this->mInfo['timestamp'] )
275  ? strval( $this->mInfo['timestamp'] )
276  : null
277  );
278  }
279 
283  function getMimeType() {
284  if ( !isset( $this->mInfo['mime'] ) ) {
285  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
286  $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
287  }
288 
289  return $this->mInfo['mime'];
290  }
291 
295  function getMediaType() {
296  if ( isset( $this->mInfo['mediatype'] ) ) {
297  return $this->mInfo['mediatype'];
298  }
299  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
300 
301  return $magic->getMediaType( null, $this->getMimeType() );
302  }
303 
307  function getDescriptionUrl() {
308  return $this->mInfo['descriptionurl'] ?? false;
309  }
310 
316  function getThumbPath( $suffix = '' ) {
317  if ( !$this->repo->canCacheThumbs() ) {
318  return null;
319  }
320 
321  $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
322  if ( $suffix ) {
323  $path .= $suffix . '/';
324  }
325  return $path;
326  }
327 
331  function getThumbnails() {
332  $dir = $this->getThumbPath( $this->getName() );
333  $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
334 
335  $files = [];
336  if ( $iter ) {
337  foreach ( $iter as $file ) {
338  $files[] = $file;
339  }
340  }
341 
342  return $files;
343  }
344 
345  function purgeCache( $options = [] ) {
346  $this->purgeThumbnails( $options );
347  $this->purgeDescriptionPage();
348  }
349 
350  function purgeDescriptionPage() {
351  $services = MediaWikiServices::getInstance();
352  $url = $this->repo->getDescriptionRenderUrl(
353  $this->getName(), $services->getContentLanguage()->getCode() );
354  $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
355 
356  $services->getMainWANObjectCache()->delete( $key );
357  }
358 
362  function purgeThumbnails( $options = [] ) {
363  $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
364  MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
365 
366  $files = $this->getThumbnails();
367  // Give media handler a chance to filter the purge list
368  $handler = $this->getHandler();
369  if ( $handler ) {
371  }
372 
373  $dir = $this->getThumbPath( $this->getName() );
374  $purgeList = [];
375  foreach ( $files as $file ) {
376  $purgeList[] = "{$dir}{$file}";
377  }
378 
379  # Delete the thumbnails
380  $this->repo->quickPurgeBatch( $purgeList );
381  # Clear out the thumbnail directory if empty
382  $this->repo->quickCleanDir( $dir );
383  }
384 
390  public function isTransformedLocally() {
391  return false;
392  }
393 }
File\getExtension
getExtension()
Get the file extension, e.g.
Definition: File.php:312
ForeignAPIFile\getMimeType
getMimeType()
Definition: ForeignAPIFile.php:283
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
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:306
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:97
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
ForeignAPIFile\getThumbPath
getThumbPath( $suffix='')
Only useful if we're locally caching thumbs anyway...
Definition: ForeignAPIFile.php:316
captcha-old.count
count
Definition: captcha-old.py:249
MediaHandler\getMetadataVersion
static getMetadataVersion()
Get metadata version.
Definition: MediaHandler.php:141
MediaHandler\filterThumbnailPurgeList
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
Definition: MediaHandler.php:711
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:1912
ForeignAPIFile\exists
exists()
Definition: ForeignAPIFile.php:104
$params
$params
Definition: styleTest.css.php:44
ForeignAPIFile\isTransformedLocally
isTransformedLocally()
The thumbnail is created on the foreign server and fetched over internet.
Definition: ForeignAPIFile.php:390
serialize
serialize()
Definition: ApiMessageTrait.php:134
ForeignAPIFile\getUser
getUser( $type='text')
Definition: ForeignAPIFile.php:243
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ForeignAPIFile\getMetadata
getMetadata()
Definition: ForeignAPIFile.php:174
File\$path
string $path
The storage path corresponding to one of the zones.
Definition: File.php:127
ForeignAPIFile\$mInfo
array $mInfo
Definition: ForeignAPIFile.php:36
ForeignAPIFile\getExtendedMetadata
getExtendedMetadata()
Definition: ForeignAPIFile.php:186
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:52
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:970
ForeignAPIFile\purgeCache
purgeCache( $options=[])
Purge shared caches such as thumbnails and DB data caching STUB Overridden by LocalFile.
Definition: ForeignAPIFile.php:345
File\$url
string $url
The URL corresponding to one of the four basic zones.
Definition: File.php:118
ForeignAPIFile\getThumbnails
getThumbnails()
Definition: ForeignAPIFile.php:331
ForeignAPIFile\getDescriptionShortUrl
getDescriptionShortUrl()
Get short description URL for a file based on the foreign API response, or if unavailable,...
Definition: ForeignAPIFile.php:227
$wgLang
$wgLang
Definition: Setup.php:875
ForeignAPIFile\__construct
__construct( $title, $repo, $info, $exists=false)
Definition: ForeignAPIFile.php:46
ForeignAPIFile\getWidth
getWidth( $page=1)
Definition: ForeignAPIFile.php:159
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ForeignAPIFile\getUrl
getUrl()
Definition: ForeignAPIFile.php:216
ForeignAPIFile\getDescriptionUrl
getDescriptionUrl()
Definition: ForeignAPIFile.php:307
ForeignAPIFile\getSize
getSize()
Definition: ForeignAPIFile.php:209
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
ForeignAPIFile\getDescription
getDescription( $audience=self::FOR_PUBLIC, User $user=null)
Definition: ForeignAPIFile.php:256
handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves set to a MediaTransformOutput the error message to be returned in an array you should do so by altering $wgNamespaceProtection and $wgNamespaceContentModels outside the handler
Definition: hooks.txt:780
File\canRender
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition: File.php:749
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
ForeignAPIFile\getPath
getPath()
Definition: ForeignAPIFile.php:111
ForeignAPIFile\getTimestamp
getTimestamp()
Definition: ForeignAPIFile.php:272
File\$handler
MediaHandler $handler
Definition: File.php:115
File\$title
Title string bool $title
Definition: File.php:100
ForeignAPIFile\getMediaType
getMediaType()
Definition: ForeignAPIFile.php:295
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1985
ForeignAPIFile\getHeight
getHeight( $page=1)
Definition: ForeignAPIFile.php:167
File\getName
getName()
Return the name of this file.
Definition: File.php:298
ForeignAPIFile\transform
transform( $params, $flags=0)
Definition: ForeignAPIFile.php:120
ForeignAPIFile\purgeThumbnails
purgeThumbnails( $options=[])
Definition: ForeignAPIFile.php:362
Title
Represents a title within MediaWiki.
Definition: Title.php:40
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
File\assertRepoDefined
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2271
ForeignAPIFile\getSha1
getSha1()
Definition: ForeignAPIFile.php:263
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ForeignAPIFile\parseMetadata
static parseMetadata( $metadata)
Definition: ForeignAPIFile.php:194
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
ForeignAPIFile\getProps
static getProps()
Get the property string for iiprop and aiprop.
Definition: ForeignAPIFile.php:95
ForeignAPIFile\purgeDescriptionPage
purgeDescriptionPage()
Definition: ForeignAPIFile.php:350
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$services
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2220
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
File\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1379
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:48
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:1513
$type
$type
Definition: testCompression.php:48