7 * Class to manage a group
14 /** string $name Group name */
16 /** integer $id Group id */
18 /** string $description Description of the group */
20 /** boolean $dataLoaded Whether data has been loaded from the database */
22 /** string $rights Contain rights values : "foo,bar,bla" */
31 /** Clear variables */
35 $this->description
= '';
36 $this->dataLoaded
= false;
37 $this->rights
= false;
40 /** Load group data from database */
41 function loadFromDatabase() {
42 $fname = 'Group::loadFromDatabase';
44 // See if it's already loaded
45 if ( $this->dataLoaded || Group
::getStaticGroups() ) {
49 // be sure it's an integer
50 $this->id
= intval($this->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
),
60 $this->loadFromRow( $r );
63 $this->dataLoaded
= true;
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
),
73 $this->loadFromRow( $r );
76 $this->dataLoaded
= true;
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;
90 /** Initialise a new row in the database */
91 function addToDatabase() {
92 if ( Group
::getStaticGroups() ) {
93 wfDebugDieBacktrace( "Can't modify groups in static mode" );
96 $fname = 'Group::addToDatabase';
97 $dbw =& wfGetDB( DB_MASTER
);
98 $dbw->insert( 'groups',
100 'gr_name' => $this->name
,
101 'gr_description' => $this->description
,
102 'gr_rights' => $this->rights
105 $this->id
= $dbw->insertId();
108 /** Save the group data into database */
112 if ( Group
::getStaticGroups() ) {
113 wfDebugDieBacktrace( "Can't modify groups in static mode" );
115 if($this->id
== 0) { return; }
117 $fname = 'Group::save';
118 $dbw =& wfGetDB( DB_MASTER
);
120 $dbw->update( 'groups',
122 'gr_name' => $this->name
,
123 'gr_description' => $this->description
,
124 'gr_rights' => $this->rights
125 ), array( /* WHERE */
130 $wgMemc->set( Group
::getCacheKey( $this->id
), $this, 3600 );
134 /** Delete a group */
138 if ( Group
::getStaticGroups() ) {
139 wfDebugDieBacktrace( "Can't modify groups in static mode" );
141 if($this->id
== 0) { return; }
143 $fname = 'Group::delete';
144 $dbw =& wfGetDB( DB_MASTER
);
146 // First remove all users from the group
147 $dbw->delete( 'user_group', array( 'ug_group' => $this->id
), $fname );
149 // Now delete the group
150 $dbw->delete( 'groups', array( 'gr_id' => $this->id
), $fname );
152 $wgMemc->delete( Group
::getCacheKey( $this->id
) );
159 function getCacheKey( $id ) {
161 return "$wgDBname:groups:id:$id";
166 * Uses Memcached if available.
167 * @param $id Integer: group database id
169 function newFromId($id) {
171 $fname = 'Group::newFromId';
173 $staticGroups =& Group
::getStaticGroups();
174 if ( $staticGroups ) {
175 if ( array_key_exists( $id, $staticGroups ) ) {
176 return $staticGroups[$id];
182 $key = Group
::getCacheKey( $id );
184 if( $group = $wgMemc->get( $key ) ) {
185 wfDebug( "$fname loaded group $id from cache\n" );
189 $group = new Group();
191 $group->loadFromDatabase();
194 wfDebug( "$fname can't find group $id\n" );
197 wfDebug( "$fname caching group $id (name {$group->name})\n" );
198 $wgMemc->add( $key, $group, 3600 );
204 /** @param $name String: group database name */
205 function newFromName($name) {
206 $fname = 'Group::newFromName';
208 $staticGroups =& Group
::getStaticGroups();
209 if ( $staticGroups ) {
210 $id = Group
::idFromName( $name );
211 if ( array_key_exists( $id, $staticGroups ) ) {
212 return $staticGroups[$id];
220 $g->loadFromDatabase();
222 if( $g->getId() != 0 ) {
230 * Get an array of Group objects, one for each valid group
234 function &getAllGroups() {
235 $staticGroups =& Group
::getStaticGroups();
236 if ( $staticGroups ) {
237 return $staticGroups;
240 $fname = 'Group::getAllGroups';
241 wfProfileIn( $fname );
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);
250 while($row = $dbr->fetchObject( $res ) ) {
252 $group->loadFromRow( $row );
253 $groups[$row->gr_id
] = $group;
256 wfProfileOut( $fname );
261 * Get static groups, if they have been defined in LocalSettings.php
265 function &getStaticGroups() {
266 global $wgStaticGroups;
267 if ( $wgStaticGroups === false ) {
268 return $wgStaticGroups;
271 if ( !is_array( $wgStaticGroups ) ) {
272 $wgStaticGroups = unserialize( $wgStaticGroups );
275 return $wgStaticGroups;
281 * @param $id Integer: group database id
282 * @return string Group database name
284 function nameFromId($id) {
285 $group = Group
::newFromId( $id );
286 if ( is_null( $group ) ) {
289 return $group->getName();
294 * @param $name String: group database name
295 * @return integer Group database id
297 function idFromName($name) {
298 $fname = 'Group::idFromName';
300 $staticGroups =& Group
::getStaticGroups();
301 if ( $staticGroups ) {
302 foreach( $staticGroups as $id => $group ) {
303 if ( $group->getName() === $name ) {
304 return $group->getId();
311 $dbr =& wfGetDB( DB_SLAVE
);
312 $r = $dbr->selectRow( 'groups', array( 'gr_id' ), array( 'gr_name' => $name ), $fname );
321 // Accessors for private variables
323 $this->loadFromDatabase();
327 function getExpandedName() {
328 $this->loadFromDatabase();
329 return $this->getMessage( $this->name
);
332 function getNameForContent() {
333 $this->loadFromDatabase();
334 return $this->getMessageForContent( $this->name
);
337 function setName($name) {
338 $this->loadFromDatabase();
342 function getId() { return $this->id
; }
343 function setId($id) {
344 $this->id
= intval($id);
345 $this->dataLoaded
= false;
348 function getDescription() {
349 return $this->description
;
352 function getExpandedDescription() {
353 return $this->getMessage( $this->description
);
356 function setDescription($desc) {
357 $this->loadFromDatabase();
358 $this->description
= $desc;
361 function getRights() { return $this->rights
; }
362 function setRights($rights) {
363 $this->loadFromDatabase();
364 $this->rights
= $rights;
368 * Gets a message if the text starts with a colon, otherwise returns the text itself
370 function getMessage( $text ) {
371 if ( strlen( $text ) && $text{0} == ':' ) {
372 return wfMsg( substr( $text, 1 ) );
379 * As for getMessage but for content
381 function getMessageForContent( $text ) {
382 if ( strlen( $text ) && $text{0} == ':' ) {
383 return wfMsgForContent( substr( $text, 1 ) );