[FileRepo] [FileBackend] Added support for custom file headers.
[lhc/web/wiklou.git] / includes / filerepo / file / ArchivedFile.php
1 <?php
2 /**
3 * Deleted file in the 'filearchive' table.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 /**
25 * Class representing a row of the 'filearchive' table
26 *
27 * @ingroup FileAbstraction
28 */
29 class ArchivedFile {
30 /**#@+
31 * @private
32 */
33 var $id, # filearchive row ID
34 $name, # image name
35 $group, # FileStore storage group
36 $key, # FileStore sha1 key
37 $size, # file dimensions
38 $bits, # size in bytes
39 $width, # width
40 $height, # height
41 $metadata, # metadata string
42 $mime, # mime type
43 $media_type, # media type
44 $description, # upload description
45 $user, # user ID of uploader
46 $user_text, # user name of uploader
47 $timestamp, # time of upload
48 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
49 $deleted, # Bitfield akin to rev_deleted
50 $sha1, # sha1 hash of file content
51 $pageCount,
52 $archive_name;
53
54 /**
55 * @var MediaHandler
56 */
57 var $handler;
58 /**
59 * @var Title
60 */
61 var $title; # image title
62
63 /**#@-*/
64
65 /**
66 * @throws MWException
67 * @param Title $title
68 * @param int $id
69 * @param string $key
70 */
71 function __construct( $title, $id=0, $key='' ) {
72 $this->id = -1;
73 $this->title = false;
74 $this->name = false;
75 $this->group = 'deleted'; // needed for direct use of constructor
76 $this->key = '';
77 $this->size = 0;
78 $this->bits = 0;
79 $this->width = 0;
80 $this->height = 0;
81 $this->metadata = '';
82 $this->mime = "unknown/unknown";
83 $this->media_type = '';
84 $this->description = '';
85 $this->user = 0;
86 $this->user_text = '';
87 $this->timestamp = null;
88 $this->deleted = 0;
89 $this->dataLoaded = false;
90 $this->exists = false;
91 $this->sha1 = '';
92
93 if( $title instanceof Title ) {
94 $this->title = File::normalizeTitle( $title, 'exception' );
95 $this->name = $title->getDBkey();
96 }
97
98 if ($id) {
99 $this->id = $id;
100 }
101
102 if ($key) {
103 $this->key = $key;
104 }
105
106 if ( !$id && !$key && !( $title instanceof Title ) ) {
107 throw new MWException( "No specifications provided to ArchivedFile constructor." );
108 }
109 }
110
111 /**
112 * Loads a file object from the filearchive table
113 * @throws MWException
114 * @return bool|null True on success or null
115 */
116 public function load() {
117 if ( $this->dataLoaded ) {
118 return true;
119 }
120 $conds = array();
121
122 if( $this->id > 0 ) {
123 $conds['fa_id'] = $this->id;
124 }
125 if( $this->key ) {
126 $conds['fa_storage_group'] = $this->group;
127 $conds['fa_storage_key'] = $this->key;
128 }
129 if( $this->title ) {
130 $conds['fa_name'] = $this->title->getDBkey();
131 }
132
133 if( !count($conds)) {
134 throw new MWException( "No specific information for retrieving archived file" );
135 }
136
137 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
138 $this->dataLoaded = true; // set it here, to have also true on miss
139 $dbr = wfGetDB( DB_SLAVE );
140 $row = $dbr->selectRow( 'filearchive',
141 array(
142 'fa_id',
143 'fa_name',
144 'fa_archive_name',
145 'fa_storage_key',
146 'fa_storage_group',
147 'fa_size',
148 'fa_bits',
149 'fa_width',
150 'fa_height',
151 'fa_metadata',
152 'fa_media_type',
153 'fa_major_mime',
154 'fa_minor_mime',
155 'fa_description',
156 'fa_user',
157 'fa_user_text',
158 'fa_timestamp',
159 'fa_deleted',
160 'fa_sha1' ),
161 $conds,
162 __METHOD__,
163 array( 'ORDER BY' => 'fa_timestamp DESC')
164 );
165 if ( !$row ) {
166 // this revision does not exist?
167 return null;
168 }
169
170 // initialize fields for filestore image object
171 $this->loadFromRow( $row );
172 } else {
173 throw new MWException( 'This title does not correspond to an image page.' );
174 }
175 $this->exists = true;
176
177 return true;
178 }
179
180 /**
181 * Loads a file object from the filearchive table
182 *
183 * @param $row
184 *
185 * @return ArchivedFile
186 */
187 public static function newFromRow( $row ) {
188 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
189 $file->loadFromRow( $row );
190 return $file;
191 }
192
193 /**
194 * Load ArchivedFile object fields from a DB row.
195 *
196 * @param $row Object database row
197 * @since 1.21
198 */
199 public function loadFromRow( $row ) {
200 $this->id = intval($row->fa_id);
201 $this->name = $row->fa_name;
202 $this->archive_name = $row->fa_archive_name;
203 $this->group = $row->fa_storage_group;
204 $this->key = $row->fa_storage_key;
205 $this->size = $row->fa_size;
206 $this->bits = $row->fa_bits;
207 $this->width = $row->fa_width;
208 $this->height = $row->fa_height;
209 $this->metadata = $row->fa_metadata;
210 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
211 $this->media_type = $row->fa_media_type;
212 $this->description = $row->fa_description;
213 $this->user = $row->fa_user;
214 $this->user_text = $row->fa_user_text;
215 $this->timestamp = $row->fa_timestamp;
216 $this->deleted = $row->fa_deleted;
217 if( isset( $row->fa_sha1 ) ) {
218 $this->sha1 = $row->fa_sha1;
219 } else {
220 // old row, populate from key
221 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
222 }
223 }
224
225 /**
226 * Return the associated title object
227 *
228 * @return Title
229 */
230 public function getTitle() {
231 return $this->title;
232 }
233
234 /**
235 * Return the file name
236 *
237 * @return string
238 */
239 public function getName() {
240 return $this->name;
241 }
242
243 /**
244 * @return int
245 */
246 public function getID() {
247 $this->load();
248 return $this->id;
249 }
250
251 /**
252 * @return bool
253 */
254 public function exists() {
255 $this->load();
256 return $this->exists;
257 }
258
259 /**
260 * Return the FileStore key
261 * @return string
262 */
263 public function getKey() {
264 $this->load();
265 return $this->key;
266 }
267
268 /**
269 * Return the FileStore key (overriding base File class)
270 * @return string
271 */
272 public function getStorageKey() {
273 return $this->getKey();
274 }
275
276 /**
277 * Return the FileStore storage group
278 * @return string
279 */
280 public function getGroup() {
281 return $this->group;
282 }
283
284 /**
285 * Return the width of the image
286 * @return int
287 */
288 public function getWidth() {
289 $this->load();
290 return $this->width;
291 }
292
293 /**
294 * Return the height of the image
295 * @return int
296 */
297 public function getHeight() {
298 $this->load();
299 return $this->height;
300 }
301
302 /**
303 * Get handler-specific metadata
304 * @return string
305 */
306 public function getMetadata() {
307 $this->load();
308 return $this->metadata;
309 }
310
311 /**
312 * Return the size of the image file, in bytes
313 * @return int
314 */
315 public function getSize() {
316 $this->load();
317 return $this->size;
318 }
319
320 /**
321 * Return the bits of the image file, in bytes
322 * @return int
323 */
324 public function getBits() {
325 $this->load();
326 return $this->bits;
327 }
328
329 /**
330 * Returns the mime type of the file.
331 * @return string
332 */
333 public function getMimeType() {
334 $this->load();
335 return $this->mime;
336 }
337
338 /**
339 * Get a MediaHandler instance for this file
340 * @return MediaHandler
341 */
342 function getHandler() {
343 if ( !isset( $this->handler ) ) {
344 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
345 }
346 return $this->handler;
347 }
348
349 /**
350 * Returns the number of pages of a multipage document, or false for
351 * documents which aren't multipage documents
352 */
353 function pageCount() {
354 if ( !isset( $this->pageCount ) ) {
355 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
356 $this->pageCount = $this->handler->pageCount( $this );
357 } else {
358 $this->pageCount = false;
359 }
360 }
361 return $this->pageCount;
362 }
363
364 /**
365 * Return the type of the media in the file.
366 * Use the value returned by this function with the MEDIATYPE_xxx constants.
367 * @return string
368 */
369 public function getMediaType() {
370 $this->load();
371 return $this->media_type;
372 }
373
374 /**
375 * Return upload timestamp.
376 *
377 * @return string
378 */
379 public function getTimestamp() {
380 $this->load();
381 return wfTimestamp( TS_MW, $this->timestamp );
382 }
383
384 /**
385 * Get the SHA-1 base 36 hash of the file
386 *
387 * @return string
388 * @since 1.21
389 */
390 function getSha1() {
391 $this->load();
392 return $this->sha1;
393 }
394
395 /**
396 * Return the user ID of the uploader.
397 *
398 * @return int
399 */
400 public function getUser() {
401 $this->load();
402 if( $this->isDeleted( File::DELETED_USER ) ) {
403 return 0;
404 } else {
405 return $this->user;
406 }
407 }
408
409 /**
410 * Return the user name of the uploader.
411 *
412 * @return string
413 */
414 public function getUserText() {
415 $this->load();
416 if( $this->isDeleted( File::DELETED_USER ) ) {
417 return 0;
418 } else {
419 return $this->user_text;
420 }
421 }
422
423 /**
424 * Return upload description.
425 *
426 * @return string
427 */
428 public function getDescription() {
429 $this->load();
430 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
431 return 0;
432 } else {
433 return $this->description;
434 }
435 }
436
437 /**
438 * Return the user ID of the uploader.
439 *
440 * @return int
441 */
442 public function getRawUser() {
443 $this->load();
444 return $this->user;
445 }
446
447 /**
448 * Return the user name of the uploader.
449 *
450 * @return string
451 */
452 public function getRawUserText() {
453 $this->load();
454 return $this->user_text;
455 }
456
457 /**
458 * Return upload description.
459 *
460 * @return string
461 */
462 public function getRawDescription() {
463 $this->load();
464 return $this->description;
465 }
466
467 /**
468 * Returns the deletion bitfield
469 * @return int
470 */
471 public function getVisibility() {
472 $this->load();
473 return $this->deleted;
474 }
475
476 /**
477 * for file or revision rows
478 *
479 * @param $field Integer: one of DELETED_* bitfield constants
480 * @return bool
481 */
482 public function isDeleted( $field ) {
483 $this->load();
484 return ($this->deleted & $field) == $field;
485 }
486
487 /**
488 * Determine if the current user is allowed to view a particular
489 * field of this FileStore image file, if it's marked as deleted.
490 * @param $field Integer
491 * @param $user User object to check, or null to use $wgUser
492 * @return bool
493 */
494 public function userCan( $field, User $user = null ) {
495 $this->load();
496 return Revision::userCanBitfield( $this->deleted, $field, $user );
497 }
498 }