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
33 var $id, # filearchive row ID
35 $group, # FileStore storage group
36 $key, # FileStore sha1 key
37 $size, # file dimensions
38 $bits, # size in bytes
41 $metadata, # metadata string
43 $media_type, # media type
44 $description, # upload description
45 $user, # user ID of uploader
46 $user_text, # user name of uploader
47 $timestamp, # time of upload
48 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
49 $deleted, # Bitfield akin to rev_deleted
60 var $title; # image title
70 function __construct( $title, $id=0, $key='' ) {
74 $this->group
= 'deleted'; // needed for direct use of constructor
81 $this->mime
= "unknown/unknown";
82 $this->media_type
= '';
83 $this->description
= '';
85 $this->user_text
= '';
86 $this->timestamp
= null;
88 $this->dataLoaded
= false;
89 $this->exists
= false;
91 if( $title instanceof Title
) {
92 $this->title
= File
::normalizeTitle( $title, 'exception' );
93 $this->name
= $title->getDBkey();
104 if ( !$id && !$key && !( $title instanceof Title
) ) {
105 throw new MWException( "No specifications provided to ArchivedFile constructor." );
110 * Loads a file object from the filearchive table
111 * @throws MWException
112 * @return bool|null True on success or null
114 public function load() {
115 if ( $this->dataLoaded
) {
120 if( $this->id
> 0 ) {
121 $conds['fa_id'] = $this->id
;
124 $conds['fa_storage_group'] = $this->group
;
125 $conds['fa_storage_key'] = $this->key
;
128 $conds['fa_name'] = $this->title
->getDBkey();
131 if( !count($conds)) {
132 throw new MWException( "No specific information for retrieving archived file" );
135 if( !$this->title ||
$this->title
->getNamespace() == NS_FILE
) {
136 $dbr = wfGetDB( DB_SLAVE
);
137 $res = $dbr->select( 'filearchive',
159 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
160 if ( $res == false ||
$dbr->numRows( $res ) == 0 ) {
161 // this revision does not exist?
164 $ret = $dbr->resultObject( $res );
165 $row = $ret->fetchObject();
167 // initialize fields for filestore image object
168 $this->id
= intval($row->fa_id
);
169 $this->name
= $row->fa_name
;
170 $this->archive_name
= $row->fa_archive_name
;
171 $this->group
= $row->fa_storage_group
;
172 $this->key
= $row->fa_storage_key
;
173 $this->size
= $row->fa_size
;
174 $this->bits
= $row->fa_bits
;
175 $this->width
= $row->fa_width
;
176 $this->height
= $row->fa_height
;
177 $this->metadata
= $row->fa_metadata
;
178 $this->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
179 $this->media_type
= $row->fa_media_type
;
180 $this->description
= $row->fa_description
;
181 $this->user
= $row->fa_user
;
182 $this->user_text
= $row->fa_user_text
;
183 $this->timestamp
= $row->fa_timestamp
;
184 $this->deleted
= $row->fa_deleted
;
186 throw new MWException( 'This title does not correspond to an image page.' );
188 $this->dataLoaded
= true;
189 $this->exists
= true;
195 * Loads a file object from the filearchive table
199 * @return ArchivedFile
201 public static function newFromRow( $row ) {
202 $file = new ArchivedFile( Title
::makeTitle( NS_FILE
, $row->fa_name
) );
204 $file->id
= intval($row->fa_id
);
205 $file->name
= $row->fa_name
;
206 $file->archive_name
= $row->fa_archive_name
;
207 $file->group
= $row->fa_storage_group
;
208 $file->key
= $row->fa_storage_key
;
209 $file->size
= $row->fa_size
;
210 $file->bits
= $row->fa_bits
;
211 $file->width
= $row->fa_width
;
212 $file->height
= $row->fa_height
;
213 $file->metadata
= $row->fa_metadata
;
214 $file->mime
= "$row->fa_major_mime/$row->fa_minor_mime";
215 $file->media_type
= $row->fa_media_type
;
216 $file->description
= $row->fa_description
;
217 $file->user
= $row->fa_user
;
218 $file->user_text
= $row->fa_user_text
;
219 $file->timestamp
= $row->fa_timestamp
;
220 $file->deleted
= $row->fa_deleted
;
226 * Return the associated title object
230 public function getTitle() {
235 * Return the file name
239 public function getName() {
246 public function getID() {
254 public function exists() {
256 return $this->exists
;
260 * Return the FileStore key
263 public function getKey() {
269 * Return the FileStore key (overriding base File class)
272 public function getStorageKey() {
273 return $this->getKey();
277 * Return the FileStore storage group
280 public function getGroup() {
285 * Return the width of the image
288 public function getWidth() {
294 * Return the height of the image
297 public function getHeight() {
299 return $this->height
;
303 * Get handler-specific metadata
306 public function getMetadata() {
308 return $this->metadata
;
312 * Return the size of the image file, in bytes
315 public function getSize() {
321 * Return the bits of the image file, in bytes
324 public function getBits() {
330 * Returns the mime type of the file.
333 public function getMimeType() {
339 * Get a MediaHandler instance for this file
340 * @return MediaHandler
342 function getHandler() {
343 if ( !isset( $this->handler
) ) {
344 $this->handler
= MediaHandler
::getHandler( $this->getMimeType() );
346 return $this->handler
;
350 * Returns the number of pages of a multipage document, or false for
351 * documents which aren't multipage documents
353 function pageCount() {
354 if ( !isset( $this->pageCount
) ) {
355 if ( $this->getHandler() && $this->handler
->isMultiPage( $this ) ) {
356 $this->pageCount
= $this->handler
->pageCount( $this );
358 $this->pageCount
= false;
361 return $this->pageCount
;
365 * Return the type of the media in the file.
366 * Use the value returned by this function with the MEDIATYPE_xxx constants.
369 public function getMediaType() {
371 return $this->media_type
;
375 * Return upload timestamp.
379 public function getTimestamp() {
381 return wfTimestamp( TS_MW
, $this->timestamp
);
385 * Return the user ID of the uploader.
389 public function getUser() {
391 if( $this->isDeleted( File
::DELETED_USER
) ) {
399 * Return the user name of the uploader.
403 public function getUserText() {
405 if( $this->isDeleted( File
::DELETED_USER
) ) {
408 return $this->user_text
;
413 * Return upload description.
417 public function getDescription() {
419 if( $this->isDeleted( File
::DELETED_COMMENT
) ) {
422 return $this->description
;
427 * Return the user ID of the uploader.
431 public function getRawUser() {
437 * Return the user name of the uploader.
441 public function getRawUserText() {
443 return $this->user_text
;
447 * Return upload description.
451 public function getRawDescription() {
453 return $this->description
;
457 * Returns the deletion bitfield
460 public function getVisibility() {
462 return $this->deleted
;
466 * for file or revision rows
468 * @param $field Integer: one of DELETED_* bitfield constants
471 public function isDeleted( $field ) {
473 return ($this->deleted
& $field) == $field;
477 * Determine if the current user is allowed to view a particular
478 * field of this FileStore image file, if it's marked as deleted.
479 * @param $field Integer
480 * @param $user User object to check, or null to use $wgUser
483 public function userCan( $field, User
$user = null ) {
485 return Revision
::userCanBitfield( $this->deleted
, $field, $user );