3 * Deleted file in the 'filearchive' table.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileAbstraction
24 use MediaWiki\MediaWikiServices
;
27 * Class representing a row of the 'filearchive' table
29 * @ingroup FileAbstraction
32 /** @var int Filearchive row ID */
35 /** @var string File name */
38 /** @var string FileStore storage group */
41 /** @var string FileStore SHA-1 key */
44 /** @var int File size in bytes */
47 /** @var int Size in bytes */
53 /** @var int Height */
56 /** @var string Metadata string */
59 /** @var string MIME type */
62 /** @var string Media type */
65 /** @var string Upload description */
68 /** @var User|null Uploader */
71 /** @var string Time of upload */
74 /** @var bool Whether or not all this has been loaded from the database (loadFromXxx) */
77 /** @var int Bitfield akin to rev_deleted */
80 /** @var string SHA-1 hash of file content */
83 /** @var int|false Number of pages of a multipage document, or false for
84 * documents which aren't multipage documents
88 /** @var string Original base filename */
89 private $archive_name;
91 /** @var MediaHandler */
95 protected $title; # image title
101 * @throws MWException
102 * @param Title $title
105 * @param string $sha1
107 function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
109 $this->title
= false;
111 $this->group
= 'deleted'; // needed for direct use of constructor
117 $this->metadata
= '';
118 $this->mime
= "unknown/unknown";
119 $this->media_type
= '';
120 $this->description
= '';
122 $this->timestamp
= null;
124 $this->dataLoaded
= false;
125 $this->exists
= false;
128 if ( $title instanceof Title
) {
129 $this->title
= File
::normalizeTitle( $title, 'exception' );
130 $this->name
= $title->getDBkey();
145 if ( !$id && !$key && !( $title instanceof Title
) && !$sha1 ) {
146 throw new MWException( "No specifications provided to ArchivedFile constructor." );
151 * Loads a file object from the filearchive table
152 * @throws MWException
153 * @return bool|null True on success or null
155 public function load() {
156 if ( $this->dataLoaded
) {
161 if ( $this->id
> 0 ) {
162 $conds['fa_id'] = $this->id
;
165 $conds['fa_storage_group'] = $this->group
;
166 $conds['fa_storage_key'] = $this->key
;
168 if ( $this->title
) {
169 $conds['fa_name'] = $this->title
->getDBkey();
172 $conds['fa_sha1'] = $this->sha1
;
175 if ( $conds === [] ) {
176 throw new MWException( "No specific information for retrieving archived file" );
179 if ( !$this->title ||
$this->title
->getNamespace() == NS_FILE
) {
180 $this->dataLoaded
= true; // set it here, to have also true on miss
181 $dbr = wfGetDB( DB_REPLICA
);
182 $fileQuery = self
::getQueryInfo();
183 $row = $dbr->selectRow(
184 $fileQuery['tables'],
185 $fileQuery['fields'],
188 [ 'ORDER BY' => 'fa_timestamp DESC' ],
192 // this revision does not exist?
196 // initialize fields for filestore image object
197 $this->loadFromRow( $row );
199 throw new MWException( 'This title does not correspond to an image page.' );
201 $this->exists
= true;
207 * Loads a file object from the filearchive table
209 * @param stdClass $row
210 * @return ArchivedFile
212 public static function newFromRow( $row ) {
213 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
214 $file->loadFromRow( $row );
220 * Return the tables, fields, and join conditions to be selected to create
221 * a new archivedfile object.
223 * @return array[] With three keys:
224 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
225 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
226 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
228 public static function getQueryInfo() {
229 $commentQuery = MediaWikiServices
::getInstance()->getCommentStore()->getJoin( 'fa_description' );
230 $actorQuery = ActorMigration
::newMigration()->getJoin( 'fa_user' );
232 'tables' => [ 'filearchive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
249 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
251 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
252 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
257 * Load ArchivedFile object fields from a DB row.
259 * @param stdClass $row Object database row
262 public function loadFromRow( $row ) {
263 $this->id
= intval( $row->fa_id
);
264 $this->name
= $row->fa_name
;
265 $this->archive_name
= $row->fa_archive_name
;
266 $this->group
= $row->fa_storage_group
;
267 $this->key
= $row->fa_storage_key
;
268 $this->size
= $row->fa_size
;
269 $this->bits
= $row->fa_bits
;
270 $this->width
= $row->fa_width
;
271 $this->height
= $row->fa_height
;
272 $this->metadata
= $row->fa_metadata
;
273 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
274 $this->media_type
= $row->fa_media_type
;
275 $this->description
= MediaWikiServices
::getInstance()->getCommentStore()
276 // Legacy because $row may have come from self::selectFields()
277 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'fa_description', $row )->text
;
278 $this->user
= User
::newFromAnyId( $row->fa_user
, $row->fa_user_text
, $row->fa_actor
);
279 $this->timestamp
= $row->fa_timestamp
;
280 $this->deleted
= $row->fa_deleted
;
281 if ( isset( $row->fa_sha1
) ) {
282 $this->sha1
= $row->fa_sha1
;
284 // old row, populate from key
285 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
287 if ( !$this->title
) {
288 $this->title
= Title
::makeTitleSafe( NS_FILE
, $row->fa_name
);
293 * Return the associated title object
297 public function getTitle() {
298 if ( !$this->title
) {
305 * Return the file name
309 public function getName() {
310 if ( $this->name
=== false ) {
320 public function getID() {
329 public function exists() {
332 return $this->exists
;
336 * Return the FileStore key
339 public function getKey() {
346 * Return the FileStore key (overriding base File class)
349 public function getStorageKey() {
350 return $this->getKey();
354 * Return the FileStore storage group
357 public function getGroup() {
362 * Return the width of the image
365 public function getWidth() {
372 * Return the height of the image
375 public function getHeight() {
378 return $this->height
;
382 * Get handler-specific metadata
385 public function getMetadata() {
388 return $this->metadata
;
392 * Return the size of the image file, in bytes
395 public function getSize() {
402 * Return the bits of the image file, in bytes
405 public function getBits() {
412 * Returns the MIME type of the file.
415 public function getMimeType() {
422 * Get a MediaHandler instance for this file
423 * @return MediaHandler
425 function getHandler() {
426 if ( !isset( $this->handler
) ) {
427 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
430 return $this->handler
;
434 * Returns the number of pages of a multipage document, or false for
435 * documents which aren't multipage documents
438 function pageCount() {
439 if ( !isset( $this->pageCount
) ) {
440 // @FIXME: callers expect File objects
441 // @phan-suppress-next-line PhanTypeMismatchArgument
442 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
443 // @phan-suppress-next-line PhanTypeMismatchArgument
444 $this->pageCount
= $this->handler
->pageCount( $this );
446 $this->pageCount
= false;
450 return $this->pageCount
;
454 * Return the type of the media in the file.
455 * Use the value returned by this function with the MEDIATYPE_xxx constants.
458 public function getMediaType() {
461 return $this->media_type
;
465 * Return upload timestamp.
469 public function getTimestamp() {
472 return wfTimestamp( TS_MW
, $this->timestamp
);
476 * Get the SHA-1 base 36 hash of the file
488 * Returns ID or name of user who uploaded the file
490 * @note Prior to MediaWiki 1.23, this method always
491 * returned the user id, and was inconsistent with
492 * the rest of the file classes.
493 * @param string $type 'text', 'id', or 'object'
494 * @return int|string|User|null
495 * @throws MWException
496 * @since 1.31 added 'object'
498 public function getUser( $type = 'text' ) {
501 if ( $type === 'object' ) {
503 } elseif ( $type === 'text' ) {
504 return $this->user ?
$this->user
->getName() : '';
505 } elseif ( $type === 'id' ) {
506 return $this->user ?
$this->user
->getId() : 0;
509 throw new MWException( "Unknown type '$type'." );
513 * Return upload description.
517 public function getDescription() {
519 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
522 return $this->description
;
527 * Return the user ID of the uploader.
531 public function getRawUser() {
532 return $this->getUser( 'id' );
536 * Return the user name of the uploader.
540 public function getRawUserText() {
541 return $this->getUser( 'text' );
545 * Return upload description.
549 public function getRawDescription() {
552 return $this->description
;
556 * Returns the deletion bitfield
559 public function getVisibility() {
562 return $this->deleted
;
566 * for file or revision rows
568 * @param int $field One of DELETED_* bitfield constants
571 public function isDeleted( $field ) {
574 return ( $this->deleted
& $field ) == $field;
578 * Determine if the current user is allowed to view a particular
579 * field of this FileStore image file, if it's marked as deleted.
581 * @param null|User $user User object to check, or null to use $wgUser
584 public function userCan( $field, User
$user = null ) {
587 $title = $this->getTitle();
588 return Revision
::userCanBitfield( $this->deleted
, $field, $user, $title ?
: null );