* (bug 2046) Make the wiki work on PHP5 again
[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 Whereas we grabbed datas 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 datas from database */
41 function loadFromDatabase() {
42 global $wgCommandLineMode;
43 $fname = 'Group::loadFromDatabase';
44
45 // See if it's already loaded
46 if ( $this->dataLoaded || $wgCommandLineMode ) {
47 return;
48 }
49
50 // be sure it's an integer
51 $this->id = IntVal($this->id);
52
53 if($this->id) {
54 // By 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 ),
59 $fname );
60 if ( $r ) {
61 $this->loadFromRow( $r );
62 } else {
63 $this->id = 0;
64 $this->dataLoaded = true;
65 }
66 } else {
67 // By name
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 ),
72 $fname );
73 if ( $r ) {
74 $this->loadFromRow( $r );
75 } else {
76 $this->id = 0;
77 $this->dataLoaded = true;
78 }
79 }
80 }
81
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;
89 }
90
91 /** Initialise a new row in the database */
92 function addToDatabase() {
93 if ( Group::getStaticGroups() ) {
94 wfDebugDieBacktrace( "Can't modify groups in static mode" );
95 }
96
97 $fname = 'Group::addToDatabase';
98 $dbw =& wfGetDB( DB_MASTER );
99 $dbw->insert( 'group',
100 array(
101 'group_name' => $this->name,
102 'group_description' => $this->description,
103 'group_rights' => $this->rights
104 ), $fname
105 );
106 $this->id = $dbw->insertId();
107 }
108
109 /** Save the group datas into database */
110 function save() {
111 if ( Group::getStaticGroups() ) {
112 wfDebugDieBacktrace( "Can't modify groups in static mode" );
113 }
114 if($this->id == 0) { return; }
115
116 $fname = 'Group::save';
117 $dbw =& wfGetDB( DB_MASTER );
118
119 $dbw->update( 'group',
120 array( /* SET */
121 'group_name' => $this->name,
122 'group_description' => $this->description,
123 'group_rights' => $this->rights
124 ), array( /* WHERE */
125 'group_id' => $this->id
126 ), $fname
127 );
128 }
129
130 /** Delete a group */
131 function delete() {
132 if ( Group::getStaticGroups() ) {
133 wfDebugDieBacktrace( "Can't modify groups in static mode" );
134 }
135 if($this->id == 0) { return; }
136
137 $fname = 'Group::delete';
138 $dbw =& wfGetDB( DB_MASTER );
139
140 // First remove all users from the group
141 $dbw->delete( 'user_group', array( 'ug_group' => $this->id ), $fname );
142
143 // Now delete the group
144 $dbw->delete( 'group', array( 'group_id' => $this->id ), $fname );
145 }
146
147 // Factories
148 /**
149 * Uses Memcached if available.
150 * @param integer $id Group database id
151 */
152 function newFromId($id) {
153 global $wgMemc, $wgDBname;
154 $fname = 'Group::newFromId';
155
156 $staticGroups =& Group::getStaticGroups();
157 if ( $staticGroups ) {
158 if ( array_key_exists( $id, $staticGroups ) ) {
159 return $staticGroups[$id];
160 } else {
161 return null;
162 }
163 }
164
165 $key = "$wgDBname:groups:id:$id";
166 if( $group = $wgMemc->get( $key ) ) {
167 wfDebug( "$fname loaded group $id from cache\n" );
168 return $group;
169 }
170
171 $group = new Group();
172 $group->id = $id;
173 $group->loadFromDatabase();
174
175 if ( !$group->id ) {
176 wfDebug( "$fname can't find group $id\n" );
177 return null;
178 } else {
179 wfDebug( "$fname caching group $id (name {$group->name})\n" );
180 $wgMemc->add( $key, $group, 3600 );
181 return $group;
182 }
183 }
184
185
186 /** @param string $name Group database name */
187 function newFromName($name) {
188 $fname = 'Group::newFromName';
189
190 $staticGroups =& Group::getStaticGroups();
191 if ( $staticGroups ) {
192 $id = Group::idFromName( $name );
193 if ( array_key_exists( $id, $staticGroups ) ) {
194 return $staticGroups[$id];
195 } else {
196 return null;
197 }
198 }
199
200 $g = new Group();
201 $g->name = $name;
202 $g->loadFromDatabase();
203
204 if( $g->getId() != 0 ) {
205 return $g;
206 } else {
207 return null;
208 }
209 }
210
211 /**
212 * Get an array of Group objects, one for each valid group
213 *
214 * @static
215 */
216 function &getAllGroups() {
217 $staticGroups =& Group::getStaticGroups();
218 if ( $staticGroups ) {
219 return $staticGroups;
220 }
221
222 $fname = 'Group::getAllGroups';
223 wfProfileIn( $fname );
224
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);
229
230 $groups = array();
231
232 while($row = $dbr->fetchObject( $res ) ) {
233 $group = new Group;
234 $group->loadFromRow( $row );
235 $groups[$row->group_id] = $group;
236 }
237
238 wfProfileOut( $fname );
239 return $groups;
240 }
241
242 /**
243 * Get static groups, if they have been defined in LocalSettings.php
244 *
245 * @static
246 */
247 function &getStaticGroups() {
248 global $wgStaticGroups;
249 if ( $wgStaticGroups === false ) {
250 return $wgStaticGroups;
251 }
252
253 if ( !is_array( $wgStaticGroups ) ) {
254 $wgStaticGroups = unserialize( $wgStaticGroups );
255 }
256
257 return $wgStaticGroups;
258 }
259
260
261 // Converters
262 /**
263 * @param integer $id Group database id
264 * @return string Group database name
265 */
266 function nameFromId($id) {
267 $group = Group::newFromId( $id );
268 if ( is_null( $group ) ) {
269 return '';
270 } else {
271 return $group->getName();
272 }
273 }
274
275 /**
276 * @param string $name Group database name
277 * @return integer Group database id
278 */
279 function idFromName($name) {
280 $fname = 'Group::idFromName';
281
282 $staticGroups =& Group::getStaticGroups();
283 if ( $staticGroups ) {
284 foreach( $staticGroups as $id => $group ) {
285 if ( $group->getName() === $name ) {
286 return $group->getID();
287 }
288 }
289 return 0;
290 }
291
292
293 $dbr =& wfGetDB( DB_SLAVE );
294 $r = $dbr->selectRow( 'group', array( 'group_id' ), array( 'group_name' => $name ), $fname );
295
296 if($r === false) {
297 return 0;
298 } else {
299 return $r->group_id;
300 }
301 }
302
303 // Accessors for private variables
304 function getName() {
305 $this->loadFromDatabase();
306 return $this->name;
307 }
308
309 function getExpandedName() {
310 $this->loadFromDatabase();
311 return $this->getMessage( $this->name );
312 }
313
314 function getNameForContent() {
315 $this->loadFromDatabase();
316 return $this->getMessageForContent( $this->name );
317 }
318
319 function setName($name) {
320 $this->loadFromDatabase();
321 $this->name = $name;
322 }
323
324 function getId() { return $this->id; }
325 function setId($id) {
326 $this->id = IntVal($id);
327 $this->dataLoaded = false;
328 }
329
330 function getDescription() {
331 return $this->description;
332 }
333
334 function getExpandedDescription() {
335 return $this->getMessage( $this->description );
336 }
337
338 function setDescription($desc) {
339 $this->loadFromDatabase();
340 $this->description = $desc;
341 }
342
343 function getRights() { return $this->rights; }
344 function setRights($rights) {
345 $this->loadFromDatabase();
346 $this->rights = $rights;
347 }
348
349 /**
350 * Gets a message if the text starts with a colon, otherwise returns the text itself
351 */
352 function getMessage( $text ) {
353 if ( strlen( $text ) && $text{0} == ':' ) {
354 return wfMsg( substr( $text, 1 ) );
355 } else {
356 return $text;
357 }
358 }
359
360 /**
361 * As for getMessage but for content
362 */
363 function getMessageForContent( $text ) {
364 if ( strlen( $text ) && $text{0} == ':' ) {
365 return wfMsgForContent( substr( $text, 1 ) );
366 } else {
367 return $text;
368 }
369 }
370
371 }
372 ?>