Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / filerepo / file / ArchivedFile.php
blobb753e18a25d7638ec6e0fb8e7fb9db1e2c13a59e
1 <?php
2 /**
3 * Deleted file in the 'filearchive' table.
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.
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.
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
20 * @file
21 * @ingroup FileAbstraction
24 /**
25 * Class representing a row of the 'filearchive' table
27 * @ingroup FileAbstraction
29 class ArchivedFile {
30 /**#@+
31 * @private
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;
54 /**
55 * @var MediaHandler
57 var $handler;
58 /**
59 * @var Title
61 var $title; # image title
63 /**#@-*/
65 /**
66 * @throws MWException
67 * @param Title $title
68 * @param int $id
69 * @param string $key
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 = '';
93 if ( $title instanceof Title ) {
94 $this->title = File::normalizeTitle( $title, 'exception' );
95 $this->name = $title->getDBkey();
98 if ( $id ) {
99 $this->id = $id;
102 if ( $key ) {
103 $this->key = $key;
106 if ( !$id && !$key && !( $title instanceof Title ) ) {
107 throw new MWException( "No specifications provided to ArchivedFile constructor." );
112 * Loads a file object from the filearchive table
113 * @throws MWException
114 * @return bool|null True on success or null
116 public function load() {
117 if ( $this->dataLoaded ) {
118 return true;
120 $conds = array();
122 if ( $this->id > 0 ) {
123 $conds['fa_id'] = $this->id;
125 if ( $this->key ) {
126 $conds['fa_storage_group'] = $this->group;
127 $conds['fa_storage_key'] = $this->key;
129 if ( $this->title ) {
130 $conds['fa_name'] = $this->title->getDBkey();
133 if ( !count( $conds ) ) {
134 throw new MWException( "No specific information for retrieving archived file" );
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' )
147 if ( !$row ) {
148 // this revision does not exist?
149 return null;
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.' );
157 $this->exists = true;
159 return true;
163 * Loads a file object from the filearchive table
165 * @param $row
167 * @return ArchivedFile
169 public static function newFromRow( $row ) {
170 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
171 $file->loadFromRow( $row );
172 return $file;
176 * Fields in the filearchive table
177 * @return array
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_sha1',
204 * Load ArchivedFile object fields from a DB row.
206 * @param $row Object database row
207 * @since 1.21
209 public function loadFromRow( $row ) {
210 $this->id = intval( $row->fa_id );
211 $this->name = $row->fa_name;
212 $this->archive_name = $row->fa_archive_name;
213 $this->group = $row->fa_storage_group;
214 $this->key = $row->fa_storage_key;
215 $this->size = $row->fa_size;
216 $this->bits = $row->fa_bits;
217 $this->width = $row->fa_width;
218 $this->height = $row->fa_height;
219 $this->metadata = $row->fa_metadata;
220 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
221 $this->media_type = $row->fa_media_type;
222 $this->description = $row->fa_description;
223 $this->user = $row->fa_user;
224 $this->user_text = $row->fa_user_text;
225 $this->timestamp = $row->fa_timestamp;
226 $this->deleted = $row->fa_deleted;
227 if ( isset( $row->fa_sha1 ) ) {
228 $this->sha1 = $row->fa_sha1;
229 } else {
230 // old row, populate from key
231 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
236 * Return the associated title object
238 * @return Title
240 public function getTitle() {
241 return $this->title;
245 * Return the file name
247 * @return string
249 public function getName() {
250 return $this->name;
254 * @return int
256 public function getID() {
257 $this->load();
258 return $this->id;
262 * @return bool
264 public function exists() {
265 $this->load();
266 return $this->exists;
270 * Return the FileStore key
271 * @return string
273 public function getKey() {
274 $this->load();
275 return $this->key;
279 * Return the FileStore key (overriding base File class)
280 * @return string
282 public function getStorageKey() {
283 return $this->getKey();
287 * Return the FileStore storage group
288 * @return string
290 public function getGroup() {
291 return $this->group;
295 * Return the width of the image
296 * @return int
298 public function getWidth() {
299 $this->load();
300 return $this->width;
304 * Return the height of the image
305 * @return int
307 public function getHeight() {
308 $this->load();
309 return $this->height;
313 * Get handler-specific metadata
314 * @return string
316 public function getMetadata() {
317 $this->load();
318 return $this->metadata;
322 * Return the size of the image file, in bytes
323 * @return int
325 public function getSize() {
326 $this->load();
327 return $this->size;
331 * Return the bits of the image file, in bytes
332 * @return int
334 public function getBits() {
335 $this->load();
336 return $this->bits;
340 * Returns the mime type of the file.
341 * @return string
343 public function getMimeType() {
344 $this->load();
345 return $this->mime;
349 * Get a MediaHandler instance for this file
350 * @return MediaHandler
352 function getHandler() {
353 if ( !isset( $this->handler ) ) {
354 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
356 return $this->handler;
360 * Returns the number of pages of a multipage document, or false for
361 * documents which aren't multipage documents
363 function pageCount() {
364 if ( !isset( $this->pageCount ) ) {
365 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
366 $this->pageCount = $this->handler->pageCount( $this );
367 } else {
368 $this->pageCount = false;
371 return $this->pageCount;
375 * Return the type of the media in the file.
376 * Use the value returned by this function with the MEDIATYPE_xxx constants.
377 * @return string
379 public function getMediaType() {
380 $this->load();
381 return $this->media_type;
385 * Return upload timestamp.
387 * @return string
389 public function getTimestamp() {
390 $this->load();
391 return wfTimestamp( TS_MW, $this->timestamp );
395 * Get the SHA-1 base 36 hash of the file
397 * @return string
398 * @since 1.21
400 function getSha1() {
401 $this->load();
402 return $this->sha1;
406 * Return the user ID of the uploader.
408 * @return int
410 public function getUser() {
411 $this->load();
412 if ( $this->isDeleted( File::DELETED_USER ) ) {
413 return 0;
414 } else {
415 return $this->user;
420 * Return the user name of the uploader.
422 * @return string
424 public function getUserText() {
425 $this->load();
426 if ( $this->isDeleted( File::DELETED_USER ) ) {
427 return 0;
428 } else {
429 return $this->user_text;
434 * Return upload description.
436 * @return string
438 public function getDescription() {
439 $this->load();
440 if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
441 return 0;
442 } else {
443 return $this->description;
448 * Return the user ID of the uploader.
450 * @return int
452 public function getRawUser() {
453 $this->load();
454 return $this->user;
458 * Return the user name of the uploader.
460 * @return string
462 public function getRawUserText() {
463 $this->load();
464 return $this->user_text;
468 * Return upload description.
470 * @return string
472 public function getRawDescription() {
473 $this->load();
474 return $this->description;
478 * Returns the deletion bitfield
479 * @return int
481 public function getVisibility() {
482 $this->load();
483 return $this->deleted;
487 * for file or revision rows
489 * @param $field Integer: one of DELETED_* bitfield constants
490 * @return bool
492 public function isDeleted( $field ) {
493 $this->load();
494 return ($this->deleted & $field) == $field;
498 * Determine if the current user is allowed to view a particular
499 * field of this FileStore image file, if it's marked as deleted.
500 * @param $field Integer
501 * @param $user User object to check, or null to use $wgUser
502 * @return bool
504 public function userCan( $field, User $user = null ) {
505 $this->load();
506 return Revision::userCanBitfield( $this->deleted, $field, $user );