Follow-up r78695: correct class name
[mediawiki.git] / includes / filerepo / ArchivedFile.php
blobb4e1cc5fb5e2d6988a329b67a2279c5c2de917d3
1 <?php
2 /**
3 * Deleted file in the 'filearchive' table
5 * @file
6 * @ingroup FileRepo
7 */
9 /**
10 * Class representing a row of the 'filearchive' table
12 * @ingroup FileRepo
14 class ArchivedFile {
15 /**#@+
16 * @private
18 var $id, # filearchive row ID
19 $title, # image title
20 $name, # image name
21 $group, # FileStore storage group
22 $key, # FileStore sha1 key
23 $size, # file dimensions
24 $bits, # size in bytes
25 $width, # width
26 $height, # height
27 $metadata, # metadata string
28 $mime, # mime type
29 $media_type, # media type
30 $description, # upload description
31 $user, # user ID of uploader
32 $user_text, # user name of uploader
33 $timestamp, # time of upload
34 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
35 $deleted; # Bitfield akin to rev_deleted
37 /**#@-*/
39 function __construct( $title, $id=0, $key='' ) {
40 $this->id = -1;
41 $this->title = false;
42 $this->name = false;
43 $this->group = 'deleted'; // needed for direct use of constructor
44 $this->key = '';
45 $this->size = 0;
46 $this->bits = 0;
47 $this->width = 0;
48 $this->height = 0;
49 $this->metadata = '';
50 $this->mime = "unknown/unknown";
51 $this->media_type = '';
52 $this->description = '';
53 $this->user = 0;
54 $this->user_text = '';
55 $this->timestamp = null;
56 $this->deleted = 0;
57 $this->dataLoaded = false;
58 $this->exists = false;
60 if( is_object($title) ) {
61 $this->title = $title;
62 $this->name = $title->getDBkey();
65 if ($id)
66 $this->id = $id;
68 if ($key)
69 $this->key = $key;
71 if (!$id && !$key && !is_object($title))
72 throw new MWException( "No specifications provided to ArchivedFile constructor." );
75 /**
76 * Loads a file object from the filearchive table
77 * @return true on success or null
79 public function load() {
80 if ( $this->dataLoaded ) {
81 return true;
83 $conds = array();
85 if( $this->id > 0 )
86 $conds['fa_id'] = $this->id;
87 if( $this->key ) {
88 $conds['fa_storage_group'] = $this->group;
89 $conds['fa_storage_key'] = $this->key;
91 if( $this->title )
92 $conds['fa_name'] = $this->title->getDBkey();
94 if( !count($conds))
95 throw new MWException( "No specific information for retrieving archived file" );
97 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
98 $dbr = wfGetDB( DB_SLAVE );
99 $res = $dbr->select( 'filearchive',
100 array(
101 'fa_id',
102 'fa_name',
103 'fa_archive_name',
104 'fa_storage_key',
105 'fa_storage_group',
106 'fa_size',
107 'fa_bits',
108 'fa_width',
109 'fa_height',
110 'fa_metadata',
111 'fa_media_type',
112 'fa_major_mime',
113 'fa_minor_mime',
114 'fa_description',
115 'fa_user',
116 'fa_user_text',
117 'fa_timestamp',
118 'fa_deleted' ),
119 $conds,
120 __METHOD__,
121 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
122 if ( $res == false || $dbr->numRows( $res ) == 0 ) {
123 // this revision does not exist?
124 return;
126 $ret = $dbr->resultObject( $res );
127 $row = $ret->fetchObject();
129 // initialize fields for filestore image object
130 $this->id = intval($row->fa_id);
131 $this->name = $row->fa_name;
132 $this->archive_name = $row->fa_archive_name;
133 $this->group = $row->fa_storage_group;
134 $this->key = $row->fa_storage_key;
135 $this->size = $row->fa_size;
136 $this->bits = $row->fa_bits;
137 $this->width = $row->fa_width;
138 $this->height = $row->fa_height;
139 $this->metadata = $row->fa_metadata;
140 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
141 $this->media_type = $row->fa_media_type;
142 $this->description = $row->fa_description;
143 $this->user = $row->fa_user;
144 $this->user_text = $row->fa_user_text;
145 $this->timestamp = $row->fa_timestamp;
146 $this->deleted = $row->fa_deleted;
147 } else {
148 throw new MWException( 'This title does not correspond to an image page.' );
150 $this->dataLoaded = true;
151 $this->exists = true;
153 return true;
157 * Loads a file object from the filearchive table
158 * @return ArchivedFile
160 public static function newFromRow( $row ) {
161 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
163 $file->id = intval($row->fa_id);
164 $file->name = $row->fa_name;
165 $file->archive_name = $row->fa_archive_name;
166 $file->group = $row->fa_storage_group;
167 $file->key = $row->fa_storage_key;
168 $file->size = $row->fa_size;
169 $file->bits = $row->fa_bits;
170 $file->width = $row->fa_width;
171 $file->height = $row->fa_height;
172 $file->metadata = $row->fa_metadata;
173 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
174 $file->media_type = $row->fa_media_type;
175 $file->description = $row->fa_description;
176 $file->user = $row->fa_user;
177 $file->user_text = $row->fa_user_text;
178 $file->timestamp = $row->fa_timestamp;
179 $file->deleted = $row->fa_deleted;
181 return $file;
185 * Return the associated title object
187 public function getTitle() {
188 return $this->title;
192 * Return the file name
194 public function getName() {
195 return $this->name;
198 public function getID() {
199 $this->load();
200 return $this->id;
203 public function exists() {
204 $this->load();
205 return $this->exists;
209 * Return the FileStore key
211 public function getKey() {
212 $this->load();
213 return $this->key;
217 * Return the FileStore key (overriding base File class)
219 public function getStorageKey() {
220 return $this->getKey();
224 * Return the FileStore storage group
226 public function getGroup() {
227 return $this->group;
231 * Return the width of the image
233 public function getWidth() {
234 $this->load();
235 return $this->width;
239 * Return the height of the image
241 public function getHeight() {
242 $this->load();
243 return $this->height;
247 * Get handler-specific metadata
249 public function getMetadata() {
250 $this->load();
251 return $this->metadata;
255 * Return the size of the image file, in bytes
257 public function getSize() {
258 $this->load();
259 return $this->size;
263 * Return the bits of the image file, in bytes
265 public function getBits() {
266 $this->load();
267 return $this->bits;
271 * Returns the mime type of the file.
273 public function getMimeType() {
274 $this->load();
275 return $this->mime;
279 * Return the type of the media in the file.
280 * Use the value returned by this function with the MEDIATYPE_xxx constants.
282 public function getMediaType() {
283 $this->load();
284 return $this->media_type;
288 * Return upload timestamp.
290 public function getTimestamp() {
291 $this->load();
292 return wfTimestamp( TS_MW, $this->timestamp );
296 * Return the user ID of the uploader.
298 public function getUser() {
299 $this->load();
300 if( $this->isDeleted( File::DELETED_USER ) ) {
301 return 0;
302 } else {
303 return $this->user;
308 * Return the user name of the uploader.
310 public function getUserText() {
311 $this->load();
312 if( $this->isDeleted( File::DELETED_USER ) ) {
313 return 0;
314 } else {
315 return $this->user_text;
320 * Return upload description.
322 public function getDescription() {
323 $this->load();
324 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
325 return 0;
326 } else {
327 return $this->description;
332 * Return the user ID of the uploader.
334 public function getRawUser() {
335 $this->load();
336 return $this->user;
340 * Return the user name of the uploader.
342 public function getRawUserText() {
343 $this->load();
344 return $this->user_text;
348 * Return upload description.
350 public function getRawDescription() {
351 $this->load();
352 return $this->description;
356 * Returns the deletion bitfield
357 * @return int
359 public function getVisibility() {
360 $this->load();
361 return $this->deleted;
365 * for file or revision rows
367 * @param $field Integer: one of DELETED_* bitfield constants
368 * @return bool
370 public function isDeleted( $field ) {
371 $this->load();
372 return ($this->deleted & $field) == $field;
376 * Determine if the current user is allowed to view a particular
377 * field of this FileStore image file, if it's marked as deleted.
378 * @param $field Integer
379 * @return bool
381 public function userCan( $field ) {
382 $this->load();
383 return Revision::userCanBitfield( $this->deleted, $field );