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 = null;
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  $services = MediaWikiServices::getInstance();
303  $this->description = $services->getCommentStore()
304  // Legacy because $row may have come from self::selectFields()
305  ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
306  $this->user = $services->getUserFactory()
307  ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
308  $this->timestamp = $row->fa_timestamp;
309  $this->deleted = $row->fa_deleted;
310  if ( isset( $row->fa_sha1 ) ) {
311  $this->sha1 = $row->fa_sha1;
312  } else {
313  // old row, populate from key
314  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
315  }
316  if ( !$this->title ) {
317  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
318  }
319  }
320 
326  public function getTitle() {
327  if ( !$this->title ) {
328  $this->load();
329  }
330  return $this->title;
331  }
332 
338  public function getName() {
339  if ( $this->name === false ) {
340  $this->load();
341  }
342 
343  return $this->name;
344  }
345 
349  public function getID() {
350  $this->load();
351 
352  return $this->id;
353  }
354 
358  public function exists() {
359  $this->load();
360 
361  return $this->exists;
362  }
363 
368  public function getKey() {
369  $this->load();
370 
371  return $this->key;
372  }
373 
378  public function getStorageKey() {
379  return $this->getKey();
380  }
381 
386  public function getGroup() {
387  return $this->group;
388  }
389 
394  public function getWidth() {
395  $this->load();
396 
397  return $this->width;
398  }
399 
404  public function getHeight() {
405  $this->load();
406 
407  return $this->height;
408  }
409 
414  public function getMetadata() {
415  $this->load();
416 
417  return $this->metadata;
418  }
419 
424  public function getSize() {
425  $this->load();
426 
427  return $this->size;
428  }
429 
434  public function getBits() {
435  $this->load();
436 
437  return $this->bits;
438  }
439 
444  public function getMimeType() {
445  $this->load();
446 
447  return $this->mime;
448  }
449 
454  private function getHandler() {
455  if ( !isset( $this->handler ) ) {
456  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
457  }
458 
459  return $this->handler;
460  }
461 
468  public function pageCount() {
469  if ( !isset( $this->pageCount ) ) {
470  // @FIXME: callers expect File objects
471  // @phan-suppress-next-line PhanTypeMismatchArgument
472  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
473  // @phan-suppress-next-line PhanTypeMismatchArgument
474  $this->pageCount = $this->handler->pageCount( $this );
475  } else {
476  $this->pageCount = false;
477  }
478  }
479 
480  return $this->pageCount;
481  }
482 
488  public function getMediaType() {
489  $this->load();
490 
491  return $this->media_type;
492  }
493 
499  public function getTimestamp() {
500  $this->load();
501 
502  return wfTimestamp( TS_MW, $this->timestamp );
503  }
504 
511  public function getSha1() {
512  $this->load();
513 
514  return $this->sha1;
515  }
516 
528  public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
529  $this->load();
530  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
531  return null;
532  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
533  return null;
534  } else {
535  return $this->user;
536  }
537  }
538 
551  public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
552  $this->load();
553  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
554  return '';
555  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
556  return '';
557  } else {
558  return $this->description;
559  }
560  }
561 
566  public function getVisibility() {
567  $this->load();
568 
569  return $this->deleted;
570  }
571 
578  public function isDeleted( $field ) {
579  $this->load();
580 
581  return ( $this->deleted & $field ) == $field;
582  }
583 
591  public function userCan( $field, Authority $performer ) {
592  $this->load();
593  $title = $this->getTitle();
594 
595  return RevisionRecord::userCanBitfield(
596  $this->deleted,
597  $field,
598  $performer,
599  $title ?: null
600  );
601  }
602 }
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:358
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ArchivedFile\getID
getID()
Definition: ArchivedFile.php:349
ArchivedFile\$sha1
string $sha1
SHA-1 hash of file content.
Definition: ArchivedFile.php:91
ArchivedFile\getKey
getKey()
Return the FileStore key.
Definition: ArchivedFile.php:368
File\DELETED_USER
const DELETED_USER
Definition: File.php:73
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:499
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:203
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1649
ArchivedFile\$width
int $width
Width.
Definition: ArchivedFile.php:61
ArchivedFile\isDeleted
isDeleted( $field)
for file or revision rows
Definition: ArchivedFile.php:578
ArchivedFile\$title
Title null $title
Definition: ArchivedFile.php:105
$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
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:378
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:225
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:206
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:414
ArchivedFile\getSize
getSize()
Return the size of the image file, in bytes.
Definition: ArchivedFile.php:424
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:1057
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:591
ArchivedFile\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: ArchivedFile.php:454
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:72
ArchivedFile\$exists
bool $exists
Definition: ArchivedFile.php:108
ArchivedFile\getMimeType
getMimeType()
Returns the MIME type of the file.
Definition: ArchivedFile.php:444
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2186
ArchivedFile\$mime
string $mime
MIME type.
Definition: ArchivedFile.php:70
ArchivedFile\getUploader
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: ArchivedFile.php:528
ArchivedFile\getTitle
getTitle()
Return the associated title object.
Definition: ArchivedFile.php:326
ArchivedFile\pageCount
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
Definition: ArchivedFile.php:468
ArchivedFile\getVisibility
getVisibility()
Returns the deletion bitfield.
Definition: ArchivedFile.php:566
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:648
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ArchivedFile\$size
int $size
File size in bytes.
Definition: ArchivedFile.php:55
ArchivedFile\$handler
MediaHandler $handler
Definition: ArchivedFile.php:102
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:674
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:511
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:338
ArchivedFile\getGroup
getGroup()
Return the FileStore storage group.
Definition: ArchivedFile.php:386
ArchivedFile\getWidth
getWidth()
Return the width of the image.
Definition: ArchivedFile.php:394
ArchivedFile\getDescription
getDescription(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Return upload description.
Definition: ArchivedFile.php:551
ArchivedFile\$timestamp
string $timestamp
Time of upload.
Definition: ArchivedFile.php:82
Title
Represents a title within MediaWiki.
Definition: Title.php:47
MediaHandler\getHandler
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Definition: MediaHandler.php:54
ArchivedFile\$description
string $description
Upload description.
Definition: ArchivedFile.php:76
ArchivedFile\getHeight
getHeight()
Return the height of the image.
Definition: ArchivedFile.php:404
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:434
NS_FILE
const NS_FILE
Definition: Defines.php:70
ArchivedFile\$height
int $height
Height.
Definition: ArchivedFile.php:64
MediaHandler
Base media handler class.
Definition: MediaHandler.php:38
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:488
ArchivedFile\FOR_THIS_USER
const FOR_THIS_USER
Definition: ArchivedFile.php:39