MediaWiki REL1_35
ArchivedFile.php
Go to the documentation of this file.
1<?php
26
35 private $id;
36
38 private $name;
39
41 private $group;
42
44 private $key;
45
47 private $size;
48
50 private $bits;
51
53 private $width;
54
56 private $height;
57
59 private $metadata;
60
62 private $mime;
63
65 private $media_type;
66
68 private $description;
69
71 private $user;
72
74 private $timestamp;
75
77 private $dataLoaded;
78
80 private $deleted;
81
83 private $sha1;
84
88 private $pageCount;
89
92
94 protected $handler;
95
97 protected $title; # image title
98
100 protected $exists;
101
110 public function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
111 $this->id = -1;
112 $this->title = false;
113 $this->name = false;
114 $this->group = 'deleted'; // needed for direct use of constructor
115 $this->key = '';
116 $this->size = 0;
117 $this->bits = 0;
118 $this->width = 0;
119 $this->height = 0;
120 $this->metadata = '';
121 $this->mime = "unknown/unknown";
122 $this->media_type = '';
123 $this->description = '';
124 $this->user = null;
125 $this->timestamp = null;
126 $this->deleted = 0;
127 $this->dataLoaded = false;
128 $this->exists = false;
129 $this->sha1 = '';
130
131 if ( $title instanceof Title ) {
132 $this->title = File::normalizeTitle( $title, 'exception' );
133 $this->name = $title->getDBkey();
134 }
135
136 if ( $id ) {
137 $this->id = $id;
138 }
139
140 if ( $key ) {
141 $this->key = $key;
142 }
143
144 if ( $sha1 ) {
145 $this->sha1 = $sha1;
146 }
147
148 if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
149 throw new MWException( "No specifications provided to ArchivedFile constructor." );
150 }
151 }
152
159 public function load() {
160 if ( $this->dataLoaded ) {
161 return true;
162 }
163 $conds = [];
164
165 if ( $this->id > 0 ) {
166 $conds['fa_id'] = $this->id;
167 }
168 if ( $this->key ) {
169 $conds['fa_storage_group'] = $this->group;
170 $conds['fa_storage_key'] = $this->key;
171 }
172 if ( $this->title ) {
173 $conds['fa_name'] = $this->title->getDBkey();
174 }
175 if ( $this->sha1 ) {
176 $conds['fa_sha1'] = $this->sha1;
177 }
178
179 if ( $conds === [] ) {
180 throw new MWException( "No specific information for retrieving archived file" );
181 }
182
183 if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
184 $this->dataLoaded = true; // set it here, to have also true on miss
186 $fileQuery = self::getQueryInfo();
187 $row = $dbr->selectRow(
188 $fileQuery['tables'],
189 $fileQuery['fields'],
190 $conds,
191 __METHOD__,
192 [ 'ORDER BY' => 'fa_timestamp DESC' ],
193 $fileQuery['joins']
194 );
195 if ( !$row ) {
196 // this revision does not exist?
197 return null;
198 }
199
200 // initialize fields for filestore image object
201 $this->loadFromRow( $row );
202 } else {
203 throw new MWException( 'This title does not correspond to an image page.' );
204 }
205 $this->exists = true;
206
207 return true;
208 }
209
217 public static function newFromRow( $row ) {
218 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
219 $file->loadFromRow( $row );
220
221 return $file;
222 }
223
234 public static function getQueryInfo() {
235 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
236 $actorQuery = ActorMigration::newMigration()->getJoin( 'fa_user' );
237 return [
238 'tables' => [ 'filearchive' ] + $commentQuery['tables'] + $actorQuery['tables'],
239 'fields' => [
240 'fa_id',
241 'fa_name',
242 'fa_archive_name',
243 'fa_storage_key',
244 'fa_storage_group',
245 'fa_size',
246 'fa_bits',
247 'fa_width',
248 'fa_height',
249 'fa_metadata',
250 'fa_media_type',
251 'fa_major_mime',
252 'fa_minor_mime',
253 'fa_timestamp',
254 'fa_deleted',
255 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
256 'fa_sha1',
257 ] + $commentQuery['fields'] + $actorQuery['fields'],
258 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
259 ];
260 }
261
269 public function loadFromRow( $row ) {
270 $this->id = intval( $row->fa_id );
271 $this->name = $row->fa_name;
272 $this->archive_name = $row->fa_archive_name;
273 $this->group = $row->fa_storage_group;
274 $this->key = $row->fa_storage_key;
275 $this->size = $row->fa_size;
276 $this->bits = $row->fa_bits;
277 $this->width = $row->fa_width;
278 $this->height = $row->fa_height;
279 $this->metadata = $row->fa_metadata;
280 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
281 $this->media_type = $row->fa_media_type;
282 $this->description = MediaWikiServices::getInstance()->getCommentStore()
283 // Legacy because $row may have come from self::selectFields()
284 ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
285 $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
286 $this->timestamp = $row->fa_timestamp;
287 $this->deleted = $row->fa_deleted;
288 if ( isset( $row->fa_sha1 ) ) {
289 $this->sha1 = $row->fa_sha1;
290 } else {
291 // old row, populate from key
292 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
293 }
294 if ( !$this->title ) {
295 $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
296 }
297 }
298
304 public function getTitle() {
305 if ( !$this->title ) {
306 $this->load();
307 }
308 return $this->title;
309 }
310
316 public function getName() {
317 if ( $this->name === false ) {
318 $this->load();
319 }
320
321 return $this->name;
322 }
323
327 public function getID() {
328 $this->load();
329
330 return $this->id;
331 }
332
336 public function exists() {
337 $this->load();
338
339 return $this->exists;
340 }
341
346 public function getKey() {
347 $this->load();
348
349 return $this->key;
350 }
351
356 public function getStorageKey() {
357 return $this->getKey();
358 }
359
364 public function getGroup() {
365 return $this->group;
366 }
367
372 public function getWidth() {
373 $this->load();
374
375 return $this->width;
376 }
377
382 public function getHeight() {
383 $this->load();
384
385 return $this->height;
386 }
387
392 public function getMetadata() {
393 $this->load();
394
395 return $this->metadata;
396 }
397
402 public function getSize() {
403 $this->load();
404
405 return $this->size;
406 }
407
412 public function getBits() {
413 $this->load();
414
415 return $this->bits;
416 }
417
422 public function getMimeType() {
423 $this->load();
424
425 return $this->mime;
426 }
427
432 private function getHandler() {
433 if ( !isset( $this->handler ) ) {
434 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
435 }
436
437 return $this->handler;
438 }
439
446 public function pageCount() {
447 if ( !isset( $this->pageCount ) ) {
448 // @FIXME: callers expect File objects
449 // @phan-suppress-next-line PhanTypeMismatchArgument
450 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
451 // @phan-suppress-next-line PhanTypeMismatchArgument
452 $this->pageCount = $this->handler->pageCount( $this );
453 } else {
454 $this->pageCount = false;
455 }
456 }
457
458 return $this->pageCount;
459 }
460
466 public function getMediaType() {
467 $this->load();
468
469 return $this->media_type;
470 }
471
477 public function getTimestamp() {
478 $this->load();
479
480 return wfTimestamp( TS_MW, $this->timestamp );
481 }
482
489 public function getSha1() {
490 $this->load();
491
492 return $this->sha1;
493 }
494
506 public function getUser( $type = 'text' ) {
507 $this->load();
508
509 if ( $type === 'object' ) {
510 return $this->user;
511 } elseif ( $type === 'text' ) {
512 return $this->user ? $this->user->getName() : '';
513 } elseif ( $type === 'id' ) {
514 return $this->user ? $this->user->getId() : 0;
515 }
516
517 throw new MWException( "Unknown type '$type'." );
518 }
519
525 public function getDescription() {
526 $this->load();
527 if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
528 return 0;
529 } else {
530 return $this->description;
531 }
532 }
533
539 public function getRawUser() {
540 return $this->getUser( 'id' );
541 }
542
548 public function getRawUserText() {
549 return $this->getUser( 'text' );
550 }
551
557 public function getRawDescription() {
558 $this->load();
559
560 return $this->description;
561 }
562
567 public function getVisibility() {
568 $this->load();
569
570 return $this->deleted;
571 }
572
579 public function isDeleted( $field ) {
580 $this->load();
581
582 return ( $this->deleted & $field ) == $field;
583 }
584
592 public function userCan( $field, User $user = null ) {
593 $this->load();
594 $title = $this->getTitle();
595
596 if ( !$user ) {
597 wfDeprecated( __METHOD__ . ' without passing a $user parameter', '1.35' );
598 global $wgUser;
599 $user = $wgUser;
600 }
601
602 return RevisionRecord::userCanBitfield(
603 $this->deleted,
604 $field,
605 $user,
606 $title ?: null
607 );
608 }
609}
getUser()
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 $function is deprecated.
Class representing a row of the 'filearchive' table.
getTimestamp()
Return upload timestamp.
User null $user
Uploader.
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
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this FileStore image file,...
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.
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.
getDescription()
Return upload description.
int $id
Filearchive row ID.
getRawUser()
Return the user ID of the uploader.
__construct( $title, $id=0, $key='', $sha1='')
Stable to call.
load()
Loads a file object from the filearchive table Stable to override.
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 Stable to override.
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)
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.
string $metadata
Metadata string.
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:42
getDBkey()
Get the main part with underscores.
Definition Title.php:1032
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2150
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition User.php:616
const NS_FILE
Definition Defines.php:76
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