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