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