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() {
284 if ( $this->name
=== false ) {
294 public function getID() {
303 public function exists() {
306 return $this->exists
;
310 * Return the FileStore key
313 public function getKey() {
320 * Return the FileStore key (overriding base File class)
323 public function getStorageKey() {
324 return $this->getKey();
328 * Return the FileStore storage group
331 public function getGroup() {
336 * Return the width of the image
339 public function getWidth() {
346 * Return the height of the image
349 public function getHeight() {
352 return $this->height
;
356 * Get handler-specific metadata
359 public function getMetadata() {
362 return $this->metadata
;
366 * Return the size of the image file, in bytes
369 public function getSize() {
376 * Return the bits of the image file, in bytes
379 public function getBits() {
386 * Returns the MIME type of the file.
389 public function getMimeType() {
396 * Get a MediaHandler instance for this file
397 * @return MediaHandler
399 function getHandler() {
400 if ( !isset( $this->handler
) ) {
401 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
404 return $this->handler
;
408 * Returns the number of pages of a multipage document, or false for
409 * documents which aren't multipage documents
412 function pageCount() {
413 if ( !isset( $this->pageCount
) ) {
414 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
415 $this->pageCount
= $this->handler
->pageCount( $this );
417 $this->pageCount
= false;
421 return $this->pageCount
;
425 * Return the type of the media in the file.
426 * Use the value returned by this function with the MEDIATYPE_xxx constants.
429 public function getMediaType() {
432 return $this->media_type
;
436 * Return upload timestamp.
440 public function getTimestamp() {
443 return wfTimestamp( TS_MW
, $this->timestamp
);
447 * Get the SHA-1 base 36 hash of the file
459 * Returns ID or name of user who uploaded the file
461 * @note Prior to MediaWiki 1.23, this method always
462 * returned the user id, and was inconsistent with
463 * the rest of the file classes.
464 * @param string $type 'text' or 'id'
466 * @throws MWException
468 public function getUser( $type = 'text' ) {
471 if ( $type == 'text' ) {
472 return $this->user_text
;
473 } elseif ( $type == 'id' ) {
477 throw new MWException( "Unknown type '$type'." );
481 * Return the user name of the uploader.
483 * @deprecated since 1.23 Use getUser( 'text' ) instead.
486 public function getUserText() {
487 wfDeprecated( __METHOD__
, '1.23' );
489 if ( $this->isDeleted( File
::DELETED_USER
) ) {
492 return $this->user_text
;
497 * Return upload description.
501 public function getDescription() {
503 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
506 return $this->description
;
511 * Return the user ID of the uploader.
515 public function getRawUser() {
522 * Return the user name of the uploader.
526 public function getRawUserText() {
529 return $this->user_text
;
533 * Return upload description.
537 public function getRawDescription() {
540 return $this->description
;
544 * Returns the deletion bitfield
547 public function getVisibility() {
550 return $this->deleted
;
554 * for file or revision rows
556 * @param int $field One of DELETED_* bitfield constants
559 public function isDeleted( $field ) {
562 return ( $this->deleted
& $field ) == $field;
566 * Determine if the current user is allowed to view a particular
567 * field of this FileStore image file, if it's marked as deleted.
569 * @param null|User $user User object to check, or null to use $wgUser
572 public function userCan( $field, User
$user = null ) {
575 return Revision
::userCanBitfield( $this->deleted
, $field, $user );