MediaWiki REL1_37
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 = false;
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 $this->description = MediaWikiServices::getInstance()->getCommentStore()
303 // Legacy because $row may have come from self::selectFields()
304 ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
305 $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
306 $this->timestamp = $row->fa_timestamp;
307 $this->deleted = $row->fa_deleted;
308 if ( isset( $row->fa_sha1 ) ) {
309 $this->sha1 = $row->fa_sha1;
310 } else {
311 // old row, populate from key
312 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
313 }
314 if ( !$this->title ) {
315 $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
316 }
317 }
318
324 public function getTitle() {
325 if ( !$this->title ) {
326 $this->load();
327 }
328 return $this->title;
329 }
330
336 public function getName() {
337 if ( $this->name === false ) {
338 $this->load();
339 }
340
341 return $this->name;
342 }
343
347 public function getID() {
348 $this->load();
349
350 return $this->id;
351 }
352
356 public function exists() {
357 $this->load();
358
359 return $this->exists;
360 }
361
366 public function getKey() {
367 $this->load();
368
369 return $this->key;
370 }
371
376 public function getStorageKey() {
377 return $this->getKey();
378 }
379
384 public function getGroup() {
385 return $this->group;
386 }
387
392 public function getWidth() {
393 $this->load();
394
395 return $this->width;
396 }
397
402 public function getHeight() {
403 $this->load();
404
405 return $this->height;
406 }
407
412 public function getMetadata() {
413 $this->load();
414
415 return $this->metadata;
416 }
417
422 public function getSize() {
423 $this->load();
424
425 return $this->size;
426 }
427
432 public function getBits() {
433 $this->load();
434
435 return $this->bits;
436 }
437
442 public function getMimeType() {
443 $this->load();
444
445 return $this->mime;
446 }
447
452 private function getHandler() {
453 if ( !isset( $this->handler ) ) {
454 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
455 }
456
457 return $this->handler;
458 }
459
466 public function pageCount() {
467 if ( !isset( $this->pageCount ) ) {
468 // @FIXME: callers expect File objects
469 // @phan-suppress-next-line PhanTypeMismatchArgument
470 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
471 // @phan-suppress-next-line PhanTypeMismatchArgument
472 $this->pageCount = $this->handler->pageCount( $this );
473 } else {
474 $this->pageCount = false;
475 }
476 }
477
478 return $this->pageCount;
479 }
480
486 public function getMediaType() {
487 $this->load();
488
489 return $this->media_type;
490 }
491
497 public function getTimestamp() {
498 $this->load();
499
500 return wfTimestamp( TS_MW, $this->timestamp );
501 }
502
509 public function getSha1() {
510 $this->load();
511
512 return $this->sha1;
513 }
514
527 public function getUser( $type = 'text' ) {
528 wfDeprecated( __METHOD__, '1.37' );
529 $this->load();
530
531 if ( $type === 'object' ) {
532 return $this->user ? User::newFromIdentity( $this->user ) : null;
533 } elseif ( $type === 'text' ) {
534 return $this->user ? $this->user->getName() : '';
535 } elseif ( $type === 'id' ) {
536 return $this->user ? $this->user->getId() : 0;
537 }
538
539 throw new MWException( "Unknown type '$type'." );
540 }
541
553 public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
554 $this->load();
555 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
556 return null;
557 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
558 return null;
559 } else {
560 return $this->user;
561 }
562 }
563
576 public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
577 $this->load();
578 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
579 return '';
580 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
581 return '';
582 } else {
583 return $this->description;
584 }
585 }
586
593 public function getRawUser() {
594 wfDeprecated( __METHOD__, '1.37' );
595 return $this->getUser( 'id' );
596 }
597
604 public function getRawUserText() {
605 wfDeprecated( __METHOD__, '1.37' );
606 return $this->getUser( 'text' );
607 }
608
615 public function getRawDescription() {
616 wfDeprecated( __METHOD__, '1.37' );
617 $this->load();
618 return $this->description;
619 }
620
625 public function getVisibility() {
626 $this->load();
627
628 return $this->deleted;
629 }
630
637 public function isDeleted( $field ) {
638 $this->load();
639
640 return ( $this->deleted & $field ) == $field;
641 }
642
650 public function userCan( $field, Authority $performer ) {
651 $this->load();
652 $title = $this->getTitle();
653
654 return RevisionRecord::userCanBitfield(
655 $this->deleted,
656 $field,
657 $performer,
658 $title ?: null
659 );
660 }
661}
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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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 $name
File name.
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.
getRawUserText()
Return the user name of the uploader.
string $mime
MIME type.
UserIdentity null $user
Uploader.
int $id
Filearchive row ID.
getRawUser()
Return the user ID of the uploader.
__construct( $title, $id=0, $key='', $sha1='')
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.
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.
getUser( $type='text')
Returns ID or name of user who uploaded 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,...
string $timestamp
Time of upload.
getName()
Return the file name.
int $bits
Size in bytes.
getMediaType()
Return the type of the media in the file.
getRawDescription()
Return upload description.
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:48
getDBkey()
Get the main part with underscores.
Definition Title.php:1069
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition User.php:713
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition User.php:684
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