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
102 * @param string $sha1
104 function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
106 $this->title
= false;
108 $this->group
= 'deleted'; // needed for direct use of constructor
114 $this->metadata
= '';
115 $this->mime
= "unknown/unknown";
116 $this->media_type
= '';
117 $this->description
= '';
119 $this->timestamp
= null;
121 $this->dataLoaded
= false;
122 $this->exists
= false;
125 if ( $title instanceof Title
) {
126 $this->title
= File
::normalizeTitle( $title, 'exception' );
127 $this->name
= $title->getDBkey();
142 if ( !$id && !$key && !( $title instanceof Title
) && !$sha1 ) {
143 throw new MWException( "No specifications provided to ArchivedFile constructor." );
148 * Loads a file object from the filearchive table
149 * @throws MWException
150 * @return bool|null True on success or null
152 public function load() {
153 if ( $this->dataLoaded
) {
158 if ( $this->id
> 0 ) {
159 $conds['fa_id'] = $this->id
;
162 $conds['fa_storage_group'] = $this->group
;
163 $conds['fa_storage_key'] = $this->key
;
165 if ( $this->title
) {
166 $conds['fa_name'] = $this->title
->getDBkey();
169 $conds['fa_sha1'] = $this->sha1
;
172 if ( $conds === [] ) {
173 throw new MWException( "No specific information for retrieving archived file" );
176 if ( !$this->title ||
$this->title
->getNamespace() == NS_FILE
) {
177 $this->dataLoaded
= true; // set it here, to have also true on miss
178 $dbr = wfGetDB( DB_REPLICA
);
179 $fileQuery = self
::getQueryInfo();
180 $row = $dbr->selectRow(
181 $fileQuery['tables'],
182 $fileQuery['fields'],
185 [ 'ORDER BY' => 'fa_timestamp DESC' ],
189 // this revision does not exist?
193 // initialize fields for filestore image object
194 $this->loadFromRow( $row );
196 throw new MWException( 'This title does not correspond to an image page.' );
198 $this->exists
= true;
204 * Loads a file object from the filearchive table
206 * @param stdClass $row
207 * @return ArchivedFile
209 public static function newFromRow( $row ) {
210 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
211 $file->loadFromRow( $row );
217 * Fields in the filearchive table
218 * @deprecated since 1.31, use self::getQueryInfo() instead.
221 static function selectFields() {
222 global $wgActorTableSchemaMigrationStage;
224 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
225 // If code is using this instead of self::getQueryInfo(), there's a
226 // decent chance it's going to try to directly access
227 // $row->fa_user or $row->fa_user_text and we can't give it
228 // useful values here once those aren't being used anymore.
229 throw new BadMethodCallException(
230 'Cannot use ' . __METHOD__
231 . ' when $wgActorTableSchemaMigrationStage has SCHEMA_COMPAT_READ_NEW'
235 wfDeprecated( __METHOD__
, '1.31' );
252 'fa_actor' => 'NULL',
255 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
257 ] + MediaWikiServices
::getInstance()->getCommentStore()->getFields( 'fa_description' );
261 * Return the tables, fields, and join conditions to be selected to create
262 * a new archivedfile object.
264 * @return array[] With three keys:
265 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
266 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
267 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
269 public static function getQueryInfo() {
270 $commentQuery = MediaWikiServices
::getInstance()->getCommentStore()->getJoin( 'fa_description' );
271 $actorQuery = ActorMigration
::newMigration()->getJoin( 'fa_user' );
273 'tables' => [ 'filearchive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
290 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
292 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
293 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
298 * Load ArchivedFile object fields from a DB row.
300 * @param stdClass $row Object database row
303 public function loadFromRow( $row ) {
304 $this->id
= intval( $row->fa_id
);
305 $this->name
= $row->fa_name
;
306 $this->archive_name
= $row->fa_archive_name
;
307 $this->group
= $row->fa_storage_group
;
308 $this->key
= $row->fa_storage_key
;
309 $this->size
= $row->fa_size
;
310 $this->bits
= $row->fa_bits
;
311 $this->width
= $row->fa_width
;
312 $this->height
= $row->fa_height
;
313 $this->metadata
= $row->fa_metadata
;
314 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
315 $this->media_type
= $row->fa_media_type
;
316 $this->description
= MediaWikiServices
::getInstance()->getCommentStore()
317 // Legacy because $row may have come from self::selectFields()
318 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'fa_description', $row )->text
;
319 $this->user
= User
::newFromAnyId( $row->fa_user
, $row->fa_user_text
, $row->fa_actor
);
320 $this->timestamp
= $row->fa_timestamp
;
321 $this->deleted
= $row->fa_deleted
;
322 if ( isset( $row->fa_sha1
) ) {
323 $this->sha1
= $row->fa_sha1
;
325 // old row, populate from key
326 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
328 if ( !$this->title
) {
329 $this->title
= Title
::makeTitleSafe( NS_FILE
, $row->fa_name
);
334 * Return the associated title object
338 public function getTitle() {
339 if ( !$this->title
) {
346 * Return the file name
350 public function getName() {
351 if ( $this->name
=== false ) {
361 public function getID() {
370 public function exists() {
373 return $this->exists
;
377 * Return the FileStore key
380 public function getKey() {
387 * Return the FileStore key (overriding base File class)
390 public function getStorageKey() {
391 return $this->getKey();
395 * Return the FileStore storage group
398 public function getGroup() {
403 * Return the width of the image
406 public function getWidth() {
413 * Return the height of the image
416 public function getHeight() {
419 return $this->height
;
423 * Get handler-specific metadata
426 public function getMetadata() {
429 return $this->metadata
;
433 * Return the size of the image file, in bytes
436 public function getSize() {
443 * Return the bits of the image file, in bytes
446 public function getBits() {
453 * Returns the MIME type of the file.
456 public function getMimeType() {
463 * Get a MediaHandler instance for this file
464 * @return MediaHandler
466 function getHandler() {
467 if ( !isset( $this->handler
) ) {
468 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
471 return $this->handler
;
475 * Returns the number of pages of a multipage document, or false for
476 * documents which aren't multipage documents
479 function pageCount() {
480 if ( !isset( $this->pageCount
) ) {
481 // @FIXME: callers expect File objects
482 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
483 $this->pageCount
= $this->handler
->pageCount( $this );
485 $this->pageCount
= false;
489 return $this->pageCount
;
493 * Return the type of the media in the file.
494 * Use the value returned by this function with the MEDIATYPE_xxx constants.
497 public function getMediaType() {
500 return $this->media_type
;
504 * Return upload timestamp.
508 public function getTimestamp() {
511 return wfTimestamp( TS_MW
, $this->timestamp
);
515 * Get the SHA-1 base 36 hash of the file
527 * Returns ID or name of user who uploaded the file
529 * @note Prior to MediaWiki 1.23, this method always
530 * returned the user id, and was inconsistent with
531 * the rest of the file classes.
532 * @param string $type 'text', 'id', or 'object'
533 * @return int|string|User|null
534 * @throws MWException
535 * @since 1.31 added 'object'
537 public function getUser( $type = 'text' ) {
540 if ( $type === 'object' ) {
542 } elseif ( $type === 'text' ) {
543 return $this->user ?
$this->user
->getName() : '';
544 } elseif ( $type === 'id' ) {
545 return $this->user ?
$this->user
->getId() : 0;
548 throw new MWException( "Unknown type '$type'." );
552 * Return upload description.
556 public function getDescription() {
558 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
561 return $this->description
;
566 * Return the user ID of the uploader.
570 public function getRawUser() {
571 return $this->getUser( 'id' );
575 * Return the user name of the uploader.
579 public function getRawUserText() {
580 return $this->getUser( 'text' );
584 * Return upload description.
588 public function getRawDescription() {
591 return $this->description
;
595 * Returns the deletion bitfield
598 public function getVisibility() {
601 return $this->deleted
;
605 * for file or revision rows
607 * @param int $field One of DELETED_* bitfield constants
610 public function isDeleted( $field ) {
613 return ( $this->deleted
& $field ) == $field;
617 * Determine if the current user is allowed to view a particular
618 * field of this FileStore image file, if it's marked as deleted.
620 * @param null|User $user User object to check, or null to use $wgUser
623 public function userCan( $field, User
$user = null ) {
626 $title = $this->getTitle();
627 return Revision
::userCanBitfield( $this->deleted
, $field, $user, $title ?
: null );