be9d3f1e09b371c803f919a26dd0b35cd5f421de
[lhc/web/wiklou.git] / includes / filerepo / ArchivedFile.php
1 <?php
2 /**
3 * Deleted file in the 'filearchive' table
4 *
5 * @file
6 * @ingroup FileRepo
7 */
8
9 /**
10 * Class representing a row of the 'filearchive' table
11 *
12 * @ingroup FileRepo
13 */
14 class ArchivedFile {
15 /**#@+
16 * @private
17 */
18 var $id, # filearchive row ID
19 $name, # image name
20 $group, # FileStore storage group
21 $key, # FileStore sha1 key
22 $size, # file dimensions
23 $bits, # size in bytes
24 $width, # width
25 $height, # height
26 $metadata, # metadata string
27 $mime, # mime type
28 $media_type, # media type
29 $description, # upload description
30 $user, # user ID of uploader
31 $user_text, # user name of uploader
32 $timestamp, # time of upload
33 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
34 $deleted; # Bitfield akin to rev_deleted
35
36 /**
37 * @var Title
38 */
39 var $title; # image title
40
41 /**#@-*/
42
43 function __construct( $title, $id=0, $key='' ) {
44 $this->id = -1;
45 $this->title = false;
46 $this->name = false;
47 $this->group = 'deleted'; // needed for direct use of constructor
48 $this->key = '';
49 $this->size = 0;
50 $this->bits = 0;
51 $this->width = 0;
52 $this->height = 0;
53 $this->metadata = '';
54 $this->mime = "unknown/unknown";
55 $this->media_type = '';
56 $this->description = '';
57 $this->user = 0;
58 $this->user_text = '';
59 $this->timestamp = null;
60 $this->deleted = 0;
61 $this->dataLoaded = false;
62 $this->exists = false;
63
64 if( is_object($title) ) {
65 $this->title = $title;
66 $this->name = $title->getDBkey();
67 }
68
69 if ($id)
70 $this->id = $id;
71
72 if ($key)
73 $this->key = $key;
74
75 if (!$id && !$key && !is_object($title))
76 throw new MWException( "No specifications provided to ArchivedFile constructor." );
77 }
78
79 /**
80 * Loads a file object from the filearchive table
81 * @return true on success or null
82 */
83 public function load() {
84 if ( $this->dataLoaded ) {
85 return true;
86 }
87 $conds = array();
88
89 if( $this->id > 0 )
90 $conds['fa_id'] = $this->id;
91 if( $this->key ) {
92 $conds['fa_storage_group'] = $this->group;
93 $conds['fa_storage_key'] = $this->key;
94 }
95 if( $this->title )
96 $conds['fa_name'] = $this->title->getDBkey();
97
98 if( !count($conds))
99 throw new MWException( "No specific information for retrieving archived file" );
100
101 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
102 $dbr = wfGetDB( DB_SLAVE );
103 $res = $dbr->select( 'filearchive',
104 array(
105 'fa_id',
106 'fa_name',
107 'fa_archive_name',
108 'fa_storage_key',
109 'fa_storage_group',
110 'fa_size',
111 'fa_bits',
112 'fa_width',
113 'fa_height',
114 'fa_metadata',
115 'fa_media_type',
116 'fa_major_mime',
117 'fa_minor_mime',
118 'fa_description',
119 'fa_user',
120 'fa_user_text',
121 'fa_timestamp',
122 'fa_deleted' ),
123 $conds,
124 __METHOD__,
125 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
126 if ( $res == false || $dbr->numRows( $res ) == 0 ) {
127 // this revision does not exist?
128 return;
129 }
130 $ret = $dbr->resultObject( $res );
131 $row = $ret->fetchObject();
132
133 // initialize fields for filestore image object
134 $this->id = intval($row->fa_id);
135 $this->name = $row->fa_name;
136 $this->archive_name = $row->fa_archive_name;
137 $this->group = $row->fa_storage_group;
138 $this->key = $row->fa_storage_key;
139 $this->size = $row->fa_size;
140 $this->bits = $row->fa_bits;
141 $this->width = $row->fa_width;
142 $this->height = $row->fa_height;
143 $this->metadata = $row->fa_metadata;
144 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
145 $this->media_type = $row->fa_media_type;
146 $this->description = $row->fa_description;
147 $this->user = $row->fa_user;
148 $this->user_text = $row->fa_user_text;
149 $this->timestamp = $row->fa_timestamp;
150 $this->deleted = $row->fa_deleted;
151 } else {
152 throw new MWException( 'This title does not correspond to an image page.' );
153 }
154 $this->dataLoaded = true;
155 $this->exists = true;
156
157 return true;
158 }
159
160 /**
161 * Loads a file object from the filearchive table
162 * @return ArchivedFile
163 */
164 public static function newFromRow( $row ) {
165 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
166
167 $file->id = intval($row->fa_id);
168 $file->name = $row->fa_name;
169 $file->archive_name = $row->fa_archive_name;
170 $file->group = $row->fa_storage_group;
171 $file->key = $row->fa_storage_key;
172 $file->size = $row->fa_size;
173 $file->bits = $row->fa_bits;
174 $file->width = $row->fa_width;
175 $file->height = $row->fa_height;
176 $file->metadata = $row->fa_metadata;
177 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
178 $file->media_type = $row->fa_media_type;
179 $file->description = $row->fa_description;
180 $file->user = $row->fa_user;
181 $file->user_text = $row->fa_user_text;
182 $file->timestamp = $row->fa_timestamp;
183 $file->deleted = $row->fa_deleted;
184
185 return $file;
186 }
187
188 /**
189 * Return the associated title object
190 */
191 public function getTitle() {
192 return $this->title;
193 }
194
195 /**
196 * Return the file name
197 */
198 public function getName() {
199 return $this->name;
200 }
201
202 public function getID() {
203 $this->load();
204 return $this->id;
205 }
206
207 public function exists() {
208 $this->load();
209 return $this->exists;
210 }
211
212 /**
213 * Return the FileStore key
214 */
215 public function getKey() {
216 $this->load();
217 return $this->key;
218 }
219
220 /**
221 * Return the FileStore key (overriding base File class)
222 */
223 public function getStorageKey() {
224 return $this->getKey();
225 }
226
227 /**
228 * Return the FileStore storage group
229 */
230 public function getGroup() {
231 return $this->group;
232 }
233
234 /**
235 * Return the width of the image
236 */
237 public function getWidth() {
238 $this->load();
239 return $this->width;
240 }
241
242 /**
243 * Return the height of the image
244 */
245 public function getHeight() {
246 $this->load();
247 return $this->height;
248 }
249
250 /**
251 * Get handler-specific metadata
252 */
253 public function getMetadata() {
254 $this->load();
255 return $this->metadata;
256 }
257
258 /**
259 * Return the size of the image file, in bytes
260 */
261 public function getSize() {
262 $this->load();
263 return $this->size;
264 }
265
266 /**
267 * Return the bits of the image file, in bytes
268 */
269 public function getBits() {
270 $this->load();
271 return $this->bits;
272 }
273
274 /**
275 * Returns the mime type of the file.
276 */
277 public function getMimeType() {
278 $this->load();
279 return $this->mime;
280 }
281
282 /**
283 * Return the type of the media in the file.
284 * Use the value returned by this function with the MEDIATYPE_xxx constants.
285 */
286 public function getMediaType() {
287 $this->load();
288 return $this->media_type;
289 }
290
291 /**
292 * Return upload timestamp.
293 */
294 public function getTimestamp() {
295 $this->load();
296 return wfTimestamp( TS_MW, $this->timestamp );
297 }
298
299 /**
300 * Return the user ID of the uploader.
301 */
302 public function getUser() {
303 $this->load();
304 if( $this->isDeleted( File::DELETED_USER ) ) {
305 return 0;
306 } else {
307 return $this->user;
308 }
309 }
310
311 /**
312 * Return the user name of the uploader.
313 */
314 public function getUserText() {
315 $this->load();
316 if( $this->isDeleted( File::DELETED_USER ) ) {
317 return 0;
318 } else {
319 return $this->user_text;
320 }
321 }
322
323 /**
324 * Return upload description.
325 */
326 public function getDescription() {
327 $this->load();
328 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
329 return 0;
330 } else {
331 return $this->description;
332 }
333 }
334
335 /**
336 * Return the user ID of the uploader.
337 */
338 public function getRawUser() {
339 $this->load();
340 return $this->user;
341 }
342
343 /**
344 * Return the user name of the uploader.
345 */
346 public function getRawUserText() {
347 $this->load();
348 return $this->user_text;
349 }
350
351 /**
352 * Return upload description.
353 */
354 public function getRawDescription() {
355 $this->load();
356 return $this->description;
357 }
358
359 /**
360 * Returns the deletion bitfield
361 * @return int
362 */
363 public function getVisibility() {
364 $this->load();
365 return $this->deleted;
366 }
367
368 /**
369 * for file or revision rows
370 *
371 * @param $field Integer: one of DELETED_* bitfield constants
372 * @return bool
373 */
374 public function isDeleted( $field ) {
375 $this->load();
376 return ($this->deleted & $field) == $field;
377 }
378
379 /**
380 * Determine if the current user is allowed to view a particular
381 * field of this FileStore image file, if it's marked as deleted.
382 * @param $field Integer
383 * @return bool
384 */
385 public function userCan( $field ) {
386 $this->load();
387 return Revision::userCanBitfield( $this->deleted, $field );
388 }
389 }