Merge "Installer: page refresh should refresh list of supported DBs"
[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(
141 'filearchive',
142 self::selectFields(),
143 $conds,
144 __METHOD__,
145 array( 'ORDER BY' => 'fa_timestamp DESC' )
146 );
147 if ( !$row ) {
148 // this revision does not exist?
149 return null;
150 }
151
152 // initialize fields for filestore image object
153 $this->loadFromRow( $row );
154 } else {
155 throw new MWException( 'This title does not correspond to an image page.' );
156 }
157 $this->exists = true;
158
159 return true;
160 }
161
162 /**
163 * Loads a file object from the filearchive table
164 *
165 * @param $row
166 *
167 * @return ArchivedFile
168 */
169 public static function newFromRow( $row ) {
170 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
171 $file->loadFromRow( $row );
172 return $file;
173 }
174
175 /**
176 * Fields in the filearchive table
177 * @return array
178 */
179 static function selectFields() {
180 return array(
181 'fa_id',
182 'fa_name',
183 'fa_archive_name',
184 'fa_storage_key',
185 'fa_storage_group',
186 'fa_size',
187 'fa_bits',
188 'fa_width',
189 'fa_height',
190 'fa_metadata',
191 'fa_media_type',
192 'fa_major_mime',
193 'fa_minor_mime',
194 'fa_description',
195 'fa_user',
196 'fa_user_text',
197 'fa_timestamp',
198 'fa_deleted',
199 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
200 'fa_sha1',
201 );
202 }
203
204 /**
205 * Load ArchivedFile object fields from a DB row.
206 *
207 * @param $row Object database row
208 * @since 1.21
209 */
210 public function loadFromRow( $row ) {
211 $this->id = intval( $row->fa_id );
212 $this->name = $row->fa_name;
213 $this->archive_name = $row->fa_archive_name;
214 $this->group = $row->fa_storage_group;
215 $this->key = $row->fa_storage_key;
216 $this->size = $row->fa_size;
217 $this->bits = $row->fa_bits;
218 $this->width = $row->fa_width;
219 $this->height = $row->fa_height;
220 $this->metadata = $row->fa_metadata;
221 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
222 $this->media_type = $row->fa_media_type;
223 $this->description = $row->fa_description;
224 $this->user = $row->fa_user;
225 $this->user_text = $row->fa_user_text;
226 $this->timestamp = $row->fa_timestamp;
227 $this->deleted = $row->fa_deleted;
228 if ( isset( $row->fa_sha1 ) ) {
229 $this->sha1 = $row->fa_sha1;
230 } else {
231 // old row, populate from key
232 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
233 }
234 }
235
236 /**
237 * Return the associated title object
238 *
239 * @return Title
240 */
241 public function getTitle() {
242 return $this->title;
243 }
244
245 /**
246 * Return the file name
247 *
248 * @return string
249 */
250 public function getName() {
251 return $this->name;
252 }
253
254 /**
255 * @return int
256 */
257 public function getID() {
258 $this->load();
259 return $this->id;
260 }
261
262 /**
263 * @return bool
264 */
265 public function exists() {
266 $this->load();
267 return $this->exists;
268 }
269
270 /**
271 * Return the FileStore key
272 * @return string
273 */
274 public function getKey() {
275 $this->load();
276 return $this->key;
277 }
278
279 /**
280 * Return the FileStore key (overriding base File class)
281 * @return string
282 */
283 public function getStorageKey() {
284 return $this->getKey();
285 }
286
287 /**
288 * Return the FileStore storage group
289 * @return string
290 */
291 public function getGroup() {
292 return $this->group;
293 }
294
295 /**
296 * Return the width of the image
297 * @return int
298 */
299 public function getWidth() {
300 $this->load();
301 return $this->width;
302 }
303
304 /**
305 * Return the height of the image
306 * @return int
307 */
308 public function getHeight() {
309 $this->load();
310 return $this->height;
311 }
312
313 /**
314 * Get handler-specific metadata
315 * @return string
316 */
317 public function getMetadata() {
318 $this->load();
319 return $this->metadata;
320 }
321
322 /**
323 * Return the size of the image file, in bytes
324 * @return int
325 */
326 public function getSize() {
327 $this->load();
328 return $this->size;
329 }
330
331 /**
332 * Return the bits of the image file, in bytes
333 * @return int
334 */
335 public function getBits() {
336 $this->load();
337 return $this->bits;
338 }
339
340 /**
341 * Returns the mime type of the file.
342 * @return string
343 */
344 public function getMimeType() {
345 $this->load();
346 return $this->mime;
347 }
348
349 /**
350 * Get a MediaHandler instance for this file
351 * @return MediaHandler
352 */
353 function getHandler() {
354 if ( !isset( $this->handler ) ) {
355 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
356 }
357 return $this->handler;
358 }
359
360 /**
361 * Returns the number of pages of a multipage document, or false for
362 * documents which aren't multipage documents
363 */
364 function pageCount() {
365 if ( !isset( $this->pageCount ) ) {
366 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
367 $this->pageCount = $this->handler->pageCount( $this );
368 } else {
369 $this->pageCount = false;
370 }
371 }
372 return $this->pageCount;
373 }
374
375 /**
376 * Return the type of the media in the file.
377 * Use the value returned by this function with the MEDIATYPE_xxx constants.
378 * @return string
379 */
380 public function getMediaType() {
381 $this->load();
382 return $this->media_type;
383 }
384
385 /**
386 * Return upload timestamp.
387 *
388 * @return string
389 */
390 public function getTimestamp() {
391 $this->load();
392 return wfTimestamp( TS_MW, $this->timestamp );
393 }
394
395 /**
396 * Get the SHA-1 base 36 hash of the file
397 *
398 * @return string
399 * @since 1.21
400 */
401 function getSha1() {
402 $this->load();
403 return $this->sha1;
404 }
405
406 /**
407 * Return the user ID of the uploader.
408 *
409 * @return int
410 */
411 public function getUser() {
412 $this->load();
413 if ( $this->isDeleted( File::DELETED_USER ) ) {
414 return 0;
415 } else {
416 return $this->user;
417 }
418 }
419
420 /**
421 * Return the user name of the uploader.
422 *
423 * @return string
424 */
425 public function getUserText() {
426 $this->load();
427 if ( $this->isDeleted( File::DELETED_USER ) ) {
428 return 0;
429 } else {
430 return $this->user_text;
431 }
432 }
433
434 /**
435 * Return upload description.
436 *
437 * @return string
438 */
439 public function getDescription() {
440 $this->load();
441 if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
442 return 0;
443 } else {
444 return $this->description;
445 }
446 }
447
448 /**
449 * Return the user ID of the uploader.
450 *
451 * @return int
452 */
453 public function getRawUser() {
454 $this->load();
455 return $this->user;
456 }
457
458 /**
459 * Return the user name of the uploader.
460 *
461 * @return string
462 */
463 public function getRawUserText() {
464 $this->load();
465 return $this->user_text;
466 }
467
468 /**
469 * Return upload description.
470 *
471 * @return string
472 */
473 public function getRawDescription() {
474 $this->load();
475 return $this->description;
476 }
477
478 /**
479 * Returns the deletion bitfield
480 * @return int
481 */
482 public function getVisibility() {
483 $this->load();
484 return $this->deleted;
485 }
486
487 /**
488 * for file or revision rows
489 *
490 * @param $field Integer: one of DELETED_* bitfield constants
491 * @return bool
492 */
493 public function isDeleted( $field ) {
494 $this->load();
495 return ( $this->deleted & $field ) == $field;
496 }
497
498 /**
499 * Determine if the current user is allowed to view a particular
500 * field of this FileStore image file, if it's marked as deleted.
501 * @param $field Integer
502 * @param $user User object to check, or null to use $wgUser
503 * @return bool
504 */
505 public function userCan( $field, User $user = null ) {
506 $this->load();
507 return Revision::userCanBitfield( $this->deleted, $field, $user );
508 }
509 }