+++ /dev/null
-<?php
-/**
- * @package MediaWiki
- */
-
-/**
- * Class to manage a group
- * @package MediaWiki
- */
-class Group {
- /**#@+
- * @private
- */
- /** string $name Group name */
- var $name;
- /** integer $id Group id */
- var $id;
- /** string $description Description of the group */
- var $description;
- /** boolean $dataLoaded Whether data has been loaded from the database */
- var $dataLoaded;
- /** string $rights Contain rights values : "foo,bar,bla" */
- var $rights;
- /**#@-*/
-
- /** Constructor */
- function Group() {
- $this->clear();
- }
-
- /** Clear variables */
- function clear() {
- $this->name = '';
- $this->id = 0;
- $this->description = '';
- $this->dataLoaded = false;
- $this->rights = false;
- }
-
- /** Load group data from database */
- function loadFromDatabase() {
- $fname = 'Group::loadFromDatabase';
-
- // See if it's already loaded
- if ( $this->dataLoaded || Group::getStaticGroups() ) {
- return;
- }
-
- // be sure it's an integer
- $this->id = intval($this->id);
-
- if($this->id) {
- // By ID
- $dbr =& wfGetDB( DB_SLAVE );
- $r = $dbr->selectRow('groups',
- array('gr_id', 'gr_name', 'gr_description', 'gr_rights'),
- array( 'gr_id' => $this->id ),
- $fname );
- if ( $r ) {
- $this->loadFromRow( $r );
- } else {
- $this->id = 0;
- $this->dataLoaded = true;
- }
- } else {
- // By name
- $dbr =& wfGetDB( DB_SLAVE );
- $r = $dbr->selectRow('groups',
- array('gr_id', 'gr_name', 'gr_description', 'gr_rights'),
- array( 'gr_name' => $this->name ),
- $fname );
- 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() ) {
- throw new MWException( "Can't modify groups in static mode" );
- }
-
- $fname = 'Group::addToDatabase';
- $dbw =& wfGetDB( DB_MASTER );
- $dbw->insert( 'groups',
- array(
- 'gr_name' => $this->name,
- 'gr_description' => $this->description,
- 'gr_rights' => $this->rights
- ), $fname
- );
- $this->id = $dbw->insertId();
- }
-
- /** Save the group data into database */
- function save() {
- global $wgMemc;
-
- if ( Group::getStaticGroups() ) {
- throw new MWException( "Can't modify groups in static mode" );
- }
- if($this->id == 0) { return; }
-
- $fname = 'Group::save';
- $dbw =& wfGetDB( DB_MASTER );
-
- $dbw->update( 'groups',
- array( /* SET */
- 'gr_name' => $this->name,
- 'gr_description' => $this->description,
- 'gr_rights' => $this->rights
- ), array( /* WHERE */
- 'gr_id' => $this->id
- ), $fname
- );
-
- $wgMemc->set( Group::getCacheKey( $this->id ), $this, 3600 );
- }
-
-
- /** Delete a group */
- function delete() {
- global $wgMemc;
-
- if ( Group::getStaticGroups() ) {
- throw new MWException( "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.
- * @param $id Integer: group database id
- */
- function newFromId($id) {
- global $wgMemc;
- $fname = 'Group::newFromId';
-
- $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;
- }
-
- $group = new Group();
- $group->id = $id;
- $group->loadFromDatabase();
-
- if ( !$group->id ) {
- wfDebug( "$fname can't find group $id\n" );
- return null;
- } else {
- wfDebug( "$fname caching group $id (name {$group->name})\n" );
- $wgMemc->add( $key, $group, 3600 );
- return $group;
- }
- }
-
-
- /** @param $name String: group database name */
- function newFromName($name) {
- $fname = 'Group::newFromName';
-
- $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 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 $id Integer: group database id
- * @return string Group database name
- */
- function nameFromId($id) {
- $group = Group::newFromId( $id );
- if ( is_null( $group ) ) {
- return '';
- } else {
- return $group->getName();
- }
- }
-
- /**
- * @param $name String: group database name
- * @return integer Group database id
- */
- 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( 'groups', array( 'gr_id' ), array( 'gr_name' => $name ), $fname );
-
- if($r === false) {
- return 0;
- } else {
- return $r->gr_id;
- }
- }
-
-// Accessors for private variables
- 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;
- }
-
- function getId() { return $this->id; }
- function setId($id) {
- $this->id = intval($id);
- $this->dataLoaded = false;
- }
-
- function getDescription() {
- return $this->description;
- }
-
- function getExpandedDescription() {
- return $this->getMessage( $this->description );
- }
-
- function setDescription($desc) {
- $this->loadFromDatabase();
- $this->description = $desc;
- }
-
- function getRights() { return $this->rights; }
- function setRights($rights) {
- $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;
- }
- }
-
-}
-?>
+++ /dev/null
-<?php
-/**
- * Provide an administration interface
- * @package MediaWiki
- * @subpackage SpecialPage
- */
-
-/** */
-require_once('HTMLForm.php');
-require_once('Group.php');
-
-/** Entry point */
-function wfSpecialGroups() {
- global $wgRequest;
-
- $form = new GroupsForm($wgRequest);
- $form->execute();
-}
-
-/**
- * A class to manage group levels rights.
- * @package MediaWiki
- * @subpackage SpecialPage
- */
-class GroupsForm extends HTMLForm {
- var $mPosted, $mRequest, $mSaveprefs, $mChangeAllowed;
- var $mNewName, $mDescription, $mOldName, $mRights, $mId;
- var $mAdd, $mEdit;
-
- /** Escaped local url name*/
- var $action, $location;
-
- /** Constructor*/
- function GroupsForm ( &$request ) {
- global $wgUser;
-
- $this->mPosted = $request->wasPosted();
- $this->mRequest =& $request;
- $this->mName = 'groups';
-
- $this->mNewName = trim( $request->getText('editgroup-name') );
- $this->mOldName = trim( $request->getText('editgroup-oldname' ) );
- $this->mDescription = trim( $request->getText( 'editgroup-description' ) );
- $this->mRights = $request->getArray( 'editgroup-getrights' );
- $this->mId = $this->mRequest->getInt('id');
- $this->mEdit = $request->getCheck('edit');
- $this->mAdd = $request->getCheck('add');
-
-
- $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
- $this->action = $titleObj->escapeLocalURL();
- if ( $this->mAdd ) {
- $this->location = $titleObj->getLocalURL( "add=1&id={$this->mId}" );
- } elseif ( $this->mEdit ) {
- $this->location = $titleObj->getLocalURL( "edit=1&id={$this->mId}" );
- } else {
- $this->location = $this->action;
- }
-
- $this->mChangeAllowed = $wgUser->isAllowed( 'grouprights' ) && !Group::getStaticGroups();
- }
-
- /**
- * Manage forms to be shown according to posted data
- * Depending on the submit button used, call a form or a saving function.
- */
- function execute() {
- global $wgOut;
-
- if ( $this->mRequest->getBool( 'showrecord' ) ) {
- $this->showRecord();
- } elseif ( $this->mPosted && $this->mChangeAllowed && $this->mRequest->getCheck('savegroup') ) {
- // save settings
- $this->saveGroup();
- } elseif ( $this->mEdit ) {
- if ( $this->mPosted ) {
- $wgOut->redirect( $this->location );
- } else {
- $this->switchForm();
- $this->editGroupForm( $this->mId );
- }
- } elseif ( $this->mAdd ) {
- if ( $this->mPosted ) {
- $wgOut->redirect( $this->location );
- } else {
- $this->switchForm();
- $this->editGroupForm( );
- }
- } else {
- $this->showAllGroups();
- if ( $this->mChangeAllowed ) {
- $this->switchForm();
- }
- }
- }
-
- /**
- * Save a group
- */
- function saveGroup() {
- global $wgOut;
-
- $this->mNewName = trim($this->mNewName);
-
- if ( $this->mNewName == '' ) {
- $this->editGroupForm( $this->mGroupID, 'groups-noname' );
- return false;
- }
-
- if($this->mOldName == '') {
- // Check if the group already exists
- $add = true;
- $g = Group::newFromName( $this->mNewName );
- if ( $g ) {
- $this->editGroupForm( 0, 'groups-already-exists' );
- return;
- }
-
- // Create a new group
- $g = new Group();
- $g->addToDatabase();
- } else {
- $add = false;
- $g = Group::newFromName($this->mOldName);
- if ( !$g ) {
- $this->editGroupForm( 0, 'groups-noname' );
- return;
- }
- }
-
- // save stuff
- $g->setName($this->mNewName);
- $g->setDescription($this->mDescription);
- if( is_array( $this->mRights ) ) {
- $g->setRights( implode(',',$this->mRights) );
- }
-
- $g->save();
-
- // Make the log entry
- $log = new LogPage( 'rights' );
- $dummyTitle = Title::makeTitle( 0, '' );
- if ( $add ) {
- $log->addEntry( 'addgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
- } else {
- if ( $this->mOldName != $this->mNewName ) {
- // Abbreviated action name, must be less than 10 bytes
- $log->addEntry( 'rngroup', $dummyTitle, '', array( Group::getMessageForContent( $this->mOldName ),
- $g->getNameForContent() ) );
- } else {
- $log->addEntry( 'chgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
- }
- }
-
- // Success, go back to all groups page
- $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
- $url = $titleObj->getLocalURL();
-
- $wgOut->redirect( $url );
- }
-
- /**
- * The entry form
- * It allows a user to edit or eventually add a group
- */
- function switchForm() {
- global $wgOut;
-
- // group selection
- $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
- $wgOut->addHTML( $this->fieldset( 'lookup-group',
- HTMLSelectGroups('id', $this->mName.'-group-edit', array(0 => $this->mRequest->getVal('id')) ) .
- ' <input type="submit" name="edit" value="'.wfMsg('editgroup').'" />' .
- '<br /><input type="submit" name="add" value="'.wfMsg('addgroup').'" />'
- ));
- $wgOut->addHTML( "</form>\n" );
- }
-
- /**
- * Edit a group properties and rights.
- * @param $groupname String: Name of a group to be edited.
- * @param $error String: message name of the error to display
- */
- function editGroupForm($groupID = 0, $error = '') {
- global $wgOut;
-
- if ( $error ) {
- $errText = wfMsg( $error );
- $wgOut->addHTML( "<p class='error'>$errText</p>" );
- }
-
- if($this->mRequest->getVal('edit')) {
- // fetch data if we edit a group
- $g = Group::newFromID($groupID);
- $fieldname = 'editgroup';
- } else {
- // default data when we add a group
- $g = new Group();
- $fieldname = 'addgroup';
- }
-
- $gName = htmlspecialchars( $g->getName() );
- $gDescription = htmlspecialchars( $g->getDescription() );
-
-
- $wgOut->addHTML( "<form name=\"editGroup\" action=\"{$this->action}\" method=\"post\">\n".
- '<input type="hidden" name="editgroup-oldname" value="'.$gName."\" />\n" );
-
- $wgOut->addHTML( $this->fieldset( $fieldname,
- '<p>' . wfMsg( 'groups-editgroup-preamble' ) . "</p>\n" .
- $this->textbox( 'editgroup-name', $gName ) .
- $this->textareabox( 'editgroup-description', $gDescription ) .
- '<br /><table border="0" align="center"><tr><td>'.
- HTMLSelectRights($g->getRights()).
- '</td></tr></table>'."\n".
- '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
- ));
-
- $wgOut->addHTML( "</form>\n" );
- }
-
- function showAllGroups() {
- global $wgOut;
- $groups =& Group::getAllGroups();
-
- $groupsExisting = wfMsg( 'groups-existing' );
- $groupsHeader = wfMsg( 'groups-tableheader' );
-
- $s = "{| border=1
-|+'''$groupsExisting'''
-|-
-!$groupsHeader
-";
- foreach ( $groups as $group ) {
- $s .= "|-\n| " . $group->getId() . ' || ' .
- $group->getExpandedName() . ' || ' .
- $group->getExpandedDescription() . ' || '.
- // Insert spaces to make it wrap
- str_replace( ',', ', ', $group->getRights() ) . "\n";
- }
- $s .= "|}\n";
- $wgOut->addWikiText( $s );
- }
-
- function showRecord() {
- global $wgOut;
-
- $groups =& Group::getAllGroups();
- $rec = serialize( $groups );
- // Split it into lines
- $rec = explode( "\r\n", chunk_split( $rec ) );
- $s = '';
- foreach ( $rec as $index => $line ) {
- if ( trim( $line ) != '' ) {
- if ( $s ) {
- $s .= "' .\n\t'";
- }
- // Escape it for PHP
- $line = str_replace( array( '\\', "'" ), array( '\\\\', "\\'" ), $line );
- // Escape it for HTML
- $line = htmlspecialchars( $line );
- // Add it to the string
- $s .= $line;
- }
- }
- $s .= "';";
- $s = "<p>Copy the following into LocalSettings.php:</p>\n" .
- "<textarea readonly rows=20>\n" .
- "\$wgStaticGroups = \n\t'$s\n" .
- "</textarea>";
- $wgOut->addHTML( $s );
- }
-
-} // end class GroupsForm
-?>