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 Whereas we grabbed datas 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 datas 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('group',
56 array('group_id', 'group_name', 'group_description', 'group_rights'),
57 array( 'group_id' => $this->id
),
60 $this->loadFromRow( $r );
63 $this->dataLoaded
= true;
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
),
73 $this->loadFromRow( $r );
76 $this->dataLoaded
= true;
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;
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( 'group',
100 'group_name' => $this->name
,
101 'group_description' => $this->description
,
102 'group_rights' => $this->rights
105 $this->id
= $dbw->insertId();
108 /** Save the group datas into database */
110 if ( Group
::getStaticGroups() ) {
111 wfDebugDieBacktrace( "Can't modify groups in static mode" );
113 if($this->id
== 0) { return; }
115 $fname = 'Group::save';
116 $dbw =& wfGetDB( DB_MASTER
);
118 $dbw->update( 'group',
120 'group_name' => $this->name
,
121 'group_description' => $this->description
,
122 'group_rights' => $this->rights
123 ), array( /* WHERE */
124 'group_id' => $this->id
129 /** Delete a group */
131 if ( Group
::getStaticGroups() ) {
132 wfDebugDieBacktrace( "Can't modify groups in static mode" );
134 if($this->id
== 0) { return; }
136 $fname = 'Group::delete';
137 $dbw =& wfGetDB( DB_MASTER
);
139 // First remove all users from the group
140 $dbw->delete( 'user_group', array( 'ug_group' => $this->id
), $fname );
142 // Now delete the group
143 $dbw->delete( 'group', array( 'group_id' => $this->id
), $fname );
148 * Uses Memcached if available.
149 * @param integer $id Group database id
151 function newFromId($id) {
152 global $wgMemc, $wgDBname;
153 $fname = 'Group::newFromId';
155 $staticGroups =& Group
::getStaticGroups();
156 if ( $staticGroups ) {
157 if ( array_key_exists( $id, $staticGroups ) ) {
158 return $staticGroups[$id];
164 $key = "$wgDBname:groups:id:$id";
165 if( $group = $wgMemc->get( $key ) ) {
166 wfDebug( "$fname loaded group $id from cache\n" );
170 $group = new Group();
172 $group->loadFromDatabase();
175 wfDebug( "$fname can't find group $id\n" );
178 wfDebug( "$fname caching group $id (name {$group->name})\n" );
179 $wgMemc->add( $key, $group, 3600 );
185 /** @param string $name Group database name */
186 function newFromName($name) {
187 $fname = 'Group::newFromName';
189 $staticGroups =& Group
::getStaticGroups();
190 if ( $staticGroups ) {
191 $id = Group
::idFromName( $name );
192 if ( array_key_exists( $id, $staticGroups ) ) {
193 return $staticGroups[$id];
201 $g->loadFromDatabase();
203 if( $g->getId() != 0 ) {
211 * Get an array of Group objects, one for each valid group
215 function &getAllGroups() {
216 $staticGroups =& Group
::getStaticGroups();
217 if ( $staticGroups ) {
218 return $staticGroups;
221 $fname = 'Group::getAllGroups';
222 wfProfileIn( $fname );
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);
231 while($row = $dbr->fetchObject( $res ) ) {
233 $group->loadFromRow( $row );
234 $groups[$row->group_id
] = $group;
237 wfProfileOut( $fname );
242 * Get static groups, if they have been defined in LocalSettings.php
246 function &getStaticGroups() {
247 global $wgStaticGroups;
248 if ( $wgStaticGroups === false ) {
249 return $wgStaticGroups;
252 if ( !is_array( $wgStaticGroups ) ) {
253 $wgStaticGroups = unserialize( $wgStaticGroups );
256 return $wgStaticGroups;
262 * @param integer $id Group database id
263 * @return string Group database name
265 function nameFromId($id) {
266 $group = Group
::newFromId( $id );
267 if ( is_null( $group ) ) {
270 return $group->getName();
275 * @param string $name Group database name
276 * @return integer Group database id
278 function idFromName($name) {
279 $fname = 'Group::idFromName';
281 $staticGroups =& Group
::getStaticGroups();
282 if ( $staticGroups ) {
283 foreach( $staticGroups as $id => $group ) {
284 if ( $group->getName() === $name ) {
285 return $group->getID();
292 $dbr =& wfGetDB( DB_SLAVE
);
293 $r = $dbr->selectRow( 'group', array( 'group_id' ), array( 'group_name' => $name ), $fname );
302 // Accessors for private variables
304 $this->loadFromDatabase();
308 function getExpandedName() {
309 $this->loadFromDatabase();
310 return $this->getMessage( $this->name
);
313 function getNameForContent() {
314 $this->loadFromDatabase();
315 return $this->getMessageForContent( $this->name
);
318 function setName($name) {
319 $this->loadFromDatabase();
323 function getId() { return $this->id
; }
324 function setId($id) {
325 $this->id
= IntVal($id);
326 $this->dataLoaded
= false;
329 function getDescription() {
330 return $this->description
;
333 function getExpandedDescription() {
334 return $this->getMessage( $this->description
);
337 function setDescription($desc) {
338 $this->loadFromDatabase();
339 $this->description
= $desc;
342 function getRights() { return $this->rights
; }
343 function setRights($rights) {
344 $this->loadFromDatabase();
345 $this->rights
= $rights;
349 * Gets a message if the text starts with a colon, otherwise returns the text itself
351 function getMessage( $text ) {
352 if ( strlen( $text ) && $text{0} == ':' ) {
353 return wfMsg( substr( $text, 1 ) );
360 * As for getMessage but for content
362 function getMessageForContent( $text ) {
363 if ( strlen( $text ) && $text{0} == ':' ) {
364 return wfMsgForContent( substr( $text, 1 ) );