43 private const MDS_EMPTY =
'empty';
46 private const MDS_LEGACY =
'legacy';
49 private const MDS_PHP =
'php';
52 private const MDS_JSON =
'json';
110 private $description;
133 private $archive_name;
148 private $metadataStorageHelper;
162 $this->group =
'deleted';
168 $this->mime =
"unknown/unknown";
169 $this->media_type =
'';
170 $this->description =
'';
172 $this->timestamp =
null;
174 $this->dataLoaded =
false;
179 $this->title = File::normalizeTitle(
$title,
'exception' );
195 if ( !$id && !$key && !(
$title instanceof
Title ) && !$sha1 ) {
196 throw new MWException(
"No specifications provided to ArchivedFile constructor." );
199 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
210 if ( $this->dataLoaded ) {
215 if ( $this->
id > 0 ) {
216 $conds[
'fa_id'] = $this->id;
219 $conds[
'fa_storage_group'] = $this->group;
220 $conds[
'fa_storage_key'] = $this->key;
222 if ( $this->title ) {
223 $conds[
'fa_name'] = $this->title->getDBkey();
226 $conds[
'fa_sha1'] = $this->sha1;
229 if ( $conds === [] ) {
230 throw new MWException(
"No specific information for retrieving archived file" );
233 if ( !$this->title || $this->title->getNamespace() ===
NS_FILE ) {
234 $this->dataLoaded =
true;
237 $row =
$dbr->selectRow(
238 $fileQuery[
'tables'],
239 $fileQuery[
'fields'],
242 [
'ORDER BY' =>
'fa_timestamp DESC' ],
253 throw new MWException(
'This title does not correspond to an image page.' );
269 $file->loadFromRow( $row );
291 $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin(
'fa_description' );
295 'filearchive_actor' =>
'actor'
296 ] + $commentQuery[
'tables'],
313 'fa_deleted_timestamp',
316 'fa_user' =>
'filearchive_actor.actor_user',
317 'fa_user_text' =>
'filearchive_actor.actor_name'
318 ] + $commentQuery[
'fields'],
320 'filearchive_actor' => [
'JOIN',
'actor_id=fa_actor' ]
321 ] + $commentQuery[
'joins'],
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()
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;
358 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
360 if ( !$this->title ) {
361 $this->title = Title::makeTitleSafe(
NS_FILE, $row->fa_name );
371 if ( !$this->title ) {
383 if ( $this->name ===
false ) {
451 return $this->height;
464 } elseif ( array_keys( $data ) === [
'_error' ] ) {
466 return $data[
'_error'];
480 if ( $this->unloadedMetadataBlobs ) {
482 array_unique( array_merge(
483 array_keys( $this->metadataArray ),
484 array_keys( $this->unloadedMetadataBlobs )
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];
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;
530 if ( !$this->metadataArray && !$this->metadataBlobs ) {
532 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
533 $s = $this->getJsonMetadata();
535 $s = serialize( $this->getMetadataArray() );
537 if ( !is_string( $s ) ) {
538 throw new MWException(
'Could not serialize image metadata value for DB' );
549 private function getJsonMetadata() {
552 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
556 if ( $this->metadataBlobs ) {
557 $envelope[
'blobs'] = $this->metadataBlobs;
560 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
563 $this->metadataBlobs += $blobAddresses;
577 $this->loadMetadataFromString( $db->
decodeBlob( $metadataBlob ) );
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;
597 if ( $metadataString[0] ===
'{' ) {
598 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
601 $this->metadataArray = [
'_error' => $metadataString ];
602 $this->metadataSerializationFormat = self::MDS_LEGACY;
604 $this->metadataSerializationFormat = self::MDS_JSON;
605 if ( isset( $envelope[
'data'] ) ) {
606 $this->metadataArray = $envelope[
'data'];
608 if ( isset( $envelope[
'blobs'] ) ) {
609 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope[
'blobs'];
614 $data = @unserialize( $metadataString );
615 if ( !is_array( $data ) ) {
617 $data = [
'_error' => $metadataString ];
618 $this->metadataSerializationFormat = self::MDS_LEGACY;
620 $this->metadataSerializationFormat = self::MDS_PHP;
622 $this->metadataArray = $data;
660 private function getHandler() {
661 if ( !isset( $this->handler ) ) {
665 return $this->handler;
675 if ( !isset( $this->pageCount ) ) {
678 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
680 $this->pageCount = $this->handler->pageCount( $this );
682 $this->pageCount =
false;
686 return $this->pageCount;
697 return $this->media_type;
736 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
738 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
759 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
761 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
764 return $this->description;
775 return $this->deleted;
787 return ( $this->deleted & $field ) == $field;
801 return RevisionRecord::userCanBitfield(
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.
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.
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.
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...
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.