MediaWiki  1.34.0
ArchivedFile.php
Go to the documentation of this file.
1 <?php
25 
31 class ArchivedFile {
33  private $id;
34 
36  private $name;
37 
39  private $group;
40 
42  private $key;
43 
45  private $size;
46 
48  private $bits;
49 
51  private $width;
52 
54  private $height;
55 
57  private $metadata;
58 
60  private $mime;
61 
63  private $media_type;
64 
66  private $description;
67 
69  private $user;
70 
72  private $timestamp;
73 
75  private $dataLoaded;
76 
78  private $deleted;
79 
81  private $sha1;
82 
86  private $pageCount;
87 
89  private $archive_name;
90 
92  protected $handler;
93 
95  protected $title; # image title
96 
98  private $exists;
99 
107  function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
108  $this->id = -1;
109  $this->title = false;
110  $this->name = false;
111  $this->group = 'deleted'; // needed for direct use of constructor
112  $this->key = '';
113  $this->size = 0;
114  $this->bits = 0;
115  $this->width = 0;
116  $this->height = 0;
117  $this->metadata = '';
118  $this->mime = "unknown/unknown";
119  $this->media_type = '';
120  $this->description = '';
121  $this->user = null;
122  $this->timestamp = null;
123  $this->deleted = 0;
124  $this->dataLoaded = false;
125  $this->exists = false;
126  $this->sha1 = '';
127 
128  if ( $title instanceof Title ) {
129  $this->title = File::normalizeTitle( $title, 'exception' );
130  $this->name = $title->getDBkey();
131  }
132 
133  if ( $id ) {
134  $this->id = $id;
135  }
136 
137  if ( $key ) {
138  $this->key = $key;
139  }
140 
141  if ( $sha1 ) {
142  $this->sha1 = $sha1;
143  }
144 
145  if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
146  throw new MWException( "No specifications provided to ArchivedFile constructor." );
147  }
148  }
149 
155  public function load() {
156  if ( $this->dataLoaded ) {
157  return true;
158  }
159  $conds = [];
160 
161  if ( $this->id > 0 ) {
162  $conds['fa_id'] = $this->id;
163  }
164  if ( $this->key ) {
165  $conds['fa_storage_group'] = $this->group;
166  $conds['fa_storage_key'] = $this->key;
167  }
168  if ( $this->title ) {
169  $conds['fa_name'] = $this->title->getDBkey();
170  }
171  if ( $this->sha1 ) {
172  $conds['fa_sha1'] = $this->sha1;
173  }
174 
175  if ( $conds === [] ) {
176  throw new MWException( "No specific information for retrieving archived file" );
177  }
178 
179  if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
180  $this->dataLoaded = true; // set it here, to have also true on miss
181  $dbr = wfGetDB( DB_REPLICA );
182  $fileQuery = self::getQueryInfo();
183  $row = $dbr->selectRow(
184  $fileQuery['tables'],
185  $fileQuery['fields'],
186  $conds,
187  __METHOD__,
188  [ 'ORDER BY' => 'fa_timestamp DESC' ],
189  $fileQuery['joins']
190  );
191  if ( !$row ) {
192  // this revision does not exist?
193  return null;
194  }
195 
196  // initialize fields for filestore image object
197  $this->loadFromRow( $row );
198  } else {
199  throw new MWException( 'This title does not correspond to an image page.' );
200  }
201  $this->exists = true;
202 
203  return true;
204  }
205 
212  public static function newFromRow( $row ) {
213  $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
214  $file->loadFromRow( $row );
215 
216  return $file;
217  }
218 
228  public static function getQueryInfo() {
229  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
230  $actorQuery = ActorMigration::newMigration()->getJoin( 'fa_user' );
231  return [
232  'tables' => [ 'filearchive' ] + $commentQuery['tables'] + $actorQuery['tables'],
233  'fields' => [
234  'fa_id',
235  'fa_name',
236  'fa_archive_name',
237  'fa_storage_key',
238  'fa_storage_group',
239  'fa_size',
240  'fa_bits',
241  'fa_width',
242  'fa_height',
243  'fa_metadata',
244  'fa_media_type',
245  'fa_major_mime',
246  'fa_minor_mime',
247  'fa_timestamp',
248  'fa_deleted',
249  'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
250  'fa_sha1',
251  ] + $commentQuery['fields'] + $actorQuery['fields'],
252  'joins' => $commentQuery['joins'] + $actorQuery['joins'],
253  ];
254  }
255 
262  public function loadFromRow( $row ) {
263  $this->id = intval( $row->fa_id );
264  $this->name = $row->fa_name;
265  $this->archive_name = $row->fa_archive_name;
266  $this->group = $row->fa_storage_group;
267  $this->key = $row->fa_storage_key;
268  $this->size = $row->fa_size;
269  $this->bits = $row->fa_bits;
270  $this->width = $row->fa_width;
271  $this->height = $row->fa_height;
272  $this->metadata = $row->fa_metadata;
273  $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
274  $this->media_type = $row->fa_media_type;
275  $this->description = MediaWikiServices::getInstance()->getCommentStore()
276  // Legacy because $row may have come from self::selectFields()
277  ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
278  $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
279  $this->timestamp = $row->fa_timestamp;
280  $this->deleted = $row->fa_deleted;
281  if ( isset( $row->fa_sha1 ) ) {
282  $this->sha1 = $row->fa_sha1;
283  } else {
284  // old row, populate from key
285  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
286  }
287  if ( !$this->title ) {
288  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
289  }
290  }
291 
297  public function getTitle() {
298  if ( !$this->title ) {
299  $this->load();
300  }
301  return $this->title;
302  }
303 
309  public function getName() {
310  if ( $this->name === false ) {
311  $this->load();
312  }
313 
314  return $this->name;
315  }
316 
320  public function getID() {
321  $this->load();
322 
323  return $this->id;
324  }
325 
329  public function exists() {
330  $this->load();
331 
332  return $this->exists;
333  }
334 
339  public function getKey() {
340  $this->load();
341 
342  return $this->key;
343  }
344 
349  public function getStorageKey() {
350  return $this->getKey();
351  }
352 
357  public function getGroup() {
358  return $this->group;
359  }
360 
365  public function getWidth() {
366  $this->load();
367 
368  return $this->width;
369  }
370 
375  public function getHeight() {
376  $this->load();
377 
378  return $this->height;
379  }
380 
385  public function getMetadata() {
386  $this->load();
387 
388  return $this->metadata;
389  }
390 
395  public function getSize() {
396  $this->load();
397 
398  return $this->size;
399  }
400 
405  public function getBits() {
406  $this->load();
407 
408  return $this->bits;
409  }
410 
415  public function getMimeType() {
416  $this->load();
417 
418  return $this->mime;
419  }
420 
425  function getHandler() {
426  if ( !isset( $this->handler ) ) {
427  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
428  }
429 
430  return $this->handler;
431  }
432 
438  function pageCount() {
439  if ( !isset( $this->pageCount ) ) {
440  // @FIXME: callers expect File objects
441  // @phan-suppress-next-line PhanTypeMismatchArgument
442  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
443  // @phan-suppress-next-line PhanTypeMismatchArgument
444  $this->pageCount = $this->handler->pageCount( $this );
445  } else {
446  $this->pageCount = false;
447  }
448  }
449 
450  return $this->pageCount;
451  }
452 
458  public function getMediaType() {
459  $this->load();
460 
461  return $this->media_type;
462  }
463 
469  public function getTimestamp() {
470  $this->load();
471 
472  return wfTimestamp( TS_MW, $this->timestamp );
473  }
474 
481  function getSha1() {
482  $this->load();
483 
484  return $this->sha1;
485  }
486 
498  public function getUser( $type = 'text' ) {
499  $this->load();
500 
501  if ( $type === 'object' ) {
502  return $this->user;
503  } elseif ( $type === 'text' ) {
504  return $this->user ? $this->user->getName() : '';
505  } elseif ( $type === 'id' ) {
506  return $this->user ? $this->user->getId() : 0;
507  }
508 
509  throw new MWException( "Unknown type '$type'." );
510  }
511 
517  public function getDescription() {
518  $this->load();
519  if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
520  return 0;
521  } else {
522  return $this->description;
523  }
524  }
525 
531  public function getRawUser() {
532  return $this->getUser( 'id' );
533  }
534 
540  public function getRawUserText() {
541  return $this->getUser( 'text' );
542  }
543 
549  public function getRawDescription() {
550  $this->load();
551 
552  return $this->description;
553  }
554 
559  public function getVisibility() {
560  $this->load();
561 
562  return $this->deleted;
563  }
564 
571  public function isDeleted( $field ) {
572  $this->load();
573 
574  return ( $this->deleted & $field ) == $field;
575  }
576 
584  public function userCan( $field, User $user = null ) {
585  $this->load();
586 
587  $title = $this->getTitle();
588  return Revision::userCanBitfield( $this->deleted, $field, $user, $title ?: null );
589  }
590 }
ArchivedFile\loadFromRow
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
Definition: ArchivedFile.php:262
ArchivedFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: ArchivedFile.php:78
ArchivedFile\exists
exists()
Definition: ArchivedFile.php:329
ArchivedFile\getID
getID()
Definition: ArchivedFile.php:320
ArchivedFile\$sha1
string $sha1
SHA-1 hash of file content.
Definition: ArchivedFile.php:81
ArchivedFile\getKey
getKey()
Return the FileStore key.
Definition: ArchivedFile.php:339
ArchivedFile\getTimestamp
getTimestamp()
Return upload timestamp.
Definition: ArchivedFile.php:469
Revision\userCanBitfield
static userCanBitfield( $bitfield, $field, User $user=null, Title $title=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: Revision.php:1046
ArchivedFile\$metadata
string $metadata
Metadata string.
Definition: ArchivedFile.php:57
ArchivedFile\$dataLoaded
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
Definition: ArchivedFile.php:75
ArchivedFile\$group
string $group
FileStore storage group.
Definition: ArchivedFile.php:39
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
ArchivedFile\$width
int $width
Width.
Definition: ArchivedFile.php:51
ArchivedFile\isDeleted
isDeleted( $field)
for file or revision rows
Definition: ArchivedFile.php:571
NS_FILE
const NS_FILE
Definition: Defines.php:66
$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:228
ArchivedFile\$key
string $key
FileStore SHA-1 key.
Definition: ArchivedFile.php:42
ArchivedFile\$bits
int $bits
Size in bytes.
Definition: ArchivedFile.php:48
ArchivedFile\getStorageKey
getStorageKey()
Return the FileStore key (overriding base File class)
Definition: ArchivedFile.php:349
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:136
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:194
ArchivedFile\$archive_name
string $archive_name
Original base filename.
Definition: ArchivedFile.php:89
$dbr
$dbr
Definition: testCompression.php:50
ArchivedFile\getMetadata
getMetadata()
Get handler-specific metadata.
Definition: ArchivedFile.php:385
ArchivedFile\$user
User null $user
Uploader.
Definition: ArchivedFile.php:69
ArchivedFile\getSize
getSize()
Return the size of the image file, in bytes.
Definition: ArchivedFile.php:395
ArchivedFile\load
load()
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:155
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1013
ArchivedFile\getHandler
getHandler()
Get a MediaHandler instance for this file.
Definition: ArchivedFile.php:425
MWException
MediaWiki exception.
Definition: MWException.php:26
ArchivedFile\$id
int $id
Filearchive row ID.
Definition: ArchivedFile.php:33
File\DELETED_COMMENT
const DELETED_COMMENT
Definition: File.php:64
ArchivedFile\$exists
bool $exists
Definition: ArchivedFile.php:98
ArchivedFile\getMimeType
getMimeType()
Returns the MIME type of the file.
Definition: ArchivedFile.php:415
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
ArchivedFile\$mime
string $mime
MIME type.
Definition: ArchivedFile.php:60
ArchivedFile\getTitle
getTitle()
Return the associated title object.
Definition: ArchivedFile.php:297
ArchivedFile\pageCount
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
Definition: ArchivedFile.php:438
ArchivedFile\getVisibility
getVisibility()
Returns the deletion bitfield.
Definition: ArchivedFile.php:559
ArchivedFile\__construct
__construct( $title, $id=0, $key='', $sha1='')
Definition: ArchivedFile.php:107
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ArchivedFile\$size
int $size
File size in bytes.
Definition: ArchivedFile.php:45
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:596
ArchivedFile\$handler
MediaHandler $handler
Definition: ArchivedFile.php:92
ArchivedFile\userCan
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
Definition: ArchivedFile.php:584
ArchivedFile\getDescription
getDescription()
Return upload description.
Definition: ArchivedFile.php:517
ArchivedFile\$title
Title $title
Definition: ArchivedFile.php:95
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
ArchivedFile
Class representing a row of the 'filearchive' table.
Definition: ArchivedFile.php:31
ArchivedFile\$pageCount
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
Definition: ArchivedFile.php:86
ArchivedFile\getSha1
getSha1()
Get the SHA-1 base 36 hash of the file.
Definition: ArchivedFile.php:481
ArchivedFile\getName
getName()
Return the file name.
Definition: ArchivedFile.php:309
ArchivedFile\getGroup
getGroup()
Return the FileStore storage group.
Definition: ArchivedFile.php:357
ArchivedFile\getWidth
getWidth()
Return the width of the image.
Definition: ArchivedFile.php:365
ArchivedFile\getRawUserText
getRawUserText()
Return the user name of the uploader.
Definition: ArchivedFile.php:540
ArchivedFile\getUser
getUser( $type='text')
Returns ID or name of user who uploaded the file.
Definition: ArchivedFile.php:498
ArchivedFile\$timestamp
string $timestamp
Time of upload.
Definition: ArchivedFile.php:72
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ArchivedFile\getRawDescription
getRawDescription()
Return upload description.
Definition: ArchivedFile.php:549
MediaHandler\getHandler
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Definition: MediaHandler.php:46
ArchivedFile\$description
string $description
Upload description.
Definition: ArchivedFile.php:66
ArchivedFile\getHeight
getHeight()
Return the height of the image.
Definition: ArchivedFile.php:375
ArchivedFile\$name
string $name
File name.
Definition: ArchivedFile.php:36
ArchivedFile\getBits
getBits()
Return the bits of the image file, in bytes.
Definition: ArchivedFile.php:405
ArchivedFile\$height
int $height
Height.
Definition: ArchivedFile.php:54
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2232
ArchivedFile\getRawUser
getRawUser()
Return the user ID of the uploader.
Definition: ArchivedFile.php:531
MediaHandler
Base media handler class.
Definition: MediaHandler.php:30
ArchivedFile\$media_type
string $media_type
Media type.
Definition: ArchivedFile.php:63
ArchivedFile\newFromRow
static newFromRow( $row)
Loads a file object from the filearchive table.
Definition: ArchivedFile.php:212
ArchivedFile\getMediaType
getMediaType()
Return the type of the media in the file.
Definition: ArchivedFile.php:458
$type
$type
Definition: testCompression.php:48