368a561edf29a86df6622399837f912cc7c454ed
[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 true on success or null
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 }
144 $this->dataLoaded = true;
145 $this->exists = true;
146
147 return true;
148 }
149
150 /**
151 * Loads a file object from the filearchive table
152 * @return ArchivedFile
153 */
154 public static function newFromRow( $row ) {
155 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
156
157 $file->id = intval($row->fa_id);
158 $file->name = $row->fa_name;
159 $file->archive_name = $row->fa_archive_name;
160 $file->group = $row->fa_storage_group;
161 $file->key = $row->fa_storage_key;
162 $file->size = $row->fa_size;
163 $file->bits = $row->fa_bits;
164 $file->width = $row->fa_width;
165 $file->height = $row->fa_height;
166 $file->metadata = $row->fa_metadata;
167 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
168 $file->media_type = $row->fa_media_type;
169 $file->description = $row->fa_description;
170 $file->user = $row->fa_user;
171 $file->user_text = $row->fa_user_text;
172 $file->timestamp = $row->fa_timestamp;
173 $file->deleted = $row->fa_deleted;
174
175 return $file;
176 }
177
178 /**
179 * Return the associated title object
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 public function exists() {
198 $this->load();
199 return $this->exists;
200 }
201
202 /**
203 * Return the FileStore key
204 */
205 public function getKey() {
206 $this->load();
207 return $this->key;
208 }
209
210 /**
211 * Return the FileStore key (overriding base File class)
212 */
213 public function getStorageKey() {
214 return $this->getKey();
215 }
216
217 /**
218 * Return the FileStore storage group
219 */
220 public function getGroup() {
221 return $file->group;
222 }
223
224 /**
225 * Return the width of the image
226 */
227 public function getWidth() {
228 $this->load();
229 return $this->width;
230 }
231
232 /**
233 * Return the height of the image
234 */
235 public function getHeight() {
236 $this->load();
237 return $this->height;
238 }
239
240 /**
241 * Get handler-specific metadata
242 */
243 public function getMetadata() {
244 $this->load();
245 return $this->metadata;
246 }
247
248 /**
249 * Return the size of the image file, in bytes
250 */
251 public function getSize() {
252 $this->load();
253 return $this->size;
254 }
255
256 /**
257 * Return the bits of the image file, in bytes
258 */
259 public function getBits() {
260 $this->load();
261 return $this->bits;
262 }
263
264 /**
265 * Returns the mime type of the file.
266 */
267 public function getMimeType() {
268 $this->load();
269 return $this->mime;
270 }
271
272 /**
273 * Return the type of the media in the file.
274 * Use the value returned by this function with the MEDIATYPE_xxx constants.
275 */
276 public function getMediaType() {
277 $this->load();
278 return $this->media_type;
279 }
280
281 /**
282 * Return upload timestamp.
283 */
284 public function getTimestamp() {
285 $this->load();
286 return wfTimestamp( TS_MW, $this->timestamp );
287 }
288
289 /**
290 * Return the user ID of the uploader.
291 */
292 public function getUser() {
293 $this->load();
294 if( $this->isDeleted( File::DELETED_USER ) ) {
295 return 0;
296 } else {
297 return $this->user;
298 }
299 }
300
301 /**
302 * Return the user name of the uploader.
303 */
304 public function getUserText() {
305 $this->load();
306 if( $this->isDeleted( File::DELETED_USER ) ) {
307 return 0;
308 } else {
309 return $this->user_text;
310 }
311 }
312
313 /**
314 * Return upload description.
315 */
316 public function getDescription() {
317 $this->load();
318 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
319 return 0;
320 } else {
321 return $this->description;
322 }
323 }
324
325 /**
326 * Return the user ID of the uploader.
327 */
328 public function getRawUser() {
329 $this->load();
330 return $this->user;
331 }
332
333 /**
334 * Return the user name of the uploader.
335 */
336 public function getRawUserText() {
337 $this->load();
338 return $this->user_text;
339 }
340
341 /**
342 * Return upload description.
343 */
344 public function getRawDescription() {
345 $this->load();
346 return $this->description;
347 }
348
349 /**
350 * Returns the deletion bitfield
351 * @return int
352 */
353 public function getVisibility() {
354 $this->load();
355 return $this->deleted;
356 }
357
358 /**
359 * for file or revision rows
360 *
361 * @param $field Integer: one of DELETED_* bitfield constants
362 * @return bool
363 */
364 public function isDeleted( $field ) {
365 $this->load();
366 return ($this->deleted & $field) == $field;
367 }
368
369 /**
370 * Determine if the current user is allowed to view a particular
371 * field of this FileStore image file, if it's marked as deleted.
372 * @param $field Integer
373 * @return bool
374 */
375 public function userCan( $field ) {
376 $this->load();
377 return Revision::userCanBitfield( $this->deleted, $field );
378 }
379 }