Index: trunk/extensions/Makesysop/SpecialMakesysop.php |
— | — | @@ -24,9 +24,16 @@ |
25 | 25 | |
26 | 26 | function wfSetupMakesysop() { |
27 | 27 | require_once( 'SpecialPage.php' ); |
| 28 | + require_once( 'SpecialUserrights.php' ); |
28 | 29 | |
| 30 | + global $wgMessageCache; |
| 31 | + $wgMessageCache->addMessages( |
| 32 | + array( |
| 33 | + 'makesysop-nodatabase' => 'Bad interwiki username: $1', |
| 34 | + ) |
| 35 | + ); |
| 36 | + |
29 | 37 | SpecialPage::addPage( new SpecialPage( 'Makesysop', 'makesysop', /*listed*/ true, /*function*/ false, /*file*/ false ) ); |
30 | | -} |
31 | 38 | |
32 | 39 | /** |
33 | 40 | * Constructor |
— | — | @@ -48,12 +55,16 @@ |
49 | 56 | return; |
50 | 57 | } |
51 | 58 | |
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 | + } |
58 | 69 | } |
59 | 70 | } |
60 | 71 | |
— | — | @@ -63,7 +74,7 @@ |
64 | 75 | * @subpackage SpecialPage |
65 | 76 | */ |
66 | 77 | class MakesysopForm { |
67 | | - var $mTarget, $mAction, $mRights, $mUser, $mSubmit, $mSetBureaucrat, $mSetSteward; |
| 78 | + var $mTarget, $mAction, $mRights, $mUser, $mSubmit, $mSetBureaucrat; |
68 | 79 | |
69 | 80 | function MakesysopForm( &$request ) { |
70 | 81 | global $wgUser; |
— | — | @@ -75,10 +86,6 @@ |
76 | 87 | $request->wasPosted() && |
77 | 88 | $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) ); |
78 | 89 | $this->mSetBureaucrat = $request->getBool( 'wpSetBureaucrat' ); |
79 | | - $this->mSetSteward = $request->getBool( 'wpSetSteward' ); |
80 | | - |
81 | | - $this->mIsSteward = $wgUser->isAllowed( 'makesysop' ) && |
82 | | - $wgUser->isAllowed( 'userrights' ); |
83 | 90 | } |
84 | 91 | |
85 | 92 | function showForm( $err = '') { |
— | — | @@ -122,18 +129,7 @@ |
123 | 130 | </tr>" |
124 | 131 | ); |
125 | 132 | |
126 | | - if ( $this->mIsSteward ) { |
127 | | - $setstewardflag = wfMsg( "setstewardflag" ); |
128 | | - $wgOut->addHTML( |
129 | | - "<tr> |
130 | | - <td> </td><td align=left> |
131 | | - <input type=checkbox name=\"wpSetSteward\" value=1>$setstewardflag |
132 | | - </td> |
133 | | - </tr>" |
134 | | - ); |
135 | | - } |
136 | 133 | |
137 | | - |
138 | 134 | $mss = wfMsg( "set_user_rights" ); |
139 | 135 | |
140 | 136 | $token = htmlspecialchars( $wgUser->editToken() ); |
— | — | @@ -156,24 +152,9 @@ |
157 | 153 | $dbw =& wfGetDB( DB_MASTER ); |
158 | 154 | $user_groups = $dbw->tableName( 'user_groups' ); |
159 | 155 | $usertable = $dbw->tableName( 'user' ); |
160 | | - $parts = explode( '@', $this->mUser ); |
161 | 156 | |
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; |
178 | 159 | |
179 | 160 | // Clean up username |
180 | 161 | $t = Title::newFromText( $username ); |
— | — | @@ -206,22 +187,11 @@ |
207 | 188 | $rightsNotation = array(); |
208 | 189 | $wasSysop = !empty( $groups['sysop'] ); |
209 | 190 | $wasBureaucrat = !empty( $groups['bureaucrat'] ); |
210 | | - $wasSteward = !empty( $groups['steward'] ); |
211 | 191 | |
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 | | - } |
220 | 192 | if ( $this->mSetBureaucrat ) { |
221 | 193 | if ( $wasBureaucrat ) { |
222 | | - if ( !$this->mSetSteward ) { |
223 | | - $this->showFail( 'already_bureaucrat' ); |
224 | | - return; |
225 | | - } |
| 194 | + $this->showFail( 'already_bureaucrat' ); |
| 195 | + return; |
226 | 196 | } else { |
227 | 197 | $dbw->insert( $user_groups, array( 'ug_user' => $id, 'ug_group' => 'bureaucrat' ), $fname ); |
228 | 198 | $rightsNotation[] = "+bureaucrat"; |
— | — | @@ -263,5 +233,192 @@ |
264 | 234 | } |
265 | 235 | } |
266 | 236 | |
| 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 | + |
267 | 424 | } // End of invocation guard |
268 | 425 | ?> |