*Clean up deletion of revisions and remove some gaps
[lhc/web/wiklou.git] / includes / filerepo / ArchivedFile.php
1 <?php
2
3 /**
4 * @addtogroup Media
5 */
6 class ArchivedFile
7 {
8 function ArchivedFile( $title, $id=0, $key='' ) {
9 if( !is_object( $title ) ) {
10 throw new MWException( 'ArchivedFile constructor given bogus title.' );
11 }
12 $this->id = -1;
13 $this->title = $title;
14 $this->name = $title->getDBKey();
15 $this->group = '';
16 $this->key = '';
17 $this->size = 0;
18 $this->bits = 0;
19 $this->width = 0;
20 $this->height = 0;
21 $this->metaData = '';
22 $this->mime = "unknown/unknown";
23 $this->type = '';
24 $this->description = '';
25 $this->user = 0;
26 $this->userText = '';
27 $this->timestamp = NULL;
28 $this->deleted = 0;
29 # BC, load if these are specified
30 if( $id || $key ) {
31 $this->load();
32 }
33 }
34
35 /**
36 * Loads a file object from the filearchive table
37 * @return ResultWrapper
38 */
39 function load() {
40 if( !is_object( $this->title ) ) {
41 throw new MWException( 'ArchivedFile constructor given bogus title.' );
42 }
43 $conds = ($this->id) ? "fa_id = {$this->id}" : "fa_storage_key = '{$this->key}'";
44 if( $this->title->getNamespace() == NS_IMAGE ) {
45 $dbr = wfGetDB( DB_SLAVE );
46 $res = $dbr->select( 'filearchive',
47 array(
48 'fa_id',
49 'fa_name',
50 'fa_archive_name',
51 'fa_storage_key',
52 'fa_storage_group',
53 'fa_size',
54 'fa_bits',
55 'fa_width',
56 'fa_height',
57 'fa_metadata',
58 'fa_media_type',
59 'fa_major_mime',
60 'fa_minor_mime',
61 'fa_description',
62 'fa_user',
63 'fa_user_text',
64 'fa_timestamp',
65 'fa_deleted' ),
66 array(
67 'fa_name' => $this->title->getDBKey(),
68 $conds ),
69 __METHOD__,
70 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
71
72 if ( $dbr->numRows( $res ) == 0 ) {
73 // this revision does not exist?
74 return;
75 }
76 $ret = $dbr->resultObject( $res );
77 $row = $ret->fetchObject();
78
79 // initialize fields for filestore image object
80 $this->id = intval($row->fa_id);
81 $this->name = $row->fa_name;
82 $this->archive_name = $row->fa_archive_name;
83 $this->group = $row->fa_storage_group;
84 $this->key = $row->fa_storage_key;
85 $this->size = $row->fa_size;
86 $this->bits = $row->fa_bits;
87 $this->width = $row->fa_width;
88 $this->height = $row->fa_height;
89 $this->metaData = $row->fa_metadata;
90 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
91 $this->type = $row->fa_media_type;
92 $this->description = $row->fa_description;
93 $this->user = $row->fa_user;
94 $this->userText = $row->fa_user_text;
95 $this->timestamp = $row->fa_timestamp;
96 $this->deleted = $row->fa_deleted;
97 } else {
98 throw new MWException( 'This title does not correspond to an image page.' );
99 return;
100 }
101 return true;
102 }
103
104 /**
105 * Loads a file object from the filearchive table
106 * @return ResultWrapper
107 */
108 public static function newFromRow( $row ) {
109 $file = new ArchivedFile( Title::makeTitle( NS_IMAGE, $row->fa_name ) );
110
111 $file->id = intval($row->fa_id);
112 $file->name = $row->fa_name;
113 $file->archive_name = $row->fa_archive_name;
114 $file->group = $row->fa_storage_group;
115 $file->key = $row->fa_storage_key;
116 $file->size = $row->fa_size;
117 $file->bits = $row->fa_bits;
118 $file->width = $row->fa_width;
119 $file->height = $row->fa_height;
120 $file->metaData = $row->fa_metadata;
121 $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
122 $file->type = $row->fa_media_type;
123 $file->description = $row->fa_description;
124 $file->user = $row->fa_user;
125 $file->userText = $row->fa_user_text;
126 $file->timestamp = $row->fa_timestamp;
127 $file->deleted = $row->fa_deleted;
128
129 return $file;
130 }
131
132 /**
133 * int $field one of DELETED_* bitfield constants
134 * for file or revision rows
135 * @return bool
136 */
137 function isDeleted( $field ) {
138 return ($this->deleted & $field) == $field;
139 }
140
141 /**
142 * Determine if the current user is allowed to view a particular
143 * field of this FileStore image file, if it's marked as deleted.
144 * @param int $field
145 * @return bool
146 */
147 function userCan( $field ) {
148 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
149 // images
150 global $wgUser;
151 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
152 ? 'hiderevision'
153 : 'deleterevision';
154 wfDebug( "Checking for $permission due to $field match on $this->deleted\n" );
155 return $wgUser->isAllowed( $permission );
156 } else {
157 return true;
158 }
159 }
160 }