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
25 * Class representing a row of the 'filearchive' table
27 * @ingroup FileAbstraction
30 /** @var int Filearchive row ID */
33 /** @var string File name */
36 /** @var string FileStore storage group */
39 /** @var string FileStore SHA-1 key */
42 /** @var int File size in bytes */
45 /** @var int Size in bytes */
51 /** @var int Height */
54 /** @var string Metadata string */
57 /** @var string MIME type */
60 /** @var string Media type */
63 /** @var string Upload description */
66 /** @var int User ID of uploader */
69 /** @var string User name of uploader */
72 /** @var string Time of upload */
75 /** @var bool Whether or not all this has been loaded from the database (loadFromXxx) */
78 /** @var int Bitfield akin to rev_deleted */
81 /** @var string SHA-1 hash of file content */
84 /** @var int|false Number of pages of a multipage document, or false for
85 * documents which aren't multipage documents
89 /** @var string Original base filename */
90 private $archive_name;
92 /** @var MediaHandler */
96 protected $title; # image title
100 * @param Title $title
103 * @param string $sha1
105 function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
107 $this->title
= false;
109 $this->group
= 'deleted'; // needed for direct use of constructor
115 $this->metadata
= '';
116 $this->mime
= "unknown/unknown";
117 $this->media_type
= '';
118 $this->description
= '';
120 $this->user_text
= '';
121 $this->timestamp
= null;
123 $this->dataLoaded
= false;
124 $this->exists
= false;
127 if ( $title instanceof Title
) {
128 $this->title
= File
::normalizeTitle( $title, 'exception' );
129 $this->name
= $title->getDBkey();
144 if ( !$id && !$key && !( $title instanceof Title
) && !$sha1 ) {
145 throw new MWException( "No specifications provided to ArchivedFile constructor." );
150 * Loads a file object from the filearchive table
151 * @throws MWException
152 * @return bool|null True on success or null
154 public function load() {
155 if ( $this->dataLoaded
) {
160 if ( $this->id
> 0 ) {
161 $conds['fa_id'] = $this->id
;
164 $conds['fa_storage_group'] = $this->group
;
165 $conds['fa_storage_key'] = $this->key
;
167 if ( $this->title
) {
168 $conds['fa_name'] = $this->title
->getDBkey();
171 $conds['fa_sha1'] = $this->sha1
;
174 if ( !count( $conds ) ) {
175 throw new MWException( "No specific information for retrieving archived file" );
178 if ( !$this->title ||
$this->title
->getNamespace() == NS_FILE
) {
179 $this->dataLoaded
= true; // set it here, to have also true on miss
180 $dbr = wfGetDB( DB_REPLICA
);
181 $fileQuery = self
::getQueryInfo();
182 $row = $dbr->selectRow(
183 $fileQuery['tables'],
184 $fileQuery['fields'],
187 [ 'ORDER BY' => 'fa_timestamp DESC' ],
191 // this revision does not exist?
195 // initialize fields for filestore image object
196 $this->loadFromRow( $row );
198 throw new MWException( 'This title does not correspond to an image page.' );
200 $this->exists
= true;
206 * Loads a file object from the filearchive table
208 * @param stdClass $row
209 * @return ArchivedFile
211 public static function newFromRow( $row ) {
212 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
213 $file->loadFromRow( $row );
219 * Fields in the filearchive table
220 * @deprecated since 1.31, use self::getQueryInfo() instead.
223 static function selectFields() {
224 wfDeprecated( __METHOD__
, '1.31' );
243 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
245 ] + CommentStore
::getStore()->getFields( 'fa_description' );
249 * Return the tables, fields, and join conditions to be selected to create
250 * a new archivedfile object.
252 * @return array With three keys:
253 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
254 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
255 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
257 public static function getQueryInfo() {
258 $commentQuery = CommentStore
::getStore()->getJoin( 'fa_description' );
260 'tables' => [ 'filearchive' ] +
$commentQuery['tables'],
279 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
281 ] +
$commentQuery['fields'],
282 'joins' => $commentQuery['joins'],
287 * Load ArchivedFile object fields from a DB row.
289 * @param stdClass $row Object database row
292 public function loadFromRow( $row ) {
293 $this->id
= intval( $row->fa_id
);
294 $this->name
= $row->fa_name
;
295 $this->archive_name
= $row->fa_archive_name
;
296 $this->group
= $row->fa_storage_group
;
297 $this->key
= $row->fa_storage_key
;
298 $this->size
= $row->fa_size
;
299 $this->bits
= $row->fa_bits
;
300 $this->width
= $row->fa_width
;
301 $this->height
= $row->fa_height
;
302 $this->metadata
= $row->fa_metadata
;
303 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
304 $this->media_type
= $row->fa_media_type
;
305 $this->description
= CommentStore
::getStore()
306 // Legacy because $row may have come from self::selectFields()
307 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'fa_description', $row )->text
;
308 $this->user
= $row->fa_user
;
309 $this->user_text
= $row->fa_user_text
;
310 $this->timestamp
= $row->fa_timestamp
;
311 $this->deleted
= $row->fa_deleted
;
312 if ( isset( $row->fa_sha1
) ) {
313 $this->sha1
= $row->fa_sha1
;
315 // old row, populate from key
316 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
318 if ( !$this->title
) {
319 $this->title
= Title
::makeTitleSafe( NS_FILE
, $row->fa_name
);
324 * Return the associated title object
328 public function getTitle() {
329 if ( !$this->title
) {
336 * Return the file name
340 public function getName() {
341 if ( $this->name
=== false ) {
351 public function getID() {
360 public function exists() {
363 return $this->exists
;
367 * Return the FileStore key
370 public function getKey() {
377 * Return the FileStore key (overriding base File class)
380 public function getStorageKey() {
381 return $this->getKey();
385 * Return the FileStore storage group
388 public function getGroup() {
393 * Return the width of the image
396 public function getWidth() {
403 * Return the height of the image
406 public function getHeight() {
409 return $this->height
;
413 * Get handler-specific metadata
416 public function getMetadata() {
419 return $this->metadata
;
423 * Return the size of the image file, in bytes
426 public function getSize() {
433 * Return the bits of the image file, in bytes
436 public function getBits() {
443 * Returns the MIME type of the file.
446 public function getMimeType() {
453 * Get a MediaHandler instance for this file
454 * @return MediaHandler
456 function getHandler() {
457 if ( !isset( $this->handler
) ) {
458 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
461 return $this->handler
;
465 * Returns the number of pages of a multipage document, or false for
466 * documents which aren't multipage documents
469 function pageCount() {
470 if ( !isset( $this->pageCount
) ) {
471 // @FIXME: callers expect File objects
472 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
473 $this->pageCount
= $this->handler
->pageCount( $this );
475 $this->pageCount
= false;
479 return $this->pageCount
;
483 * Return the type of the media in the file.
484 * Use the value returned by this function with the MEDIATYPE_xxx constants.
487 public function getMediaType() {
490 return $this->media_type
;
494 * Return upload timestamp.
498 public function getTimestamp() {
501 return wfTimestamp( TS_MW
, $this->timestamp
);
505 * Get the SHA-1 base 36 hash of the file
517 * Returns ID or name of user who uploaded the file
519 * @note Prior to MediaWiki 1.23, this method always
520 * returned the user id, and was inconsistent with
521 * the rest of the file classes.
522 * @param string $type 'text' or 'id'
524 * @throws MWException
526 public function getUser( $type = 'text' ) {
529 if ( $type == 'text' ) {
530 return $this->user_text
;
531 } elseif ( $type == 'id' ) {
532 return (int)$this->user
;
535 throw new MWException( "Unknown type '$type'." );
539 * Return upload description.
543 public function getDescription() {
545 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
548 return $this->description
;
553 * Return the user ID of the uploader.
557 public function getRawUser() {
564 * Return the user name of the uploader.
568 public function getRawUserText() {
571 return $this->user_text
;
575 * Return upload description.
579 public function getRawDescription() {
582 return $this->description
;
586 * Returns the deletion bitfield
589 public function getVisibility() {
592 return $this->deleted
;
596 * for file or revision rows
598 * @param int $field One of DELETED_* bitfield constants
601 public function isDeleted( $field ) {
604 return ( $this->deleted
& $field ) == $field;
608 * Determine if the current user is allowed to view a particular
609 * field of this FileStore image file, if it's marked as deleted.
611 * @param null|User $user User object to check, or null to use $wgUser
614 public function userCan( $field, User
$user = null ) {
617 $title = $this->getTitle();
618 return Revision
::userCanBitfield( $this->deleted
, $field, $user, $title ?
: null );