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