ApiEmailUser.php: fix copyright symbol, spacing and coding style tweaks, spaces ...
[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 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 ArchivedFile
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 */
182 public function getTitle() {
183 return $this->title;
184 }
185
186 /**
187 * Return the file name
188 */
189 public function getName() {
190 return $this->name;
191 }
192
193 public function getID() {
194 $this->load();
195 return $this->id;
196 }
197
198 public function exists() {
199 $this->load();
200 return $this->exists;
201 }
202
203 /**
204 * Return the FileStore key
205 */
206 public function getKey() {
207 $this->load();
208 return $this->key;
209 }
210
211 /**
212 * Return the FileStore key (overriding base File class)
213 */
214 public function getStorageKey() {
215 return $this->getKey();
216 }
217
218 /**
219 * Return the FileStore storage group
220 */
221 public function getGroup() {
222 return $file->group;
223 }
224
225 /**
226 * Return the width of the image
227 */
228 public function getWidth() {
229 $this->load();
230 return $this->width;
231 }
232
233 /**
234 * Return the height of the image
235 */
236 public function getHeight() {
237 $this->load();
238 return $this->height;
239 }
240
241 /**
242 * Get handler-specific metadata
243 */
244 public function getMetadata() {
245 $this->load();
246 return $this->metadata;
247 }
248
249 /**
250 * Return the size of the image file, in bytes
251 */
252 public function getSize() {
253 $this->load();
254 return $this->size;
255 }
256
257 /**
258 * Return the bits of the image file, in bytes
259 */
260 public function getBits() {
261 $this->load();
262 return $this->bits;
263 }
264
265 /**
266 * Returns the mime type of the file.
267 */
268 public function getMimeType() {
269 $this->load();
270 return $this->mime;
271 }
272
273 /**
274 * Return the type of the media in the file.
275 * Use the value returned by this function with the MEDIATYPE_xxx constants.
276 */
277 public function getMediaType() {
278 $this->load();
279 return $this->media_type;
280 }
281
282 /**
283 * Return upload timestamp.
284 */
285 public function getTimestamp() {
286 $this->load();
287 return wfTimestamp( TS_MW, $this->timestamp );
288 }
289
290 /**
291 * Return the user ID of the uploader.
292 */
293 public function getUser() {
294 $this->load();
295 if( $this->isDeleted( File::DELETED_USER ) ) {
296 return 0;
297 } else {
298 return $this->user;
299 }
300 }
301
302 /**
303 * Return the user name of the uploader.
304 */
305 public function getUserText() {
306 $this->load();
307 if( $this->isDeleted( File::DELETED_USER ) ) {
308 return 0;
309 } else {
310 return $this->user_text;
311 }
312 }
313
314 /**
315 * Return upload description.
316 */
317 public function getDescription() {
318 $this->load();
319 if( $this->isDeleted( File::DELETED_COMMENT ) ) {
320 return 0;
321 } else {
322 return $this->description;
323 }
324 }
325
326 /**
327 * Return the user ID of the uploader.
328 */
329 public function getRawUser() {
330 $this->load();
331 return $this->user;
332 }
333
334 /**
335 * Return the user name of the uploader.
336 */
337 public function getRawUserText() {
338 $this->load();
339 return $this->user_text;
340 }
341
342 /**
343 * Return upload description.
344 */
345 public function getRawDescription() {
346 $this->load();
347 return $this->description;
348 }
349
350 /**
351 * Returns the deletion bitfield
352 * @return int
353 */
354 public function getVisibility() {
355 $this->load();
356 return $this->deleted;
357 }
358
359 /**
360 * for file or revision rows
361 *
362 * @param $field Integer: one of DELETED_* bitfield constants
363 * @return bool
364 */
365 public function isDeleted( $field ) {
366 $this->load();
367 return ($this->deleted & $field) == $field;
368 }
369
370 /**
371 * Determine if the current user is allowed to view a particular
372 * field of this FileStore image file, if it's marked as deleted.
373 * @param $field Integer
374 * @return bool
375 */
376 public function userCan( $field ) {
377 $this->load();
378 return Revision::userCanBitfield( $this->deleted, $field );
379 }
380 }