23use BadMethodCallException;
33use UnexpectedValueException;
52 private const MDS_EMPTY =
'empty';
55 private const MDS_LEGACY =
'legacy';
58 private const MDS_PHP =
'php';
61 private const MDS_JSON =
'json';
119 private $description;
142 private $archive_name;
157 private $metadataStorageHelper;
170 $this->group =
'deleted';
176 $this->mime =
"unknown/unknown";
177 $this->media_type =
'';
178 $this->description =
'';
180 $this->timestamp =
null;
182 $this->dataLoaded =
false;
203 if ( !$id && !$key && !(
$title instanceof
Title ) && !$sha1 ) {
204 throw new BadMethodCallException(
"No specifications provided to ArchivedFile constructor." );
217 if ( $this->dataLoaded ) {
222 if ( $this->
id > 0 ) {
223 $conds[
'fa_id'] = $this->id;
226 $conds[
'fa_storage_group'] = $this->group;
227 $conds[
'fa_storage_key'] = $this->key;
229 if ( $this->title ) {
230 $conds[
'fa_name'] = $this->title->getDBkey();
233 $conds[
'fa_sha1'] = $this->sha1;
236 if ( $conds === [] ) {
237 throw new RuntimeException(
"No specific information for retrieving archived file" );
240 if ( !$this->title || $this->title->getNamespace() ===
NS_FILE ) {
241 $this->dataLoaded =
true;
242 $dbr = $this->repo->getReplicaDB();
244 $row = $queryBuilder->where( $conds )
245 ->orderBy(
'fa_timestamp', SelectQueryBuilder::SORT_DESC )
246 ->caller( __METHOD__ )->fetchRow();
255 throw new UnexpectedValueException(
'This title does not correspond to an image page.' );
270 $file->loadFromRow( $row );
296 'tables' => $queryInfo[
'tables'],
297 'fields' => $queryInfo[
'fields'],
298 'joins' => $queryInfo[
'join_conds'],
310 $this->
id = intval( $row->fa_id );
311 $this->name = $row->fa_name;
312 $this->archive_name = $row->fa_archive_name;
313 $this->group = $row->fa_storage_group;
314 $this->key = $row->fa_storage_key;
315 $this->size = $row->fa_size;
316 $this->bits = $row->fa_bits;
317 $this->width = $row->fa_width;
318 $this->height = $row->fa_height;
320 $this->repo->getReplicaDB(), $row->fa_metadata );
321 $this->mime =
"$row->fa_major_mime/$row->fa_minor_mime";
322 $this->media_type = $row->fa_media_type;
324 $this->description = $services->getCommentStore()
326 ->getCommentLegacy( $this->repo->getReplicaDB(),
'fa_description', $row )->text;
327 $this->user = $services->getUserFactory()
328 ->newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
329 $this->timestamp = $row->fa_timestamp;
330 $this->deleted = $row->fa_deleted;
331 if ( isset( $row->fa_sha1 ) ) {
332 $this->sha1 = $row->fa_sha1;
337 if ( !$this->title ) {
338 $this->title = Title::makeTitleSafe(
NS_FILE, $row->fa_name );
340 $this->
exists = $row->fa_archive_name !==
'';
349 if ( !$this->title ) {
361 if ( $this->name ===
false ) {
429 return $this->height;
442 } elseif ( array_keys( $data ) === [
'_error' ] ) {
444 return $data[
'_error'];
458 if ( $this->unloadedMetadataBlobs ) {
460 array_unique( array_merge(
461 array_keys( $this->metadataArray ),
462 array_keys( $this->unloadedMetadataBlobs )
473 foreach ( $itemNames as $itemName ) {
474 if ( array_key_exists( $itemName, $this->metadataArray ) ) {
475 $result[$itemName] = $this->metadataArray[$itemName];
476 } elseif ( isset( $this->unloadedMetadataBlobs[$itemName] ) ) {
477 $addresses[$itemName] = $this->unloadedMetadataBlobs[$itemName];
482 $resultFromBlob = $this->metadataStorageHelper->getMetadataFromBlobStore( $addresses );
483 foreach ( $addresses as $itemName => $address ) {
484 unset( $this->unloadedMetadataBlobs[$itemName] );
485 $value = $resultFromBlob[$itemName] ??
null;
486 if ( $value !==
null ) {
487 $result[$itemName] = $value;
488 $this->metadataArray[$itemName] = $value;
508 if ( !$this->metadataArray && !$this->metadataBlobs ) {
510 } elseif ( $this->repo->isJsonMetadataEnabled() ) {
511 $s = $this->getJsonMetadata();
513 $s = serialize( $this->getMetadataArray() );
515 if ( !is_string( $s ) ) {
516 throw new RuntimeException(
'Could not serialize image metadata value for DB' );
527 private function getJsonMetadata() {
530 'data' => array_diff_key( $this->metadataArray, $this->metadataBlobs )
534 if ( $this->metadataBlobs ) {
535 $envelope[
'blobs'] = $this->metadataBlobs;
538 [ $s, $blobAddresses ] = $this->metadataStorageHelper->getJsonMetadata( $this, $envelope );
541 $this->metadataBlobs += $blobAddresses;
555 $this->loadMetadataFromString( $db->
decodeBlob( $metadataBlob ) );
566 $this->extraDataLoaded =
true;
567 $this->metadataArray = [];
568 $this->metadataBlobs = [];
569 $this->unloadedMetadataBlobs = [];
570 $metadataString = (string)$metadataString;
571 if ( $metadataString ===
'' ) {
572 $this->metadataSerializationFormat = self::MDS_EMPTY;
575 if ( $metadataString[0] ===
'{' ) {
576 $envelope = $this->metadataStorageHelper->jsonDecode( $metadataString );
579 $this->metadataArray = [
'_error' => $metadataString ];
580 $this->metadataSerializationFormat = self::MDS_LEGACY;
582 $this->metadataSerializationFormat = self::MDS_JSON;
583 if ( isset( $envelope[
'data'] ) ) {
584 $this->metadataArray = $envelope[
'data'];
586 if ( isset( $envelope[
'blobs'] ) ) {
587 $this->metadataBlobs = $this->unloadedMetadataBlobs = $envelope[
'blobs'];
592 $data = @unserialize( $metadataString );
593 if ( !is_array( $data ) ) {
595 $data = [
'_error' => $metadataString ];
596 $this->metadataSerializationFormat = self::MDS_LEGACY;
598 $this->metadataSerializationFormat = self::MDS_PHP;
600 $this->metadataArray = $data;
637 private function getHandler() {
638 if ( !$this->handler ) {
639 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
642 return $this->handler;
652 if ( $this->pageCount ===
null ) {
655 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
657 $this->pageCount = $this->handler->pageCount( $this );
659 $this->pageCount =
false;
663 return $this->pageCount;
674 return $this->media_type;
713 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_USER ) ) {
715 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_USER, $performer ) ) {
736 if ( $audience === self::FOR_PUBLIC && $this->isDeleted( File::DELETED_COMMENT ) ) {
738 } elseif ( $audience === self::FOR_THIS_USER && !$this->userCan( File::DELETED_COMMENT, $performer ) ) {
741 return $this->description;
752 return $this->deleted;
764 return ( $this->deleted & $field ) == $field;
778 return RevisionRecord::userCanBitfield(
788class_alias( ArchivedFile::class,
'ArchivedFile' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.