MediaWiki  1.29.2
ForeignAPIFile.php
Go to the documentation of this file.
1 <?php
30 class ForeignAPIFile extends File {
32  private $mExists;
34  private $mInfo = [];
35 
36  protected $repoClass = 'ForeignApiRepo';
37 
44  function __construct( $title, $repo, $info, $exists = false ) {
45  parent::__construct( $title, $repo );
46 
47  $this->mInfo = $info;
48  $this->mExists = $exists;
49 
50  $this->assertRepoDefined();
51  }
52 
58  static function newFromTitle( Title $title, $repo ) {
59  $data = $repo->fetchImageQuery( [
60  'titles' => 'File:' . $title->getDBkey(),
61  'iiprop' => self::getProps(),
62  'prop' => 'imageinfo',
63  'iimetadataversion' => MediaHandler::getMetadataVersion(),
64  // extmetadata is language-dependant, accessing the current language here
65  // would be problematic, so we just get them all
66  'iiextmetadatamultilang' => 1,
67  ] );
68 
69  $info = $repo->getImageInfo( $data );
70 
71  if ( $info ) {
72  $lastRedirect = isset( $data['query']['redirects'] )
73  ? count( $data['query']['redirects'] ) - 1
74  : -1;
75  if ( $lastRedirect >= 0 ) {
76  $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
77  $img = new self( $newtitle, $repo, $info, true );
78  if ( $img ) {
79  $img->redirectedFrom( $title->getDBkey() );
80  }
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 = isset( $params['width'] ) ? $params['width'] : -1;
130  $height = isset( $params['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  if ( isset( $this->mInfo['extmetadata'] ) ) {
188  return $this->mInfo['extmetadata'];
189  }
190 
191  return null;
192  }
193 
198  public static function parseMetadata( $metadata ) {
199  if ( !is_array( $metadata ) ) {
200  return $metadata;
201  }
202  $ret = [];
203  foreach ( $metadata as $meta ) {
204  $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
205  }
206 
207  return $ret;
208  }
209 
213  public function getSize() {
214  return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
215  }
216 
220  public function getUrl() {
221  return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
222  }
223 
231  public function getDescriptionShortUrl() {
232  if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
233  return $this->mInfo['descriptionshorturl'];
234  } elseif ( isset( $this->mInfo['pageid'] ) ) {
235  $url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
236  if ( $url !== false ) {
237  return $url;
238  }
239  }
240  return null;
241  }
242 
247  public function getUser( $type = 'text' ) {
248  if ( $type == 'text' ) {
249  return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
250  } else {
251  return 0; // What makes sense here, for a remote user?
252  }
253  }
254 
260  public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
261  return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
262  }
263 
267  function getSha1() {
268  return isset( $this->mInfo['sha1'] )
269  ? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
270  : null;
271  }
272 
276  function getTimestamp() {
277  return wfTimestamp( TS_MW,
278  isset( $this->mInfo['timestamp'] )
279  ? strval( $this->mInfo['timestamp'] )
280  : null
281  );
282  }
283 
287  function getMimeType() {
288  if ( !isset( $this->mInfo['mime'] ) ) {
289  $magic = MimeMagic::singleton();
290  $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
291  }
292 
293  return $this->mInfo['mime'];
294  }
295 
299  function getMediaType() {
300  if ( isset( $this->mInfo['mediatype'] ) ) {
301  return $this->mInfo['mediatype'];
302  }
303  $magic = MimeMagic::singleton();
304 
305  return $magic->getMediaType( null, $this->getMimeType() );
306  }
307 
311  function getDescriptionUrl() {
312  return isset( $this->mInfo['descriptionurl'] )
313  ? $this->mInfo['descriptionurl']
314  : false;
315  }
316 
322  function getThumbPath( $suffix = '' ) {
323  if ( $this->repo->canCacheThumbs() ) {
324  $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
325  if ( $suffix ) {
326  $path = $path . $suffix . '/';
327  }
328 
329  return $path;
330  } else {
331  return null;
332  }
333  }
334 
338  function getThumbnails() {
339  $dir = $this->getThumbPath( $this->getName() );
340  $iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
341 
342  $files = [];
343  foreach ( $iter as $file ) {
344  $files[] = $file;
345  }
346 
347  return $files;
348  }
349 
350  function purgeCache( $options = [] ) {
351  $this->purgeThumbnails( $options );
352  $this->purgeDescriptionPage();
353  }
354 
355  function purgeDescriptionPage() {
357 
358  $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
359  $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
360 
361  ObjectCache::getMainWANInstance()->delete( $key );
362  }
363 
367  function purgeThumbnails( $options = [] ) {
368  $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
369  ObjectCache::getMainWANInstance()->delete( $key );
370 
371  $files = $this->getThumbnails();
372  // Give media handler a chance to filter the purge list
373  $handler = $this->getHandler();
374  if ( $handler ) {
376  }
377 
378  $dir = $this->getThumbPath( $this->getName() );
379  $purgeList = [];
380  foreach ( $files as $file ) {
381  $purgeList[] = "{$dir}{$file}";
382  }
383 
384  # Delete the thumbnails
385  $this->repo->quickPurgeBatch( $purgeList );
386  # Clear out the thumbnail directory if empty
387  $this->repo->quickCleanDir( $dir );
388  }
389 
395  public function isTransformedLocally() {
396  return false;
397  }
398 }
File\getExtension
getExtension()
Get the file extension, e.g.
Definition: File.php:311
ForeignAPIFile\getMimeType
getMimeType()
Definition: ForeignAPIFile.php:287
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:265
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:96
ForeignAPIFile\getThumbPath
getThumbPath( $suffix='')
Only useful if we're locally caching thumbs anyway...
Definition: ForeignAPIFile.php:322
captcha-old.count
count
Definition: captcha-old.py:225
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:722
ForeignAPIFile\newFromTitle
static newFromTitle(Title $title, $repo)
Definition: ForeignAPIFile.php:58
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
ForeignAPIFile\exists
exists()
Definition: ForeignAPIFile.php:104
$params
$params
Definition: styleTest.css.php:40
serialize
serialize()
Definition: ApiMessage.php:177
ForeignAPIFile\isTransformedLocally
isTransformedLocally()
The thumbnail is created on the foreign server and fetched over internet.
Definition: ForeignAPIFile.php:395
$type
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2536
ForeignAPIFile\getUser
getUser( $type='text')
Definition: ForeignAPIFile.php:247
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:126
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 probably a stub 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:783
ForeignAPIFile\$mInfo
array $mInfo
Definition: ForeignAPIFile.php:34
ForeignAPIFile\getExtendedMetadata
getExtendedMetadata()
Definition: ForeignAPIFile.php:186
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:901
ForeignAPIFile\purgeCache
purgeCache( $options=[])
Purge shared caches such as thumbnails and DB data caching STUB Overridden by LocalFile.
Definition: ForeignAPIFile.php:350
File\$url
string $url
The URL corresponding to one of the four basic zones.
Definition: File.php:117
ForeignAPIFile\getThumbnails
getThumbnails()
Definition: ForeignAPIFile.php:338
$page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2536
ForeignAPIFile\getDescriptionShortUrl
getDescriptionShortUrl()
Get short description URL for a file based on the foreign API response, or if unavailable,...
Definition: ForeignAPIFile.php:231
ForeignAPIFile\__construct
__construct( $title, $repo, $info, $exists=false)
Definition: ForeignAPIFile.php:44
ForeignAPIFile\getWidth
getWidth( $page=1)
Definition: ForeignAPIFile.php:159
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MimeMagic\singleton
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:33
ForeignAPIFile\getUrl
getUrl()
Definition: ForeignAPIFile.php:220
ForeignAPIFile\getDescriptionUrl
getDescriptionUrl()
Definition: ForeignAPIFile.php:311
ForeignAPIFile\getSize
getSize()
Definition: ForeignAPIFile.php:213
ForeignAPIFile\getDescription
getDescription( $audience=self::FOR_PUBLIC, User $user=null)
Definition: ForeignAPIFile.php:260
$dir
$dir
Definition: Autoload.php:8
File\canRender
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition: File.php:734
ForeignAPIFile\getPath
getPath()
Definition: ForeignAPIFile.php:111
ForeignAPIFile\getTimestamp
getTimestamp()
Definition: ForeignAPIFile.php:276
File\$handler
MediaHandler $handler
Definition: File.php:114
File\$title
Title string bool $title
Definition: File.php:99
ForeignAPIFile\getMediaType
getMediaType()
Definition: ForeignAPIFile.php:299
$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:1956
ForeignAPIFile\getHeight
getHeight( $page=1)
Definition: ForeignAPIFile.php:167
File\getName
getName()
Return the name of this file.
Definition: File.php:297
ForeignAPIFile\transform
transform( $params, $flags=0)
Definition: ForeignAPIFile.php:120
ForeignAPIFile\purgeThumbnails
purgeThumbnails( $options=[])
Definition: ForeignAPIFile.php:367
Title
Represents a title within MediaWiki.
Definition: Title.php:39
File\assertRepoDefined
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:2248
ObjectCache\getMainWANInstance
static getMainWANInstance()
Get the main WAN cache object.
Definition: ObjectCache.php:370
ForeignAPIFile\getSha1
getSha1()
Definition: ForeignAPIFile.php:267
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:198
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:1956
ForeignAPIFile\getProps
static getProps()
Get the property string for iiprop and aiprop.
Definition: ForeignAPIFile.php:95
ForeignAPIFile\purgeDescriptionPage
purgeDescriptionPage()
Definition: ForeignAPIFile.php:355
File\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1365
ForeignAPIFile
Foreign file accessible through api.php requests.
Definition: ForeignAPIFile.php:30
ForeignAPIFile\$repoClass
$repoClass
Definition: ForeignAPIFile.php:36
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
ForeignAPIFile\$mExists
bool $mExists
Definition: ForeignAPIFile.php:32
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
File\getHashPath
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1497
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2749
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56