MediaWiki  master
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 }
MediaHandler $handler
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
getDescription()
Return upload description.
string $media_type
Media type.
static newFromRow( $row)
Loads a file object from the filearchive table.
const DELETED_COMMENT
Definition: File.php:64
getRawUser()
Return the user ID of the uploader.
int false $pageCount
Number of pages of a multipage document, or false for documents which aren&#39;t multipage documents...
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object...
int $deleted
Bitfield akin to rev_deleted.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:596
getRawUserText()
Return the user name of the uploader.
int $bits
Size in bytes.
getStorageKey()
Return the FileStore key (overriding base File class)
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
string $group
FileStore storage group.
getSha1()
Get the SHA-1 base 36 hash of the file.
getHeight()
Return the height of the image.
getMetadata()
Get handler-specific metadata.
getHandler()
Get a MediaHandler instance for this file.
string $timestamp
Time of upload.
User null $user
Uploader.
getName()
Return the file name.
getGroup()
Return the FileStore storage group.
string $description
Upload description.
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2229
getWidth()
Return the width of the image.
isDeleted( $field)
for file or revision rows
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren&#39;t multipage do...
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
string $mime
MIME type.
Class representing a row of the &#39;filearchive&#39; table.
getTitle()
Return the associated title object.
static newMigration()
Static constructor.
getSize()
Return the size of the image file, in bytes.
getDBkey()
Get the main part with underscores.
Definition: Title.php:1016
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, if it&#39;s marked as deleted.
Definition: Revision.php:1040
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this FileStore image file...
getMediaType()
Return the type of the media in the file.
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
const NS_FILE
Definition: Defines.php:66
string $sha1
SHA-1 hash of file content.
getKey()
Return the FileStore key.
int $size
File size in bytes.
__construct( $title, $id=0, $key='', $sha1='')
int $width
Width.
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:183
string $metadata
Metadata string.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:612
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
string $key
FileStore SHA-1 key.
string $archive_name
Original base filename.
int $height
Height.
string $name
File name.
getUser( $type='text')
Returns ID or name of user who uploaded the file.
const DB_REPLICA
Definition: defines.php:25
int $id
Filearchive row ID.
load()
Loads a file object from the filearchive table.
getRawDescription()
Return upload description.
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
getTimestamp()
Return upload timestamp.
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
getMimeType()
Returns the MIME type of the file.
getBits()
Return the bits of the image file, in bytes.
getVisibility()
Returns the deletion bitfield.