MediaWiki REL1_39
ArchivedFile.php
Go to the documentation of this file.
1<?php
27
35
36 // Audience options for ::getDescription() and ::getUploader()
37 public const FOR_PUBLIC = 1;
38 public const FOR_THIS_USER = 2;
39 public const RAW = 3;
40
42 private const MDS_EMPTY = 'empty';
43
45 private const MDS_LEGACY = 'legacy';
46
48 private const MDS_PHP = 'php';
49
51 private const MDS_JSON = 'json';
52
54 private $id;
55
57 private $name;
58
60 private $group;
61
63 private $key;
64
66 private $size;
67
69 private $bits;
70
72 private $width;
73
75 private $height;
76
78 protected $metadataArray = [];
79
81 protected $extraDataLoaded = false;
82
90
92 protected $metadataBlobs = [];
93
101
103 private $mime;
104
106 private $media_type;
107
109 private $description;
110
112 private $user;
113
115 private $timestamp;
116
118 private $dataLoaded;
119
121 private $deleted;
122
124 private $sha1;
125
129 private $pageCount;
130
132 private $archive_name;
133
135 protected $handler;
136
138 protected $title; # image title
139
141 protected $exists;
142
144 private $repo;
145
147 private $metadataStorageHelper;
148
157 public function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
158 $this->id = -1;
159 $this->title = null;
160 $this->name = false;
161 $this->group = 'deleted'; // needed for direct use of constructor
162 $this->key = '';
163 $this->size = 0;
164 $this->bits = 0;
165 $this->width = 0;
166 $this->height = 0;
167 $this->mime = "unknown/unknown";
168 $this->media_type = '';
169 $this->description = '';
170 $this->user = null;
171 $this->timestamp = null;
172 $this->deleted = 0;
173 $this->dataLoaded = false;
174 $this->exists = false;
175 $this->sha1 = '';
176
177 if ( $title instanceof Title ) {
178 $this->title = File::normalizeTitle( $title, 'exception' );
179 $this->name = $title->getDBkey();
180 }
181
182 if ( $id ) {
183 $this->id = $id;
184 }
185
186 if ( $key ) {
187 $this->key = $key;
188 }
189
190 if ( $sha1 ) {
191 $this->sha1 = $sha1;
192 }
193
194 if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
195 throw new MWException( "No specifications provided to ArchivedFile constructor." );
196 }
197
198 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
199 $this->metadataStorageHelper = new MetadataStorageHelper( $this->repo );
200 }
201
208 public function load() {
209 if ( $this->dataLoaded ) {
210 return true;
211 }
212 $conds = [];
213
214 if ( $this->id > 0 ) {
215 $conds['fa_id'] = $this->id;
216 }
217 if ( $this->key ) {
218 $conds['fa_storage_group'] = $this->group;
219 $conds['fa_storage_key'] = $this->key;
220 }
221 if ( $this->title ) {
222 $conds['fa_name'] = $this->title->getDBkey();
223 }
224 if ( $this->sha1 ) {
225 $conds['fa_sha1'] = $this->sha1;
226 }
227
228 if ( $conds === [] ) {
229 throw new MWException( "No specific information for retrieving archived file" );
230 }
231
232 if ( !$this->title || $this->title->getNamespace() === NS_FILE ) {
233 $this->dataLoaded = true; // set it here, to have also true on miss
235 $fileQuery = self::getQueryInfo();
236 $row = $dbr->selectRow(
237 $fileQuery['tables'],
238 $fileQuery['fields'],
239 $conds,
240 __METHOD__,
241 [ 'ORDER BY' => 'fa_timestamp DESC' ],
242 $fileQuery['joins']
243 );
244 if ( !$row ) {
245 // this revision does not exist?
246 return null;
247 }
248
249 // initialize fields for filestore image object
250 $this->loadFromRow( $row );
251 } else {
252 throw new MWException( 'This title does not correspond to an image page.' );
253 }
254 $this->exists = true;
255
256 return true;
257 }
258
266 public static function newFromRow( $row ) {
267 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
268 $file->loadFromRow( $row );
269
270 return $file;
271 }
272
289 public static function getQueryInfo() {
290 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
291 return [
292 'tables' => [
293 'filearchive',
294 'filearchive_actor' => 'actor'
295 ] + $commentQuery['tables'],
296 'fields' => [
297 'fa_id',
298 'fa_name',
299 'fa_archive_name',
300 'fa_storage_key',
301 'fa_storage_group',
302 'fa_size',
303 'fa_bits',
304 'fa_width',
305 'fa_height',
306 'fa_metadata',
307 'fa_media_type',
308 'fa_major_mime',
309 'fa_minor_mime',
310 'fa_timestamp',
311 'fa_deleted',
312 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
313 'fa_sha1',
314 'fa_actor',
315 'fa_user' => 'filearchive_actor.actor_user',
316 'fa_user_text' => 'filearchive_actor.actor_name'
317 ] + $commentQuery['fields'],
318 'joins' => [
319 'filearchive_actor' => [ 'JOIN', 'actor_id=fa_actor' ]
320 ] + $commentQuery['joins'],
321 ];
322 }
323
331 public function loadFromRow( $row ) {
332 $this->id = intval( $row->fa_id );
333 $this->name = $row->fa_name;
334 $this->archive_name = $row->fa_archive_name;
335 $this->group = $row->fa_storage_group;
336 $this->key = $row->fa_storage_key;
337 $this->size = $row->fa_size;
338 $this->bits = $row->fa_bits;
339 $this->width = $row->fa_width;
340 $this->height = $row->fa_height;
342 $this->repo->getReplicaDB(), $row->fa_metadata );
343 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
344 $this->media_type = $row->fa_media_type;
345 $services = MediaWikiServices::getInstance();
346 $this->description = $services->getCommentStore()
347 // Legacy because $row may have come from self::selectFields()
348 ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
349 $this->user = $services->getUserFactory()
350 ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
351 $this->timestamp = $row->fa_timestamp;
352 $this->deleted = $row->fa_deleted;
353 if ( isset( $row->fa_sha1 ) ) {
354 $this->sha1 = $row->fa_sha1;
355 } else {
356 // old row, populate from key
357 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
358 }
359 if ( !$this->title ) {
360 $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
361 }
362 }
363
369 public function getTitle() {
370 if ( !$this->title ) {
371 $this->load();
372 }
373 return $this->title;
374 }
375
381 public function getName() {
382 if ( $this->name === false ) {
383 $this->load();
384 }
385
386 return $this->name;
387 }
388
392 public function getID() {
393 $this->load();
394
395 return $this->id;
396 }
397
401 public function exists() {
402 $this->load();
403
404 return $this->exists;
405 }
406
411 public function getKey() {
412 $this->load();
413
414 return $this->key;
415 }
416
421 public function getStorageKey() {
422 return $this->getKey();
423 }
424
429 public function getGroup() {
430 return $this->group;
431 }
432
437 public function getWidth() {
438 $this->load();
439
440 return $this->width;
441 }
442
447 public function getHeight() {
448 $this->load();
449
450 return $this->height;
451 }
452
459 public function getMetadata() {
460 $data = $this->getMetadataArray();
461 if ( !$data ) {
462 return '';
463 } elseif ( array_keys( $data ) === [ '_error' ] ) {
464 // Legacy error encoding
465 return $data['_error'];
466 } else {
467 return serialize( $this->getMetadataArray() );
468 }
469 }
470
477 public function getMetadataArray(): array {
478 $this->load();
479 if ( $this->unloadedMetadataBlobs ) {
480 return $this->getMetadataItems(
481 array_unique( array_merge(
482 array_keys( $this->metadataArray ),
483 array_keys( $this->unloadedMetadataBlobs )
484 ) )
485 );
486 }
488 }
489
490 public function getMetadataItems( array $itemNames ): array {
491 $this->load();
492 $result = [];
493 $addresses = [];
494 foreach ( $itemNames as $itemName ) {
495 if ( array_key_exists( $itemName, $this->metadataArray ) ) {
496 $result[$itemName] = $this->metadataArray[$itemName];
497 } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
498 $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
499 }
500 }
501
502 if ( $addresses ) {
503 $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
504 foreach ( $addresses as $itemName => $address ) {
505 unset( $this->unloadedMetadataBlobs[$itemName] );
506 $value = $resultFromBlob[$itemName] ?? null;
507 if ( $value !== null ) {
508 $result[$itemName] = $value;
509 $this->metadataArray[$itemName] = $value;
510 }
511 }
512 }
513 return $result;
514 }
515
527 public function getMetadataForDb( IDatabase $db ) {
528 $this->load();
529 if ( !$this->metadataArray && !$this->metadataBlobs ) {
530 $s = '';
531 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
532 $s = $this->getJsonMetadata();
533 } else {
534 $s = serialize( $this->getMetadataArray() );
535 }
536 if ( !is_string( $s ) ) {
537 throw new MWException( 'Could not serialize image metadata value for DB' );
538 }
539 return $db->encodeBlob( $s );
540 }
541
548 private function getJsonMetadata() {
549 // Directly store data that is not already in BlobStore
550 $envelope = [
551 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
552 ];
553
554 // Also store the blob addresses
555 if ( $this->metadataBlobs ) {
556 $envelope['blobs'] = $this->metadataBlobs;
557 }
558
559 list( $s, $blobAddresses ) = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
560
561 // Repeated calls to this function should not keep inserting more blobs
562 $this->metadataBlobs += $blobAddresses;
563
564 return $s;
565 }
566
575 protected function loadMetadataFromDbFieldValue( IDatabase $db, $metadataBlob ) {
576 $this->loadMetadataFromString( $db->decodeBlob( $metadataBlob ) );
577 }
578
586 protected function loadMetadataFromString( $metadataString ) {
587 $this->extraDataLoaded = true;
588 $this->metadataArray = [];
589 $this->metadataBlobs = [];
590 $this->unloadedMetadataBlobs = [];
591 $metadataString = (string)$metadataString;
592 if ( $metadataString === '' ) {
593 $this->metadataSerializationFormat = self::MDS_EMPTY;
594 return;
595 }
596 if ( $metadataString[0] === '{' ) {
597 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
598 if ( !$envelope ) {
599 // Legacy error encoding
600 $this->metadataArray = [ '_error' => $metadataString ];
601 $this->metadataSerializationFormat = self::MDS_LEGACY;
602 } else {
603 $this->metadataSerializationFormat = self::MDS_JSON;
604 if ( isset( $envelope['data'] ) ) {
605 $this->metadataArray = $envelope['data'];
606 }
607 if ( isset( $envelope['blobs'] ) ) {
608 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope['blobs'];
609 }
610 }
611 } else {
612 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
613 $data = @unserialize( $metadataString );
614 if ( !is_array( $data ) ) {
615 // Legacy error encoding
616 $data = [ '_error' => $metadataString ];
617 $this->metadataSerializationFormat = self::MDS_LEGACY;
618 } else {
619 $this->metadataSerializationFormat = self::MDS_PHP;
620 }
621 $this->metadataArray = $data;
622 }
623 }
624
629 public function getSize() {
630 $this->load();
631
632 return $this->size;
633 }
634
639 public function getBits() {
640 $this->load();
641
642 return $this->bits;
643 }
644
649 public function getMimeType() {
650 $this->load();
651
652 return $this->mime;
653 }
654
659 private function getHandler() {
660 if ( !isset( $this->handler ) ) {
661 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
662 }
663
664 return $this->handler;
665 }
666
673 public function pageCount() {
674 if ( !isset( $this->pageCount ) ) {
675 // @FIXME: callers expect File objects
676 // @phan-suppress-next-line PhanTypeMismatchArgument
677 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
678 // @phan-suppress-next-line PhanTypeMismatchArgument
679 $this->pageCount = $this->handler->pageCount( $this );
680 } else {
681 $this->pageCount = false;
682 }
683 }
684
685 return $this->pageCount;
686 }
687
693 public function getMediaType() {
694 $this->load();
695
696 return $this->media_type;
697 }
698
704 public function getTimestamp() {
705 $this->load();
706
707 return wfTimestamp( TS_MW, $this->timestamp );
708 }
709
716 public function getSha1() {
717 $this->load();
718
719 return $this->sha1;
720 }
721
733 public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
734 $this->load();
735 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
736 return null;
737 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
738 return null;
739 } else {
740 return $this->user;
741 }
742 }
743
756 public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
757 $this->load();
758 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
759 return '';
760 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
761 return '';
762 } else {
763 return $this->description;
764 }
765 }
766
771 public function getVisibility() {
772 $this->load();
773
774 return $this->deleted;
775 }
776
783 public function isDeleted( $field ) {
784 $this->load();
785
786 return ( $this->deleted & $field ) == $field;
787 }
788
796 public function userCan( $field, Authority $performer ) {
797 $this->load();
798 $title = $this->getTitle();
799
800 return RevisionRecord::userCanBitfield(
801 $this->deleted,
802 $field,
803 $performer,
804 $title ?: null
805 );
806 }
807}
serialize()
unserialize( $serialized)
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.
Deleted file in the 'filearchive' table.
getTimestamp()
Return upload timestamp.
getSize()
Return the size of the image file, in bytes.
MediaHandler $handler
isDeleted( $field)
for file or revision rows
getHeight()
Return the height of the image.
string[] $metadataBlobs
Map of metadata item name to blob address.
getBits()
Return the bits of the image file, in bytes.
array $metadataArray
Unserialized metadata.
const FOR_THIS_USER
getMetadataForDb(IDatabase $db)
Serialize the metadata array for insertion into img_metadata, oi_metadata or fa_metadata.
string[] $unloadedMetadataBlobs
Map of metadata item name to blob address for items that exist but have not yet been loaded into $thi...
getSha1()
Get the SHA-1 base 36 hash of the 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.
loadMetadataFromDbFieldValue(IDatabase $db, $metadataBlob)
Unserialize a metadata blob which came from the database and store it in $this.
string null $metadataSerializationFormat
One of the MDS_* constants, giving the format of the metadata as stored in the DB,...
getMetadataItems(array $itemNames)
__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.
Title null $title
getWidth()
Return the width of the image.
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,...
getMetadataArray()
Get unserialized handler-specific metadata.
loadMetadataFromString( $metadataString)
Unserialize a metadata string which came from some non-DB source, or is the return value of IDatabase...
getName()
Return the file name.
getMediaType()
Return the type of the media in the file.
getVisibility()
Returns the deletion bitfield.
getMetadata()
Get handler-specific metadata as a serialized string.
bool $extraDataLoaded
Whether or not lazy-loaded data has been loaded from the database.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:39
MediaWiki exception.
Base media handler class.
static getHandler( $type)
Get a MediaHandler for a given MIME type from the instance cache.
Service locator for MediaWiki core services.
Page revision base class.
Helper for storage of metadata.
Represents a title within MediaWiki.
Definition Title.php:49
getDBkey()
Get the main part with underscores.
Definition Title.php:1057
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.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
decodeBlob( $b)
Some DBMSs return a special placeholder object representing blob fields in result objects.
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don't allow simple quoted strin...
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
const DB_REPLICA
Definition defines.php:26
$mime
Definition router.php:60
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42