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