fixes #22501 follow up r61101 remove superfluous, buggy code that was over-rideing...
[lhc/web/wiklou.git] / includes / filerepo / ArchivedFile.php
1 <?php
2
3 /**
4 * @ingroup Media
5 */
6 class ArchivedFile
7 {
8 /**#@+
9 * @private
10 */
11 var $id, # filearchive row ID
12 $title, # image title
13 $name, # image name
14 $group, # FileStore storage group
15 $key, # FileStore sha1 key
16 $size, # file dimensions
17 $bits, # size in bytes
18 $width, # width
19 $height, # height
20 $metadata, # metadata string
21 $mime, # mime type
22 $media_type, # media type
23 $description, # upload description
24 $user, # user ID of uploader
25 $user_text, # user name of uploader
26 $timestamp, # time of upload
27 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
28 $deleted; # Bitfield akin to rev_deleted
29
30 /**#@-*/
31
32 function ArchivedFile( $title, $id=0, $key='' ) {
33 $this->id = -1;
34 $this->title = false;
35 $this->name = false;
36 $this->group = 'deleted'; // needed for direct use of constructor
37 $this->key = '';
38 $this->size = 0;
39 $this->bits = 0;
40 $this->width = 0;
41 $this->height = 0;
42 $this->metadata = '';
43 $this->mime = "unknown/unknown";
44 $this->media_type = '';
45 $this->description = '';
46 $this->user = 0;
47 $this->user_text = '';
48 $this->timestamp = null;
49 $this->deleted = 0;
50 $this->dataLoaded = false;
51 $this->exists = false;
52
53 if( is_object($title) ) {
54 $this->title = $title;
55 $this->name = $title->getDBkey();
56 }
57
58 if ($id)
59 $this->id = $id;
60
61 if ($key)
62 $this->key = $key;
63
64 if (!$id && !$key && !is_object($title))
65 throw new MWException( "No specifications provided to ArchivedFile constructor." );
66 }
67
68 /**
69 * Loads a file object from the filearchive table
70 * @return ResultWrapper
71 */
72 public function load() {
73 if ( $this->dataLoaded ) {
74 return true;
75 }
76 $conds = array();
77
78 if( $this->id > 0 )
79 $conds['fa_id'] = $this->id;
80 if( $this->key ) {
81 $conds['fa_storage_group'] = $this->group;
82 $conds['fa_storage_key'] = $this->key;
83 }
84 if( $this->title )
85 $conds['fa_name'] = $this->title->getDBkey();
86
87 if( !count($conds))
88 throw new MWException( "No specific information for retrieving archived file" );
89
90 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
91 $dbr = wfGetDB( DB_SLAVE );
92 $res = $dbr->select( 'filearchive',
93 array(
94 'fa_id',
95 'fa_name',
96 'fa_archive_name',
97 'fa_storage_key',
98 'fa_storage_group',
99 'fa_size',
100 'fa_bits',
101 'fa_width',
102 'fa_height',
103 'fa_metadata',
104 'fa_media_type',
105 'fa_major_mime',
106 'fa_minor_mime',
107 'fa_description',
108 'fa_user',
109 'fa_user_text',
110 'fa_timestamp',
111 'fa_deleted' ),
112 $conds,
113 __METHOD__,
114 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
115
116 if ( $dbr->numRows( $res ) == 0 ) {
117 // this revision does not exist?
118 return;
119 }
120 $ret = $dbr->resultObject( $res );
121 $row = $ret->fetchObject();
122
123 // initialize fields for filestore image object
124 $this->id = intval($row->fa_id);
125 $this->name = $row->fa_name;
126 $this->archive_name = $row->fa_archive_name;
127 $this->group = $row->fa_storage_group;
128 $this->key = $row->fa_storage_key;
129 $this->size = $row->fa_size;
130 $this->bits = $row->fa_bits;
131 $this->width = $row->fa_width;
132 $this->height = $row->fa_height;
133 $this->metadata = $row->fa_metadata;
134 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
135 $this->media_type = $row->fa_media_type;
136 $this->description = $row->fa_description;
137 $this->user = $row->fa_user;
138 $this->user_text = $row->fa_user_text;
139 $this->timestamp = $row->fa_timestamp;
140 $this->deleted = $row->fa_deleted;
141 } else {
142 throw new MWException( 'This title does not correspond to an image page.' );
143 return;
144 }
145 $this->dataLoaded = true;
146 $this->exists = true;
147
148 return true;
149 }
150
151 /**
152 * Loads a file object from the filearchive table
153 * @return ResultWrapper
154 */
155 public static function newFromRow( $row ) {
156 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
157
158 $file->id = intval($row->fa_id);
159 $file->name = $row->fa_name;
160 $file->archive_name = $row->fa_archive_name;
161 $file->group = $row->fa_storage_group;
162 $file->key = $row->fa_storage_key;
163 $file->size = $row->fa_size;
164 $file->bits = $row->fa_bits;
165 $file->width = $row->fa_width;
166 $file->height = $row->fa_height;
167 $file->metadata = $row->fa_metadata;
168 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
169 $file->media_type = $row->fa_media_type;
170 $file->description = $row->fa_description;
171 $file->user = $row->fa_user;
172 $file->user_text = $row->fa_user_text;
173 $file->timestamp = $row->fa_timestamp;
174 $file->deleted = $row->fa_deleted;
175
176 return $file;
177 }
178
179 /**
180 * Return the associated title object
181 * @public
182 */
183 public function getTitle() {
184 return $this->title;
185 }
186
187 /**
188 * Return the file name
189 */
190 public function getName() {
191 return $this->name;
192 }
193
194 public function getID() {
195 $this->load();
196 return $this->id;
197 }
198
199 public function exists() {
200 $this->load();
201 return $this->exists;
202 }
203
204 /**
205 * Return the FileStore key
206 */
207 public function getKey() {
208 $this->load();
209 return $this->key;
210 }
211
212 /**
213 * Return the FileStore key (overriding base File class)
214 */
215 public function getStorageKey() {
216 return $this->getKey();
217 }
218
219 /**
220 * Return the FileStore storage group
221 */
222 public function getGroup() {
223 return $file->group;
224 }
225
226 /**
227 * Return the width of the image
228 */
229 public function getWidth() {
230 $this->load();
231 return $this->width;
232 }
233
234 /**
235 * Return the height of the image
236 */
237 public function getHeight() {
238 $this->load();
239 return $this->height;
240 }
241
242 /**
243 * Get handler-specific metadata
244 */
245 public function getMetadata() {
246 $this->load();
247 return $this->metadata;
248 }
249
250 /**
251 * Return the size of the image file, in bytes
252 * @public
253 */
254 public function getSize() {
255 $this->load();
256 return $this->size;
257 }
258
259 /**
260 * Return the bits of the image file, in bytes
261 * @public
262 */
263 public function getBits() {
264 $this->load();
265 return $this->bits;
266 }
267
268 /**
269 * Returns the mime type of the file.
270 */
271 public function getMimeType() {
272 $this->load();
273 return $this->mime;
274 }
275
276 /**
277 * Return the type of the media in the file.
278 * Use the value returned by this function with the MEDIATYPE_xxx constants.
279 */
280 public function getMediaType() {
281 $this->load();
282 return $this->media_type;
283 }
284
285 /**
286 * Return upload timestamp.
287 */
288 public function getTimestamp() {
289 $this->load();
290 return wfTimestamp( TS_MW, $this->timestamp );
291 }
292
293 /**
294 * Return the user ID of the uploader.
295 */
296 public function getUser() {
297 $this->load();
298 if( $this->isDeleted( File::DELETED_USER ) ) {
299 return 0;
300 } else {
301 return $this->user;
302 }
303 }
304
305 /**
306 * Return the user name of the uploader.
307 */
308 public function getUserText() {
309 $this->load();
310 if( $this->isDeleted( File::DELETED_USER ) ) {
311 return 0;
312 } else {
313 return $this->user_text;
314 }
315 }
316
317 /**
318 * Return upload description.
319 */
320 public function getDescription() {
321 $this->load();
322 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
323 return 0;
324 } else {
325 return $this->description;
326 }
327 }
328
329 /**
330 * Return the user ID of the uploader.
331 */
332 public function getRawUser() {
333 $this->load();
334 return $this->user;
335 }
336
337 /**
338 * Return the user name of the uploader.
339 */
340 public function getRawUserText() {
341 $this->load();
342 return $this->user_text;
343 }
344
345 /**
346 * Return upload description.
347 */
348 public function getRawDescription() {
349 $this->load();
350 return $this->description;
351 }
352
353 /**
354 * Returns the deletion bitfield
355 * @return int
356 */
357 public function getVisibility() {
358 $this->load();
359 return $this->deleted;
360 }
361
362 /**
363 * int $field one of DELETED_* bitfield constants
364 * for file or revision rows
365 * @return bool
366 */
367 public function isDeleted( $field ) {
368 $this->load();
369 return ($this->deleted & $field) == $field;
370 }
371
372 /**
373 * Determine if the current user is allowed to view a particular
374 * field of this FileStore image file, if it's marked as deleted.
375 * @param int $field
376 * @return bool
377 */
378 public function userCan( $field ) {
379 $this->load();
380 return Revision::userCanBitfield( $this->deleted, $field );
381 }
382 }