MediaWiki  master
ArchivedFile.php
Go to the documentation of this file.
1 <?php
28 
35 class ArchivedFile {
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 const MDS_EMPTY = 'empty';
44 
46  private const MDS_LEGACY = 'legacy';
47 
49  private const MDS_PHP = 'php';
50 
52  private const MDS_JSON = 'json';
53 
55  private $id;
56 
58  private $name;
59 
61  private $group;
62 
64  private $key;
65 
67  private $size;
68 
70  private $bits;
71 
73  private $width;
74 
76  private $height;
77 
79  protected $metadataArray = [];
80 
82  protected $extraDataLoaded = false;
83 
91 
93  protected $metadataBlobs = [];
94 
101  protected $unloadedMetadataBlobs = [];
102 
104  private $mime;
105 
107  private $media_type;
108 
110  private $description;
111 
113  private $user;
114 
116  private $timestamp;
117 
119  private $dataLoaded;
120 
122  private $deleted;
123 
125  private $sha1;
126 
130  private $pageCount;
131 
133  private $archive_name;
134 
136  protected $handler;
137 
139  protected $title; # image title
140 
142  protected $exists;
143 
145  private $repo;
146 
148  private $metadataStorageHelper;
149 
158  public function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
159  $this->id = -1;
160  $this->title = null;
161  $this->name = false;
162  $this->group = 'deleted'; // needed for direct use of constructor
163  $this->key = '';
164  $this->size = 0;
165  $this->bits = 0;
166  $this->width = 0;
167  $this->height = 0;
168  $this->mime = "unknown/unknown";
169  $this->media_type = '';
170  $this->description = '';
171  $this->user = null;
172  $this->timestamp = null;
173  $this->deleted = 0;
174  $this->dataLoaded = false;
175  $this->exists = false;
176  $this->sha1 = '';
177 
178  if ( $title instanceof Title ) {
179  $this->title = File::normalizeTitle( $title, 'exception' );
180  $this->name = $title->getDBkey();
181  }
182 
183  if ( $id ) {
184  $this->id = $id;
185  }
186 
187  if ( $key ) {
188  $this->key = $key;
189  }
190 
191  if ( $sha1 ) {
192  $this->sha1 = $sha1;
193  }
194 
195  if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
196  throw new MWException( "No specifications provided to ArchivedFile constructor." );
197  }
198 
199  $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
200  $this->metadataStorageHelper = new MetadataStorageHelper( $this->repo );
201  }
202 
209  public function load() {
210  if ( $this->dataLoaded ) {
211  return true;
212  }
213  $conds = [];
214 
215  if ( $this->id > 0 ) {
216  $conds['fa_id'] = $this->id;
217  }
218  if ( $this->key ) {
219  $conds['fa_storage_group'] = $this->group;
220  $conds['fa_storage_key'] = $this->key;
221  }
222  if ( $this->title ) {
223  $conds['fa_name'] = $this->title->getDBkey();
224  }
225  if ( $this->sha1 ) {
226  $conds['fa_sha1'] = $this->sha1;
227  }
228 
229  if ( $conds === [] ) {
230  throw new MWException( "No specific information for retrieving archived file" );
231  }
232 
233  if ( !$this->title || $this->title->getNamespace() === NS_FILE ) {
234  $this->dataLoaded = true; // set it here, to have also true on miss
235  $dbr = wfGetDB( DB_REPLICA );
236  $fileQuery = self::getQueryInfo();
237  $row = $dbr->selectRow(
238  $fileQuery['tables'],
239  $fileQuery['fields'],
240  $conds,
241  __METHOD__,
242  [ 'ORDER BY' => 'fa_timestamp DESC' ],
243  $fileQuery['joins']
244  );
245  if ( !$row ) {
246  // this revision does not exist?
247  return null;
248  }
249 
250  // initialize fields for filestore image object
251  $this->loadFromRow( $row );
252  } else {
253  throw new MWException( 'This title does not correspond to an image page.' );
254  }
255  $this->exists = true;
256 
257  return true;
258  }
259 
267  public static function newFromRow( $row ) {
268  $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
269  $file->loadFromRow( $row );
270 
271  return $file;
272  }
273 
290  public static function getQueryInfo() {
291  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
292  return [
293  'tables' => [
294  'filearchive',
295  'filearchive_actor' => 'actor'
296  ] + $commentQuery['tables'],
297  'fields' => [
298  'fa_id',
299  'fa_name',
300  'fa_archive_name',
301  'fa_storage_key',
302  'fa_storage_group',
303  'fa_size',
304  'fa_bits',
305  'fa_width',
306  'fa_height',
307  'fa_metadata',
308  'fa_media_type',
309  'fa_major_mime',
310  'fa_minor_mime',
311  'fa_timestamp',
312  'fa_deleted',
313  'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
314  'fa_sha1',
315  'fa_actor',
316  'fa_user' => 'filearchive_actor.actor_user',
317  'fa_user_text' => 'filearchive_actor.actor_name'
318  ] + $commentQuery['fields'],
319  'joins' => [
320  'filearchive_actor' => [ 'JOIN', 'actor_id=fa_actor' ]
321  ] + $commentQuery['joins'],
322  ];
323  }
324 
332  public function loadFromRow( $row ) {
333  $this->id = intval( $row->fa_id );
334  $this->name = $row->fa_name;
335  $this->archive_name = $row->fa_archive_name;
336  $this->group = $row->fa_storage_group;
337  $this->key = $row->fa_storage_key;
338  $this->size = $row->fa_size;
339  $this->bits = $row->fa_bits;
340  $this->width = $row->fa_width;
341  $this->height = $row->fa_height;
343  $this->repo->getReplicaDB(), $row->fa_metadata );
344  $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
345  $this->media_type = $row->fa_media_type;
346  $services = MediaWikiServices::getInstance();
347  $this->description = $services->getCommentStore()
348  // Legacy because $row may have come from self::selectFields()
349  ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
350  $this->user = $services->getUserFactory()
351  ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
352  $this->timestamp = $row->fa_timestamp;
353  $this->deleted = $row->fa_deleted;
354  if ( isset( $row->fa_sha1 ) ) {
355  $this->sha1 = $row->fa_sha1;
356  } else {
357  // old row, populate from key
358  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
359  }
360  if ( !$this->title ) {
361  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
362  }
363  }
364 
370  public function getTitle() {
371  if ( !$this->title ) {
372  $this->load();
373  }
374  return $this->title;
375  }
376 
382  public function getName() {
383  if ( $this->name === false ) {
384  $this->load();
385  }
386 
387  return $this->name;
388  }
389 
393  public function getID() {
394  $this->load();
395 
396  return $this->id;
397  }
398 
402  public function exists() {
403  $this->load();
404 
405  return $this->exists;
406  }
407 
412  public function getKey() {
413  $this->load();
414 
415  return $this->key;
416  }
417 
422  public function getStorageKey() {
423  return $this->getKey();
424  }
425 
430  public function getGroup() {
431  return $this->group;
432  }
433 
438  public function getWidth() {
439  $this->load();
440 
441  return $this->width;
442  }
443 
448  public function getHeight() {
449  $this->load();
450 
451  return $this->height;
452  }
453 
460  public function getMetadata() {
461  $data = $this->getMetadataArray();
462  if ( !$data ) {
463  return '';
464  } elseif ( array_keys( $data ) === [ '_error' ] ) {
465  // Legacy error encoding
466  return $data['_error'];
467  } else {
468  return serialize( $this->getMetadataArray() );
469  }
470  }
471 
478  public function getMetadataArray(): array {
479  $this->load();
480  if ( $this->unloadedMetadataBlobs ) {
481  return $this->getMetadataItems(
482  array_unique( array_merge(
483  array_keys( $this->metadataArray ),
484  array_keys( $this->unloadedMetadataBlobs )
485  ) )
486  );
487  }
488  return $this->metadataArray;
489  }
490 
491  public function getMetadataItems( array $itemNames ): array {
492  $this->load();
493  $result = [];
494  $addresses = [];
495  foreach ( $itemNames as $itemName ) {
496  if ( array_key_exists( $itemName, $this->metadataArray ) ) {
497  $result[$itemName] = $this->metadataArray[$itemName];
498  } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
499  $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
500  }
501  }
502 
503  if ( $addresses ) {
504  $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
505  foreach ( $addresses as $itemName => $address ) {
506  unset( $this->unloadedMetadataBlobs[$itemName] );
507  $value = $resultFromBlob[$itemName] ?? null;
508  if ( $value !== null ) {
509  $result[$itemName] = $value;
510  $this->metadataArray[$itemName] = $value;
511  }
512  }
513  }
514  return $result;
515  }
516 
528  public function getMetadataForDb( IDatabase $db ) {
529  $this->load();
530  if ( !$this->metadataArray && !$this->metadataBlobs ) {
531  $s = '';
532  } elseif ( $this->repo->isJsonMetadataEnabled() ) {
533  $s = $this->getJsonMetadata();
534  } else {
535  $s = serialize( $this->getMetadataArray() );
536  }
537  if ( !is_string( $s ) ) {
538  throw new MWException( 'Could not serialize image metadata value for DB' );
539  }
540  return $db->encodeBlob( $s );
541  }
542 
549  private function getJsonMetadata() {
550  // Directly store data that is not already in BlobStore
551  $envelope = [
552  'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
553  ];
554 
555  // Also store the blob addresses
556  if ( $this->metadataBlobs ) {
557  $envelope['blobs'] = $this->metadataBlobs;
558  }
559 
560  [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
561 
562  // Repeated calls to this function should not keep inserting more blobs
563  $this->metadataBlobs += $blobAddresses;
564 
565  return $s;
566  }
567 
576  protected function loadMetadataFromDbFieldValue( IDatabase $db, $metadataBlob ) {
577  $this->loadMetadataFromString( $db->decodeBlob( $metadataBlob ) );
578  }
579 
587  protected function loadMetadataFromString( $metadataString ) {
588  $this->extraDataLoaded = true;
589  $this->metadataArray = [];
590  $this->metadataBlobs = [];
591  $this->unloadedMetadataBlobs = [];
592  $metadataString = (string)$metadataString;
593  if ( $metadataString === '' ) {
594  $this->metadataSerializationFormat = self::MDS_EMPTY;
595  return;
596  }
597  if ( $metadataString[0] === '{' ) {
598  $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
599  if ( !$envelope ) {
600  // Legacy error encoding
601  $this->metadataArray = [ '_error' => $metadataString ];
602  $this->metadataSerializationFormat = self::MDS_LEGACY;
603  } else {
604  $this->metadataSerializationFormat = self::MDS_JSON;
605  if ( isset( $envelope['data'] ) ) {
606  $this->metadataArray = $envelope['data'];
607  }
608  if ( isset( $envelope['blobs'] ) ) {
609  $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope['blobs'];
610  }
611  }
612  } else {
613  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
614  $data = @unserialize( $metadataString );
615  if ( !is_array( $data ) ) {
616  // Legacy error encoding
617  $data = [ '_error' => $metadataString ];
618  $this->metadataSerializationFormat = self::MDS_LEGACY;
619  } else {
620  $this->metadataSerializationFormat = self::MDS_PHP;
621  }
622  $this->metadataArray = $data;
623  }
624  }
625 
630  public function getSize() {
631  $this->load();
632 
633  return $this->size;
634  }
635 
640  public function getBits() {
641  $this->load();
642 
643  return $this->bits;
644  }
645 
650  public function getMimeType() {
651  $this->load();
652 
653  return $this->mime;
654  }
655 
660  private function getHandler() {
661  if ( !isset( $this->handler ) ) {
662  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
663  }
664 
665  return $this->handler;
666  }
667 
674  public function pageCount() {
675  if ( !isset( $this->pageCount ) ) {
676  // @FIXME: callers expect File objects
677  // @phan-suppress-next-line PhanTypeMismatchArgument
678  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
679  // @phan-suppress-next-line PhanTypeMismatchArgument
680  $this->pageCount = $this->handler->pageCount( $this );
681  } else {
682  $this->pageCount = false;
683  }
684  }
685 
686  return $this->pageCount;
687  }
688 
694  public function getMediaType() {
695  $this->load();
696 
697  return $this->media_type;
698  }
699 
705  public function getTimestamp() {
706  $this->load();
707 
708  return wfTimestamp( TS_MW, $this->timestamp );
709  }
710 
717  public function getSha1() {
718  $this->load();
719 
720  return $this->sha1;
721  }
722 
734  public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
735  $this->load();
736  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
737  return null;
738  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
739  return null;
740  } else {
741  return $this->user;
742  }
743  }
744 
757  public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
758  $this->load();
759  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
760  return '';
761  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
762  return '';
763  } else {
764  return $this->description;
765  }
766  }
767 
772  public function getVisibility() {
773  $this->load();
774 
775  return $this->deleted;
776  }
777 
784  public function isDeleted( $field ) {
785  $this->load();
786 
787  return ( $this->deleted & $field ) == $field;
788  }
789 
797  public function userCan( $field, Authority $performer ) {
798  $this->load();
799  $title = $this->getTitle();
800 
801  return RevisionRecord::userCanBitfield(
802  $this->deleted,
803  $field,
804  $performer,
805  $title ?: null
806  );
807  }
808 }
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.
const FOR_PUBLIC
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.
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
Definition: File.php:207
const DELETED_COMMENT
Definition: File.php:73
const DELETED_USER
Definition: File.php:74
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:225
MediaWiki exception.
Definition: MWException.php:32
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:82
getDBkey()
Get the main part with underscores.
Definition: Title.php:1096
Helper for storage of metadata.
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
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.
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