MediaWiki  1.23.2
ForeignAPIFile.php
Go to the documentation of this file.
1 <?php
30 class ForeignAPIFile extends File {
31  private $mExists;
32 
33  protected $repoClass = 'ForeignApiRepo';
34 
41  function __construct( $title, $repo, $info, $exists = false ) {
42  parent::__construct( $title, $repo );
43 
44  $this->mInfo = $info;
45  $this->mExists = $exists;
46 
47  $this->assertRepoDefined();
48  }
49 
55  static function newFromTitle( Title $title, $repo ) {
56  $data = $repo->fetchImageQuery( array(
57  'titles' => 'File:' . $title->getDBkey(),
58  'iiprop' => self::getProps(),
59  'prop' => 'imageinfo',
60  'iimetadataversion' => MediaHandler::getMetadataVersion(),
61  // extmetadata is language-dependant, accessing the current language here
62  // would be problematic, so we just get them all
63  'iiextmetadatamultilang' => 1,
64  ) );
65 
66  $info = $repo->getImageInfo( $data );
67 
68  if ( $info ) {
69  $lastRedirect = isset( $data['query']['redirects'] )
70  ? count( $data['query']['redirects'] ) - 1
71  : -1;
72  if ( $lastRedirect >= 0 ) {
73  $newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
74  $img = new self( $newtitle, $repo, $info, true );
75  if ( $img ) {
76  $img->redirectedFrom( $title->getDBkey() );
77  }
78  } else {
79  $img = new self( $title, $repo, $info, true );
80  }
81 
82  return $img;
83  } else {
84  return null;
85  }
86  }
87 
92  static function getProps() {
93  return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
94  }
95 
96  // Dummy functions...
97 
101  public function exists() {
102  return $this->mExists;
103  }
104 
108  public function getPath() {
109  return false;
110  }
111 
117  function transform( $params, $flags = 0 ) {
118  if ( !$this->canRender() ) {
119  // show icon
120  return parent::transform( $params, $flags );
121  }
122 
123  // Note, the this->canRender() check above implies
124  // that we have a handler, and it can do makeParamString.
125  $otherParams = $this->handler->makeParamString( $params );
126  $width = isset( $params['width'] ) ? $params['width'] : -1;
127  $height = isset( $params['height'] ) ? $params['height'] : -1;
128 
129  $thumbUrl = $this->repo->getThumbUrlFromCache(
130  $this->getName(),
131  $width,
132  $height,
133  $otherParams
134  );
135  if ( $thumbUrl === false ) {
136  global $wgLang;
137 
138  return $this->repo->getThumbError(
139  $this->getName(),
140  $width,
141  $height,
142  $otherParams,
143  $wgLang->getCode()
144  );
145  }
146 
147  return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
148  }
149 
150  // Info we can get from API...
151 
156  public function getWidth( $page = 1 ) {
157  return isset( $this->mInfo['width'] ) ? intval( $this->mInfo['width'] ) : 0;
158  }
159 
164  public function getHeight( $page = 1 ) {
165  return isset( $this->mInfo['height'] ) ? intval( $this->mInfo['height'] ) : 0;
166  }
167 
171  public function getMetadata() {
172  if ( isset( $this->mInfo['metadata'] ) ) {
173  return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
174  }
175 
176  return null;
177  }
178 
183  public function getExtendedMetadata() {
184  if ( isset( $this->mInfo['extmetadata'] ) ) {
185  return $this->mInfo['extmetadata'];
186  }
187 
188  return null;
189  }
190 
195  public static function parseMetadata( $metadata ) {
196  if ( !is_array( $metadata ) ) {
197  return $metadata;
198  }
199  $ret = array();
200  foreach ( $metadata as $meta ) {
201  $ret[$meta['name']] = self::parseMetadata( $meta['value'] );
202  }
203 
204  return $ret;
205  }
206 
210  public function getSize() {
211  return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
212  }
213 
217  public function getUrl() {
218  return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
219  }
220 
225  public function getUser( $method = 'text' ) {
226  return isset( $this->mInfo['user'] ) ? strval( $this->mInfo['user'] ) : null;
227  }
228 
234  public function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) {
235  return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
236  }
237 
241  function getSha1() {
242  return isset( $this->mInfo['sha1'] )
243  ? wfBaseConvert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
244  : null;
245  }
246 
250  function getTimestamp() {
251  return wfTimestamp( TS_MW,
252  isset( $this->mInfo['timestamp'] )
253  ? strval( $this->mInfo['timestamp'] )
254  : null
255  );
256  }
257 
261  function getMimeType() {
262  if ( !isset( $this->mInfo['mime'] ) ) {
263  $magic = MimeMagic::singleton();
264  $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
265  }
266 
267  return $this->mInfo['mime'];
268  }
269 
273  function getMediaType() {
274  if ( isset( $this->mInfo['mediatype'] ) ) {
275  return $this->mInfo['mediatype'];
276  }
277  $magic = MimeMagic::singleton();
278 
279  return $magic->getMediaType( null, $this->getMimeType() );
280  }
281 
285  function getDescriptionUrl() {
286  return isset( $this->mInfo['descriptionurl'] )
287  ? $this->mInfo['descriptionurl']
288  : false;
289  }
290 
296  function getThumbPath( $suffix = '' ) {
297  if ( $this->repo->canCacheThumbs() ) {
298  $path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath( $this->getName() );
299  if ( $suffix ) {
300  $path = $path . $suffix . '/';
301  }
302 
303  return $path;
304  } else {
305  return null;
306  }
307  }
308 
312  function getThumbnails() {
313  $dir = $this->getThumbPath( $this->getName() );
314  $iter = $this->repo->getBackend()->getFileList( array( 'dir' => $dir ) );
315 
316  $files = array();
317  foreach ( $iter as $file ) {
318  $files[] = $file;
319  }
320 
321  return $files;
322  }
323 
327  function purgeCache( $options = array() ) {
328  $this->purgeThumbnails( $options );
329  $this->purgeDescriptionPage();
330  }
331 
332  function purgeDescriptionPage() {
334 
335  $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
336  $key = $this->repo->getLocalCacheKey( 'RemoteFileDescription', 'url', md5( $url ) );
337 
338  $wgMemc->delete( $key );
339  }
340 
344  function purgeThumbnails( $options = array() ) {
345  global $wgMemc;
346 
347  $key = $this->repo->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
348  $wgMemc->delete( $key );
349 
350  $files = $this->getThumbnails();
351  // Give media handler a chance to filter the purge list
352  $handler = $this->getHandler();
353  if ( $handler ) {
355  }
356 
357  $dir = $this->getThumbPath( $this->getName() );
358  $purgeList = array();
359  foreach ( $files as $file ) {
360  $purgeList[] = "{$dir}{$file}";
361  }
362 
363  # Delete the thumbnails
364  $this->repo->quickPurgeBatch( $purgeList );
365  # Clear out the thumbnail directory if empty
366  $this->repo->quickCleanDir( $dir );
367  }
368 }
File\getExtension
getExtension()
Get the file extension, e.g.
Definition: File.php:287
ForeignAPIFile\getMimeType
getMimeType()
Definition: ForeignAPIFile.php:261
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:189
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:94
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$files
$files
Definition: importImages.php:67
ForeignAPIFile\getUser
getUser( $method='text')
Definition: ForeignAPIFile.php:225
ForeignAPIFile\getThumbPath
getThumbPath( $suffix='')
Only useful if we're locally caching thumbs anyway...
Definition: ForeignAPIFile.php:296
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
MediaHandler\getMetadataVersion
static getMetadataVersion()
Get metadata version.
Definition: MediaHandler.php:145
MediaHandler\filterThumbnailPurgeList
filterThumbnailPurgeList(&$files, $options)
Remove files from the purge list.
Definition: MediaHandler.php:724
ForeignAPIFile\newFromTitle
static newFromTitle(Title $title, $repo)
Definition: ForeignAPIFile.php:55
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
$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:1530
ForeignAPIFile\exists
exists()
Definition: ForeignAPIFile.php:101
$params
$params
Definition: styleTest.css.php:40
ForeignAPIFile\purgeCache
purgeCache( $options=array())
Definition: ForeignAPIFile.php:327
$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
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2113
ForeignAPIFile\$mExists
$mExists
Definition: ForeignAPIFile.php:31
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:1530
ForeignAPIFile\getMetadata
getMetadata()
Definition: ForeignAPIFile.php:171
File\$path
string $path
The storage path corresponding to one of the zones *.
Definition: File.php:114
ForeignAPIFile\getExtendedMetadata
getExtendedMetadata()
Definition: ForeignAPIFile.php:183
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:857
File\$url
string $url
The URL corresponding to one of the four basic zones *.
Definition: File.php:108
ForeignAPIFile\getThumbnails
getThumbnails()
Definition: ForeignAPIFile.php:312
ForeignAPIFile\__construct
__construct( $title, $repo, $info, $exists=false)
Definition: ForeignAPIFile.php:41
ForeignAPIFile\getWidth
getWidth( $page=1)
Definition: ForeignAPIFile.php:156
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ForeignAPIFile\purgeThumbnails
purgeThumbnails( $options=array())
Definition: ForeignAPIFile.php:344
ForeignAPIFile\getUrl
getUrl()
Definition: ForeignAPIFile.php:217
ForeignAPIFile\getDescriptionUrl
getDescriptionUrl()
Definition: ForeignAPIFile.php:285
ForeignAPIFile\getSize
getSize()
Definition: ForeignAPIFile.php:210
ForeignAPIFile\getDescription
getDescription( $audience=self::FOR_PUBLIC, User $user=null)
Definition: ForeignAPIFile.php:234
$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:1530
File\canRender
canRender()
Checks if the output of transform() for this file is likely to be valid.
Definition: File.php:650
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
ForeignAPIFile\getPath
getPath()
Definition: ForeignAPIFile.php:108
ForeignAPIFile\getTimestamp
getTimestamp()
Definition: ForeignAPIFile.php:250
File\$handler
MediaHandler $handler
Definition: File.php:106
File\$title
Title string bool $title
Definition: File.php:96
ForeignAPIFile\getMediaType
getMediaType()
Definition: ForeignAPIFile.php:273
ForeignAPIFile\getHeight
getHeight( $page=1)
Definition: ForeignAPIFile.php:164
File\getName
getName()
Return the name of this file.
Definition: File.php:273
ForeignAPIFile\transform
transform( $params, $flags=0)
Definition: ForeignAPIFile.php:117
$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:237
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$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
File\assertRepoDefined
assertRepoDefined()
Assert that $this->repo is set to a valid FileRepo instance.
Definition: File.php:1984
$dir
if(count( $args)==0) $dir
Definition: importImages.php:49
ForeignAPIFile\getSha1
getSha1()
Definition: ForeignAPIFile.php:241
wfBaseConvert
wfBaseConvert( $input, $sourceBase, $destBase, $pad=1, $lowercase=true, $engine='auto')
Convert an arbitrarily-long digit string from one numeric base to another, optionally zero-padding to...
Definition: GlobalFunctions.php:3368
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:195
ForeignAPIFile\getProps
static getProps()
Get the property string for iiprop and aiprop.
Definition: ForeignAPIFile.php:92
ForeignAPIFile\purgeDescriptionPage
purgeDescriptionPage()
Definition: ForeignAPIFile.php:332
File\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: File.php:1078
ForeignAPIFile
Foreign file accessible through api.php requests.
Definition: ForeignAPIFile.php:30
ForeignAPIFile\$repoClass
$repoClass
Definition: ForeignAPIFile.php:33
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
File\getHashPath
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1210