Remove silly 'm' prefix from StubObject/DeprecatedGlobal members
[mediawiki.git] / includes / filerepo / file / ArchivedFile.php
blob845fd7157782f668ca70cfe98ea8c800b3940536
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 /** @var int filearchive row ID */
31 private $id;
33 /** @var string File name */
34 private $name;
36 /** @var string FileStore storage group */
37 private $group;
39 /** @var string FileStore SHA-1 key */
40 private $key;
42 /** @var int File size in bytes */
43 private $size;
45 /** @var int size in bytes */
46 private $bits;
48 /** @var int Width */
49 private $width;
51 /** @var int Height */
52 private $height;
54 /** @var string Metadata string */
55 private $metadata;
57 /** @var string MIME type */
58 private $mime;
60 /** @var string Media type */
61 private $media_type;
63 /** @var string Upload description */
64 private $description;
66 /** @var int User ID of uploader */
67 private $user;
69 /** @var string User name of uploader */
70 private $user_text;
72 /** @var string Time of upload */
73 private $timestamp;
75 /** @var bool Whether or not all this has been loaded from the database (loadFromXxx) */
76 private $dataLoaded;
78 /** @var int Bitfield akin to rev_deleted */
79 private $deleted;
81 /** @var string SHA-1 hash of file content */
82 private $sha1;
84 /** @var string Number of pages of a multipage document, or false for
85 * documents which aren't multipage documents
87 private $pageCount;
89 /** @var string Original base filename */
90 private $archive_name;
92 /** @var MediaHandler */
93 protected $handler;
95 /** @var Title */
96 protected $title; # image title
98 /**
99 * @throws MWException
100 * @param Title $title
101 * @param int $id
102 * @param string $key
104 function __construct( $title, $id = 0, $key = '' ) {
105 $this->id = -1;
106 $this->title = false;
107 $this->name = false;
108 $this->group = 'deleted'; // needed for direct use of constructor
109 $this->key = '';
110 $this->size = 0;
111 $this->bits = 0;
112 $this->width = 0;
113 $this->height = 0;
114 $this->metadata = '';
115 $this->mime = "unknown/unknown";
116 $this->media_type = '';
117 $this->description = '';
118 $this->user = 0;
119 $this->user_text = '';
120 $this->timestamp = null;
121 $this->deleted = 0;
122 $this->dataLoaded = false;
123 $this->exists = false;
124 $this->sha1 = '';
126 if ( $title instanceof Title ) {
127 $this->title = File::normalizeTitle( $title, 'exception' );
128 $this->name = $title->getDBkey();
131 if ( $id ) {
132 $this->id = $id;
135 if ( $key ) {
136 $this->key = $key;
139 if ( !$id && !$key && !( $title instanceof Title ) ) {
140 throw new MWException( "No specifications provided to ArchivedFile constructor." );
145 * Loads a file object from the filearchive table
146 * @throws MWException
147 * @return bool|null True on success or null
149 public function load() {
150 if ( $this->dataLoaded ) {
151 return true;
153 $conds = array();
155 if ( $this->id > 0 ) {
156 $conds['fa_id'] = $this->id;
158 if ( $this->key ) {
159 $conds['fa_storage_group'] = $this->group;
160 $conds['fa_storage_key'] = $this->key;
162 if ( $this->title ) {
163 $conds['fa_name'] = $this->title->getDBkey();
166 if ( !count( $conds ) ) {
167 throw new MWException( "No specific information for retrieving archived file" );
170 if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
171 $this->dataLoaded = true; // set it here, to have also true on miss
172 $dbr = wfGetDB( DB_SLAVE );
173 $row = $dbr->selectRow(
174 'filearchive',
175 self::selectFields(),
176 $conds,
177 __METHOD__,
178 array( 'ORDER BY' => 'fa_timestamp DESC' )
180 if ( !$row ) {
181 // this revision does not exist?
182 return null;
185 // initialize fields for filestore image object
186 $this->loadFromRow( $row );
187 } else {
188 throw new MWException( 'This title does not correspond to an image page.' );
190 $this->exists = true;
192 return true;
196 * Loads a file object from the filearchive table
198 * @param stdClass $row
199 * @return ArchivedFile
201 public static function newFromRow( $row ) {
202 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
203 $file->loadFromRow( $row );
205 return $file;
209 * Fields in the filearchive table
210 * @return array
212 static function selectFields() {
213 return array(
214 'fa_id',
215 'fa_name',
216 'fa_archive_name',
217 'fa_storage_key',
218 'fa_storage_group',
219 'fa_size',
220 'fa_bits',
221 'fa_width',
222 'fa_height',
223 'fa_metadata',
224 'fa_media_type',
225 'fa_major_mime',
226 'fa_minor_mime',
227 'fa_description',
228 'fa_user',
229 'fa_user_text',
230 'fa_timestamp',
231 'fa_deleted',
232 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
233 'fa_sha1',
238 * Load ArchivedFile object fields from a DB row.
240 * @param stdClass $row Object database row
241 * @since 1.21
243 public function loadFromRow( $row ) {
244 $this->id = intval( $row->fa_id );
245 $this->name = $row->fa_name;
246 $this->archive_name = $row->fa_archive_name;
247 $this->group = $row->fa_storage_group;
248 $this->key = $row->fa_storage_key;
249 $this->size = $row->fa_size;
250 $this->bits = $row->fa_bits;
251 $this->width = $row->fa_width;
252 $this->height = $row->fa_height;
253 $this->metadata = $row->fa_metadata;
254 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
255 $this->media_type = $row->fa_media_type;
256 $this->description = $row->fa_description;
257 $this->user = $row->fa_user;
258 $this->user_text = $row->fa_user_text;
259 $this->timestamp = $row->fa_timestamp;
260 $this->deleted = $row->fa_deleted;
261 if ( isset( $row->fa_sha1 ) ) {
262 $this->sha1 = $row->fa_sha1;
263 } else {
264 // old row, populate from key
265 $this->sha1 = LocalRepo::getHashFromKey( $this->key );
270 * Return the associated title object
272 * @return Title
274 public function getTitle() {
275 return $this->title;
279 * Return the file name
281 * @return string
283 public function getName() {
284 return $this->name;
288 * @return int
290 public function getID() {
291 $this->load();
293 return $this->id;
297 * @return bool
299 public function exists() {
300 $this->load();
302 return $this->exists;
306 * Return the FileStore key
307 * @return string
309 public function getKey() {
310 $this->load();
312 return $this->key;
316 * Return the FileStore key (overriding base File class)
317 * @return string
319 public function getStorageKey() {
320 return $this->getKey();
324 * Return the FileStore storage group
325 * @return string
327 public function getGroup() {
328 return $this->group;
332 * Return the width of the image
333 * @return int
335 public function getWidth() {
336 $this->load();
338 return $this->width;
342 * Return the height of the image
343 * @return int
345 public function getHeight() {
346 $this->load();
348 return $this->height;
352 * Get handler-specific metadata
353 * @return string
355 public function getMetadata() {
356 $this->load();
358 return $this->metadata;
362 * Return the size of the image file, in bytes
363 * @return int
365 public function getSize() {
366 $this->load();
368 return $this->size;
372 * Return the bits of the image file, in bytes
373 * @return int
375 public function getBits() {
376 $this->load();
378 return $this->bits;
382 * Returns the mime type of the file.
383 * @return string
385 public function getMimeType() {
386 $this->load();
388 return $this->mime;
392 * Get a MediaHandler instance for this file
393 * @return MediaHandler
395 function getHandler() {
396 if ( !isset( $this->handler ) ) {
397 $this->handler = MediaHandler::getHandler( $this->getMimeType() );
400 return $this->handler;
404 * Returns the number of pages of a multipage document, or false for
405 * documents which aren't multipage documents
407 function pageCount() {
408 if ( !isset( $this->pageCount ) ) {
409 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
410 $this->pageCount = $this->handler->pageCount( $this );
411 } else {
412 $this->pageCount = false;
416 return $this->pageCount;
420 * Return the type of the media in the file.
421 * Use the value returned by this function with the MEDIATYPE_xxx constants.
422 * @return string
424 public function getMediaType() {
425 $this->load();
427 return $this->media_type;
431 * Return upload timestamp.
433 * @return string
435 public function getTimestamp() {
436 $this->load();
438 return wfTimestamp( TS_MW, $this->timestamp );
442 * Get the SHA-1 base 36 hash of the file
444 * @return string
445 * @since 1.21
447 function getSha1() {
448 $this->load();
450 return $this->sha1;
454 * Returns ID or name of user who uploaded the file
456 * @note Prior to MediaWiki 1.23, this method always
457 * returned the user id, and was inconsistent with
458 * the rest of the file classes.
459 * @param string $type 'text' or 'id'
460 * @return int|string
461 * @throws MWException
463 public function getUser( $type = 'text' ) {
464 $this->load();
466 if ( $type == 'text' ) {
467 return $this->user_text;
468 } elseif ( $type == 'id' ) {
469 return $this->user;
472 throw new MWException( "Unknown type '$type'." );
476 * Return the user name of the uploader.
478 * @deprecated since 1.23 Use getUser( 'text' ) instead.
479 * @return string
481 public function getUserText() {
482 wfDeprecated( __METHOD__, '1.23' );
483 $this->load();
484 if ( $this->isDeleted( File::DELETED_USER ) ) {
485 return 0;
486 } else {
487 return $this->user_text;
492 * Return upload description.
494 * @return string
496 public function getDescription() {
497 $this->load();
498 if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
499 return 0;
500 } else {
501 return $this->description;
506 * Return the user ID of the uploader.
508 * @return int
510 public function getRawUser() {
511 $this->load();
513 return $this->user;
517 * Return the user name of the uploader.
519 * @return string
521 public function getRawUserText() {
522 $this->load();
524 return $this->user_text;
528 * Return upload description.
530 * @return string
532 public function getRawDescription() {
533 $this->load();
535 return $this->description;
539 * Returns the deletion bitfield
540 * @return int
542 public function getVisibility() {
543 $this->load();
545 return $this->deleted;
549 * for file or revision rows
551 * @param int $field One of DELETED_* bitfield constants
552 * @return bool
554 public function isDeleted( $field ) {
555 $this->load();
557 return ( $this->deleted & $field ) == $field;
561 * Determine if the current user is allowed to view a particular
562 * field of this FileStore image file, if it's marked as deleted.
563 * @param int $field
564 * @param null|User $user User object to check, or null to use $wgUser
565 * @return bool
567 public function userCan( $field, User $user = null ) {
568 $this->load();
570 return Revision::userCanBitfield( $this->deleted, $field, $user );