API: Fix list=deletedrevs paging bug pointed out by Splarka on IRC
[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 = '';
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
52 if( is_object($title) ) {
53 $this->title = $title;
54 $this->name = $title->getDBkey();
55 }
56
57 if ($id)
58 $this->id = $id;
59
60 if ($key)
61 $this->key = $key;
62
63 if (!$id && !$key && !is_object($title))
64 throw new MWException( "No specifications provided to ArchivedFile constructor." );
65 }
66
67 /**
68 * Loads a file object from the filearchive table
69 * @return ResultWrapper
70 */
71 public function load() {
72 if ( $this->dataLoaded ) {
73 return true;
74 }
75 $conds = array();
76
77 if( $this->id > 0 )
78 $conds['fa_id'] = $this->id;
79 if( $this->key ) {
80 $conds['fa_storage_group'] = $this->group;
81 $conds['fa_storage_key'] = $this->key;
82 }
83 if( $this->title )
84 $conds['fa_name'] = $this->title->getDBkey();
85
86 if( !count($conds))
87 throw new MWException( "No specific information for retrieving archived file" );
88
89 if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
90 $dbr = wfGetDB( DB_SLAVE );
91 $res = $dbr->select( 'filearchive',
92 array(
93 'fa_id',
94 'fa_name',
95 'fa_archive_name',
96 'fa_storage_key',
97 'fa_storage_group',
98 'fa_size',
99 'fa_bits',
100 'fa_width',
101 'fa_height',
102 'fa_metadata',
103 'fa_media_type',
104 'fa_major_mime',
105 'fa_minor_mime',
106 'fa_description',
107 'fa_user',
108 'fa_user_text',
109 'fa_timestamp',
110 'fa_deleted' ),
111 $conds,
112 __METHOD__,
113 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
114
115 if ( $dbr->numRows( $res ) == 0 ) {
116 // this revision does not exist?
117 return;
118 }
119 $ret = $dbr->resultObject( $res );
120 $row = $ret->fetchObject();
121
122 // initialize fields for filestore image object
123 $this->id = intval($row->fa_id);
124 $this->name = $row->fa_name;
125 $this->archive_name = $row->fa_archive_name;
126 $this->group = $row->fa_storage_group;
127 $this->key = $row->fa_storage_key;
128 $this->size = $row->fa_size;
129 $this->bits = $row->fa_bits;
130 $this->width = $row->fa_width;
131 $this->height = $row->fa_height;
132 $this->metadata = $row->fa_metadata;
133 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
134 $this->media_type = $row->fa_media_type;
135 $this->description = $row->fa_description;
136 $this->user = $row->fa_user;
137 $this->user_text = $row->fa_user_text;
138 $this->timestamp = $row->fa_timestamp;
139 $this->deleted = $row->fa_deleted;
140 } else {
141 throw new MWException( 'This title does not correspond to an image page.' );
142 return;
143 }
144 $this->dataLoaded = true;
145
146 return true;
147 }
148
149 /**
150 * Loads a file object from the filearchive table
151 * @return ResultWrapper
152 */
153 public static function newFromRow( $row ) {
154 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
155
156 $file->id = intval($row->fa_id);
157 $file->name = $row->fa_name;
158 $file->archive_name = $row->fa_archive_name;
159 $file->group = $row->fa_storage_group;
160 $file->key = $row->fa_storage_key;
161 $file->size = $row->fa_size;
162 $file->bits = $row->fa_bits;
163 $file->width = $row->fa_width;
164 $file->height = $row->fa_height;
165 $file->metadata = $row->fa_metadata;
166 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
167 $file->media_type = $row->fa_media_type;
168 $file->description = $row->fa_description;
169 $file->user = $row->fa_user;
170 $file->user_text = $row->fa_user_text;
171 $file->timestamp = $row->fa_timestamp;
172 $file->deleted = $row->fa_deleted;
173
174 return $file;
175 }
176
177 /**
178 * Return the associated title object
179 * @public
180 */
181 public function getTitle() {
182 return $this->title;
183 }
184
185 /**
186 * Return the file name
187 */
188 public function getName() {
189 return $this->name;
190 }
191
192 public function getID() {
193 $this->load();
194 return $this->id;
195 }
196
197 /**
198 * Return the FileStore key
199 */
200 public function getKey() {
201 $this->load();
202 return $this->key;
203 }
204
205 /**
206 * Return the FileStore storage group
207 */
208 public function getGroup() {
209 return $file->group;
210 }
211
212 /**
213 * Return the width of the image
214 */
215 public function getWidth() {
216 $this->load();
217 return $this->width;
218 }
219
220 /**
221 * Return the height of the image
222 */
223 public function getHeight() {
224 $this->load();
225 return $this->height;
226 }
227
228 /**
229 * Get handler-specific metadata
230 */
231 public function getMetadata() {
232 $this->load();
233 return $this->metadata;
234 }
235
236 /**
237 * Return the size of the image file, in bytes
238 * @public
239 */
240 public function getSize() {
241 $this->load();
242 return $this->size;
243 }
244
245 /**
246 * Return the bits of the image file, in bytes
247 * @public
248 */
249 public function getBits() {
250 $this->load();
251 return $this->bits;
252 }
253
254 /**
255 * Returns the mime type of the file.
256 */
257 public function getMimeType() {
258 $this->load();
259 return $this->mime;
260 }
261
262 /**
263 * Return the type of the media in the file.
264 * Use the value returned by this function with the MEDIATYPE_xxx constants.
265 */
266 public function getMediaType() {
267 $this->load();
268 return $this->media_type;
269 }
270
271 /**
272 * Return upload timestamp.
273 */
274 public function getTimestamp() {
275 $this->load();
276 return wfTimestamp( TS_MW, $this->timestamp );
277 }
278
279 /**
280 * Return the user ID of the uploader.
281 */
282 public function getUser() {
283 $this->load();
284 if( $this->isDeleted( File::DELETED_USER ) ) {
285 return 0;
286 } else {
287 return $this->user;
288 }
289 }
290
291 /**
292 * Return the user name of the uploader.
293 */
294 public function getUserText() {
295 $this->load();
296 if( $this->isDeleted( File::DELETED_USER ) ) {
297 return 0;
298 } else {
299 return $this->user_text;
300 }
301 }
302
303 /**
304 * Return upload description.
305 */
306 public function getDescription() {
307 $this->load();
308 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
309 return 0;
310 } else {
311 return $this->description;
312 }
313 }
314
315 /**
316 * Return the user ID of the uploader.
317 */
318 public function getRawUser() {
319 $this->load();
320 return $this->user;
321 }
322
323 /**
324 * Return the user name of the uploader.
325 */
326 public function getRawUserText() {
327 $this->load();
328 return $this->user_text;
329 }
330
331 /**
332 * Return upload description.
333 */
334 public function getRawDescription() {
335 $this->load();
336 return $this->description;
337 }
338
339 /**
340 * int $field one of DELETED_* bitfield constants
341 * for file or revision rows
342 * @return bool
343 */
344 public function isDeleted( $field ) {
345 return ($this->deleted & $field) == $field;
346 }
347
348 /**
349 * Determine if the current user is allowed to view a particular
350 * field of this FileStore image file, if it's marked as deleted.
351 * @param int $field
352 * @return bool
353 */
354 public function userCan( $field ) {
355 if( ($this->deleted & $field) == $field ) {
356 global $wgUser;
357 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
358 ? 'suppressrevision'
359 : 'deleterevision';
360 wfDebug( "Checking for $permission due to $field match on $this->deleted\n" );
361 return $wgUser->isAllowed( $permission );
362 } else {
363 return true;
364 }
365 }
366 }