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 global $wgCommandLineMode;
43 $fname = 'Group::loadFromDatabase';
45 // See if it's already loaded
46 if ( $this->dataLoaded ||
$wgCommandLineMode ) {
50 // be sure it's an integer
51 $this->id
= IntVal($this->id
);
55 $dbr =& wfGetDB( DB_SLAVE
);
56 $r = $dbr->selectRow('group',
57 array('group_id', 'group_name', 'group_description', 'group_rights'),
58 array( 'group_id' => $this->id
),
61 $this->loadFromRow( $r );
64 $this->dataLoaded
= true;
68 $dbr =& wfGetDB( DB_SLAVE
);
69 $r = $dbr->selectRow('group',
70 array('group_id', 'group_name', 'group_description', 'group_rights'),
71 array( 'group_name' => $this->name
),
74 $this->loadFromRow( $r );
77 $this->dataLoaded
= true;
82 /** Initialise from a result row */
83 function loadFromRow( &$row ) {
84 $this->id
= $row->group_id
;
85 $this->name
= $row->group_name
;
86 $this->description
= $row->group_description
;
87 $this->rights
= $row->group_rights
;
88 $this->dataLoaded
= true;
91 /** Initialise a new row in the database */
92 function addToDatabase() {
93 if ( Group
::getStaticGroups() ) {
94 wfDebugDieBacktrace( "Can't modify groups in static mode" );
97 $fname = 'Group::addToDatabase';
98 $dbw =& wfGetDB( DB_MASTER
);
99 $dbw->insert( 'group',
101 'group_name' => $this->name
,
102 'group_description' => $this->description
,
103 'group_rights' => $this->rights
106 $this->id
= $dbw->insertId();
109 /** Save the group datas into database */
111 if ( Group
::getStaticGroups() ) {
112 wfDebugDieBacktrace( "Can't modify groups in static mode" );
114 if($this->id
== 0) { return; }
116 $fname = 'Group::save';
117 $dbw =& wfGetDB( DB_MASTER
);
119 $dbw->update( 'group',
121 'group_name' => $this->name
,
122 'group_description' => $this->description
,
123 'group_rights' => $this->rights
124 ), array( /* WHERE */
125 'group_id' => $this->id
130 /** Delete a group */
132 if ( Group
::getStaticGroups() ) {
133 wfDebugDieBacktrace( "Can't modify groups in static mode" );
135 if($this->id
== 0) { return; }
137 $fname = 'Group::delete';
138 $dbw =& wfGetDB( DB_MASTER
);
140 // First remove all users from the group
141 $dbw->delete( 'user_group', array( 'ug_group' => $this->id
), $fname );
143 // Now delete the group
144 $dbw->delete( 'group', array( 'group_id' => $this->id
), $fname );
149 * Uses Memcached if available.
150 * @param integer $id Group database id
152 function newFromId($id) {
153 global $wgMemc, $wgDBname;
154 $fname = 'Group::newFromId';
156 $staticGroups =& Group
::getStaticGroups();
157 if ( $staticGroups ) {
158 if ( array_key_exists( $id, $staticGroups ) ) {
159 return $staticGroups[$id];
165 $key = "$wgDBname:groups:id:$id";
166 if( $group = $wgMemc->get( $key ) ) {
167 wfDebug( "$fname loaded group $id from cache\n" );
171 $group = new Group();
173 $group->loadFromDatabase();
176 wfDebug( "$fname can't find group $id\n" );
179 wfDebug( "$fname caching group $id (name {$group->name})\n" );
180 $wgMemc->add( $key, $group, 3600 );
186 /** @param string $name Group database name */
187 function newFromName($name) {
188 $fname = 'Group::newFromName';
190 $staticGroups =& Group
::getStaticGroups();
191 if ( $staticGroups ) {
192 $id = Group
::idFromName( $name );
193 if ( array_key_exists( $id, $staticGroups ) ) {
194 return $staticGroups[$id];
202 $g->loadFromDatabase();
204 if( $g->getId() != 0 ) {
212 * Get an array of Group objects, one for each valid group
216 function &getAllGroups() {
217 $staticGroups =& Group
::getStaticGroups();
218 if ( $staticGroups ) {
219 return $staticGroups;
222 $fname = 'Group::getAllGroups';
223 wfProfileIn( $fname );
225 $dbr =& wfGetDB( DB_SLAVE
);
226 $groupTable = $dbr->tableName( 'group' );
227 $sql = "SELECT group_id, group_name, group_description, group_rights FROM $groupTable";
228 $res = $dbr->query($sql, $fname);
232 while($row = $dbr->fetchObject( $res ) ) {
234 $group->loadFromRow( $row );
235 $groups[$row->group_id
] = $group;
238 wfProfileOut( $fname );
243 * Get static groups, if they have been defined in LocalSettings.php
247 function &getStaticGroups() {
248 global $wgStaticGroups;
249 if ( $wgStaticGroups === false ) {
250 return $wgStaticGroups;
253 if ( !is_array( $wgStaticGroups ) ) {
254 $wgStaticGroups = unserialize( $wgStaticGroups );
257 return $wgStaticGroups;
263 * @param integer $id Group database id
264 * @return string Group database name
266 function nameFromId($id) {
267 $group = Group
::newFromId( $id );
268 if ( is_null( $group ) ) {
271 return $group->getName();
276 * @param string $name Group database name
277 * @return integer Group database id
279 function idFromName($name) {
280 $fname = 'Group::idFromName';
282 $staticGroups =& Group
::getStaticGroups();
283 if ( $staticGroups ) {
284 foreach( $staticGroups as $id => $group ) {
285 if ( $group->getName() === $name ) {
286 return $group->getID();
293 $dbr =& wfGetDB( DB_SLAVE
);
294 $r = $dbr->selectRow( 'group', array( 'group_id' ), array( 'group_name' => $name ), $fname );
303 // Accessors for private variables
305 $this->loadFromDatabase();
309 function getExpandedName() {
310 $this->loadFromDatabase();
311 return $this->getMessage( $this->name
);
314 function getNameForContent() {
315 $this->loadFromDatabase();
316 return $this->getMessageForContent( $this->name
);
319 function setName($name) {
320 $this->loadFromDatabase();
324 function getId() { return $this->id
; }
325 function setId($id) {
326 $this->id
= IntVal($id);
327 $this->dataLoaded
= false;
330 function getDescription() {
331 return $this->description
;
334 function getExpandedDescription() {
335 return $this->getMessage( $this->description
);
338 function setDescription($desc) {
339 $this->loadFromDatabase();
340 $this->description
= $desc;
343 function getRights() { return $this->rights
; }
344 function setRights($rights) {
345 $this->loadFromDatabase();
346 $this->rights
= $rights;
350 * Gets a message if the text starts with a colon, otherwise returns the text itself
352 function getMessage( $text ) {
353 if ( strlen( $text ) && $text{0} == ':' ) {
354 return wfMsg( substr( $text, 1 ) );
361 * As for getMessage but for content
363 function getMessageForContent( $text ) {
364 if ( strlen( $text ) && $text{0} == ':' ) {
365 return wfMsgForContent( substr( $text, 1 ) );