r10026 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r10025‎ | r10026 | r10027 >
Date:10:25, 9 July 2005
Author:vibber
Status:old
Tags:
Comment:
Base the magic steward mode on the Special:Userrights form so all groups
can be set and unset by a steward. Note: the local groups definition array
is used, so if different groups are defined on each wiki... too bad.
Modified paths:
  • /trunk/extensions/Makesysop/SpecialMakesysop.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Makesysop/SpecialMakesysop.php
@@ -24,9 +24,16 @@
2525
2626 function wfSetupMakesysop() {
2727 require_once( 'SpecialPage.php' );
 28+ require_once( 'SpecialUserrights.php' );
2829
 30+ global $wgMessageCache;
 31+ $wgMessageCache->addMessages(
 32+ array(
 33+ 'makesysop-nodatabase' => 'Bad interwiki username: $1',
 34+ )
 35+ );
 36+
2937 SpecialPage::addPage( new SpecialPage( 'Makesysop', 'makesysop', /*listed*/ true, /*function*/ false, /*file*/ false ) );
30 -}
3138
3239 /**
3340 * Constructor
@@ -48,12 +55,16 @@
4956 return;
5057 }
5158
52 - $f = new MakesysopForm( $wgRequest );
53 -
54 - if ( $f->mSubmit ) {
55 - $f->doSubmit();
56 - } else {
57 - $f->showForm( '' );
 59+ if( $wgUser->isAllowed( 'userrights' ) ) {
 60+ $f = new MakesysopStewardForm( $wgRequest );
 61+ $f->execute();
 62+ } else {
 63+ $f = new MakesysopForm( $wgRequest );
 64+ if ( $f->mSubmit ) {
 65+ $f->doSubmit();
 66+ } else {
 67+ $f->showForm( '' );
 68+ }
5869 }
5970 }
6071
@@ -63,7 +74,7 @@
6475 * @subpackage SpecialPage
6576 */
6677 class MakesysopForm {
67 - var $mTarget, $mAction, $mRights, $mUser, $mSubmit, $mSetBureaucrat, $mSetSteward;
 78+ var $mTarget, $mAction, $mRights, $mUser, $mSubmit, $mSetBureaucrat;
6879
6980 function MakesysopForm( &$request ) {
7081 global $wgUser;
@@ -75,10 +86,6 @@
7687 $request->wasPosted() &&
7788 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
7889 $this->mSetBureaucrat = $request->getBool( 'wpSetBureaucrat' );
79 - $this->mSetSteward = $request->getBool( 'wpSetSteward' );
80 -
81 - $this->mIsSteward = $wgUser->isAllowed( 'makesysop' ) &&
82 - $wgUser->isAllowed( 'userrights' );
8390 }
8491
8592 function showForm( $err = '') {
@@ -122,18 +129,7 @@
123130 </tr>"
124131 );
125132
126 - if ( $this->mIsSteward ) {
127 - $setstewardflag = wfMsg( "setstewardflag" );
128 - $wgOut->addHTML(
129 - "<tr>
130 - <td>&nbsp;</td><td align=left>
131 - <input type=checkbox name=\"wpSetSteward\" value=1>$setstewardflag
132 - </td>
133 - </tr>"
134 - );
135 - }
136133
137 -
138134 $mss = wfMsg( "set_user_rights" );
139135
140136 $token = htmlspecialchars( $wgUser->editToken() );
@@ -156,24 +152,9 @@
157153 $dbw =& wfGetDB( DB_MASTER );
158154 $user_groups = $dbw->tableName( 'user_groups' );
159155 $usertable = $dbw->tableName( 'user' );
160 - $parts = explode( '@', $this->mUser );
161156
162 - if( count( $parts ) == 2 && $this->mIsSteward && strpos( '.', $user_groups ) === false ){
163 - $username = $parts[0];
164 - if ( array_key_exists( $parts[1], $wgLocalDatabases ) ) {
165 - $dbName = $wgLocalDatabases[$parts[1]];
166 - $user_groups = "`$dbName`.$user_groups";
167 - if ( !$wgSharedDB ) {
168 - $usertable = "`$dbName`.$usertable";
169 - }
170 - } else {
171 - $this->showFail();
172 - return;
173 - }
174 - } else {
175 - $username = $this->mUser;
176 - $dbName = $wgDBname;
177 - }
 157+ $username = $this->mUser;
 158+ $dbName = $wgDBname;
178159
179160 // Clean up username
180161 $t = Title::newFromText( $username );
@@ -206,22 +187,11 @@
207188 $rightsNotation = array();
208189 $wasSysop = !empty( $groups['sysop'] );
209190 $wasBureaucrat = !empty( $groups['bureaucrat'] );
210 - $wasSteward = !empty( $groups['steward'] );
211191
212 - if ( $this->mSetSteward ) {
213 - if ( $wasSteward ) {
214 - $this->showFail( 'already_steward' );
215 - return;
216 - } else {
217 - $dbw->insert( $user_groups, array( 'ug_user' => $id, 'ug_group' => 'steward' ), $fname );
218 - }
219 - }
220192 if ( $this->mSetBureaucrat ) {
221193 if ( $wasBureaucrat ) {
222 - if ( !$this->mSetSteward ) {
223 - $this->showFail( 'already_bureaucrat' );
224 - return;
225 - }
 194+ $this->showFail( 'already_bureaucrat' );
 195+ return;
226196 } else {
227197 $dbw->insert( $user_groups, array( 'ug_user' => $id, 'ug_group' => 'bureaucrat' ), $fname );
228198 $rightsNotation[] = "+bureaucrat";
@@ -263,5 +233,192 @@
264234 }
265235 }
266236
 237+/**
 238+ *
 239+ * @package MediaWiki
 240+ * @subpackage SpecialPage
 241+ */
 242+class MakesysopStewardForm extends UserrightsForm {
 243+ function MakesysopStewardForm( $request ) {
 244+ $this->mPosted = $request->wasPosted();
 245+ $this->mRequest =& $request;
 246+ $this->mName = 'userrights';
 247+
 248+ $titleObj = Title::makeTitle( NS_SPECIAL, 'Makesysop' );
 249+ $this->action = $titleObj->escapeLocalURL();
 250+
 251+ $this->db =& wfGetDB( DB_MASTER );
 252+ }
 253+
 254+ function saveUserGroups( $username, $removegroup, $addgroup) {
 255+ $split = $this->splitUsername( $username );
 256+ if( WikiError::isError( $split ) ) {
 257+ $wgOut->addWikiText( wfMsg( 'makesysop-nodatabase', $split->getMessage() ) );
 258+ return;
 259+ }
 260+
 261+ list( $database, $name ) = $split;
 262+ $userid = $this->getUserId( $database, $name );
 263+
 264+ if( $userid == 0) {
 265+ $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
 266+ return;
 267+ }
 268+
 269+ $oldGroups = $this->getUserGroups( $database, $userid );
 270+ $newGroups = $oldGroups;
 271+ $logcomment = ' ';
 272+ // remove then add groups
 273+ if(isset($removegroup)) {
 274+ $newGroups = array_diff($newGroups, $removegroup);
 275+ foreach( $removegroup as $group ) {
 276+ $this->removeUserGroup( $database, $userid, $group );
 277+ }
 278+ }
 279+ if(isset($addgroup)) {
 280+ $newGroups = array_merge($newGroups, $addgroup);
 281+ foreach( $addgroup as $group ) {
 282+ $this->addUserGroup( $database, $userid, $group );
 283+ }
 284+ }
 285+ $newGroups = array_unique( $newGroups );
 286+
 287+ // Ensure that caches are cleared
 288+ $this->touchUser( $database, $userid );
 289+
 290+ wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
 291+ wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
 292+
 293+ $log = new LogPage( 'rights' );
 294+ $log->addEntry( 'rights', Title::makeTitle( NS_USER, $username ), '', array( $this->makeGroupNameList( $oldGroups ),
 295+ $this->makeGroupNameList( $newGroups ) ) );
 296+ }
 297+
 298+ /**
 299+ * Edit user groups membership
 300+ * @param string $username Name of the user.
 301+ */
 302+ function editUserGroupsForm($username) {
 303+ global $wgOut, $wgUser;
 304+
 305+ $split = $this->splitUsername( $username );
 306+ if( WikiError::isError( $split ) ) {
 307+ $wgOut->addWikiText( wfMsg( 'makesysop-nodatabase', $split->getMessage() ) );
 308+ return;
 309+ }
 310+
 311+ list( $database, $name ) = $split;
 312+ $userid = $this->getUserId( $database, $name );
 313+
 314+ if( $userid == 0) {
 315+ $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
 316+ return;
 317+ }
 318+
 319+ $groups = $this->getUserGroups( $database, $userid );
 320+
 321+ $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
 322+ wfElement( 'input', array(
 323+ 'type' => 'hidden',
 324+ 'name' => 'user-editname',
 325+ 'value' => $username ) ) .
 326+ wfElement( 'input', array(
 327+ 'type' => 'hidden',
 328+ 'name' => 'wpEditToken',
 329+ 'value' => $wgUser->editToken( $username ) ) ) .
 330+ $this->fieldset( 'editusergroup',
 331+ $wgOut->parse( wfMsg('editing', $username ) ) .
 332+ '<table border="0" align="center"><tr><td>'.
 333+ HTMLSelectGroups('member', $this->mName.'-groupsmember', $groups,true,6).
 334+ '</td><td>'.
 335+ HTMLSelectGroups('available', $this->mName.'-groupsavailable', $groups,true,6,true).
 336+ '</td></tr></table>'."\n".
 337+ $wgOut->parse( wfMsg('userrights-groupshelp') ) .
 338+ wfElement( 'input', array(
 339+ 'type' => 'submit',
 340+ 'name' => 'saveusergroups',
 341+ 'value' => wfMsg( 'saveusergroups' ) ) )
 342+ ));
 343+ $wgOut->addHTML( "</form>\n" );
 344+ }
 345+
 346+ function splitUsername( $username ) {
 347+ $parts = explode( '@', $username );
 348+ if( count( $parts ) < 2 ) {
 349+ return array( '', $username );
 350+ }
 351+ list( $name, $database ) = $parts;
 352+
 353+ global $wgLocalDatabases;
 354+ return array_key_exists( $database, $wgLocalDatabases )
 355+ ? array( $database, $name )
 356+ : new WikiError( 'Bogus database suffix "' . wfEscapeWikiText( $database ) . '"' );
 357+ }
 358+
 359+ function tableName( $database, $base ) {
 360+ global $wgSharedDB;
 361+ return ( $database == '' || ( $base == 'user' && $wgSharedDB ) )
 362+ ? $base
 363+ : "`{$database}`." . $this->db->tableName( $base );
 364+ }
 365+
 366+ function getUserId( $database, $name ) {
 367+ $table = $this->tableName( $database, 'user' );
 368+ return IntVal( $this->db->selectField( $table,
 369+ 'user_id',
 370+ array( 'user_name' => $name ),
 371+ 'MakesysopStewardForm::getUserId' ) );
 372+ }
 373+
 374+ function getUserGroups( $database, $userid ) {
 375+ $table = $this->tableName( $database, 'user_groups' );
 376+ $res = $this->db->select( $table,
 377+ array( 'ug_group' ),
 378+ array( 'ug_user' => $userid ),
 379+ 'MakesysopStewardForm::getUserGroups' );
 380+ $groups = array();
 381+ while( $row = $this->db->fetchObject( $res ) ) {
 382+ $groups[] = $row->ug_group;
 383+ }
 384+ return $groups;
 385+ }
 386+
 387+ function addUserGroup( $database, $userid, $group ) {
 388+ $table = $this->tableName( $database, 'user_groups' );
 389+ $this->db->insert( $table,
 390+ array(
 391+ 'ug_user' => $userid,
 392+ 'ug_group' => $group,
 393+ ),
 394+ 'MakesysopStewardForm::addUserGroup',
 395+ array( 'IGNORE' ) );
 396+ }
 397+
 398+ function removeUserGroup( $database, $userid, $group ) {
 399+ $table = $this->tableName( $database, 'user_groups' );
 400+ $this->db->delete( $table,
 401+ array(
 402+ 'ug_user' => $userid,
 403+ 'ug_group' => $group,
 404+ ),
 405+ 'MakesysopStewardForm::addUserGroup' );
 406+ }
 407+
 408+ function touchUser( $database, $userid ) {
 409+ $table = $this->tableName( $database, 'user' );
 410+ $this->db->update( $table,
 411+ array( 'user_touched' => $this->db->timestamp() ),
 412+ array( 'user_id' => $userid ),
 413+ 'MakesysopStewardForm::touchUser' );
 414+
 415+ global $wgMemc;
 416+ $key = "$database:user:id:$userid";
 417+ $wgMemc->delete( $key );
 418+ }
 419+
 420+}
 421+
 422+}
 423+
267424 } // End of invocation guard
268425 ?>

Status & tagging log