Documentation
[mediawiki.git] / includes / Group.php
blobbec94e9efaa2a7a30921a93fa742b07a136478a3
1 <?php
2 /**
3 * @package MediaWiki
4 */
6 /**
7 * Class to manage a group
8 * @package MediaWiki
9 */
10 class Group {
11 /**#@+
12 * @access private
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 /**#@-*/
26 /** Constructor */
27 function Group() {
28 $this->clear();
31 /** Clear variables */
32 function clear() {
33 $this->name = '';
34 $this->id = 0;
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() ) {
46 return;
49 // be sure it's an integer
50 $this->id = intval($this->id);
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;
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;
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',
99 array(
100 'gr_name' => $this->name,
101 'gr_description' => $this->description,
102 'gr_rights' => $this->rights
103 ), $fname
105 $this->id = $dbw->insertId();
108 /** Save the group data into database */
109 function save() {
110 global $wgMemc;
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',
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
130 $wgMemc->set( Group::getCacheKey( $this->id ), $this, 3600 );
134 /** Delete a group */
135 function delete() {
136 global $wgMemc;
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 ) );
156 * Get memcached key
157 * @static
159 function getCacheKey( $id ) {
160 global $wgDBname;
161 return "$wgDBname:groups:id:$id";
164 // Factories
166 * Uses Memcached if available.
167 * @param integer $id Group database id
169 function newFromId($id) {
170 global $wgMemc;
171 $fname = 'Group::newFromId';
173 $staticGroups =& Group::getStaticGroups();
174 if ( $staticGroups ) {
175 if ( array_key_exists( $id, $staticGroups ) ) {
176 return $staticGroups[$id];
177 } else {
178 return null;
182 $key = Group::getCacheKey( $id );
184 if( $group = $wgMemc->get( $key ) ) {
185 wfDebug( "$fname loaded group $id from cache\n" );
186 return $group;
189 $group = new Group();
190 $group->id = $id;
191 $group->loadFromDatabase();
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;
204 /** @param string $name 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];
213 } else {
214 return null;
218 $g = new Group();
219 $g->name = $name;
220 $g->loadFromDatabase();
222 if( $g->getId() != 0 ) {
223 return $g;
224 } else {
225 return null;
230 * Get an array of Group objects, one for each valid group
232 * @static
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);
248 $groups = array();
250 while($row = $dbr->fetchObject( $res ) ) {
251 $group = new Group;
252 $group->loadFromRow( $row );
253 $groups[$row->gr_id] = $group;
256 wfProfileOut( $fname );
257 return $groups;
261 * Get static groups, if they have been defined in LocalSettings.php
263 * @static
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;
279 // Converters
281 * @param integer $id Group database id
282 * @return string Group database name
284 function nameFromId($id) {
285 $group = Group::newFromId( $id );
286 if ( is_null( $group ) ) {
287 return '';
288 } else {
289 return $group->getName();
294 * @param string $name 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();
307 return 0;
311 $dbr =& wfGetDB( DB_SLAVE );
312 $r = $dbr->selectRow( 'groups', array( 'gr_id' ), array( 'gr_name' => $name ), $fname );
314 if($r === false) {
315 return 0;
316 } else {
317 return $r->gr_id;
321 // Accessors for private variables
322 function getName() {
323 $this->loadFromDatabase();
324 return $this->name;
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();
339 $this->name = $name;
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 ) );
373 } else {
374 return $text;
379 * As for getMessage but for content
381 function getMessageForContent( $text ) {
382 if ( strlen( $text ) && $text{0} == ':' ) {
383 return wfMsgForContent( substr( $text, 1 ) );
384 } else {
385 return $text;