Localisation updates for core and extension messages from translatewiki.net (2010...
[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 $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
36
37 /**#@-*/
38
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;
59
60 if( is_object($title) ) {
61 $this->title = $title;
62 $this->name = $title->getDBkey();
63 }
64
65 if ($id)
66 $this->id = $id;
67
68 if ($key)
69 $this->key = $key;
70
71 if (!$id && !$key && !is_object($title))
72 throw new MWException( "No specifications provided to ArchivedFile constructor." );
73 }
74
75 /**
76 * Loads a file object from the filearchive table
77 * @return true on success or null
78 */
79 public function load() {
80 if ( $this->dataLoaded ) {
81 return true;
82 }
83 $conds = array();
84
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;
90 }
91 if( $this->title )
92 $conds['fa_name'] = $this->title->getDBkey();
93
94 if( !count($conds))
95 throw new MWException( "No specific information for retrieving archived file" );
96
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;
125 }
126 $ret = $dbr->resultObject( $res );
127 $row = $ret->fetchObject();
128
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.' );
149 }
150 $this->dataLoaded = true;
151 $this->exists = true;
152
153 return true;
154 }
155
156 /**
157 * Loads a file object from the filearchive table
158 * @return ArchivedFile
159 */
160 public static function newFromRow( $row ) {
161 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
162
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;
180
181 return $file;
182 }
183
184 /**
185 * Return the associated title object
186 */
187 public function getTitle() {
188 return $this->title;
189 }
190
191 /**
192 * Return the file name
193 */
194 public function getName() {
195 return $this->name;
196 }
197
198 public function getID() {
199 $this->load();
200 return $this->id;
201 }
202
203 public function exists() {
204 $this->load();
205 return $this->exists;
206 }
207
208 /**
209 * Return the FileStore key
210 */
211 public function getKey() {
212 $this->load();
213 return $this->key;
214 }
215
216 /**
217 * Return the FileStore key (overriding base File class)
218 */
219 public function getStorageKey() {
220 return $this->getKey();
221 }
222
223 /**
224 * Return the FileStore storage group
225 */
226 public function getGroup() {
227 return $this->group;
228 }
229
230 /**
231 * Return the width of the image
232 */
233 public function getWidth() {
234 $this->load();
235 return $this->width;
236 }
237
238 /**
239 * Return the height of the image
240 */
241 public function getHeight() {
242 $this->load();
243 return $this->height;
244 }
245
246 /**
247 * Get handler-specific metadata
248 */
249 public function getMetadata() {
250 $this->load();
251 return $this->metadata;
252 }
253
254 /**
255 * Return the size of the image file, in bytes
256 */
257 public function getSize() {
258 $this->load();
259 return $this->size;
260 }
261
262 /**
263 * Return the bits of the image file, in bytes
264 */
265 public function getBits() {
266 $this->load();
267 return $this->bits;
268 }
269
270 /**
271 * Returns the mime type of the file.
272 */
273 public function getMimeType() {
274 $this->load();
275 return $this->mime;
276 }
277
278 /**
279 * Return the type of the media in the file.
280 * Use the value returned by this function with the MEDIATYPE_xxx constants.
281 */
282 public function getMediaType() {
283 $this->load();
284 return $this->media_type;
285 }
286
287 /**
288 * Return upload timestamp.
289 */
290 public function getTimestamp() {
291 $this->load();
292 return wfTimestamp( TS_MW, $this->timestamp );
293 }
294
295 /**
296 * Return the user ID of the uploader.
297 */
298 public function getUser() {
299 $this->load();
300 if( $this->isDeleted( File::DELETED_USER ) ) {
301 return 0;
302 } else {
303 return $this->user;
304 }
305 }
306
307 /**
308 * Return the user name of the uploader.
309 */
310 public function getUserText() {
311 $this->load();
312 if( $this->isDeleted( File::DELETED_USER ) ) {
313 return 0;
314 } else {
315 return $this->user_text;
316 }
317 }
318
319 /**
320 * Return upload description.
321 */
322 public function getDescription() {
323 $this->load();
324 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
325 return 0;
326 } else {
327 return $this->description;
328 }
329 }
330
331 /**
332 * Return the user ID of the uploader.
333 */
334 public function getRawUser() {
335 $this->load();
336 return $this->user;
337 }
338
339 /**
340 * Return the user name of the uploader.
341 */
342 public function getRawUserText() {
343 $this->load();
344 return $this->user_text;
345 }
346
347 /**
348 * Return upload description.
349 */
350 public function getRawDescription() {
351 $this->load();
352 return $this->description;
353 }
354
355 /**
356 * Returns the deletion bitfield
357 * @return int
358 */
359 public function getVisibility() {
360 $this->load();
361 return $this->deleted;
362 }
363
364 /**
365 * for file or revision rows
366 *
367 * @param $field Integer: one of DELETED_* bitfield constants
368 * @return bool
369 */
370 public function isDeleted( $field ) {
371 $this->load();
372 return ($this->deleted & $field) == $field;
373 }
374
375 /**
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
380 */
381 public function userCan( $field ) {
382 $this->load();
383 return Revision::userCanBitfield( $this->deleted, $field );
384 }
385 }