MediaWiki 1.41.2
ArchivedFile.php
Go to the documentation of this file.
1<?php
31
39
40 // Audience options for ::getDescription() and ::getUploader()
41 public const FOR_PUBLIC = 1;
42 public const FOR_THIS_USER = 2;
43 public const RAW = 3;
44
46 private const MDS_EMPTY = 'empty';
47
49 private const MDS_LEGACY = 'legacy';
50
52 private const MDS_PHP = 'php';
53
55 private const MDS_JSON = 'json';
56
58 private $id;
59
61 private $name;
62
64 private $group;
65
67 private $key;
68
70 private $size;
71
73 private $bits;
74
76 private $width;
77
79 private $height;
80
82 protected $metadataArray = [];
83
85 protected $extraDataLoaded = false;
86
94
96 protected $metadataBlobs = [];
97
104 protected $unloadedMetadataBlobs = [];
105
107 private $mime;
108
110 private $media_type;
111
113 private $description;
114
116 private $user;
117
119 private $timestamp;
120
122 private $dataLoaded;
123
125 private $deleted;
126
128 private $sha1;
129
133 private $pageCount;
134
136 private $archive_name;
137
139 protected $handler;
140
142 protected $title; # image title
143
145 protected $exists;
146
148 private $repo;
149
151 private $metadataStorageHelper;
152
161 public function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
162 $this->id = -1;
163 $this->title = null;
164 $this->name = false;
165 $this->group = 'deleted'; // needed for direct use of constructor
166 $this->key = '';
167 $this->size = 0;
168 $this->bits = 0;
169 $this->width = 0;
170 $this->height = 0;
171 $this->mime = "unknown/unknown";
172 $this->media_type = '';
173 $this->description = '';
174 $this->user = null;
175 $this->timestamp = null;
176 $this->deleted = 0;
177 $this->dataLoaded = false;
178 $this->exists = false;
179 $this->sha1 = '';
180
181 if ( $title instanceof Title ) {
182 $this->title = File::normalizeTitle( $title, 'exception' );
183 $this->name = $title->getDBkey();
184 }
185
186 if ( $id ) {
187 $this->id = $id;
188 }
189
190 if ( $key ) {
191 $this->key = $key;
192 }
193
194 if ( $sha1 ) {
195 $this->sha1 = $sha1;
196 }
197
198 if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
199 throw new MWException( "No specifications provided to ArchivedFile constructor." );
200 }
201
202 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
203 $this->metadataStorageHelper = new MetadataStorageHelper( $this->repo );
204 }
205
212 public function load() {
213 if ( $this->dataLoaded ) {
214 return true;
215 }
216 $conds = [];
217
218 if ( $this->id > 0 ) {
219 $conds['fa_id'] = $this->id;
220 }
221 if ( $this->key ) {
222 $conds['fa_storage_group'] = $this->group;
223 $conds['fa_storage_key'] = $this->key;
224 }
225 if ( $this->title ) {
226 $conds['fa_name'] = $this->title->getDBkey();
227 }
228 if ( $this->sha1 ) {
229 $conds['fa_sha1'] = $this->sha1;
230 }
231
232 if ( $conds === [] ) {
233 throw new MWException( "No specific information for retrieving archived file" );
234 }
235
236 if ( !$this->title || $this->title->getNamespace() === NS_FILE ) {
237 $this->dataLoaded = true; // set it here, to have also true on miss
238 $dbr = $this->repo->getReplicaDB();
239 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
240 $row = $queryBuilder->where( $conds )
241 ->orderBy( 'fa_timestamp', SelectQueryBuilder::SORT_DESC )
242 ->caller( __METHOD__ )->fetchRow();
243 if ( !$row ) {
244 // this revision does not exist?
245 return null;
246 }
247
248 // initialize fields for filestore image object
249 $this->loadFromRow( $row );
250 } else {
251 throw new MWException( 'This title does not correspond to an image page.' );
252 }
253
254 return true;
255 }
256
264 public static function newFromRow( $row ) {
265 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
266 $file->loadFromRow( $row );
267
268 return $file;
269 }
270
288 public static function getQueryInfo() {
289 $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
290 $queryInfo = ( FileSelectQueryBuilder::newForArchivedFile( $dbr ) )->getQueryInfo();
291 return [
292 'tables' => $queryInfo['tables'],
293 'fields' => $queryInfo['fields'],
294 'joins' => $queryInfo['join_conds'],
295 ];
296 }
297
305 public function loadFromRow( $row ) {
306 $this->id = intval( $row->fa_id );
307 $this->name = $row->fa_name;
308 $this->archive_name = $row->fa_archive_name;
309 $this->group = $row->fa_storage_group;
310 $this->key = $row->fa_storage_key;
311 $this->size = $row->fa_size;
312 $this->bits = $row->fa_bits;
313 $this->width = $row->fa_width;
314 $this->height = $row->fa_height;
316 $this->repo->getReplicaDB(), $row->fa_metadata );
317 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
318 $this->media_type = $row->fa_media_type;
319 $services = MediaWikiServices::getInstance();
320 $this->description = $services->getCommentStore()
321 // Legacy because $row may have come from self::selectFields()
322 ->getCommentLegacy( $this->repo->getReplicaDB(), 'fa_description', $row )->text;
323 $this->user = $services->getUserFactory()
324 ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
325 $this->timestamp = $row->fa_timestamp;
326 $this->deleted = $row->fa_deleted;
327 if ( isset( $row->fa_sha1 ) ) {
328 $this->sha1 = $row->fa_sha1;
329 } else {
330 // old row, populate from key
331 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
332 }
333 if ( !$this->title ) {
334 $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
335 }
336 $this->exists = $row->fa_archive_name !== '';
337 }
338
344 public function getTitle() {
345 if ( !$this->title ) {
346 $this->load();
347 }
348 return $this->title;
349 }
350
356 public function getName() {
357 if ( $this->name === false ) {
358 $this->load();
359 }
360
361 return $this->name;
362 }
363
367 public function getID() {
368 $this->load();
369
370 return $this->id;
371 }
372
376 public function exists() {
377 $this->load();
378
379 return $this->exists;
380 }
381
386 public function getKey() {
387 $this->load();
388
389 return $this->key;
390 }
391
396 public function getStorageKey() {
397 return $this->getKey();
398 }
399
404 public function getGroup() {
405 return $this->group;
406 }
407
412 public function getWidth() {
413 $this->load();
414
415 return $this->width;
416 }
417
422 public function getHeight() {
423 $this->load();
424
425 return $this->height;
426 }
427
434 public function getMetadata() {
435 $data = $this->getMetadataArray();
436 if ( !$data ) {
437 return '';
438 } elseif ( array_keys( $data ) === [ '_error' ] ) {
439 // Legacy error encoding
440 return $data['_error'];
441 } else {
442 return serialize( $this->getMetadataArray() );
443 }
444 }
445
452 public function getMetadataArray(): array {
453 $this->load();
454 if ( $this->unloadedMetadataBlobs ) {
455 return $this->getMetadataItems(
456 array_unique( array_merge(
457 array_keys( $this->metadataArray ),
458 array_keys( $this->unloadedMetadataBlobs )
459 ) )
460 );
461 }
462 return $this->metadataArray;
463 }
464
465 public function getMetadataItems( array $itemNames ): array {
466 $this->load();
467 $result = [];
468 $addresses = [];
469 foreach ( $itemNames as $itemName ) {
470 if ( array_key_exists( $itemName, $this->metadataArray ) ) {
471 $result[$itemName] = $this->metadataArray[$itemName];
472 } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
473 $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
474 }
475 }
476
477 if ( $addresses ) {
478 $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
479 foreach ( $addresses as $itemName => $address ) {
480 unset( $this->unloadedMetadataBlobs[$itemName] );
481 $value = $resultFromBlob[$itemName] ?? null;
482 if ( $value !== null ) {
483 $result[$itemName] = $value;
484 $this->metadataArray[$itemName] = $value;
485 }
486 }
487 }
488 return $result;
489 }
490
502 public function getMetadataForDb( IDatabase $db ) {
503 $this->load();
504 if ( !$this->metadataArray && !$this->metadataBlobs ) {
505 $s = '';
506 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
507 $s = $this->getJsonMetadata();
508 } else {
509 $s = serialize( $this->getMetadataArray() );
510 }
511 if ( !is_string( $s ) ) {
512 throw new MWException( 'Could not serialize image metadata value for DB' );
513 }
514 return $db->encodeBlob( $s );
515 }
516
523 private function getJsonMetadata() {
524 // Directly store data that is not already in BlobStore
525 $envelope = [
526 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
527 ];
528
529 // Also store the blob addresses
530 if ( $this->metadataBlobs ) {
531 $envelope['blobs'] = $this->metadataBlobs;
532 }
533
534 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
535
536 // Repeated calls to this function should not keep inserting more blobs
537 $this->metadataBlobs += $blobAddresses;
538
539 return $s;
540 }
541
550 protected function loadMetadataFromDbFieldValue( IReadableDatabase $db, $metadataBlob ) {
551 $this->loadMetadataFromString( $db->decodeBlob( $metadataBlob ) );
552 }
553
561 protected function loadMetadataFromString( $metadataString ) {
562 $this->extraDataLoaded = true;
563 $this->metadataArray = [];
564 $this->metadataBlobs = [];
565 $this->unloadedMetadataBlobs = [];
566 $metadataString = (string)$metadataString;
567 if ( $metadataString === '' ) {
568 $this->metadataSerializationFormat = self::MDS_EMPTY;
569 return;
570 }
571 if ( $metadataString[0] === '{' ) {
572 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
573 if ( !$envelope ) {
574 // Legacy error encoding
575 $this->metadataArray = [ '_error' => $metadataString ];
576 $this->metadataSerializationFormat = self::MDS_LEGACY;
577 } else {
578 $this->metadataSerializationFormat = self::MDS_JSON;
579 if ( isset( $envelope['data'] ) ) {
580 $this->metadataArray = $envelope['data'];
581 }
582 if ( isset( $envelope['blobs'] ) ) {
583 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope['blobs'];
584 }
585 }
586 } else {
587 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
588 $data = @unserialize( $metadataString );
589 if ( !is_array( $data ) ) {
590 // Legacy error encoding
591 $data = [ '_error' => $metadataString ];
592 $this->metadataSerializationFormat = self::MDS_LEGACY;
593 } else {
594 $this->metadataSerializationFormat = self::MDS_PHP;
595 }
596 $this->metadataArray = $data;
597 }
598 }
599
604 public function getSize() {
605 $this->load();
606
607 return $this->size;
608 }
609
614 public function getBits() {
615 $this->load();
616
617 return $this->bits;
618 }
619
624 public function getMimeType() {
625 $this->load();
626
627 return $this->mime;
628 }
629
634 private function getHandler() {
635 if ( !isset( $this->handler ) ) {
636 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
637 }
638
639 return $this->handler;
640 }
641
648 public function pageCount() {
649 if ( !isset( $this->pageCount ) ) {
650 // @FIXME: callers expect File objects
651 // @phan-suppress-next-line PhanTypeMismatchArgument
652 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
653 // @phan-suppress-next-line PhanTypeMismatchArgument
654 $this->pageCount = $this->handler->pageCount( $this );
655 } else {
656 $this->pageCount = false;
657 }
658 }
659
660 return $this->pageCount;
661 }
662
668 public function getMediaType() {
669 $this->load();
670
671 return $this->media_type;
672 }
673
679 public function getTimestamp() {
680 $this->load();
681
682 return wfTimestamp( TS_MW, $this->timestamp );
683 }
684
691 public function getSha1() {
692 $this->load();
693
694 return $this->sha1;
695 }
696
708 public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
709 $this->load();
710 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
711 return null;
712 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
713 return null;
714 } else {
715 return $this->user;
716 }
717 }
718
731 public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
732 $this->load();
733 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
734 return '';
735 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
736 return '';
737 } else {
738 return $this->description;
739 }
740 }
741
746 public function getVisibility() {
747 $this->load();
748
749 return $this->deleted;
750 }
751
758 public function isDeleted( $field ) {
759 $this->load();
760
761 return ( $this->deleted & $field ) == $field;
762 }
763
771 public function userCan( $field, Authority $performer ) {
772 $this->load();
773 $title = $this->getTitle();
774
775 return RevisionRecord::userCanBitfield(
776 $this->deleted,
777 $field,
778 $performer,
779 $title ?: null
780 );
781 }
782}
const NS_FILE
Definition Defines.php:70
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.
loadMetadataFromDbFieldValue(IReadableDatabase $db, $metadataBlob)
Unserialize a metadata blob which came from the database and store it in $this.
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.
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:45
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.
Represents a title within MediaWiki.
Definition Title.php:76
getDBkey()
Get the main part with underscores.
Definition Title.php:1049
Helper for storage of metadata.
Build SELECT queries with a fluent interface.
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:36
A database connection without write operations.
encodeBlob( $b)
Some DBMSs have a special format for inserting into blob fields, they don't allow simple quoted strin...
decodeBlob( $b)
Some DBMSs return a special placeholder object representing blob fields in result objects.
$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