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
200 * @return ArchivedFile
202 public static function newFromRow( $row ) {
203 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
204 $file->loadFromRow( $row );
210 * Fields in the filearchive table
213 static function selectFields() {
233 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
239 * Load ArchivedFile object fields from a DB row.
241 * @param $row Object database row
244 public function loadFromRow( $row ) {
245 $this->id
= intval( $row->fa_id
);
246 $this->name
= $row->fa_name
;
247 $this->archive_name
= $row->fa_archive_name
;
248 $this->group
= $row->fa_storage_group
;
249 $this->key
= $row->fa_storage_key
;
250 $this->size
= $row->fa_size
;
251 $this->bits
= $row->fa_bits
;
252 $this->width
= $row->fa_width
;
253 $this->height
= $row->fa_height
;
254 $this->metadata
= $row->fa_metadata
;
255 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
256 $this->media_type
= $row->fa_media_type
;
257 $this->description
= $row->fa_description
;
258 $this->user
= $row->fa_user
;
259 $this->user_text
= $row->fa_user_text
;
260 $this->timestamp
= $row->fa_timestamp
;
261 $this->deleted
= $row->fa_deleted
;
262 if ( isset( $row->fa_sha1
) ) {
263 $this->sha1
= $row->fa_sha1
;
265 // old row, populate from key
266 $this->sha1
= LocalRepo
::getHashFromKey( $this->key
);
271 * Return the associated title object
275 public function getTitle() {
280 * Return the file name
284 public function getName() {
291 public function getID() {
300 public function exists() {
303 return $this->exists
;
307 * Return the FileStore key
310 public function getKey() {
317 * Return the FileStore key (overriding base File class)
320 public function getStorageKey() {
321 return $this->getKey();
325 * Return the FileStore storage group
328 public function getGroup() {
333 * Return the width of the image
336 public function getWidth() {
343 * Return the height of the image
346 public function getHeight() {
349 return $this->height
;
353 * Get handler-specific metadata
356 public function getMetadata() {
359 return $this->metadata
;
363 * Return the size of the image file, in bytes
366 public function getSize() {
373 * Return the bits of the image file, in bytes
376 public function getBits() {
383 * Returns the mime type of the file.
386 public function getMimeType() {
393 * Get a MediaHandler instance for this file
394 * @return MediaHandler
396 function getHandler() {
397 if ( !isset( $this->handler
) ) {
398 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
401 return $this->handler
;
405 * Returns the number of pages of a multipage document, or false for
406 * documents which aren't multipage documents
408 function pageCount() {
409 if ( !isset( $this->pageCount
) ) {
410 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
411 $this->pageCount
= $this->handler
->pageCount( $this );
413 $this->pageCount
= false;
417 return $this->pageCount
;
421 * Return the type of the media in the file.
422 * Use the value returned by this function with the MEDIATYPE_xxx constants.
425 public function getMediaType() {
428 return $this->media_type
;
432 * Return upload timestamp.
436 public function getTimestamp() {
439 return wfTimestamp( TS_MW
, $this->timestamp
);
443 * Get the SHA-1 base 36 hash of the file
455 * Return the user ID of the uploader.
459 public function getUser() {
461 if ( $this->isDeleted( File
::DELETED_USER
) ) {
469 * Return the user name of the uploader.
473 public function getUserText() {
475 if ( $this->isDeleted( File
::DELETED_USER
) ) {
478 return $this->user_text
;
483 * Return upload description.
487 public function getDescription() {
489 if ( $this->isDeleted( File
::DELETED_COMMENT
) ) {
492 return $this->description
;
497 * Return the user ID of the uploader.
501 public function getRawUser() {
508 * Return the user name of the uploader.
512 public function getRawUserText() {
515 return $this->user_text
;
519 * Return upload description.
523 public function getRawDescription() {
526 return $this->description
;
530 * Returns the deletion bitfield
533 public function getVisibility() {
536 return $this->deleted
;
540 * for file or revision rows
542 * @param $field Integer: one of DELETED_* bitfield constants
545 public function isDeleted( $field ) {
548 return ( $this->deleted
& $field ) == $field;
552 * Determine if the current user is allowed to view a particular
553 * field of this FileStore image file, if it's marked as deleted.
554 * @param $field Integer
555 * @param $user User object to check, or null to use $wgUser
558 public function userCan( $field, User
$user = null ) {
561 return Revision
::userCanBitfield( $this->deleted
, $field, $user );