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 $this->title ??= Title::makeTitleSafe(
NS_FILE, $row->fa_name );
325 $this->
exists = $row->fa_archive_name !==
'';
334 if ( !$this->title ) {
346 if ( $this->name ===
false ) {
414 return $this->height;
428 } elseif ( array_keys( $data ) === [
'_error' ] ) {
430 return $data[
'_error'];
444 if ( $this->unloadedMetadataBlobs ) {
446 array_unique( array_merge(
447 array_keys( $this->metadataArray ),
448 array_keys( $this->unloadedMetadataBlobs )
459 foreach ( $itemNames as $itemName ) {
460 if ( array_key_exists( $itemName, $this->metadataArray ) ) {
461 $result[$itemName] = $this->metadataArray[$itemName];
462 } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
463 $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
468 $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
469 foreach ( $addresses as $itemName => $address ) {
470 unset( $this->unloadedMetadataBlobs[$itemName] );
471 $value = $resultFromBlob[$itemName] ??
null;
472 if ( $value !==
null ) {
473 $result[$itemName] = $value;
474 $this->metadataArray[$itemName] = $value;
494 if ( !$this->metadataArray && !$this->metadataBlobs ) {
496 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
497 $s = $this->getJsonMetadata();
501 if ( !is_string( $s ) ) {
502 throw new RuntimeException(
'Could not serialize image metadata value for DB' );
513 private function getJsonMetadata() {
516 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
520 if ( $this->metadataBlobs ) {
521 $envelope[
'blobs'] = $this->metadataBlobs;
524 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
527 $this->metadataBlobs += $blobAddresses;
552 $this->extraDataLoaded =
true;
553 $this->metadataArray = [];
554 $this->metadataBlobs = [];
555 $this->unloadedMetadataBlobs = [];
556 $metadataString = (string)$metadataString;
557 if ( $metadataString ===
'' ) {
558 $this->metadataSerializationFormat = self::MDS_EMPTY;
561 if ( $metadataString[0] ===
'{' ) {
562 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
565 $this->metadataArray = [
'_error' => $metadataString ];
566 $this->metadataSerializationFormat = self::MDS_LEGACY;
568 $this->metadataSerializationFormat = self::MDS_JSON;
569 if ( isset( $envelope[
'data'] ) ) {
570 $this->metadataArray = $envelope[
'data'];
572 if ( isset( $envelope[
'blobs'] ) ) {
573 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope[
'blobs'];
578 $data = @unserialize( $metadataString );
579 if ( !is_array( $data ) ) {
581 $data = [
'_error' => $metadataString ];
582 $this->metadataSerializationFormat = self::MDS_LEGACY;
584 $this->metadataSerializationFormat = self::MDS_PHP;
586 $this->metadataArray = $data;
623 private function getHandler() {
624 $this->handler ??= MediaHandler::getHandler( $this->getMimeType() );
625 return $this->handler;
638 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
640 $this->
pageCount = $this->handler->pageCount( $this );
646 return $this->pageCount;
657 return $this->media_type;
698 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan(
File::DELETED_USER, $performer ) ) {
721 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan(
File::DELETED_COMMENT, $performer ) ) {
724 return $this->description;
735 return $this->deleted;
747 return ( $this->deleted & $field ) == $field;
761 return RevisionRecord::userCanBitfield(
771class_alias( ArchivedFile::class,
'ArchivedFile' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title