Merge "Remove unused variables."
[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 $pageCount,
51 $archive_name;
52
53 /**
54 * @var MediaHandler
55 */
56 var $handler;
57 /**
58 * @var Title
59 */
60 var $title; # image title
61
62 /**#@-*/
63
64 /**
65 * @throws MWException
66 * @param Title $title
67 * @param int $id
68 * @param string $key
69 */
70 function __construct( $title, $id=0, $key='' ) {
71 $this->id = -1;
72 $this->title = false;
73 $this->name = false;
74 $this->group = 'deleted'; // needed for direct use of constructor
75 $this->key = '';
76 $this->size = 0;
77 $this->bits = 0;
78 $this->width = 0;
79 $this->height = 0;
80 $this->metadata = '';
81 $this->mime = "unknown/unknown";
82 $this->media_type = '';
83 $this->description = '';
84 $this->user = 0;
85 $this->user_text = '';
86 $this->timestamp = null;
87 $this->deleted = 0;
88 $this->dataLoaded = false;
89 $this->exists = false;
90
91 if( $title instanceof Title ) {
92 $this->title = File::normalizeTitle( $title, 'exception' );
93 $this->name = $title->getDBkey();
94 }
95
96 if ($id) {
97 $this->id = $id;
98 }
99
100 if ($key) {
101 $this->key = $key;
102 }
103
104 if ( !$id && !$key && !( $title instanceof Title ) ) {
105 throw new MWException( "No specifications provided to ArchivedFile constructor." );
106 }
107 }
108
109 /**
110 * Loads a file object from the filearchive table
111 * @throws MWException
112 * @return bool|null True on success or null
113 */
114 public function load() {
115 if ( $this->dataLoaded ) {
116 return true;
117 }
118 $conds = array();
119
120 if( $this->id > 0 ) {
121 $conds['fa_id'] = $this->id;
122 }
123 if( $this->key ) {
124 $conds['fa_storage_group'] = $this->group;
125 $conds['fa_storage_key'] = $this->key;
126 }
127 if( $this->title ) {
128 $conds['fa_name'] = $this->title->getDBkey();
129 }
130
131 if( !count($conds)) {
132 throw new MWException( "No specific information for retrieving archived file" );
133 }
134
135 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
136 $dbr = wfGetDB( DB_SLAVE );
137 $res = $dbr->select( 'filearchive',
138 array(
139 'fa_id',
140 'fa_name',
141 'fa_archive_name',
142 'fa_storage_key',
143 'fa_storage_group',
144 'fa_size',
145 'fa_bits',
146 'fa_width',
147 'fa_height',
148 'fa_metadata',
149 'fa_media_type',
150 'fa_major_mime',
151 'fa_minor_mime',
152 'fa_description',
153 'fa_user',
154 'fa_user_text',
155 'fa_timestamp',
156 'fa_deleted' ),
157 $conds,
158 __METHOD__,
159 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
160 if ( $res == false || $dbr->numRows( $res ) == 0 ) {
161 // this revision does not exist?
162 return null;
163 }
164 $ret = $dbr->resultObject( $res );
165 $row = $ret->fetchObject();
166
167 // initialize fields for filestore image object
168 $this->id = intval($row->fa_id);
169 $this->name = $row->fa_name;
170 $this->archive_name = $row->fa_archive_name;
171 $this->group = $row->fa_storage_group;
172 $this->key = $row->fa_storage_key;
173 $this->size = $row->fa_size;
174 $this->bits = $row->fa_bits;
175 $this->width = $row->fa_width;
176 $this->height = $row->fa_height;
177 $this->metadata = $row->fa_metadata;
178 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
179 $this->media_type = $row->fa_media_type;
180 $this->description = $row->fa_description;
181 $this->user = $row->fa_user;
182 $this->user_text = $row->fa_user_text;
183 $this->timestamp = $row->fa_timestamp;
184 $this->deleted = $row->fa_deleted;
185 } else {
186 throw new MWException( 'This title does not correspond to an image page.' );
187 }
188 $this->dataLoaded = true;
189 $this->exists = true;
190
191 return true;
192 }
193
194 /**
195 * Loads a file object from the filearchive table
196 *
197 * @param $row
198 *
199 * @return ArchivedFile
200 */
201 public static function newFromRow( $row ) {
202 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
203
204 $file->id = intval($row->fa_id);
205 $file->name = $row->fa_name;
206 $file->archive_name = $row->fa_archive_name;
207 $file->group = $row->fa_storage_group;
208 $file->key = $row->fa_storage_key;
209 $file->size = $row->fa_size;
210 $file->bits = $row->fa_bits;
211 $file->width = $row->fa_width;
212 $file->height = $row->fa_height;
213 $file->metadata = $row->fa_metadata;
214 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
215 $file->media_type = $row->fa_media_type;
216 $file->description = $row->fa_description;
217 $file->user = $row->fa_user;
218 $file->user_text = $row->fa_user_text;
219 $file->timestamp = $row->fa_timestamp;
220 $file->deleted = $row->fa_deleted;
221
222 return $file;
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 * Return the user ID of the uploader.
386 *
387 * @return int
388 */
389 public function getUser() {
390 $this->load();
391 if( $this->isDeleted( File::DELETED_USER ) ) {
392 return 0;
393 } else {
394 return $this->user;
395 }
396 }
397
398 /**
399 * Return the user name of the uploader.
400 *
401 * @return string
402 */
403 public function getUserText() {
404 $this->load();
405 if( $this->isDeleted( File::DELETED_USER ) ) {
406 return 0;
407 } else {
408 return $this->user_text;
409 }
410 }
411
412 /**
413 * Return upload description.
414 *
415 * @return string
416 */
417 public function getDescription() {
418 $this->load();
419 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
420 return 0;
421 } else {
422 return $this->description;
423 }
424 }
425
426 /**
427 * Return the user ID of the uploader.
428 *
429 * @return int
430 */
431 public function getRawUser() {
432 $this->load();
433 return $this->user;
434 }
435
436 /**
437 * Return the user name of the uploader.
438 *
439 * @return string
440 */
441 public function getRawUserText() {
442 $this->load();
443 return $this->user_text;
444 }
445
446 /**
447 * Return upload description.
448 *
449 * @return string
450 */
451 public function getRawDescription() {
452 $this->load();
453 return $this->description;
454 }
455
456 /**
457 * Returns the deletion bitfield
458 * @return int
459 */
460 public function getVisibility() {
461 $this->load();
462 return $this->deleted;
463 }
464
465 /**
466 * for file or revision rows
467 *
468 * @param $field Integer: one of DELETED_* bitfield constants
469 * @return bool
470 */
471 public function isDeleted( $field ) {
472 $this->load();
473 return ($this->deleted & $field) == $field;
474 }
475
476 /**
477 * Determine if the current user is allowed to view a particular
478 * field of this FileStore image file, if it's marked as deleted.
479 * @param $field Integer
480 * @param $user User object to check, or null to use $wgUser
481 * @return bool
482 */
483 public function userCan( $field, User $user = null ) {
484 $this->load();
485 return Revision::userCanBitfield( $this->deleted, $field, $user );
486 }
487 }