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 $row = $dbr->selectRow(
183 self
::selectFields(),
186 [ '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
220 static function selectFields() {
240 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
246 * Load ArchivedFile object fields from a DB row.
248 * @param stdClass $row Object database row
251 public function loadFromRow( $row ) {
252 $this->id
= intval( $row->fa_id
);
253 $this->name
= $row->fa_name
;
254 $this->archive_name
= $row->fa_archive_name
;
255 $this->group
= $row->fa_storage_group
;
256 $this->key
= $row->fa_storage_key
;
257 $this->size
= $row->fa_size
;
258 $this->bits
= $row->fa_bits
;
259 $this->width
= $row->fa_width
;
260 $this->height
= $row->fa_height
;
261 $this->metadata
= $row->fa_metadata
;
262 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
263 $this->media_type
= $row->fa_media_type
;
264 $this->description
= $row->fa_description
;
265 $this->user
= $row->fa_user
;
266 $this->user_text
= $row->fa_user_text
;
267 $this->timestamp
= $row->fa_timestamp
;
268 $this->deleted
= $row->fa_deleted
;
269 if ( isset( $row->fa_sha1
) ) {
270 $this->sha1
= $row->fa_sha1
;
272 // old row, populate from key
273 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
275 if ( !$this->title
) {
276 $this->title
= Title
::makeTitleSafe( NS_FILE
, $row->fa_name
);
281 * Return the associated title object
285 public function getTitle() {
286 if ( !$this->title
) {
293 * Return the file name
297 public function getName() {
298 if ( $this->name
=== false ) {
308 public function getID() {
317 public function exists() {
320 return $this->exists
;
324 * Return the FileStore key
327 public function getKey() {
334 * Return the FileStore key (overriding base File class)
337 public function getStorageKey() {
338 return $this->getKey();
342 * Return the FileStore storage group
345 public function getGroup() {
350 * Return the width of the image
353 public function getWidth() {
360 * Return the height of the image
363 public function getHeight() {
366 return $this->height
;
370 * Get handler-specific metadata
373 public function getMetadata() {
376 return $this->metadata
;
380 * Return the size of the image file, in bytes
383 public function getSize() {
390 * Return the bits of the image file, in bytes
393 public function getBits() {
400 * Returns the MIME type of the file.
403 public function getMimeType() {
410 * Get a MediaHandler instance for this file
411 * @return MediaHandler
413 function getHandler() {
414 if ( !isset( $this->handler
) ) {
415 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
418 return $this->handler
;
422 * Returns the number of pages of a multipage document, or false for
423 * documents which aren't multipage documents
426 function pageCount() {
427 if ( !isset( $this->pageCount
) ) {
428 // @FIXME: callers expect File objects
429 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
430 $this->pageCount
= $this->handler
->pageCount( $this );
432 $this->pageCount
= false;
436 return $this->pageCount
;
440 * Return the type of the media in the file.
441 * Use the value returned by this function with the MEDIATYPE_xxx constants.
444 public function getMediaType() {
447 return $this->media_type
;
451 * Return upload timestamp.
455 public function getTimestamp() {
458 return wfTimestamp( TS_MW
, $this->timestamp
);
462 * Get the SHA-1 base 36 hash of the file
474 * Returns ID or name of user who uploaded the file
476 * @note Prior to MediaWiki 1.23, this method always
477 * returned the user id, and was inconsistent with
478 * the rest of the file classes.
479 * @param string $type 'text' or 'id'
481 * @throws MWException
483 public function getUser( $type = 'text' ) {
486 if ( $type == 'text' ) {
487 return $this->user_text
;
488 } elseif ( $type == 'id' ) {
489 return (int)$this->user
;
492 throw new MWException( "Unknown type '$type'." );
496 * Return the user name of the uploader.
498 * @deprecated since 1.23 Use getUser( 'text' ) instead.
501 public function getUserText() {
502 wfDeprecated( __METHOD__
, '1.23' );
504 if ( $this->isDeleted( File
::DELETED_USER
) ) {
507 return $this->user_text
;
512 * Return upload description.
516 public function getDescription() {
518 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
521 return $this->description
;
526 * Return the user ID of the uploader.
530 public function getRawUser() {
537 * Return the user name of the uploader.
541 public function getRawUserText() {
544 return $this->user_text
;
548 * Return upload description.
552 public function getRawDescription() {
555 return $this->description
;
559 * Returns the deletion bitfield
562 public function getVisibility() {
565 return $this->deleted
;
569 * for file or revision rows
571 * @param int $field One of DELETED_* bitfield constants
574 public function isDeleted( $field ) {
577 return ( $this->deleted
& $field ) == $field;
581 * Determine if the current user is allowed to view a particular
582 * field of this FileStore image file, if it's marked as deleted.
584 * @param null|User $user User object to check, or null to use $wgUser
587 public function userCan( $field, User
$user = null ) {
590 $title = $this->getTitle();
591 return Revision
::userCanBitfield( $this->deleted
, $field, $user, $title ?
: null );