enhance filerepo doc structure
[lhc/web/wiklou.git] / includes / filerepo / file / ArchivedFile.php
1 <?php
2 /**
3 * Deleted file in the 'filearchive' table
4 *
5 * @file
6 * @ingroup FileAbstraction
7 */
8
9 /**
10 * Class representing a row of the 'filearchive' table
11 *
12 * @ingroup FileAbstraction
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( $title instanceof Title ) {
77 $this->title = File::normalizeTitle( $title, 'exception' );
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 && !( $title instanceof 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 *
181 * @param $row
182 *
183 * @return ArchivedFile
184 */
185 public static function newFromRow( $row ) {
186 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
187
188 $file->id = intval($row->fa_id);
189 $file->name = $row->fa_name;
190 $file->archive_name = $row->fa_archive_name;
191 $file->group = $row->fa_storage_group;
192 $file->key = $row->fa_storage_key;
193 $file->size = $row->fa_size;
194 $file->bits = $row->fa_bits;
195 $file->width = $row->fa_width;
196 $file->height = $row->fa_height;
197 $file->metadata = $row->fa_metadata;
198 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
199 $file->media_type = $row->fa_media_type;
200 $file->description = $row->fa_description;
201 $file->user = $row->fa_user;
202 $file->user_text = $row->fa_user_text;
203 $file->timestamp = $row->fa_timestamp;
204 $file->deleted = $row->fa_deleted;
205
206 return $file;
207 }
208
209 /**
210 * Return the associated title object
211 *
212 * @return Title
213 */
214 public function getTitle() {
215 return $this->title;
216 }
217
218 /**
219 * Return the file name
220 *
221 * @return string
222 */
223 public function getName() {
224 return $this->name;
225 }
226
227 /**
228 * @return int
229 */
230 public function getID() {
231 $this->load();
232 return $this->id;
233 }
234
235 /**
236 * @return bool
237 */
238 public function exists() {
239 $this->load();
240 return $this->exists;
241 }
242
243 /**
244 * Return the FileStore key
245 * @return string
246 */
247 public function getKey() {
248 $this->load();
249 return $this->key;
250 }
251
252 /**
253 * Return the FileStore key (overriding base File class)
254 * @return string
255 */
256 public function getStorageKey() {
257 return $this->getKey();
258 }
259
260 /**
261 * Return the FileStore storage group
262 * @return string
263 */
264 public function getGroup() {
265 return $this->group;
266 }
267
268 /**
269 * Return the width of the image
270 * @return int
271 */
272 public function getWidth() {
273 $this->load();
274 return $this->width;
275 }
276
277 /**
278 * Return the height of the image
279 * @return int
280 */
281 public function getHeight() {
282 $this->load();
283 return $this->height;
284 }
285
286 /**
287 * Get handler-specific metadata
288 * @return string
289 */
290 public function getMetadata() {
291 $this->load();
292 return $this->metadata;
293 }
294
295 /**
296 * Return the size of the image file, in bytes
297 * @return int
298 */
299 public function getSize() {
300 $this->load();
301 return $this->size;
302 }
303
304 /**
305 * Return the bits of the image file, in bytes
306 * @return int
307 */
308 public function getBits() {
309 $this->load();
310 return $this->bits;
311 }
312
313 /**
314 * Returns the mime type of the file.
315 * @return string
316 */
317 public function getMimeType() {
318 $this->load();
319 return $this->mime;
320 }
321
322 /**
323 * Get a MediaHandler instance for this file
324 * @return MediaHandler
325 */
326 function getHandler() {
327 if ( !isset( $this->handler ) ) {
328 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
329 }
330 return $this->handler;
331 }
332
333 /**
334 * Returns the number of pages of a multipage document, or false for
335 * documents which aren't multipage documents
336 */
337 function pageCount() {
338 if ( !isset( $this->pageCount ) ) {
339 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
340 $this->pageCount = $this->handler->pageCount( $this );
341 } else {
342 $this->pageCount = false;
343 }
344 }
345 return $this->pageCount;
346 }
347
348 /**
349 * Return the type of the media in the file.
350 * Use the value returned by this function with the MEDIATYPE_xxx constants.
351 * @return string
352 */
353 public function getMediaType() {
354 $this->load();
355 return $this->media_type;
356 }
357
358 /**
359 * Return upload timestamp.
360 *
361 * @return string
362 */
363 public function getTimestamp() {
364 $this->load();
365 return wfTimestamp( TS_MW, $this->timestamp );
366 }
367
368 /**
369 * Return the user ID of the uploader.
370 *
371 * @return int
372 */
373 public function getUser() {
374 $this->load();
375 if( $this->isDeleted( File::DELETED_USER ) ) {
376 return 0;
377 } else {
378 return $this->user;
379 }
380 }
381
382 /**
383 * Return the user name of the uploader.
384 *
385 * @return string
386 */
387 public function getUserText() {
388 $this->load();
389 if( $this->isDeleted( File::DELETED_USER ) ) {
390 return 0;
391 } else {
392 return $this->user_text;
393 }
394 }
395
396 /**
397 * Return upload description.
398 *
399 * @return string
400 */
401 public function getDescription() {
402 $this->load();
403 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
404 return 0;
405 } else {
406 return $this->description;
407 }
408 }
409
410 /**
411 * Return the user ID of the uploader.
412 *
413 * @return int
414 */
415 public function getRawUser() {
416 $this->load();
417 return $this->user;
418 }
419
420 /**
421 * Return the user name of the uploader.
422 *
423 * @return string
424 */
425 public function getRawUserText() {
426 $this->load();
427 return $this->user_text;
428 }
429
430 /**
431 * Return upload description.
432 *
433 * @return string
434 */
435 public function getRawDescription() {
436 $this->load();
437 return $this->description;
438 }
439
440 /**
441 * Returns the deletion bitfield
442 * @return int
443 */
444 public function getVisibility() {
445 $this->load();
446 return $this->deleted;
447 }
448
449 /**
450 * for file or revision rows
451 *
452 * @param $field Integer: one of DELETED_* bitfield constants
453 * @return bool
454 */
455 public function isDeleted( $field ) {
456 $this->load();
457 return ($this->deleted & $field) == $field;
458 }
459
460 /**
461 * Determine if the current user is allowed to view a particular
462 * field of this FileStore image file, if it's marked as deleted.
463 * @param $field Integer
464 * @param $user User object to check, or null to use $wgUser
465 * @return bool
466 */
467 public function userCan( $field, User $user = null ) {
468 $this->load();
469 return Revision::userCanBitfield( $this->deleted, $field, $user );
470 }
471 }