MediaWiki  master
ArchivedFile.php
Go to the documentation of this file.
1 <?php
27 
34 class ArchivedFile {
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 
100  protected $unloadedMetadataBlobs = [];
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
234  $dbr = wfGetDB( DB_REPLICA );
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 
288  public static function getQueryInfo() {
289  $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'fa_description' );
290  return [
291  'tables' => [
292  'filearchive',
293  'filearchive_actor' => 'actor'
294  ] + $commentQuery['tables'],
295  'fields' => [
296  'fa_id',
297  'fa_name',
298  'fa_archive_name',
299  'fa_storage_key',
300  'fa_storage_group',
301  'fa_size',
302  'fa_bits',
303  'fa_width',
304  'fa_height',
305  'fa_metadata',
306  'fa_media_type',
307  'fa_major_mime',
308  'fa_minor_mime',
309  'fa_timestamp',
310  'fa_deleted',
311  'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
312  'fa_sha1',
313  'fa_actor',
314  'fa_user' => 'filearchive_actor.actor_user',
315  'fa_user_text' => 'filearchive_actor.actor_name'
316  ] + $commentQuery['fields'],
317  'joins' => [
318  'filearchive_actor' => [ 'JOIN', 'actor_id=fa_actor' ]
319  ] + $commentQuery['joins'],
320  ];
321  }
322 
330  public function loadFromRow( $row ) {
331  $this->id = intval( $row->fa_id );
332  $this->name = $row->fa_name;
333  $this->archive_name = $row->fa_archive_name;
334  $this->group = $row->fa_storage_group;
335  $this->key = $row->fa_storage_key;
336  $this->size = $row->fa_size;
337  $this->bits = $row->fa_bits;
338  $this->width = $row->fa_width;
339  $this->height = $row->fa_height;
341  $this->repo->getReplicaDB(), $row->fa_metadata );
342  $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
343  $this->media_type = $row->fa_media_type;
344  $services = MediaWikiServices::getInstance();
345  $this->description = $services->getCommentStore()
346  // Legacy because $row may have come from self::selectFields()
347  ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
348  $this->user = $services->getUserFactory()
349  ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
350  $this->timestamp = $row->fa_timestamp;
351  $this->deleted = $row->fa_deleted;
352  if ( isset( $row->fa_sha1 ) ) {
353  $this->sha1 = $row->fa_sha1;
354  } else {
355  // old row, populate from key
356  $this->sha1 = LocalRepo::getHashFromKey( $this->key );
357  }
358  if ( !$this->title ) {
359  $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
360  }
361  }
362 
368  public function getTitle() {
369  if ( !$this->title ) {
370  $this->load();
371  }
372  return $this->title;
373  }
374 
380  public function getName() {
381  if ( $this->name === false ) {
382  $this->load();
383  }
384 
385  return $this->name;
386  }
387 
391  public function getID() {
392  $this->load();
393 
394  return $this->id;
395  }
396 
400  public function exists() {
401  $this->load();
402 
403  return $this->exists;
404  }
405 
410  public function getKey() {
411  $this->load();
412 
413  return $this->key;
414  }
415 
420  public function getStorageKey() {
421  return $this->getKey();
422  }
423 
428  public function getGroup() {
429  return $this->group;
430  }
431 
436  public function getWidth() {
437  $this->load();
438 
439  return $this->width;
440  }
441 
446  public function getHeight() {
447  $this->load();
448 
449  return $this->height;
450  }
451 
458  public function getMetadata() {
459  $data = $this->getMetadataArray();
460  if ( !$data ) {
461  return '';
462  } elseif ( array_keys( $data ) === [ '_error' ] ) {
463  // Legacy error encoding
464  return $data['_error'];
465  } else {
466  return serialize( $this->getMetadataArray() );
467  }
468  }
469 
476  public function getMetadataArray(): array {
477  $this->load();
478  if ( $this->unloadedMetadataBlobs ) {
479  return $this->getMetadataItems(
480  array_unique( array_merge(
481  array_keys( $this->metadataArray ),
482  array_keys( $this->unloadedMetadataBlobs )
483  ) )
484  );
485  }
486  return $this->metadataArray;
487  }
488 
489  public function getMetadataItems( array $itemNames ): array {
490  $this->load();
491  $result = [];
492  $addresses = [];
493  foreach ( $itemNames as $itemName ) {
494  if ( array_key_exists( $itemName, $this->metadataArray ) ) {
495  $result[$itemName] = $this->metadataArray[$itemName];
496  } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
497  $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
498  }
499  }
500 
501  if ( $addresses ) {
502  $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
503  foreach ( $addresses as $itemName => $address ) {
504  unset( $this->unloadedMetadataBlobs[$itemName] );
505  $value = $resultFromBlob[$itemName] ?? null;
506  if ( $value !== null ) {
507  $result[$itemName] = $value;
508  $this->metadataArray[$itemName] = $value;
509  }
510  }
511  }
512  return $result;
513  }
514 
526  public function getMetadataForDb( IDatabase $db ) {
527  $this->load();
528  if ( !$this->metadataArray && !$this->metadataBlobs ) {
529  $s = '';
530  } elseif ( $this->repo->isJsonMetadataEnabled() ) {
531  $s = $this->getJsonMetadata();
532  } else {
533  $s = serialize( $this->getMetadataArray() );
534  }
535  if ( !is_string( $s ) ) {
536  throw new MWException( 'Could not serialize image metadata value for DB' );
537  }
538  return $db->encodeBlob( $s );
539  }
540 
547  private function getJsonMetadata() {
548  // Directly store data that is not already in BlobStore
549  $envelope = [
550  'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
551  ];
552 
553  // Also store the blob addresses
554  if ( $this->metadataBlobs ) {
555  $envelope['blobs'] = $this->metadataBlobs;
556  }
557 
558  list( $s, $blobAddresses ) = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
559 
560  // Repeated calls to this function should not keep inserting more blobs
561  $this->metadataBlobs += $blobAddresses;
562 
563  return $s;
564  }
565 
574  protected function loadMetadataFromDbFieldValue( IDatabase $db, $metadataBlob ) {
575  $this->loadMetadataFromString( $db->decodeBlob( $metadataBlob ) );
576  }
577 
585  protected function loadMetadataFromString( $metadataString ) {
586  $this->extraDataLoaded = true;
587  $this->metadataArray = [];
588  $this->metadataBlobs = [];
589  $this->unloadedMetadataBlobs = [];
590  $metadataString = (string)$metadataString;
591  if ( $metadataString === '' ) {
592  $this->metadataSerializationFormat = self::MDS_EMPTY;
593  return;
594  }
595  if ( $metadataString[0] === '{' ) {
596  $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
597  if ( !$envelope ) {
598  // Legacy error encoding
599  $this->metadataArray = [ '_error' => $metadataString ];
600  $this->metadataSerializationFormat = self::MDS_LEGACY;
601  } else {
602  $this->metadataSerializationFormat = self::MDS_JSON;
603  if ( isset( $envelope['data'] ) ) {
604  $this->metadataArray = $envelope['data'];
605  }
606  if ( isset( $envelope['blobs'] ) ) {
607  $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope['blobs'];
608  }
609  }
610  } else {
611  // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
612  $data = @unserialize( $metadataString );
613  if ( !is_array( $data ) ) {
614  // Legacy error encoding
615  $data = [ '_error' => $metadataString ];
616  $this->metadataSerializationFormat = self::MDS_LEGACY;
617  } else {
618  $this->metadataSerializationFormat = self::MDS_PHP;
619  }
620  $this->metadataArray = $data;
621  }
622  }
623 
628  public function getSize() {
629  $this->load();
630 
631  return $this->size;
632  }
633 
638  public function getBits() {
639  $this->load();
640 
641  return $this->bits;
642  }
643 
648  public function getMimeType() {
649  $this->load();
650 
651  return $this->mime;
652  }
653 
658  private function getHandler() {
659  if ( !isset( $this->handler ) ) {
660  $this->handler = MediaHandler::getHandler( $this->getMimeType() );
661  }
662 
663  return $this->handler;
664  }
665 
672  public function pageCount() {
673  if ( !isset( $this->pageCount ) ) {
674  // @FIXME: callers expect File objects
675  // @phan-suppress-next-line PhanTypeMismatchArgument
676  if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
677  // @phan-suppress-next-line PhanTypeMismatchArgument
678  $this->pageCount = $this->handler->pageCount( $this );
679  } else {
680  $this->pageCount = false;
681  }
682  }
683 
684  return $this->pageCount;
685  }
686 
692  public function getMediaType() {
693  $this->load();
694 
695  return $this->media_type;
696  }
697 
703  public function getTimestamp() {
704  $this->load();
705 
706  return wfTimestamp( TS_MW, $this->timestamp );
707  }
708 
715  public function getSha1() {
716  $this->load();
717 
718  return $this->sha1;
719  }
720 
732  public function getUploader( int $audience = self::FOR_PUBLIC, Authority $performer = null ): ?UserIdentity {
733  $this->load();
734  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
735  return null;
736  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
737  return null;
738  } else {
739  return $this->user;
740  }
741  }
742 
755  public function getDescription( int $audience = self::FOR_PUBLIC, Authority $performer = null ): string {
756  $this->load();
757  if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
758  return '';
759  } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
760  return '';
761  } else {
762  return $this->description;
763  }
764  }
765 
770  public function getVisibility() {
771  $this->load();
772 
773  return $this->deleted;
774  }
775 
782  public function isDeleted( $field ) {
783  $this->load();
784 
785  return ( $this->deleted & $field ) == $field;
786  }
787 
795  public function userCan( $field, Authority $performer ) {
796  $this->load();
797  $title = $this->getTitle();
798 
799  return RevisionRecord::userCanBitfield(
800  $this->deleted,
801  $field,
802  $performer,
803  $title ?: null
804  );
805  }
806 }
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.
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:223
MediaWiki exception.
Definition: MWException.php:29
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
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:664
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
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