MediaWiki REL1_38
ArchivedFile.php
Go to the documentation of this file.
1<?php
28
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
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
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}
const NS_FILE
Definition Defines.php:70
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Class representing a row of the 'filearchive' table.
getTimestamp()
Return upload timestamp.
getSize()
Return the size of the image file, in bytes.
MediaHandler $handler
bool $dataLoaded
Whether or not all this has been loaded from the database (loadFromXxx)
isDeleted( $field)
for file or revision rows
getHeight()
Return the height of the image.
int $height
Height.
getBits()
Return the bits of the image file, in bytes.
int $size
File size in bytes.
string $sha1
SHA-1 hash of file content.
string $media_type
Media type.
string $key
FileStore SHA-1 key.
string $group
FileStore storage group.
string $description
Upload description.
const FOR_THIS_USER
getSha1()
Get the SHA-1 base 36 hash of the file.
string $archive_name
Original base filename.
int false $pageCount
Number of pages of a multipage document, or false for documents which aren't multipage documents.
getHandler()
Get a MediaHandler instance for this file.
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archivedfile object.
loadFromRow( $row)
Load ArchivedFile object fields from a DB row.
string $mime
MIME type.
UserIdentity null $user
Uploader.
int $id
Filearchive row ID.
__construct( $title, $id=0, $key='', $sha1='')
string null $timestamp
Time of upload.
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
load()
Loads a file object from the filearchive table.
pageCount()
Returns the number of pages of a multipage document, or false for documents which aren't multipage do...
getGroup()
Return the FileStore storage group.
static newFromRow( $row)
Loads a file object from the filearchive table.
getTitle()
Return the associated title object.
Title null $title
string false $name
File name.
getWidth()
Return the width of the image.
int $deleted
Bitfield akin to rev_deleted.
getStorageKey()
Return the FileStore key (overriding base File class)
getDescription(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Return upload description.
getMimeType()
Returns the MIME type of the file.
getKey()
Return the FileStore key.
userCan( $field, Authority $performer)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
getName()
Return the file name.
int $bits
Size in bytes.
getMediaType()
Return the type of the media in the file.
getVisibility()
Returns the deletion bitfield.
getMetadata()
Get handler-specific metadata.
int $width
Width.
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
MediaWiki exception.
Base media handler class.
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
Represents a title within MediaWiki.
Definition Title.php:47
getDBkey()
Get the main part with underscores.
Definition Title.php:1056
This interface represents the authority associated the current execution context, such as a web reque...
Definition Authority.php:37
Interface for objects representing user identity.
const DB_REPLICA
Definition defines.php:25
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42