X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FGroup.php;h=cfaeb46feff35bbf569dea534481c9ffcc8621a0;hb=323ed8f2e2a2442e3456ed62d1ff6dee41336643;hp=88b516662849d7177afcfd9631ff4b2bae5f3c5d;hpb=7d8cfdca21cfc957747aef1bdedbe4d467379f4b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Group.php b/includes/Group.php index 88b5166628..cfaeb46fef 100644 --- a/includes/Group.php +++ b/includes/Group.php @@ -17,7 +17,7 @@ class Group { var $id; /** string $description Description of the group */ var $description; - /** boolean $dataLoaded Whereas we grabbed datas from the database */ + /** boolean $dataLoaded Whether data has been loaded from the database */ var $dataLoaded; /** string $rights Contain rights values : "foo,bar,bla" */ var $rights; @@ -37,11 +37,12 @@ class Group { $this->rights = false; } - /** Load group datas from database */ + /** Load group data from database */ function loadFromDatabase() { - global $wgCommandLineMode; $fname = 'Group::loadFromDatabase'; - if ( $this->dataLoaded || $wgCommandLineMode ) { + + // See if it's already loaded + if ( $this->dataLoaded || Group::getStaticGroups() ) { return; } @@ -49,62 +50,117 @@ class Group { $this->id = IntVal($this->id); if($this->id) { + // By ID $dbr =& wfGetDB( DB_SLAVE ); - $r = $dbr->selectRow('group', - array('group_id', 'group_name', 'group_description', 'group_rights'), - array( 'group_id' => $this->id ), + $r = $dbr->selectRow('groups', + array('gr_id', 'gr_name', 'gr_description', 'gr_rights'), + array( 'gr_id' => $this->id ), $fname ); - $this->id = $r->group_id; - $this->name = $r->group_name; - $this->description = $r->group_description; - $this->rights = $r->group_rights; - $this->dataLoaded = true; + if ( $r ) { + $this->loadFromRow( $r ); + } else { + $this->id = 0; + $this->dataLoaded = true; + } } else { + // By name $dbr =& wfGetDB( DB_SLAVE ); - $r = $dbr->selectRow('group', - array('group_id', 'group_name', 'group_description', 'group_rights'), - array( 'group_name' => $this->name ), + $r = $dbr->selectRow('groups', + array('gr_id', 'gr_name', 'gr_description', 'gr_rights'), + array( 'gr_name' => $this->name ), $fname ); - $this->id = $r->group_id; - $this->name = $r->group_name; - $this->description = $r->group_description; - $this->rights = $r->group_rights; - $this->dataLoaded = true; + if ( $r ) { + $this->loadFromRow( $r ); + } else { + $this->id = 0; + $this->dataLoaded = true; + } } } + + /** Initialise from a result row */ + function loadFromRow( &$row ) { + $this->id = $row->gr_id; + $this->name = $row->gr_name; + $this->description = $row->gr_description; + $this->rights = $row->gr_rights; + $this->dataLoaded = true; + } /** Initialise a new row in the database */ function addToDatabase() { + if ( Group::getStaticGroups() ) { + wfDebugDieBacktrace( "Can't modify groups in static mode" ); + } + $fname = 'Group::addToDatabase'; $dbw =& wfGetDB( DB_MASTER ); - $dbw->insert( 'group', + $dbw->insert( 'groups', array( - 'group_name' => $this->name, - 'group_description' => $this->description, - 'group_rights' => $this->rights + 'gr_name' => $this->name, + 'gr_description' => $this->description, + 'gr_rights' => $this->rights ), $fname ); $this->id = $dbw->insertId(); } - /** Save the group datas into database */ + /** Save the group data into database */ function save() { - $fname = 'Group::save'; - if($this->id == 0) { return; } + global $wgMemc; + if ( Group::getStaticGroups() ) { + wfDebugDieBacktrace( "Can't modify groups in static mode" ); + } + if($this->id == 0) { return; } + + $fname = 'Group::save'; $dbw =& wfGetDB( DB_MASTER ); - $dbw->update( 'group', + $dbw->update( 'groups', array( /* SET */ - 'group_name' => $this->name, - 'group_description' => $this->description, - 'group_rights' => $this->rights + 'gr_name' => $this->name, + 'gr_description' => $this->description, + 'gr_rights' => $this->rights ), array( /* WHERE */ - 'group_id' => $this->id + 'gr_id' => $this->id ), $fname - ); + ); + + $wgMemc->set( Group::getCacheKey( $this->id ), $this, 3600 ); + } + + + /** Delete a group */ + function delete() { + global $wgMemc; + + if ( Group::getStaticGroups() ) { + wfDebugDieBacktrace( "Can't modify groups in static mode" ); + } + if($this->id == 0) { return; } + + $fname = 'Group::delete'; + $dbw =& wfGetDB( DB_MASTER ); + + // First remove all users from the group + $dbw->delete( 'user_group', array( 'ug_group' => $this->id ), $fname ); + + // Now delete the group + $dbw->delete( 'groups', array( 'gr_id' => $this->id ), $fname ); + + $wgMemc->delete( Group::getCacheKey( $this->id ) ); } + /** + * Get memcached key + * @static + */ + function getCacheKey( $id ) { + global $wgDBname; + return "$wgDBname:groups:id:$id"; + } + // Factories /** * Uses Memcached if available. @@ -114,27 +170,32 @@ class Group { global $wgMemc, $wgDBname; $fname = 'Group::newFromId'; - $key = "$wgDBname:groups:id:$id"; + $staticGroups =& Group::getStaticGroups(); + if ( $staticGroups ) { + if ( array_key_exists( $id, $staticGroups ) ) { + return $staticGroups[$id]; + } else { + return null; + } + } + + $key = Group::getCacheKey( $id ); + if( $group = $wgMemc->get( $key ) ) { wfDebug( "$fname loaded group $id from cache\n" ); return $group; } - $g = new Group(); - $name = $g->nameFromId(IntVal($id)); + $group = new Group(); + $group->id = $id; + $group->loadFromDatabase(); - if($name == '') { + if ( !$group->id ) { wfDebug( "$fname can't find group $id\n" ); return null; } else { - $group = $g->newFromName($name); - if( $group ) { - wfDebug( "$fname caching group $id (name $name)\n" ); - $group->loadFromDatabase(); - $wgMemc->add( $key, $group, 3600 ); - } else { - wfDebug( "$fname failed to laod group id $d (name $name)\n" ); - } + wfDebug( "$fname caching group $id (name {$group->name})\n" ); + $wgMemc->add( $key, $group, 3600 ); return $group; } } @@ -143,31 +204,89 @@ class Group { /** @param string $name Group database name */ function newFromName($name) { $fname = 'Group::newFromName'; - $g = new Group(); - $g->setId( $g->idFromName($name) ); + $staticGroups =& Group::getStaticGroups(); + if ( $staticGroups ) { + $id = Group::idFromName( $name ); + if ( array_key_exists( $id, $staticGroups ) ) { + return $staticGroups[$id]; + } else { + return null; + } + } + + $g = new Group(); + $g->name = $name; + $g->loadFromDatabase(); + if( $g->getId() != 0 ) { return $g; } else { - return; + return null; + } + } + + /** + * Get an array of Group objects, one for each valid group + * + * @static + */ + function &getAllGroups() { + $staticGroups =& Group::getStaticGroups(); + if ( $staticGroups ) { + return $staticGroups; } + + $fname = 'Group::getAllGroups'; + wfProfileIn( $fname ); + + $dbr =& wfGetDB( DB_SLAVE ); + $groupTable = $dbr->tableName( 'groups' ); + $sql = "SELECT gr_id, gr_name, gr_description, gr_rights FROM $groupTable"; + $res = $dbr->query($sql, $fname); + + $groups = array(); + + while($row = $dbr->fetchObject( $res ) ) { + $group = new Group; + $group->loadFromRow( $row ); + $groups[$row->gr_id] = $group; + } + + wfProfileOut( $fname ); + return $groups; } + /** + * Get static groups, if they have been defined in LocalSettings.php + * + * @static + */ + function &getStaticGroups() { + global $wgStaticGroups; + if ( $wgStaticGroups === false ) { + return $wgStaticGroups; + } + + if ( !is_array( $wgStaticGroups ) ) { + $wgStaticGroups = unserialize( $wgStaticGroups ); + } + + return $wgStaticGroups; + } + + // Converters /** * @param integer $id Group database id * @return string Group database name */ function nameFromId($id) { - $fname = 'Group::nameFromId'; - $dbr =& wfGetDB( DB_SLAVE ); - $r = $dbr->selectRow( 'group', array( 'group_name' ), array( 'group_id' => $id ), $fname ); - if ($dbr == null) - return; - if($r === false) { + $group = Group::newFromId( $id ); + if ( is_null( $group ) ) { return ''; } else { - return $r->group_name; + return $group->getName(); } } @@ -177,13 +296,25 @@ class Group { */ function idFromName($name) { $fname = 'Group::idFromName'; + + $staticGroups =& Group::getStaticGroups(); + if ( $staticGroups ) { + foreach( $staticGroups as $id => $group ) { + if ( $group->getName() === $name ) { + return $group->getId(); + } + } + return 0; + } + + $dbr =& wfGetDB( DB_SLAVE ); - $r = $dbr->selectRow( 'group', array( 'group_id' ), array( 'group_name' => $name ), $fname ); + $r = $dbr->selectRow( 'groups', array( 'gr_id' ), array( 'gr_name' => $name ), $fname ); if($r === false) { return 0; } else { - return $r->group_id; + return $r->gr_id; } } @@ -191,7 +322,18 @@ class Group { function getName() { $this->loadFromDatabase(); return $this->name; - } + } + + function getExpandedName() { + $this->loadFromDatabase(); + return $this->getMessage( $this->name ); + } + + function getNameForContent() { + $this->loadFromDatabase(); + return $this->getMessageForContent( $this->name ); + } + function setName($name) { $this->loadFromDatabase(); $this->name = $name; @@ -201,9 +343,16 @@ class Group { function setId($id) { $this->id = IntVal($id); $this->dataLoaded = false; - } + } - function getDescription() { return $this->description; } + function getDescription() { + return $this->description; + } + + function getExpandedDescription() { + return $this->getMessage( $this->description ); + } + function setDescription($desc) { $this->loadFromDatabase(); $this->description = $desc; @@ -214,5 +363,28 @@ class Group { $this->loadFromDatabase(); $this->rights = $rights; } + + /** + * Gets a message if the text starts with a colon, otherwise returns the text itself + */ + function getMessage( $text ) { + if ( strlen( $text ) && $text{0} == ':' ) { + return wfMsg( substr( $text, 1 ) ); + } else { + return $text; + } + } + + /** + * As for getMessage but for content + */ + function getMessageForContent( $text ) { + if ( strlen( $text ) && $text{0} == ':' ) { + return wfMsgForContent( substr( $text, 1 ) ); + } else { + return $text; + } + } + } -?> \ No newline at end of file +?>