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