46 private const MDS_EMPTY =
'empty';
49 private const MDS_LEGACY =
'legacy';
52 private const MDS_PHP =
'php';
55 private const MDS_JSON =
'json';
82 protected $metadataArray = [];
85 protected $extraDataLoaded =
false;
96 protected $metadataBlobs = [];
104 protected $unloadedMetadataBlobs = [];
113 private $description;
136 private $archive_name;
151 private $metadataStorageHelper;
160 public function __construct( $title, $id = 0, $key =
'', $sha1 =
'' ) {
164 $this->group =
'deleted';
170 $this->mime =
"unknown/unknown";
171 $this->media_type =
'';
172 $this->description =
'';
174 $this->timestamp =
null;
176 $this->dataLoaded =
false;
181 $this->title = File::normalizeTitle(
$title,
'exception' );
197 if ( !$id && !$key && !(
$title instanceof
Title ) && !$sha1 ) {
198 throw new BadMethodCallException(
"No specifications provided to ArchivedFile constructor." );
201 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
211 if ( $this->dataLoaded ) {
216 if ( $this->
id > 0 ) {
217 $conds[
'fa_id'] = $this->id;
220 $conds[
'fa_storage_group'] = $this->group;
221 $conds[
'fa_storage_key'] = $this->key;
223 if ( $this->title ) {
224 $conds[
'fa_name'] = $this->title->getDBkey();
227 $conds[
'fa_sha1'] = $this->sha1;
230 if ( $conds === [] ) {
231 throw new RuntimeException(
"No specific information for retrieving archived file" );
234 if ( !$this->title || $this->title->getNamespace() ===
NS_FILE ) {
235 $this->dataLoaded =
true;
236 $dbr = $this->repo->getReplicaDB();
237 $queryBuilder = FileSelectQueryBuilder::newForArchivedFile( $dbr );
238 $row = $queryBuilder->where( $conds )
239 ->orderBy(
'fa_timestamp', SelectQueryBuilder::SORT_DESC )
240 ->caller( __METHOD__ )->fetchRow();
249 throw new UnexpectedValueException(
'This title does not correspond to an image page.' );
264 $file->loadFromRow( $row );
287 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
288 $queryInfo = ( FileSelectQueryBuilder::newForArchivedFile( $dbr ) )->
getQueryInfo();
290 'tables' => $queryInfo[
'tables'],
291 'fields' => $queryInfo[
'fields'],
292 'joins' => $queryInfo[
'join_conds'],
304 $this->
id = intval( $row->fa_id );
305 $this->name = $row->fa_name;
306 $this->archive_name = $row->fa_archive_name;
307 $this->group = $row->fa_storage_group;
308 $this->key = $row->fa_storage_key;
309 $this->size = $row->fa_size;
310 $this->bits = $row->fa_bits;
311 $this->width = $row->fa_width;
312 $this->height = $row->fa_height;
314 $this->repo->getReplicaDB(), $row->fa_metadata );
315 $this->mime =
"$row->fa_major_mime/$row->fa_minor_mime";
316 $this->media_type = $row->fa_media_type;
317 $services = MediaWikiServices::getInstance();
318 $this->description = $services->getCommentStore()
320 ->getCommentLegacy( $this->repo->getReplicaDB(),
'fa_description', $row )->text;
321 $this->user = $services->getUserFactory()
322 ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
323 $this->timestamp = $row->fa_timestamp;
324 $this->deleted = $row->fa_deleted;
325 if ( isset( $row->fa_sha1 ) ) {
326 $this->sha1 = $row->fa_sha1;
329 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
331 if ( !$this->title ) {
332 $this->title = Title::makeTitleSafe(
NS_FILE, $row->fa_name );
334 $this->
exists = $row->fa_archive_name !==
'';
343 if ( !$this->title ) {
355 if ( $this->name ===
false ) {
377 return $this->exists;
423 return $this->height;
436 } elseif ( array_keys( $data ) === [
'_error' ] ) {
438 return $data[
'_error'];
452 if ( $this->unloadedMetadataBlobs ) {
454 array_unique( array_merge(
455 array_keys( $this->metadataArray ),
456 array_keys( $this->unloadedMetadataBlobs )
460 return $this->metadataArray;
467 foreach ( $itemNames as $itemName ) {
468 if ( array_key_exists( $itemName, $this->metadataArray ) ) {
469 $result[$itemName] = $this->metadataArray[$itemName];
470 } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
471 $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
476 $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
477 foreach ( $addresses as $itemName => $address ) {
478 unset( $this->unloadedMetadataBlobs[$itemName] );
479 $value = $resultFromBlob[$itemName] ??
null;
480 if ( $value !==
null ) {
481 $result[$itemName] = $value;
482 $this->metadataArray[$itemName] = $value;
502 if ( !$this->metadataArray && !$this->metadataBlobs ) {
504 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
505 $s = $this->getJsonMetadata();
507 $s = serialize( $this->getMetadataArray() );
509 if ( !is_string( $s ) ) {
510 throw new RuntimeException(
'Could not serialize image metadata value for DB' );
521 private function getJsonMetadata() {
524 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
528 if ( $this->metadataBlobs ) {
529 $envelope[
'blobs'] = $this->metadataBlobs;
532 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
535 $this->metadataBlobs += $blobAddresses;
549 $this->loadMetadataFromString( $db->
decodeBlob( $metadataBlob ) );
560 $this->extraDataLoaded =
true;
561 $this->metadataArray = [];
562 $this->metadataBlobs = [];
563 $this->unloadedMetadataBlobs = [];
564 $metadataString = (string)$metadataString;
565 if ( $metadataString ===
'' ) {
566 $this->metadataSerializationFormat = self::MDS_EMPTY;
569 if ( $metadataString[0] ===
'{' ) {
570 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
573 $this->metadataArray = [
'_error' => $metadataString ];
574 $this->metadataSerializationFormat = self::MDS_LEGACY;
576 $this->metadataSerializationFormat = self::MDS_JSON;
577 if ( isset( $envelope[
'data'] ) ) {
578 $this->metadataArray = $envelope[
'data'];
580 if ( isset( $envelope[
'blobs'] ) ) {
581 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope[
'blobs'];
586 $data = @unserialize( $metadataString );
587 if ( !is_array( $data ) ) {
589 $data = [
'_error' => $metadataString ];
590 $this->metadataSerializationFormat = self::MDS_LEGACY;
592 $this->metadataSerializationFormat = self::MDS_PHP;
594 $this->metadataArray = $data;
632 private function getHandler() {
633 if ( !isset( $this->handler ) ) {
634 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
637 return $this->handler;
647 if ( !isset( $this->pageCount ) ) {
650 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
652 $this->pageCount = $this->handler->pageCount( $this );
654 $this->pageCount =
false;
658 return $this->pageCount;
669 return $this->media_type;
708 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
710 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
731 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
733 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
736 return $this->description;
747 return $this->deleted;
759 return ( $this->deleted & $field ) == $field;
773 return RevisionRecord::userCanBitfield(
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.
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.
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.
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...