d9763c64b42c50bd15cd2b581e46d7d5a440c172
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 User|null Uploader */
69 /** @var string Time of upload */
72 /** @var bool Whether or not all this has been loaded from the database (loadFromXxx) */
75 /** @var int Bitfield akin to rev_deleted */
78 /** @var string SHA-1 hash of file content */
81 /** @var int|false Number of pages of a multipage document, or false for
82 * documents which aren't multipage documents
86 /** @var string Original base filename */
87 private $archive_name;
89 /** @var MediaHandler */
93 protected $title; # image title
100 * @param string $sha1
102 function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
104 $this->title
= false;
106 $this->group
= 'deleted'; // needed for direct use of constructor
112 $this->metadata
= '';
113 $this->mime
= "unknown/unknown";
114 $this->media_type
= '';
115 $this->description
= '';
117 $this->timestamp
= null;
119 $this->dataLoaded
= false;
120 $this->exists
= false;
123 if ( $title instanceof Title
) {
124 $this->title
= File
::normalizeTitle( $title, 'exception' );
125 $this->name
= $title->getDBkey();
140 if ( !$id && !$key && !( $title instanceof Title
) && !$sha1 ) {
141 throw new MWException( "No specifications provided to ArchivedFile constructor." );
146 * Loads a file object from the filearchive table
147 * @throws MWException
148 * @return bool|null True on success or null
150 public function load() {
151 if ( $this->dataLoaded
) {
156 if ( $this->id
> 0 ) {
157 $conds['fa_id'] = $this->id
;
160 $conds['fa_storage_group'] = $this->group
;
161 $conds['fa_storage_key'] = $this->key
;
163 if ( $this->title
) {
164 $conds['fa_name'] = $this->title
->getDBkey();
167 $conds['fa_sha1'] = $this->sha1
;
170 if ( !count( $conds ) ) {
171 throw new MWException( "No specific information for retrieving archived file" );
174 if ( !$this->title ||
$this->title
->getNamespace() == NS_FILE
) {
175 $this->dataLoaded
= true; // set it here, to have also true on miss
176 $dbr = wfGetDB( DB_REPLICA
);
177 $fileQuery = self
::getQueryInfo();
178 $row = $dbr->selectRow(
179 $fileQuery['tables'],
180 $fileQuery['fields'],
183 [ 'ORDER BY' => 'fa_timestamp DESC' ],
187 // this revision does not exist?
191 // initialize fields for filestore image object
192 $this->loadFromRow( $row );
194 throw new MWException( 'This title does not correspond to an image page.' );
196 $this->exists
= true;
202 * Loads a file object from the filearchive table
204 * @param stdClass $row
205 * @return ArchivedFile
207 public static function newFromRow( $row ) {
208 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
209 $file->loadFromRow( $row );
215 * Fields in the filearchive table
216 * @deprecated since 1.31, use self::getQueryInfo() instead.
219 static function selectFields() {
220 global $wgActorTableSchemaMigrationStage;
222 if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
223 // If code is using this instead of self::getQueryInfo(), there's a
224 // decent chance it's going to try to directly access
225 // $row->fa_user or $row->fa_user_text and we can't give it
226 // useful values here once those aren't being written anymore.
227 throw new BadMethodCallException(
228 'Cannot use ' . __METHOD__
. ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
232 wfDeprecated( __METHOD__
, '1.31' );
249 'fa_actor' => $wgActorTableSchemaMigrationStage > MIGRATION_OLD ?
'fa_actor' : null,
252 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
254 ] + CommentStore
::getStore()->getFields( 'fa_description' );
258 * Return the tables, fields, and join conditions to be selected to create
259 * a new archivedfile object.
261 * @return array[] With three keys:
262 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
263 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
264 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
266 public static function getQueryInfo() {
267 $commentQuery = CommentStore
::getStore()->getJoin( 'fa_description' );
268 $actorQuery = ActorMigration
::newMigration()->getJoin( 'fa_user' );
270 'tables' => [ 'filearchive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
287 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
289 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
290 'joins' => $commentQuery['joins'] +
$actorQuery['joins'],
295 * Load ArchivedFile object fields from a DB row.
297 * @param stdClass $row Object database row
300 public function loadFromRow( $row ) {
301 $this->id
= intval( $row->fa_id
);
302 $this->name
= $row->fa_name
;
303 $this->archive_name
= $row->fa_archive_name
;
304 $this->group
= $row->fa_storage_group
;
305 $this->key
= $row->fa_storage_key
;
306 $this->size
= $row->fa_size
;
307 $this->bits
= $row->fa_bits
;
308 $this->width
= $row->fa_width
;
309 $this->height
= $row->fa_height
;
310 $this->metadata
= $row->fa_metadata
;
311 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
312 $this->media_type
= $row->fa_media_type
;
313 $this->description
= CommentStore
::getStore()
314 // Legacy because $row may have come from self::selectFields()
315 ->getCommentLegacy( wfGetDB( DB_REPLICA
), 'fa_description', $row )->text
;
316 $this->user
= User
::newFromAnyId( $row->fa_user
, $row->fa_user_text
, $row->fa_actor
);
317 $this->timestamp
= $row->fa_timestamp
;
318 $this->deleted
= $row->fa_deleted
;
319 if ( isset( $row->fa_sha1
) ) {
320 $this->sha1
= $row->fa_sha1
;
322 // old row, populate from key
323 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
325 if ( !$this->title
) {
326 $this->title
= Title
::makeTitleSafe( NS_FILE
, $row->fa_name
);
331 * Return the associated title object
335 public function getTitle() {
336 if ( !$this->title
) {
343 * Return the file name
347 public function getName() {
348 if ( $this->name
=== false ) {
358 public function getID() {
367 public function exists() {
370 return $this->exists
;
374 * Return the FileStore key
377 public function getKey() {
384 * Return the FileStore key (overriding base File class)
387 public function getStorageKey() {
388 return $this->getKey();
392 * Return the FileStore storage group
395 public function getGroup() {
400 * Return the width of the image
403 public function getWidth() {
410 * Return the height of the image
413 public function getHeight() {
416 return $this->height
;
420 * Get handler-specific metadata
423 public function getMetadata() {
426 return $this->metadata
;
430 * Return the size of the image file, in bytes
433 public function getSize() {
440 * Return the bits of the image file, in bytes
443 public function getBits() {
450 * Returns the MIME type of the file.
453 public function getMimeType() {
460 * Get a MediaHandler instance for this file
461 * @return MediaHandler
463 function getHandler() {
464 if ( !isset( $this->handler
) ) {
465 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
468 return $this->handler
;
472 * Returns the number of pages of a multipage document, or false for
473 * documents which aren't multipage documents
476 function pageCount() {
477 if ( !isset( $this->pageCount
) ) {
478 // @FIXME: callers expect File objects
479 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
480 $this->pageCount
= $this->handler
->pageCount( $this );
482 $this->pageCount
= false;
486 return $this->pageCount
;
490 * Return the type of the media in the file.
491 * Use the value returned by this function with the MEDIATYPE_xxx constants.
494 public function getMediaType() {
497 return $this->media_type
;
501 * Return upload timestamp.
505 public function getTimestamp() {
508 return wfTimestamp( TS_MW
, $this->timestamp
);
512 * Get the SHA-1 base 36 hash of the file
524 * Returns ID or name of user who uploaded the file
526 * @note Prior to MediaWiki 1.23, this method always
527 * returned the user id, and was inconsistent with
528 * the rest of the file classes.
529 * @param string $type 'text', 'id', or 'object'
530 * @return int|string|User|null
531 * @throws MWException
532 * @since 1.31 added 'object'
534 public function getUser( $type = 'text' ) {
537 if ( $type === 'object' ) {
539 } elseif ( $type === 'text' ) {
540 return $this->user ?
$this->user
->getName() : '';
541 } elseif ( $type === 'id' ) {
542 return $this->user ?
$this->user
->getId() : 0;
545 throw new MWException( "Unknown type '$type'." );
549 * Return upload description.
553 public function getDescription() {
555 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
558 return $this->description
;
563 * Return the user ID of the uploader.
567 public function getRawUser() {
568 return $this->getUser( 'id' );
572 * Return the user name of the uploader.
576 public function getRawUserText() {
577 return $this->getUser( 'text' );
581 * Return upload description.
585 public function getRawDescription() {
588 return $this->description
;
592 * Returns the deletion bitfield
595 public function getVisibility() {
598 return $this->deleted
;
602 * for file or revision rows
604 * @param int $field One of DELETED_* bitfield constants
607 public function isDeleted( $field ) {
610 return ( $this->deleted
& $field ) == $field;
614 * Determine if the current user is allowed to view a particular
615 * field of this FileStore image file, if it's marked as deleted.
617 * @param null|User $user User object to check, or null to use $wgUser
620 public function userCan( $field, User
$user = null ) {
623 $title = $this->getTitle();
624 return Revision
::userCanBitfield( $this->deleted
, $field, $user, $title ?
: null );