46 private const MDS_EMPTY =
'empty';
49 private const MDS_LEGACY =
'legacy';
52 private const MDS_PHP =
'php';
55 private const MDS_JSON =
'json';
113 private $description;
136 private $archive_name;
151 private $metadataStorageHelper;
165 $this->group =
'deleted';
171 $this->mime =
"unknown/unknown";
172 $this->media_type =
'';
173 $this->description =
'';
175 $this->timestamp =
null;
177 $this->dataLoaded =
false;
198 if ( !$id && !$key && !(
$title instanceof
Title ) && !$sha1 ) {
199 throw new MWException(
"No specifications provided to ArchivedFile constructor." );
202 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
213 if ( $this->dataLoaded ) {
218 if ( $this->
id > 0 ) {
219 $conds[
'fa_id'] = $this->id;
222 $conds[
'fa_storage_group'] = $this->group;
223 $conds[
'fa_storage_key'] = $this->key;
225 if ( $this->title ) {
226 $conds[
'fa_name'] = $this->title->getDBkey();
229 $conds[
'fa_sha1'] = $this->sha1;
232 if ( $conds === [] ) {
233 throw new MWException(
"No specific information for retrieving archived file" );
236 if ( !$this->title || $this->title->getNamespace() ===
NS_FILE ) {
237 $this->dataLoaded =
true;
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();
251 throw new MWException(
'This title does not correspond to an image page.' );
266 $file->loadFromRow( $row );
289 $dbr = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getReplicaDatabase();
290 $queryInfo = ( FileSelectQueryBuilder::newForArchivedFile( $dbr ) )->
getQueryInfo();
292 'tables' => $queryInfo[
'tables'],
293 'fields' => $queryInfo[
'fields'],
294 'joins' => $queryInfo[
'join_conds'],
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()
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;
333 if ( !$this->title ) {
334 $this->title = Title::makeTitleSafe(
NS_FILE, $row->fa_name );
336 $this->
exists = $row->fa_archive_name !==
'';
345 if ( !$this->title ) {
357 if ( $this->name ===
false ) {
425 return $this->height;
438 } elseif ( array_keys( $data ) === [
'_error' ] ) {
440 return $data[
'_error'];
454 if ( $this->unloadedMetadataBlobs ) {
456 array_unique( array_merge(
457 array_keys( $this->metadataArray ),
458 array_keys( $this->unloadedMetadataBlobs )
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];
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;
504 if ( !$this->metadataArray && !$this->metadataBlobs ) {
506 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
507 $s = $this->getJsonMetadata();
509 $s = serialize( $this->getMetadataArray() );
511 if ( !is_string( $s ) ) {
512 throw new MWException(
'Could not serialize image metadata value for DB' );
523 private function getJsonMetadata() {
526 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
530 if ( $this->metadataBlobs ) {
531 $envelope[
'blobs'] = $this->metadataBlobs;
534 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
537 $this->metadataBlobs += $blobAddresses;
551 $this->loadMetadataFromString( $db->
decodeBlob( $metadataBlob ) );
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;
571 if ( $metadataString[0] ===
'{' ) {
572 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
575 $this->metadataArray = [
'_error' => $metadataString ];
576 $this->metadataSerializationFormat = self::MDS_LEGACY;
578 $this->metadataSerializationFormat = self::MDS_JSON;
579 if ( isset( $envelope[
'data'] ) ) {
580 $this->metadataArray = $envelope[
'data'];
582 if ( isset( $envelope[
'blobs'] ) ) {
583 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope[
'blobs'];
588 $data = @unserialize( $metadataString );
589 if ( !is_array( $data ) ) {
591 $data = [
'_error' => $metadataString ];
592 $this->metadataSerializationFormat = self::MDS_LEGACY;
594 $this->metadataSerializationFormat = self::MDS_PHP;
596 $this->metadataArray = $data;
634 private function getHandler() {
635 if ( !isset( $this->handler ) ) {
639 return $this->handler;
649 if ( !isset( $this->pageCount ) ) {
652 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
654 $this->pageCount = $this->handler->pageCount( $this );
656 $this->pageCount =
false;
660 return $this->pageCount;
671 return $this->media_type;
712 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan(
File::DELETED_USER, $performer ) ) {
735 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan(
File::DELETED_COMMENT, $performer ) ) {
738 return $this->description;
749 return $this->deleted;
761 return ( $this->deleted & $field ) == $field;
775 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.
static normalizeTitle( $title, $exception=false)
Given a string or Title object return either a valid Title object with namespace NS_FILE or null.
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.