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 string 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
104 function __construct( $title, $id = 0, $key = '' ) {
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->user_text
= '';
120 $this->timestamp
= null;
122 $this->dataLoaded
= false;
123 $this->exists
= false;
126 if ( $title instanceof Title
) {
127 $this->title
= File
::normalizeTitle( $title, 'exception' );
128 $this->name
= $title->getDBkey();
139 if ( !$id && !$key && !( $title instanceof Title
) ) {
140 throw new MWException( "No specifications provided to ArchivedFile constructor." );
145 * Loads a file object from the filearchive table
146 * @throws MWException
147 * @return bool|null True on success or null
149 public function load() {
150 if ( $this->dataLoaded
) {
155 if ( $this->id
> 0 ) {
156 $conds['fa_id'] = $this->id
;
159 $conds['fa_storage_group'] = $this->group
;
160 $conds['fa_storage_key'] = $this->key
;
162 if ( $this->title
) {
163 $conds['fa_name'] = $this->title
->getDBkey();
166 if ( !count( $conds ) ) {
167 throw new MWException( "No specific information for retrieving archived file" );
170 if ( !$this->title ||
$this->title
->getNamespace() == NS_FILE
) {
171 $this->dataLoaded
= true; // set it here, to have also true on miss
172 $dbr = wfGetDB( DB_SLAVE
);
173 $row = $dbr->selectRow(
175 self
::selectFields(),
178 array( 'ORDER BY' => 'fa_timestamp DESC' )
181 // this revision does not exist?
185 // initialize fields for filestore image object
186 $this->loadFromRow( $row );
188 throw new MWException( 'This title does not correspond to an image page.' );
190 $this->exists
= true;
196 * Loads a file object from the filearchive table
198 * @param stdClass $row
199 * @return ArchivedFile
201 public static function newFromRow( $row ) {
202 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
203 $file->loadFromRow( $row );
209 * Fields in the filearchive table
212 static function selectFields() {
232 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
238 * Load ArchivedFile object fields from a DB row.
240 * @param stdClass $row Object database row
243 public function loadFromRow( $row ) {
244 $this->id
= intval( $row->fa_id
);
245 $this->name
= $row->fa_name
;
246 $this->archive_name
= $row->fa_archive_name
;
247 $this->group
= $row->fa_storage_group
;
248 $this->key
= $row->fa_storage_key
;
249 $this->size
= $row->fa_size
;
250 $this->bits
= $row->fa_bits
;
251 $this->width
= $row->fa_width
;
252 $this->height
= $row->fa_height
;
253 $this->metadata
= $row->fa_metadata
;
254 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
255 $this->media_type
= $row->fa_media_type
;
256 $this->description
= $row->fa_description
;
257 $this->user
= $row->fa_user
;
258 $this->user_text
= $row->fa_user_text
;
259 $this->timestamp
= $row->fa_timestamp
;
260 $this->deleted
= $row->fa_deleted
;
261 if ( isset( $row->fa_sha1
) ) {
262 $this->sha1
= $row->fa_sha1
;
264 // old row, populate from key
265 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
270 * Return the associated title object
274 public function getTitle() {
279 * Return the file name
283 public function getName() {
290 public function getID() {
299 public function exists() {
302 return $this->exists
;
306 * Return the FileStore key
309 public function getKey() {
316 * Return the FileStore key (overriding base File class)
319 public function getStorageKey() {
320 return $this->getKey();
324 * Return the FileStore storage group
327 public function getGroup() {
332 * Return the width of the image
335 public function getWidth() {
342 * Return the height of the image
345 public function getHeight() {
348 return $this->height
;
352 * Get handler-specific metadata
355 public function getMetadata() {
358 return $this->metadata
;
362 * Return the size of the image file, in bytes
365 public function getSize() {
372 * Return the bits of the image file, in bytes
375 public function getBits() {
382 * Returns the mime type of the file.
385 public function getMimeType() {
392 * Get a MediaHandler instance for this file
393 * @return MediaHandler
395 function getHandler() {
396 if ( !isset( $this->handler
) ) {
397 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
400 return $this->handler
;
404 * Returns the number of pages of a multipage document, or false for
405 * documents which aren't multipage documents
407 function pageCount() {
408 if ( !isset( $this->pageCount
) ) {
409 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
410 $this->pageCount
= $this->handler
->pageCount( $this );
412 $this->pageCount
= false;
416 return $this->pageCount
;
420 * Return the type of the media in the file.
421 * Use the value returned by this function with the MEDIATYPE_xxx constants.
424 public function getMediaType() {
427 return $this->media_type
;
431 * Return upload timestamp.
435 public function getTimestamp() {
438 return wfTimestamp( TS_MW
, $this->timestamp
);
442 * Get the SHA-1 base 36 hash of the file
454 * Returns ID or name of user who uploaded the file
456 * @note Prior to MediaWiki 1.23, this method always
457 * returned the user id, and was inconsistent with
458 * the rest of the file classes.
459 * @param string $type 'text' or 'id'
461 * @throws MWException
463 public function getUser( $type = 'text' ) {
466 if ( $type == 'text' ) {
467 return $this->user_text
;
468 } elseif ( $type == 'id' ) {
472 throw new MWException( "Unknown type '$type'." );
476 * Return the user name of the uploader.
478 * @deprecated since 1.23 Use getUser( 'text' ) instead.
481 public function getUserText() {
482 wfDeprecated( __METHOD__
, '1.23' );
484 if ( $this->isDeleted( File
::DELETED_USER
) ) {
487 return $this->user_text
;
492 * Return upload description.
496 public function getDescription() {
498 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
501 return $this->description
;
506 * Return the user ID of the uploader.
510 public function getRawUser() {
517 * Return the user name of the uploader.
521 public function getRawUserText() {
524 return $this->user_text
;
528 * Return upload description.
532 public function getRawDescription() {
535 return $this->description
;
539 * Returns the deletion bitfield
542 public function getVisibility() {
545 return $this->deleted
;
549 * for file or revision rows
551 * @param int $field One of DELETED_* bitfield constants
554 public function isDeleted( $field ) {
557 return ( $this->deleted
& $field ) == $field;
561 * Determine if the current user is allowed to view a particular
562 * field of this FileStore image file, if it's marked as deleted.
564 * @param null|User $user User object to check, or null to use $wgUser
567 public function userCan( $field, User
$user = null ) {
570 return Revision
::userCanBitfield( $this->deleted
, $field, $user );