Add (somewhat redundant) check for static groups to loadFromDatabase(). It's redundan...
[lhc/web/wiklou.git] / includes / Group.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 * Class to manage a group
8 * @package MediaWiki
9 */
10 class Group {
11 /**#@+
12 * @access private
13 */
14 /** string $name Group name */
15 var $name;
16 /** integer $id Group id */
17 var $id;
18 /** string $description Description of the group */
19 var $description;
20 /** boolean $dataLoaded Whereas we grabbed datas from the database */
21 var $dataLoaded;
22 /** string $rights Contain rights values : "foo,bar,bla" */
23 var $rights;
24 /**#@-*/
25
26 /** Constructor */
27 function Group() {
28 $this->clear();
29 }
30
31 /** Clear variables */
32 function clear() {
33 $this->name = '';
34 $this->id = 0;
35 $this->description = '';
36 $this->dataLoaded = false;
37 $this->rights = false;
38 }
39
40 /** Load group datas from database */
41 function loadFromDatabase() {
42 $fname = 'Group::loadFromDatabase';
43
44 // See if it's already loaded
45 if ( $this->dataLoaded || Group::getStaticGroups() ) {
46 return;
47 }
48
49 // be sure it's an integer
50 $this->id = IntVal($this->id);
51
52 if($this->id) {
53 // By ID
54 $dbr =& wfGetDB( DB_SLAVE );
55 $r = $dbr->selectRow('group',
56 array('group_id', 'group_name', 'group_description', 'group_rights'),
57 array( 'group_id' => $this->id ),
58 $fname );
59 if ( $r ) {
60 $this->loadFromRow( $r );
61 } else {
62 $this->id = 0;
63 $this->dataLoaded = true;
64 }
65 } else {
66 // By name
67 $dbr =& wfGetDB( DB_SLAVE );
68 $r = $dbr->selectRow('group',
69 array('group_id', 'group_name', 'group_description', 'group_rights'),
70 array( 'group_name' => $this->name ),
71 $fname );
72 if ( $r ) {
73 $this->loadFromRow( $r );
74 } else {
75 $this->id = 0;
76 $this->dataLoaded = true;
77 }
78 }
79 }
80
81 /** Initialise from a result row */
82 function loadFromRow( &$row ) {
83 $this->id = $row->group_id;
84 $this->name = $row->group_name;
85 $this->description = $row->group_description;
86 $this->rights = $row->group_rights;
87 $this->dataLoaded = true;
88 }
89
90 /** Initialise a new row in the database */
91 function addToDatabase() {
92 if ( Group::getStaticGroups() ) {
93 wfDebugDieBacktrace( "Can't modify groups in static mode" );
94 }
95
96 $fname = 'Group::addToDatabase';
97 $dbw =& wfGetDB( DB_MASTER );
98 $dbw->insert( 'group',
99 array(
100 'group_name' => $this->name,
101 'group_description' => $this->description,
102 'group_rights' => $this->rights
103 ), $fname
104 );
105 $this->id = $dbw->insertId();
106 }
107
108 /** Save the group datas into database */
109 function save() {
110 if ( Group::getStaticGroups() ) {
111 wfDebugDieBacktrace( "Can't modify groups in static mode" );
112 }
113 if($this->id == 0) { return; }
114
115 $fname = 'Group::save';
116 $dbw =& wfGetDB( DB_MASTER );
117
118 $dbw->update( 'group',
119 array( /* SET */
120 'group_name' => $this->name,
121 'group_description' => $this->description,
122 'group_rights' => $this->rights
123 ), array( /* WHERE */
124 'group_id' => $this->id
125 ), $fname
126 );
127 }
128
129 /** Delete a group */
130 function delete() {
131 if ( Group::getStaticGroups() ) {
132 wfDebugDieBacktrace( "Can't modify groups in static mode" );
133 }
134 if($this->id == 0) { return; }
135
136 $fname = 'Group::delete';
137 $dbw =& wfGetDB( DB_MASTER );
138
139 // First remove all users from the group
140 $dbw->delete( 'user_group', array( 'ug_group' => $this->id ), $fname );
141
142 // Now delete the group
143 $dbw->delete( 'group', array( 'group_id' => $this->id ), $fname );
144 }
145
146 // Factories
147 /**
148 * Uses Memcached if available.
149 * @param integer $id Group database id
150 */
151 function newFromId($id) {
152 global $wgMemc, $wgDBname;
153 $fname = 'Group::newFromId';
154
155 $staticGroups =& Group::getStaticGroups();
156 if ( $staticGroups ) {
157 if ( array_key_exists( $id, $staticGroups ) ) {
158 return $staticGroups[$id];
159 } else {
160 return null;
161 }
162 }
163
164 $key = "$wgDBname:groups:id:$id";
165 if( $group = $wgMemc->get( $key ) ) {
166 wfDebug( "$fname loaded group $id from cache\n" );
167 return $group;
168 }
169
170 $group = new Group();
171 $group->id = $id;
172 $group->loadFromDatabase();
173
174 if ( !$group->id ) {
175 wfDebug( "$fname can't find group $id\n" );
176 return null;
177 } else {
178 wfDebug( "$fname caching group $id (name {$group->name})\n" );
179 $wgMemc->add( $key, $group, 3600 );
180 return $group;
181 }
182 }
183
184
185 /** @param string $name Group database name */
186 function newFromName($name) {
187 $fname = 'Group::newFromName';
188
189 $staticGroups =& Group::getStaticGroups();
190 if ( $staticGroups ) {
191 $id = Group::idFromName( $name );
192 if ( array_key_exists( $id, $staticGroups ) ) {
193 return $staticGroups[$id];
194 } else {
195 return null;
196 }
197 }
198
199 $g = new Group();
200 $g->name = $name;
201 $g->loadFromDatabase();
202
203 if( $g->getId() != 0 ) {
204 return $g;
205 } else {
206 return null;
207 }
208 }
209
210 /**
211 * Get an array of Group objects, one for each valid group
212 *
213 * @static
214 */
215 function &getAllGroups() {
216 $staticGroups =& Group::getStaticGroups();
217 if ( $staticGroups ) {
218 return $staticGroups;
219 }
220
221 $fname = 'Group::getAllGroups';
222 wfProfileIn( $fname );
223
224 $dbr =& wfGetDB( DB_SLAVE );
225 $groupTable = $dbr->tableName( 'group' );
226 $sql = "SELECT group_id, group_name, group_description, group_rights FROM $groupTable";
227 $res = $dbr->query($sql, $fname);
228
229 $groups = array();
230
231 while($row = $dbr->fetchObject( $res ) ) {
232 $group = new Group;
233 $group->loadFromRow( $row );
234 $groups[$row->group_id] = $group;
235 }
236
237 wfProfileOut( $fname );
238 return $groups;
239 }
240
241 /**
242 * Get static groups, if they have been defined in LocalSettings.php
243 *
244 * @static
245 */
246 function &getStaticGroups() {
247 global $wgStaticGroups;
248 if ( $wgStaticGroups === false ) {
249 return $wgStaticGroups;
250 }
251
252 if ( !is_array( $wgStaticGroups ) ) {
253 $wgStaticGroups = unserialize( $wgStaticGroups );
254 }
255
256 return $wgStaticGroups;
257 }
258
259
260 // Converters
261 /**
262 * @param integer $id Group database id
263 * @return string Group database name
264 */
265 function nameFromId($id) {
266 $group = Group::newFromId( $id );
267 if ( is_null( $group ) ) {
268 return '';
269 } else {
270 return $group->getName();
271 }
272 }
273
274 /**
275 * @param string $name Group database name
276 * @return integer Group database id
277 */
278 function idFromName($name) {
279 $fname = 'Group::idFromName';
280
281 $staticGroups =& Group::getStaticGroups();
282 if ( $staticGroups ) {
283 foreach( $staticGroups as $id => $group ) {
284 if ( $group->getName() === $name ) {
285 return $group->getID();
286 }
287 }
288 return 0;
289 }
290
291
292 $dbr =& wfGetDB( DB_SLAVE );
293 $r = $dbr->selectRow( 'group', array( 'group_id' ), array( 'group_name' => $name ), $fname );
294
295 if($r === false) {
296 return 0;
297 } else {
298 return $r->group_id;
299 }
300 }
301
302 // Accessors for private variables
303 function getName() {
304 $this->loadFromDatabase();
305 return $this->name;
306 }
307
308 function getExpandedName() {
309 $this->loadFromDatabase();
310 return $this->getMessage( $this->name );
311 }
312
313 function getNameForContent() {
314 $this->loadFromDatabase();
315 return $this->getMessageForContent( $this->name );
316 }
317
318 function setName($name) {
319 $this->loadFromDatabase();
320 $this->name = $name;
321 }
322
323 function getId() { return $this->id; }
324 function setId($id) {
325 $this->id = IntVal($id);
326 $this->dataLoaded = false;
327 }
328
329 function getDescription() {
330 return $this->description;
331 }
332
333 function getExpandedDescription() {
334 return $this->getMessage( $this->description );
335 }
336
337 function setDescription($desc) {
338 $this->loadFromDatabase();
339 $this->description = $desc;
340 }
341
342 function getRights() { return $this->rights; }
343 function setRights($rights) {
344 $this->loadFromDatabase();
345 $this->rights = $rights;
346 }
347
348 /**
349 * Gets a message if the text starts with a colon, otherwise returns the text itself
350 */
351 function getMessage( $text ) {
352 if ( strlen( $text ) && $text{0} == ':' ) {
353 return wfMsg( substr( $text, 1 ) );
354 } else {
355 return $text;
356 }
357 }
358
359 /**
360 * As for getMessage but for content
361 */
362 function getMessageForContent( $text ) {
363 if ( strlen( $text ) && $text{0} == ':' ) {
364 return wfMsgForContent( substr( $text, 1 ) );
365 } else {
366 return $text;
367 }
368 }
369
370 }
371 ?>