MediaWiki  master
ArchivedFile.php
Go to the documentation of this file.
1 <?php
26 
33 class ArchivedFile {
35  private $id;
36 
38  private $name;
39 
41  private $group;
42 
44  private $key;
45 
47  private $size;
48 
50  private $bits;
51 
53  private $width;
54 
56  private $height;
57 
59  private $metadata;
60 
62  private $mime;
63 
65  private $media_type;
66 
68  private $description;
69 
71  private $user;
72 
74  private $timestamp;
75 
77  private $dataLoaded;
78 
80  private $deleted;
81 
83  private $sha1;
84 
88  private $pageCount;
89 
91  private $archive_name;
92 
94  protected $handler;
95 
97  protected $title; # image title
98 
100  protected $exists;
101 
110  public function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
111  $this->id = -1;
112  $this->title = false;
113  $this->name = false;
114  $this->group = 'deleted'; // needed for direct use of constructor
115  $this->key = '';
116  $this->size = 0;
117  $this->bits = 0;
118  $this->width = 0;
119  $this->height = 0;
120  $this->metadata = '';
121  $this->mime = "unknown/unknown";
122  $this->media_type = '';
123  $this->description = '';
124  $this->user = null;
125  $this->timestamp = null;
126  $this->deleted = 0;
127  $this->dataLoaded = false;
128  $this->exists = false;
129  $this->sha1 = '';
130 
131  if ( $title instanceof Title ) {
132  $this->title = File::normalizeTitle( $title, 'exception' );
133  $this->name = $title->getDBkey();
134  }
135 
136  if ( $id ) {
137  $this->id = $id;
138  }
139 
140  if ( $key ) {
141  $this->key = $key;
142  }
143 
144  if ( $sha1 ) {
145  $this->sha1 = $sha1;
146  }
147 
148  if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
149  throw new MWException( "No specifications provided to ArchivedFile constructor." );
150  }
151  }
152 
159  public function load() {
160  if ( $this->dataLoaded ) {
161  return true;
162  }
163  $conds = [];
164 
165  if ( $this->id > 0 ) {
166  $conds['fa_id'] = $this->id;
167  }
168  if ( $this->key ) {
169  $conds['fa_storage_group'] = $this->group;
170  $conds['fa_storage_key'] = $this->key;
171  }
172  if ( $this->title ) {
173  $conds['fa_name'] = $this->title->getDBkey();
174  }
175  if ( $this->sha1 ) {
176  $conds['fa_sha1'] = $this->sha1;
177  }
178 
179  if ( $conds === [] ) {
180  throw new MWException( "No specific information for retrieving archived file" );
181  }
182 
183  if ( !$this->title || $this->title->getNamespace() === NS_FILE ) {
184  $this->dataLoaded = true; // set it here, to have also true on miss
185  $dbr = wfGetDB( DB_REPLICA );
186  $fileQuery = self::getQueryInfo();
187  $row = $dbr->selectRow(
188  $fileQuery['tables'],
189  $fileQuery['fields'],
190  $conds,
191  __METHOD__,
192  [ 'ORDER BY' => 'fa_timestamp DESC' ],
193  $fileQuery['joins']
194  );
195  if ( !$row ) {
196  // this revision does not exist?
197  return null;
198  }
199 
200  // initialize fields for filestore image object
201  $this->loadFromRow( $row );
202  } else {
203  throw new MWException( 'This title does not correspond to an image page.' );
204  }
205  $this->exists = true;
206 
207  return true;
208  }
209 
217  public static function newFromRow( $row ) {
218  $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
219  $file->loadFromRow( $row );
220 
221  return $file;
222  }
223 
234  public static function getQueryInfo() {
235  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
236  $actorQuery = ActorMigration::newMigration()->getJoin( 'fa_user' );
237  return [
238  'tables' => [ 'filearchive' ] + $commentQuery['tables'] + $actorQuery['tables'],
239  'fields' => [
240  'fa_id',
241  'fa_name',
242  'fa_archive_name',
243  'fa_storage_key',
244  'fa_storage_group',
245  'fa_size',
246  'fa_bits',
247  'fa_width',
248  'fa_height',
249  'fa_metadata',
250  'fa_media_type',
251  'fa_major_mime',
252  'fa_minor_mime',
253  'fa_timestamp',
254  'fa_deleted',
255  'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
256  'fa_sha1',
257  ] + $commentQuery['fields'] + $actorQuery['fields'],
258  'joins' => $commentQuery['joins'] + $actorQuery['joins'],
259  ];
260  }
261 
269  public function loadFromRow( $row ) {
270  $this->id = intval( $row->fa_id );
271  $this->name = $row->fa_name;
272  $this->archive_name = $row->fa_archive_name;
273  $this->group = $row->fa_storage_group;
274  $this->key = $row->fa_storage_key;
275  $this->size = $row->fa_size;
276  $this->bits = $row->fa_bits;
277  $this->width = $row->fa_width;
278  $this->height = $row->fa_height;
279  $this->metadata = $row->fa_metadata;
280  $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
281  $this->media_type = $row->fa_media_type;
282  $this->description = MediaWikiServices::getInstance()->getCommentStore()
283  // Legacy because $row may have come from self::selectFields()
284  ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
285  $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
286  $this->timestamp = $row->fa_timestamp;
287  $this->deleted = $row->fa_deleted;
288  if ( isset( $row->fa_sha1 ) ) {
289  $this->sha1 = $row->fa_sha1;
290  } else {
291  // old row, populate from key
292  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
293  }
294  if ( !$this->title ) {
295  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
296  }
297  }
298 
304  public function getTitle() {
305  if ( !$this->title ) {
306  $this->load();
307  }
308  return $this->title;
309  }
310 
316  public function getName() {
317  if ( $this->name === false ) {
318  $this->load();
319  }
320 
321  return $this->name;
322  }
323 
327  public function getID() {
328  $this->load();
329 
330  return $this->id;
331  }
332 
336  public function exists() {
337  $this->load();
338 
339  return $this->exists;
340  }
341 
346  public function getKey() {
347  $this->load();
348 
349  return $this->key;
350  }
351 
356  public function getStorageKey() {
357  return $this->getKey();
358  }
359 
364  public function getGroup() {
365  return $this->group;
366  }
367 
372  public function getWidth() {
373  $this->load();
374 
375  return $this->width;
376  }
377 
382  public function getHeight() {
383  $this->load();
384 
385  return $this->height;
386  }
387 
392  public function getMetadata() {
393  $this->load();
394 
395  return $this->metadata;
396  }
397 
402  public function getSize() {
403  $this->load();
404 
405  return $this->size;
406  }
407 
412  public function getBits() {
413  $this->load();
414 
415  return $this->bits;
416  }
417 
422  public function getMimeType() {
423  $this->load();
424 
425  return $this->mime;
426  }
427 
432  private function getHandler() {
433  if ( !isset( $this->handler ) ) {
434  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
435  }
436 
437  return $this->handler;
438  }
439 
446  public function pageCount() {
447  if ( !isset( $this->pageCount ) ) {
448  // @FIXME: callers expect File objects
449  // @phan-suppress-next-line PhanTypeMismatchArgument
450  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
451  // @phan-suppress-next-line PhanTypeMismatchArgument
452  $this->pageCount = $this->handler->pageCount( $this );
453  } else {
454  $this->pageCount = false;
455  }
456  }
457 
458  return $this->pageCount;
459  }
460 
466  public function getMediaType() {
467  $this->load();
468 
469  return $this->media_type;
470  }
471 
477  public function getTimestamp() {
478  $this->load();
479 
480  return wfTimestamp( TS_MW, $this->timestamp );
481  }
482 
489  public function getSha1() {
490  $this->load();
491 
492  return $this->sha1;
493  }
494 
506  public function getUser( $type = 'text' ) {
507  $this->load();
508 
509  if ( $type === 'object' ) {
510  return $this->user;
511  } elseif ( $type === 'text' ) {
512  return $this->user ? $this->user->getName() : '';
513  } elseif ( $type === 'id' ) {
514  return $this->user ? $this->user->getId() : 0;
515  }
516 
517  throw new MWException( "Unknown type '$type'." );
518  }
519 
525  public function getDescription() {
526  $this->load();
527  if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
528  return 0;
529  } else {
530  return $this->description;
531  }
532  }
533 
539  public function getRawUser() {
540  return $this->getUser( 'id' );
541  }
542 
548  public function getRawUserText() {
549  return $this->getUser( 'text' );
550  }
551 
557  public function getRawDescription() {
558  $this->load();
559 
560  return $this->description;
561  }
562 
567  public function getVisibility() {
568  $this->load();
569 
570  return $this->deleted;
571  }
572 
579  public function isDeleted( $field ) {
580  $this->load();
581 
582  return ( $this->deleted & $field ) == $field;
583  }
584 
592  public function userCan( $field, User $user ) {
593  $this->load();
594  $title = $this->getTitle();
595 
596  return RevisionRecord::userCanBitfield(
597  $this->deleted,
598  $field,
599  $user,
600  $title ?: null
601  );
602  }
603 }
ArchivedFile\loadFromRow
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
Definition: ArchivedFile.php:269
ArchivedFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: ArchivedFile.php:80
ArchivedFile\exists
exists()
Definition: ArchivedFile.php:336
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
ArchivedFile\getID
getID()
Definition: ArchivedFile.php:327
ArchivedFile\$sha1
string $sha1
SHA-1 hash of file content.
Definition: ArchivedFile.php:83
ArchivedFile\getKey
getKey()
Return the FileStore key.
Definition: ArchivedFile.php:346
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:477
ArchivedFile\$metadata
string $metadata
Metadata string.
Definition: ArchivedFile.php:59
ArchivedFile\$dataLoaded
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
Definition: ArchivedFile.php:77
ArchivedFile\$group
string $group
FileStore storage group.
Definition: ArchivedFile.php:41
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:160
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1815
ArchivedFile\$width
int $width
Width.
Definition: ArchivedFile.php:53
ArchivedFile\isDeleted
isDeleted( $field)
for file or revision rows
Definition: ArchivedFile.php:579
NS_FILE
const NS_FILE
Definition: Defines.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
ArchivedFile\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
Definition: ArchivedFile.php:234
ArchivedFile\$key
string $key
FileStore SHA-1 key.
Definition: ArchivedFile.php:44
ArchivedFile\$bits
int $bits
Size in bytes.
Definition: ArchivedFile.php:50
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:356
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:140
LocalRepo\getHashFromKey
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:183
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:199
ArchivedFile\userCan
userCan( $field, User $user)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
Definition: ArchivedFile.php:592
ArchivedFile\$archive_name
string $archive_name
Original base filename.
Definition: ArchivedFile.php:91
$dbr
$dbr
Definition: testCompression.php:54
ArchivedFile\getMetadata
getMetadata()
Get handler-specific metadata.
Definition: ArchivedFile.php:392
ArchivedFile\$user
User null $user
Uploader.
Definition: ArchivedFile.php:71
ArchivedFile\getSize
getSize()
Return the size of the image file, in bytes.
Definition: ArchivedFile.php:402
ArchivedFile\load
load()
Loads a file object from the filearchive table Stable to override.
Definition: ArchivedFile.php:159
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1025
ArchivedFile\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: ArchivedFile.php:432
MWException
MediaWiki exception.
Definition: MWException.php:29
ArchivedFile\$id
int $id
Filearchive row ID.
Definition: ArchivedFile.php:35
File\DELETED_COMMENT
const DELETED_COMMENT
Definition: File.php:68
ArchivedFile\$exists
bool $exists
Definition: ArchivedFile.php:100
ArchivedFile\getMimeType
getMimeType()
Returns the MIME type of the file.
Definition: ArchivedFile.php:422
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2475
ArchivedFile\$mime
string $mime
MIME type.
Definition: ArchivedFile.php:62
ArchivedFile\getTitle
getTitle()
Return the associated title object.
Definition: ArchivedFile.php:304
ArchivedFile\pageCount
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
Definition: ArchivedFile.php:446
ArchivedFile\getVisibility
getVisibility()
Returns the deletion bitfield.
Definition: ArchivedFile.php:567
ArchivedFile\__construct
__construct( $title, $id=0, $key='', $sha1='')
Stable to call.
Definition: ArchivedFile.php:110
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:591
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ArchivedFile\$size
int $size
File size in bytes.
Definition: ArchivedFile.php:47
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:630
ArchivedFile\$handler
MediaHandler $handler
Definition: ArchivedFile.php:94
ArchivedFile\getDescription
getDescription()
Return upload description.
Definition: ArchivedFile.php:525
ArchivedFile\$title
Title $title
Definition: ArchivedFile.php:97
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:617
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:33
ArchivedFile\$pageCount
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
Definition: ArchivedFile.php:88
ArchivedFile\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: ArchivedFile.php:489
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:316
ArchivedFile\getGroup
getGroup()
Return the FileStore storage group.
Definition: ArchivedFile.php:364
ArchivedFile\getWidth
getWidth()
Return the width of the image.
Definition: ArchivedFile.php:372
ArchivedFile\getRawUserText
getRawUserText()
Return the user name of the uploader.
Definition: ArchivedFile.php:548
ArchivedFile\getUser
getUser( $type='text')
Returns ID or name of user who uploaded the file.
Definition: ArchivedFile.php:506
ArchivedFile\$timestamp
string $timestamp
Time of upload.
Definition: ArchivedFile.php:74
Title
Represents a title within MediaWiki.
Definition: Title.php:41
ArchivedFile\getRawDescription
getRawDescription()
Return upload description.
Definition: ArchivedFile.php:557
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:68
ArchivedFile\getHeight
getHeight()
Return the height of the image.
Definition: ArchivedFile.php:382
ArchivedFile\$name
string $name
File name.
Definition: ArchivedFile.php:38
ArchivedFile\getBits
getBits()
Return the bits of the image file, in bytes.
Definition: ArchivedFile.php:412
ArchivedFile\$height
int $height
Height.
Definition: ArchivedFile.php:56
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2062
ArchivedFile\getRawUser
getRawUser()
Return the user ID of the uploader.
Definition: ArchivedFile.php:539
MediaHandler
Base media handler class.
Definition: MediaHandler.php:37
ArchivedFile\$media_type
string $media_type
Media type.
Definition: ArchivedFile.php:65
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table Stable to override.
Definition: ArchivedFile.php:217
ArchivedFile\getMediaType
getMediaType()
Return the type of the media in the file.
Definition: ArchivedFile.php:466
$type
$type
Definition: testCompression.php:52