MediaWiki  master
ArchivedFile.php
Go to the documentation of this file.
1 <?php
28 
35 class ArchivedFile {
36 
37  // Audience options for ::getDescription() and ::getUploader()
38  public const FOR_PUBLIC = 1;
39  public const FOR_THIS_USER = 2;
40  public const RAW = 3;
41 
43  private $id;
44 
46  private $name;
47 
49  private $group;
50 
52  private $key;
53 
55  private $size;
56 
58  private $bits;
59 
61  private $width;
62 
64  private $height;
65 
67  private $metadata;
68 
70  private $mime;
71 
73  private $media_type;
74 
76  private $description;
77 
79  private $user;
80 
82  private $timestamp;
83 
85  private $dataLoaded;
86 
88  private $deleted;
89 
91  private $sha1;
92 
96  private $pageCount;
97 
99  private $archive_name;
100 
102  protected $handler;
103 
105  protected $title; # image title
106 
108  protected $exists;
109 
118  public function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
119  $this->id = -1;
120  $this->title = false;
121  $this->name = false;
122  $this->group = 'deleted'; // needed for direct use of constructor
123  $this->key = '';
124  $this->size = 0;
125  $this->bits = 0;
126  $this->width = 0;
127  $this->height = 0;
128  $this->metadata = '';
129  $this->mime = "unknown/unknown";
130  $this->media_type = '';
131  $this->description = '';
132  $this->user = null;
133  $this->timestamp = null;
134  $this->deleted = 0;
135  $this->dataLoaded = false;
136  $this->exists = false;
137  $this->sha1 = '';
138 
139  if ( $title instanceof Title ) {
140  $this->title = File::normalizeTitle( $title, 'exception' );
141  $this->name = $title->getDBkey();
142  }
143 
144  if ( $id ) {
145  $this->id = $id;
146  }
147 
148  if ( $key ) {
149  $this->key = $key;
150  }
151 
152  if ( $sha1 ) {
153  $this->sha1 = $sha1;
154  }
155 
156  if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
157  throw new MWException( "No specifications provided to ArchivedFile constructor." );
158  }
159  }
160 
167  public function load() {
168  if ( $this->dataLoaded ) {
169  return true;
170  }
171  $conds = [];
172 
173  if ( $this->id > 0 ) {
174  $conds['fa_id'] = $this->id;
175  }
176  if ( $this->key ) {
177  $conds['fa_storage_group'] = $this->group;
178  $conds['fa_storage_key'] = $this->key;
179  }
180  if ( $this->title ) {
181  $conds['fa_name'] = $this->title->getDBkey();
182  }
183  if ( $this->sha1 ) {
184  $conds['fa_sha1'] = $this->sha1;
185  }
186 
187  if ( $conds === [] ) {
188  throw new MWException( "No specific information for retrieving archived file" );
189  }
190 
191  if ( !$this->title || $this->title->getNamespace() === NS_FILE ) {
192  $this->dataLoaded = true; // set it here, to have also true on miss
193  $dbr = wfGetDB( DB_REPLICA );
194  $fileQuery = self::getQueryInfo();
195  $row = $dbr->selectRow(
196  $fileQuery['tables'],
197  $fileQuery['fields'],
198  $conds,
199  __METHOD__,
200  [ 'ORDER BY' => 'fa_timestamp DESC' ],
201  $fileQuery['joins']
202  );
203  if ( !$row ) {
204  // this revision does not exist?
205  return null;
206  }
207 
208  // initialize fields for filestore image object
209  $this->loadFromRow( $row );
210  } else {
211  throw new MWException( 'This title does not correspond to an image page.' );
212  }
213  $this->exists = true;
214 
215  return true;
216  }
217 
225  public static function newFromRow( $row ) {
226  $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
227  $file->loadFromRow( $row );
228 
229  return $file;
230  }
231 
247  public static function getQueryInfo() {
248  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
249  return [
250  'tables' => [
251  'filearchive',
252  'filearchive_actor' => 'actor'
253  ] + $commentQuery['tables'],
254  'fields' => [
255  'fa_id',
256  'fa_name',
257  'fa_archive_name',
258  'fa_storage_key',
259  'fa_storage_group',
260  'fa_size',
261  'fa_bits',
262  'fa_width',
263  'fa_height',
264  'fa_metadata',
265  'fa_media_type',
266  'fa_major_mime',
267  'fa_minor_mime',
268  'fa_timestamp',
269  'fa_deleted',
270  'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
271  'fa_sha1',
272  'fa_actor',
273  'fa_user' => 'filearchive_actor.actor_user',
274  'fa_user_text' => 'filearchive_actor.actor_name'
275  ] + $commentQuery['fields'],
276  'joins' => [
277  'filearchive_actor' => [ 'JOIN', 'actor_id=fa_actor' ]
278  ] + $commentQuery['joins'],
279  ];
280  }
281 
289  public function loadFromRow( $row ) {
290  $this->id = intval( $row->fa_id );
291  $this->name = $row->fa_name;
292  $this->archive_name = $row->fa_archive_name;
293  $this->group = $row->fa_storage_group;
294  $this->key = $row->fa_storage_key;
295  $this->size = $row->fa_size;
296  $this->bits = $row->fa_bits;
297  $this->width = $row->fa_width;
298  $this->height = $row->fa_height;
299  $this->metadata = $row->fa_metadata;
300  $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
301  $this->media_type = $row->fa_media_type;
302  $this->description = MediaWikiServices::getInstance()->getCommentStore()
303  // Legacy because $row may have come from self::selectFields()
304  ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
305  $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
306  $this->timestamp = $row->fa_timestamp;
307  $this->deleted = $row->fa_deleted;
308  if ( isset( $row->fa_sha1 ) ) {
309  $this->sha1 = $row->fa_sha1;
310  } else {
311  // old row, populate from key
312  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
313  }
314  if ( !$this->title ) {
315  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
316  }
317  }
318 
324  public function getTitle() {
325  if ( !$this->title ) {
326  $this->load();
327  }
328  return $this->title;
329  }
330 
336  public function getName() {
337  if ( $this->name === false ) {
338  $this->load();
339  }
340 
341  return $this->name;
342  }
343 
347  public function getID() {
348  $this->load();
349 
350  return $this->id;
351  }
352 
356  public function exists() {
357  $this->load();
358 
359  return $this->exists;
360  }
361 
366  public function getKey() {
367  $this->load();
368 
369  return $this->key;
370  }
371 
376  public function getStorageKey() {
377  return $this->getKey();
378  }
379 
384  public function getGroup() {
385  return $this->group;
386  }
387 
392  public function getWidth() {
393  $this->load();
394 
395  return $this->width;
396  }
397 
402  public function getHeight() {
403  $this->load();
404 
405  return $this->height;
406  }
407 
412  public function getMetadata() {
413  $this->load();
414 
415  return $this->metadata;
416  }
417 
422  public function getSize() {
423  $this->load();
424 
425  return $this->size;
426  }
427 
432  public function getBits() {
433  $this->load();
434 
435  return $this->bits;
436  }
437 
442  public function getMimeType() {
443  $this->load();
444 
445  return $this->mime;
446  }
447 
452  private function getHandler() {
453  if ( !isset( $this->handler ) ) {
454  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
455  }
456 
457  return $this->handler;
458  }
459 
466  public function pageCount() {
467  if ( !isset( $this->pageCount ) ) {
468  // @FIXME: callers expect File objects
469  // @phan-suppress-next-line PhanTypeMismatchArgument
470  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
471  // @phan-suppress-next-line PhanTypeMismatchArgument
472  $this->pageCount = $this->handler->pageCount( $this );
473  } else {
474  $this->pageCount = false;
475  }
476  }
477 
478  return $this->pageCount;
479  }
480 
486  public function getMediaType() {
487  $this->load();
488 
489  return $this->media_type;
490  }
491 
497  public function getTimestamp() {
498  $this->load();
499 
500  return wfTimestamp( TS_MW, $this->timestamp );
501  }
502 
509  public function getSha1() {
510  $this->load();
511 
512  return $this->sha1;
513  }
514 
527  public function getUser( $type = 'text' ) {
528  wfDeprecated( __METHOD__, '1.37' );
529  $this->load();
530 
531  if ( $type === 'object' ) {
532  return $this->user ? User::newFromIdentity( $this->user ) : null;
533  } elseif ( $type === 'text' ) {
534  return $this->user ? $this->user->getName() : '';
535  } elseif ( $type === 'id' ) {
536  return $this->user ? $this->user->getId() : 0;
537  }
538 
539  throw new MWException( "Unknown type '$type'." );
540  }
541 
553  public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
554  $this->load();
555  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
556  return null;
557  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
558  return null;
559  } else {
560  return $this->user;
561  }
562  }
563 
576  public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
577  $this->load();
578  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
579  return '';
580  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
581  return '';
582  } else {
583  return $this->description;
584  }
585  }
586 
593  public function getRawUser() {
594  wfDeprecated( __METHOD__, '1.37' );
595  return $this->getUser( 'id' );
596  }
597 
604  public function getRawUserText() {
605  wfDeprecated( __METHOD__, '1.37' );
606  return $this->getUser( 'text' );
607  }
608 
615  public function getRawDescription() {
616  wfDeprecated( __METHOD__, '1.37' );
617  $this->load();
618  return $this->description;
619  }
620 
625  public function getVisibility() {
626  $this->load();
627 
628  return $this->deleted;
629  }
630 
637  public function isDeleted( $field ) {
638  $this->load();
639 
640  return ( $this->deleted & $field ) == $field;
641  }
642 
650  public function userCan( $field, Authority $performer ) {
651  $this->load();
652  $title = $this->getTitle();
653 
654  return RevisionRecord::userCanBitfield(
655  $this->deleted,
656  $field,
657  $performer,
658  $title ?: null
659  );
660  }
661 }
ArchivedFile\loadFromRow
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
Definition: ArchivedFile.php:289
ArchivedFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: ArchivedFile.php:88
ArchivedFile\exists
exists()
Definition: ArchivedFile.php:356
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ArchivedFile\getID
getID()
Definition: ArchivedFile.php:347
ArchivedFile\$sha1
string $sha1
SHA-1 hash of file content.
Definition: ArchivedFile.php:91
ArchivedFile\getKey
getKey()
Return the FileStore key.
Definition: ArchivedFile.php:366
File\DELETED_USER
const DELETED_USER
Definition: File.php:72
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:497
ArchivedFile\$metadata
string $metadata
Definition: ArchivedFile.php:67
ArchivedFile\$dataLoaded
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
Definition: ArchivedFile.php:85
ArchivedFile\$group
string $group
FileStore storage group.
Definition: ArchivedFile.php:49
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:180
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1692
ArchivedFile\$width
int $width
Width.
Definition: ArchivedFile.php:61
ArchivedFile\isDeleted
isDeleted( $field)
for file or revision rows
Definition: ArchivedFile.php:637
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ArchivedFile\FOR_PUBLIC
const FOR_PUBLIC
Definition: ArchivedFile.php:38
ArchivedFile\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
Definition: ArchivedFile.php:247
ArchivedFile\$key
string $key
FileStore SHA-1 key.
Definition: ArchivedFile.php:52
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:666
ArchivedFile\$bits
int $bits
Size in bytes.
Definition: ArchivedFile.php:58
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:376
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
LocalRepo\getHashFromKey
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:194
File\normalizeTitle
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
Definition: File.php:205
ArchivedFile\$archive_name
string $archive_name
Original base filename.
Definition: ArchivedFile.php:99
$dbr
$dbr
Definition: testCompression.php:54
ArchivedFile\getMetadata
getMetadata()
Get handler-specific metadata.
Definition: ArchivedFile.php:412
ArchivedFile\getSize
getSize()
Return the size of the image file, in bytes.
Definition: ArchivedFile.php:422
ArchivedFile\load
load()
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:167
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1058
ArchivedFile\userCan
userCan( $field, Authority $performer)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
Definition: ArchivedFile.php:650
ArchivedFile\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: ArchivedFile.php:452
MWException
MediaWiki exception.
Definition: MWException.php:29
ArchivedFile\$id
int $id
Filearchive row ID.
Definition: ArchivedFile.php:43
File\DELETED_COMMENT
const DELETED_COMMENT
Definition: File.php:71
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:996
ArchivedFile\$exists
bool $exists
Definition: ArchivedFile.php:108
ArchivedFile\getMimeType
getMimeType()
Returns the MIME type of the file.
Definition: ArchivedFile.php:442
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2306
ArchivedFile\$mime
string $mime
MIME type.
Definition: ArchivedFile.php:70
ArchivedFile\getUploader
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: ArchivedFile.php:553
ArchivedFile\getTitle
getTitle()
Return the associated title object.
Definition: ArchivedFile.php:324
ArchivedFile\pageCount
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
Definition: ArchivedFile.php:466
ArchivedFile\getVisibility
getVisibility()
Returns the deletion bitfield.
Definition: ArchivedFile.php:625
ArchivedFile\__construct
__construct( $title, $id=0, $key='', $sha1='')
Definition: ArchivedFile.php:118
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:650
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ArchivedFile\$size
int $size
File size in bytes.
Definition: ArchivedFile.php:55
User\newFromAnyId
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:695
ArchivedFile\$handler
MediaHandler $handler
Definition: ArchivedFile.php:102
ArchivedFile\$title
Title $title
Definition: ArchivedFile.php:105
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:35
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:676
ArchivedFile\RAW
const RAW
Definition: ArchivedFile.php:40
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:35
ArchivedFile\$user
UserIdentity null $user
Uploader.
Definition: ArchivedFile.php:79
ArchivedFile\$pageCount
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
Definition: ArchivedFile.php:96
ArchivedFile\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: ArchivedFile.php:509
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:336
ArchivedFile\getGroup
getGroup()
Return the FileStore storage group.
Definition: ArchivedFile.php:384
ArchivedFile\getWidth
getWidth()
Return the width of the image.
Definition: ArchivedFile.php:392
ArchivedFile\getDescription
getDescription(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Return upload description.
Definition: ArchivedFile.php:576
ArchivedFile\getRawUserText
getRawUserText()
Return the user name of the uploader.
Definition: ArchivedFile.php:604
ArchivedFile\getUser
getUser( $type='text')
Returns ID or name of user who uploaded the file.
Definition: ArchivedFile.php:527
ArchivedFile\$timestamp
string $timestamp
Time of upload.
Definition: ArchivedFile.php:82
Title
Represents a title within MediaWiki.
Definition: Title.php:49
ArchivedFile\getRawDescription
getRawDescription()
Return upload description.
Definition: ArchivedFile.php:615
MediaHandler\getHandler
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Definition: MediaHandler.php:53
ArchivedFile\$description
string $description
Upload description.
Definition: ArchivedFile.php:76
ArchivedFile\getHeight
getHeight()
Return the height of the image.
Definition: ArchivedFile.php:402
ArchivedFile\$name
string $name
File name.
Definition: ArchivedFile.php:46
ArchivedFile\getBits
getBits()
Return the bits of the image file, in bytes.
Definition: ArchivedFile.php:432
NS_FILE
const NS_FILE
Definition: Defines.php:70
ArchivedFile\$height
int $height
Height.
Definition: ArchivedFile.php:64
ArchivedFile\getRawUser
getRawUser()
Return the user ID of the uploader.
Definition: ArchivedFile.php:593
MediaHandler
Base media handler class.
Definition: MediaHandler.php:37
ArchivedFile\$media_type
string $media_type
Media type.
Definition: ArchivedFile.php:73
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:225
ArchivedFile\getMediaType
getMediaType()
Return the type of the media in the file.
Definition: ArchivedFile.php:486
ArchivedFile\FOR_THIS_USER
const FOR_THIS_USER
Definition: ArchivedFile.php:39
$type
$type
Definition: testCompression.php:52