* GROUP BY support in makeSelectOptions()
[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 Whether data has been loaded 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 data 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('groups',
56 array('gr_id', 'gr_name', 'gr_description', 'gr_rights'),
57 array( 'gr_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('groups',
69 array('gr_id', 'gr_name', 'gr_description', 'gr_rights'),
70 array( 'gr_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->gr_id;
84 $this->name = $row->gr_name;
85 $this->description = $row->gr_description;
86 $this->rights = $row->gr_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( 'groups',
99 array(
100 'gr_name' => $this->name,
101 'gr_description' => $this->description,
102 'gr_rights' => $this->rights
103 ), $fname
104 );
105 $this->id = $dbw->insertId();
106 }
107
108 /** Save the group data into database */
109 function save() {
110 global $wgMemc;
111
112 if ( Group::getStaticGroups() ) {
113 wfDebugDieBacktrace( "Can't modify groups in static mode" );
114 }
115 if($this->id == 0) { return; }
116
117 $fname = 'Group::save';
118 $dbw =& wfGetDB( DB_MASTER );
119
120 $dbw->update( 'groups',
121 array( /* SET */
122 'gr_name' => $this->name,
123 'gr_description' => $this->description,
124 'gr_rights' => $this->rights
125 ), array( /* WHERE */
126 'gr_id' => $this->id
127 ), $fname
128 );
129
130 $wgMemc->set( Group::getCacheKey( $this->id ), $this, 3600 );
131 }
132
133
134 /** Delete a group */
135 function delete() {
136 global $wgMemc;
137
138 if ( Group::getStaticGroups() ) {
139 wfDebugDieBacktrace( "Can't modify groups in static mode" );
140 }
141 if($this->id == 0) { return; }
142
143 $fname = 'Group::delete';
144 $dbw =& wfGetDB( DB_MASTER );
145
146 // First remove all users from the group
147 $dbw->delete( 'user_group', array( 'ug_group' => $this->id ), $fname );
148
149 // Now delete the group
150 $dbw->delete( 'groups', array( 'gr_id' => $this->id ), $fname );
151
152 $wgMemc->delete( Group::getCacheKey( $this->id ) );
153 }
154
155 /**
156 * Get memcached key
157 * @static
158 */
159 function getCacheKey( $id ) {
160 global $wgDBname;
161 return "$wgDBname:groups:id:$id";
162 }
163
164 // Factories
165 /**
166 * Uses Memcached if available.
167 * @param integer $id Group database id
168 */
169 function newFromId($id) {
170 global $wgMemc, $wgDBname;
171 $fname = 'Group::newFromId';
172
173 $staticGroups =& Group::getStaticGroups();
174 if ( $staticGroups ) {
175 if ( array_key_exists( $id, $staticGroups ) ) {
176 return $staticGroups[$id];
177 } else {
178 return null;
179 }
180 }
181
182 $key = Group::getCacheKey( $id );
183
184 if( $group = $wgMemc->get( $key ) ) {
185 wfDebug( "$fname loaded group $id from cache\n" );
186 return $group;
187 }
188
189 $group = new Group();
190 $group->id = $id;
191 $group->loadFromDatabase();
192
193 if ( !$group->id ) {
194 wfDebug( "$fname can't find group $id\n" );
195 return null;
196 } else {
197 wfDebug( "$fname caching group $id (name {$group->name})\n" );
198 $wgMemc->add( $key, $group, 3600 );
199 return $group;
200 }
201 }
202
203
204 /** @param string $name Group database name */
205 function newFromName($name) {
206 $fname = 'Group::newFromName';
207
208 $staticGroups =& Group::getStaticGroups();
209 if ( $staticGroups ) {
210 $id = Group::idFromName( $name );
211 if ( array_key_exists( $id, $staticGroups ) ) {
212 return $staticGroups[$id];
213 } else {
214 return null;
215 }
216 }
217
218 $g = new Group();
219 $g->name = $name;
220 $g->loadFromDatabase();
221
222 if( $g->getId() != 0 ) {
223 return $g;
224 } else {
225 return null;
226 }
227 }
228
229 /**
230 * Get an array of Group objects, one for each valid group
231 *
232 * @static
233 */
234 function &getAllGroups() {
235 $staticGroups =& Group::getStaticGroups();
236 if ( $staticGroups ) {
237 return $staticGroups;
238 }
239
240 $fname = 'Group::getAllGroups';
241 wfProfileIn( $fname );
242
243 $dbr =& wfGetDB( DB_SLAVE );
244 $groupTable = $dbr->tableName( 'groups' );
245 $sql = "SELECT gr_id, gr_name, gr_description, gr_rights FROM $groupTable";
246 $res = $dbr->query($sql, $fname);
247
248 $groups = array();
249
250 while($row = $dbr->fetchObject( $res ) ) {
251 $group = new Group;
252 $group->loadFromRow( $row );
253 $groups[$row->gr_id] = $group;
254 }
255
256 wfProfileOut( $fname );
257 return $groups;
258 }
259
260 /**
261 * Get static groups, if they have been defined in LocalSettings.php
262 *
263 * @static
264 */
265 function &getStaticGroups() {
266 global $wgStaticGroups;
267 if ( $wgStaticGroups === false ) {
268 return $wgStaticGroups;
269 }
270
271 if ( !is_array( $wgStaticGroups ) ) {
272 $wgStaticGroups = unserialize( $wgStaticGroups );
273 }
274
275 return $wgStaticGroups;
276 }
277
278
279 // Converters
280 /**
281 * @param integer $id Group database id
282 * @return string Group database name
283 */
284 function nameFromId($id) {
285 $group = Group::newFromId( $id );
286 if ( is_null( $group ) ) {
287 return '';
288 } else {
289 return $group->getName();
290 }
291 }
292
293 /**
294 * @param string $name Group database name
295 * @return integer Group database id
296 */
297 function idFromName($name) {
298 $fname = 'Group::idFromName';
299
300 $staticGroups =& Group::getStaticGroups();
301 if ( $staticGroups ) {
302 foreach( $staticGroups as $id => $group ) {
303 if ( $group->getName() === $name ) {
304 return $group->getId();
305 }
306 }
307 return 0;
308 }
309
310
311 $dbr =& wfGetDB( DB_SLAVE );
312 $r = $dbr->selectRow( 'groups', array( 'gr_id' ), array( 'gr_name' => $name ), $fname );
313
314 if($r === false) {
315 return 0;
316 } else {
317 return $r->gr_id;
318 }
319 }
320
321 // Accessors for private variables
322 function getName() {
323 $this->loadFromDatabase();
324 return $this->name;
325 }
326
327 function getExpandedName() {
328 $this->loadFromDatabase();
329 return $this->getMessage( $this->name );
330 }
331
332 function getNameForContent() {
333 $this->loadFromDatabase();
334 return $this->getMessageForContent( $this->name );
335 }
336
337 function setName($name) {
338 $this->loadFromDatabase();
339 $this->name = $name;
340 }
341
342 function getId() { return $this->id; }
343 function setId($id) {
344 $this->id = IntVal($id);
345 $this->dataLoaded = false;
346 }
347
348 function getDescription() {
349 return $this->description;
350 }
351
352 function getExpandedDescription() {
353 return $this->getMessage( $this->description );
354 }
355
356 function setDescription($desc) {
357 $this->loadFromDatabase();
358 $this->description = $desc;
359 }
360
361 function getRights() { return $this->rights; }
362 function setRights($rights) {
363 $this->loadFromDatabase();
364 $this->rights = $rights;
365 }
366
367 /**
368 * Gets a message if the text starts with a colon, otherwise returns the text itself
369 */
370 function getMessage( $text ) {
371 if ( strlen( $text ) && $text{0} == ':' ) {
372 return wfMsg( substr( $text, 1 ) );
373 } else {
374 return $text;
375 }
376 }
377
378 /**
379 * As for getMessage but for content
380 */
381 function getMessageForContent( $text ) {
382 if ( strlen( $text ) && $text{0} == ':' ) {
383 return wfMsgForContent( substr( $text, 1 ) );
384 } else {
385 return $text;
386 }
387 }
388
389 }
390 ?>