9use BadMethodCallException;
19use UnexpectedValueException;
23use Wikimedia\Timestamp\TimestampFormat as TS;
39 private const MDS_EMPTY =
'empty';
42 private const MDS_LEGACY =
'legacy';
45 private const MDS_PHP =
'php';
48 private const MDS_JSON =
'json';
106 private $description;
129 private $archive_name;
144 private $metadataStorageHelper;
157 $this->group =
'deleted';
163 $this->mime =
"unknown/unknown";
164 $this->media_type =
'';
165 $this->description =
'';
167 $this->timestamp =
null;
169 $this->dataLoaded =
false;
190 if ( !$id && !$key && !(
$title instanceof
Title ) && !$sha1 ) {
191 throw new BadMethodCallException(
"No specifications provided to ArchivedFile constructor." );
204 if ( $this->dataLoaded ) {
209 if ( $this->
id > 0 ) {
210 $conds[
'fa_id'] = $this->id;
213 $conds[
'fa_storage_group'] = $this->group;
214 $conds[
'fa_storage_key'] = $this->key;
216 if ( $this->title ) {
217 $conds[
'fa_name'] = $this->title->getDBkey();
220 $conds[
'fa_sha1'] = $this->sha1;
223 if ( $conds === [] ) {
224 throw new RuntimeException(
"No specific information for retrieving archived file" );
227 if ( !$this->title || $this->title->getNamespace() ===
NS_FILE ) {
228 $this->dataLoaded =
true;
229 $dbr = $this->repo->getReplicaDB();
231 $row = $queryBuilder->where( $conds )
232 ->orderBy(
'fa_timestamp', SelectQueryBuilder::SORT_DESC )
233 ->caller( __METHOD__ )->fetchRow();
242 throw new UnexpectedValueException(
'This title does not correspond to an image page.' );
257 $file->loadFromRow( $row );
283 'tables' => $queryInfo[
'tables'],
284 'fields' => $queryInfo[
'fields'],
285 'joins' => $queryInfo[
'join_conds'],
297 $this->
id = intval( $row->fa_id );
298 $this->name = $row->fa_name;
299 $this->archive_name = $row->fa_archive_name;
300 $this->group = $row->fa_storage_group;
301 $this->key = $row->fa_storage_key;
302 $this->size = $row->fa_size;
303 $this->bits = $row->fa_bits;
304 $this->width = $row->fa_width;
305 $this->height = $row->fa_height;
307 $this->repo->getReplicaDB(), $row->fa_metadata );
308 $this->mime =
"$row->fa_major_mime/$row->fa_minor_mime";
309 $this->media_type = $row->fa_media_type;
311 $this->description = $services->getCommentStore()
313 ->getCommentLegacy( $this->repo->getReplicaDB(),
'fa_description', $row )->text;
314 $this->user = $services->getUserFactory()
315 ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
316 $this->timestamp = $row->fa_timestamp;
317 $this->deleted = $row->fa_deleted;
318 if ( isset( $row->fa_sha1 ) ) {
319 $this->sha1 = $row->fa_sha1;
324 if ( !$this->title ) {
325 $this->title = Title::makeTitleSafe(
NS_FILE, $row->fa_name );
327 $this->
exists = $row->fa_archive_name !==
'';
336 if ( !$this->title ) {
348 if ( $this->name ===
false ) {
416 return $this->height;
429 } elseif ( array_keys( $data ) === [
'_error' ] ) {
431 return $data[
'_error'];
445 if ( $this->unloadedMetadataBlobs ) {
447 array_unique( array_merge(
448 array_keys( $this->metadataArray ),
449 array_keys( $this->unloadedMetadataBlobs )
460 foreach ( $itemNames as $itemName ) {
461 if ( array_key_exists( $itemName, $this->metadataArray ) ) {
462 $result[$itemName] = $this->metadataArray[$itemName];
463 } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
464 $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
469 $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
470 foreach ( $addresses as $itemName => $address ) {
471 unset( $this->unloadedMetadataBlobs[$itemName] );
472 $value = $resultFromBlob[$itemName] ??
null;
473 if ( $value !==
null ) {
474 $result[$itemName] = $value;
475 $this->metadataArray[$itemName] = $value;
495 if ( !$this->metadataArray && !$this->metadataBlobs ) {
497 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
498 $s = $this->getJsonMetadata();
500 $s = serialize( $this->getMetadataArray() );
502 if ( !is_string( $s ) ) {
503 throw new RuntimeException(
'Could not serialize image metadata value for DB' );
514 private function getJsonMetadata() {
517 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
521 if ( $this->metadataBlobs ) {
522 $envelope[
'blobs'] = $this->metadataBlobs;
525 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
528 $this->metadataBlobs += $blobAddresses;
542 $this->loadMetadataFromString( $db->
decodeBlob( $metadataBlob ) );
553 $this->extraDataLoaded =
true;
554 $this->metadataArray = [];
555 $this->metadataBlobs = [];
556 $this->unloadedMetadataBlobs = [];
557 $metadataString = (string)$metadataString;
558 if ( $metadataString ===
'' ) {
559 $this->metadataSerializationFormat = self::MDS_EMPTY;
562 if ( $metadataString[0] ===
'{' ) {
563 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
566 $this->metadataArray = [
'_error' => $metadataString ];
567 $this->metadataSerializationFormat = self::MDS_LEGACY;
569 $this->metadataSerializationFormat = self::MDS_JSON;
570 if ( isset( $envelope[
'data'] ) ) {
571 $this->metadataArray = $envelope[
'data'];
573 if ( isset( $envelope[
'blobs'] ) ) {
574 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope[
'blobs'];
579 $data = @unserialize( $metadataString );
580 if ( !is_array( $data ) ) {
582 $data = [
'_error' => $metadataString ];
583 $this->metadataSerializationFormat = self::MDS_LEGACY;
585 $this->metadataSerializationFormat = self::MDS_PHP;
587 $this->metadataArray = $data;
624 private function getHandler() {
625 if ( !$this->handler ) {
626 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
629 return $this->handler;
639 if ( $this->pageCount ===
null ) {
642 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
644 $this->pageCount = $this->handler->pageCount( $this );
646 $this->pageCount =
false;
650 return $this->pageCount;
661 return $this->media_type;
700 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
702 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
723 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
725 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
728 return $this->description;
739 return $this->deleted;
751 return ( $this->deleted & $field ) == $field;
763 $title = $this->getTitle();
765 return RevisionRecord::userCanBitfield(
775class_alias( ArchivedFile::class,
'ArchivedFile' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.