Page MenuHomePhabricator

SpecialMakebot.php

Authored By
bzimport
Nov 21 2014, 8:54 PM
Size
12 KB
Referenced Files
None
Subscribers
None

SpecialMakebot.php

<?php
/**
* @package MediaWiki
* @subpackage SpecialPage
*/
/**
*
*/
if ( defined( 'MEDIAWIKI' ) ) {
$wgExtensionFunctions[] = 'wfSetupMakebot';
$wgExtensionCredits['specialpage'][] = array(
'name' => 'Makebot',
'description' => 'gives bureaucrats/stewards the ability to modify user levels'
);
// Set groups to the appropriate bot/bureaucrat structure:
// * Steward can do 'full' work (makebot && userrights)
// * Bureaucrat can only do limited work (makebot)
$wgGroupPermissions['steward' ]['makebot' ] = true;
$wgGroupPermissions['steward' ]['userrights'] = true;
$wgGroupPermissions['bureaucrat']['makebot' ] = true;
$wgGroupPermissions['bureaucrat']['userrights'] = false;
$wgAvailableRights[] = 'makebot';
function wfSetupMakebot() {
require_once( 'SpecialPage.php' );
require_once( 'SpecialUserrights.php' );
global $wgMessageCache;
$wgMessageCache->addMessages(
array(
'makebottitle' => 'Set bot status',
'makebottext' => 'This form is used by bureaucrats to set bot status. Type the name of the account in the box and press the button to set bot status.',
'makebotname' => 'Name of the user:',
'makebotsubmit' => 'Set bot status',
'makebotok' => "<b>User \"$1\" has been set as a bot</b>",
'makebotfail' => "<b>User \"$1\" could not be set as a bot. (Did you enter the name correctly?)</b>",
'setbureaucratflag' => 'Set bureaucrat flag',
'setstewardflag' => 'Set steward flag',
'bureaucratlog' => 'Bureaucrat_log',
'rightslogtext' => 'This is a log of changes to user rights.',
'bureaucratlogentry' => "Changed group membership for $1 from $2 to $3",
'rights' => 'Rights:',
'set_user_rights' => 'Set user rights',
'user_rights_set' => "<b>User rights for \"$1\" updated</b>",
'set_rights_fail' => "<b>User rights for \"$1\" could not be set. (Did you enter the name correctly?)</b>",
'makebot' => 'Set bot status',
'already_bot' => 'This user is already set as a bot',
'makebot-nodatabase' => 'Bad interwiki username: $1',
)
);
SpecialPage::addPage( new SpecialPage( 'Makebot', 'makebot', /*listed*/ true, /*function*/ false, /*file*/ false ) );
/**
* Constructor
*/
function wfSpecialMakebot() {
require_once( "LinksUpdate.php" );
global $wgUser, $wgOut, $wgRequest;
global $wgMessageCache;
$wgMessageCache->addMessage('bureaucrattitle', 'Bureaucrat access required');
$wgMessageCache->addMessage('bureaucrattext', 'The action you have requested can only be performed by bots with "bureaucrat" status.');
if ( $wgUser->isAnon() or $wgUser->isBlocked() ) {
$wgOut->errorpage( "movenologin", "movenologintext" );
return;
}
if ( wfReadOnly() ) {
$wgOut->readOnlyPage();
return;
}
if( $wgUser->isAllowed( 'userrights' ) ) {
$f = new MakebotStewardForm( $wgRequest );
$f->execute();
} else {
$f = new MakebotForm( $wgRequest );
if ( $f->mSubmit ) {
$f->doSubmit();
} else {
$f->showForm( '' );
}
}
}
/**
*
* @package MediaWiki
* @subpackage SpecialPage
*/
class MakebotForm {
var $mTarget, $mAction, $mRights, $mUser, $mSubmit, $mSetBureaucrat;
function MakebotForm( &$request ) {
global $wgUser;
$this->mAction = $request->getText( 'action' );
$this->mRights = $request->getVal( 'wpRights' );
$this->mUser = $request->getText( 'wpMakebotUser' );
$this->mSubmit = $request->getBool( 'wpMakebotSubmit' ) &&
$request->wasPosted() &&
$wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
}
function showForm( $err = '') {
global $wgOut, $wgUser, $wgLang;
$wgOut->setPagetitle( wfMsg( "makebottitle" ) );
$wgOut->addWikiText( wfMsg( "makebottext" ) );
$titleObj = Title::makeTitle( NS_SPECIAL, "Makebot" );
$action = $titleObj->escapeLocalURL( "action=submit" );
if ( "" != $err ) {
$wgOut->setSubtitle( wfMsg( "formerror" ) );
$wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
}
$namedesc = wfMsg( "makebotname" );
if ( !is_null( $this->mUser ) ) {
$encUser = htmlspecialchars( $this->mUser );
} else {
$encUser = "";
}
$wgOut->addHTML( "
<form id=\"makebot\" method=\"post\" action=\"{$action}\">
<table border='0'>
<tr>
<td align='right'>$namedesc</td>
<td align='left'>
<input type='text' size='40' name=\"wpMakebotUser\" value=\"$encUser\" />
</td>
</tr>"
);
$mss = wfMsg( "set_user_rights" );
$token = htmlspecialchars( $wgUser->editToken() );
$wgOut->addHTML(
"<tr>
<td>&nbsp;</td><td align='left'>
<input type='submit' name=\"wpMakebotSubmit\" value=\"{$mss}\" />
</td></tr></table>
<input type='hidden' name='wpEditToken' value=\"{$token}\" />
</form>\n"
);
}
function doSubmit() {
global $wgOut, $wgUser, $wgLang;
global $wgDBname, $wgMemc, $wgLocalDatabases, $wgSharedDB;
$fname = 'MakebotForm::doSubmit';
$dbw =& wfGetDB( DB_MASTER );
$user_groups = $dbw->tableName( 'user_groups' );
$usertable = $dbw->tableName( 'user' );
$username = $this->mUser;
$dbName = $wgDBname;
// Clean up username
$t = Title::newFromText( $username );
if ( !$t ) {
$this->showFail();
return;
}
$username = $t->getText();
if ( $username{0} == "#" ) {
$id = intval( substr( $username, 1 ) );
} else {
$id = $dbw->selectField( $usertable, 'user_id', array( 'user_name' => $username ), $fname );
}
if ( !$id ) {
$this->showFail();
return;
}
$sql = "SELECT ug_user,ug_group FROM $user_groups WHERE ug_user=$id FOR UPDATE";
$res = $dbw->query( $sql, $fname );
$row = false;
$groups = array();
while ( $row = $dbw->fetchObject( $res ) ) {
$groups[$row->ug_group] = true;
}
$dbw->freeResult( $res );
$rightsNotation = array();
$wasbot = !empty( $groups['bot'] );
if ( $wasbot ) {
$this->showFail( 'already_bot' );
return;
}
if ( !$wasbot ) {
$dbw->insert( $user_groups, array( 'ug_user' => $id, 'ug_group' => 'bot' ), $fname );
$rightsNotation[] = "+bot";
}
$wgMemc->delete( "$dbName:user:id:$id" );
$log = new LogPage( 'rights' );
$log->addEntry( 'rights', Title::makeTitle( NS_USER, $this->mUser ),
implode( " ", $rightsNotation ) );
$this->showSuccess();
}
function showSuccess() {
global $wgOut, $wgUser;
$wgOut->setPagetitle( wfMsg( "makebottitle" ) );
$text = wfMsg( "makebotok", $this->mUser );
$text .= "\n\n";
$wgOut->addWikiText( $text );
$this->showForm();
}
function showFail( $msg = 'set_rights_fail' ) {
global $wgOut, $wgUser;
$wgOut->setPagetitle( wfMsg( "makebottitle" ) );
$this->showForm( wfMsg( $msg, $this->mUser ) );
}
}
/**
*
* @package MediaWiki
* @subpackage SpecialPage
*/
class MakebotStewardForm extends UserrightsForm {
function MakebotStewardForm( $request ) {
$this->mPosted = $request->wasPosted();
$this->mRequest =& $request;
$this->mName = 'userrights';
$titleObj = Title::makeTitle( NS_SPECIAL, 'Makebot' );
$this->action = $titleObj->escapeLocalURL();
$this->db = null;
}
function saveUserGroups( $username, $removegroup, $addgroup) {
$split = $this->splitUsername( $username );
if( WikiError::isError( $split ) ) {
$wgOut->addWikiText( wfMsg( 'makebot-nodatabase', $split->getMessage() ) );
return;
}
list( $database, $name ) = $split;
$this->db =& $this->getDB( $database );
$userid = $this->getUserId( $database, $name );
if( $userid == 0) {
$wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
return;
}
$oldGroups = $this->getUserGroups( $database, $userid );
$newGroups = $oldGroups;
$logcomment = ' ';
// remove then add groups
if(isset($removegroup)) {
$newGroups = array_diff($newGroups, $removegroup);
foreach( $removegroup as $group ) {
$this->removeUserGroup( $database, $userid, $group );
}
}
if(isset($addgroup)) {
$newGroups = array_merge($newGroups, $addgroup);
foreach( $addgroup as $group ) {
$this->addUserGroup( $database, $userid, $group );
}
}
$newGroups = array_unique( $newGroups );
// Ensure that caches are cleared
$this->touchUser( $database, $userid );
wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
$log = new LogPage( 'rights' );
$log->addEntry( 'rights', Title::makeTitle( NS_USER, $username ), '', array( $this->makeGroupNameList( $oldGroups ),
$this->makeGroupNameList( $newGroups ) ) );
}
/**
* Edit user groups membership
* @param string $username Name of the user.
*/
function editUserGroupsForm($username) {
global $wgOut, $wgUser;
$split = $this->splitUsername( $username );
if( WikiError::isError( $split ) ) {
$wgOut->addWikiText( wfMsg( 'makebot-nodatabase', $split->getMessage() ) );
return;
}
list( $database, $name ) = $split;
$this->db =& $this->getDB( $database );
$userid = $this->getUserId( $database, $name );
if( $userid == 0) {
$wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
return;
}
$groups = $this->getUserGroups( $database, $userid );
$wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
wfElement( 'input', array(
'type' => 'hidden',
'name' => 'user-editname',
'value' => $username ) ) .
wfElement( 'input', array(
'type' => 'hidden',
'name' => 'wpEditToken',
'value' => $wgUser->editToken( $username ) ) ) .
$this->fieldset( 'editusergroup',
$wgOut->parse( wfMsg('editing', $username ) ) .
'<table border="0" align="center"><tr><td>'.
HTMLSelectGroups('member', $this->mName.'-groupsmember', $groups,true,6).
'</td><td>'.
HTMLSelectGroups('available', $this->mName.'-groupsavailable', $groups,true,6,true).
'</td></tr></table>'."\n".
$wgOut->parse( wfMsg('userrights-groupshelp') ) .
wfElement( 'input', array(
'type' => 'submit',
'name' => 'saveusergroups',
'value' => wfMsg( 'saveusergroups' ) ) )
));
$wgOut->addHTML( "</form>\n" );
}
function splitUsername( $username ) {
$parts = explode( '@', $username );
if( count( $parts ) < 2 ) {
return array( '', $username );
}
list( $name, $database ) = $parts;
global $wgLocalDatabases;
return array_search( $database, $wgLocalDatabases ) !== false
? array( $database, $name )
: new WikiError( 'Bogus database suffix "' . wfEscapeWikiText( $database ) . '"' );
}
/**
* Open a database connection to work on for the requested user.
* This may be a new connection to another database for remote users.
* @param string $database
* @return Database
*/
function &getDB( $database ) {
global $wgDBname;
if( $database == '' ) {
$db =& wfGetDB( DB_MASTER );
} else {
global $wgDBserver, $wgDBuser, $wgDBpassword;
$db =& new Database( $wgDBserver, $wgDBuser, $wgDBpassword, $database );
}
return $db;
}
function getUserId( $database, $name ) {
return ( $name{0} == "#" )
? IntVal( substr( $name, 1 ) )
: IntVal( $this->db->selectField( 'user',
'user_id',
array( 'user_name' => $name ),
'MakebotStewardForm::getUserId' ) );
}
function getUserGroups( $database, $userid ) {
$res = $this->db->select( 'user_groups',
array( 'ug_group' ),
array( 'ug_user' => $userid ),
'MakebotStewardForm::getUserGroups' );
$groups = array();
while( $row = $this->db->fetchObject( $res ) ) {
$groups[] = $row->ug_group;
}
return $groups;
}
function addUserGroup( $database, $userid, $group ) {
$this->db->insert( 'user_groups',
array(
'ug_user' => $userid,
'ug_group' => $group,
),
'MakebotStewardForm::addUserGroup',
array( 'IGNORE' ) );
}
function removeUserGroup( $database, $userid, $group ) {
$this->db->delete( 'user_groups',
array(
'ug_user' => $userid,
'ug_group' => $group,
),
'MakebotStewardForm::addUserGroup' );
}
function touchUser( $database, $userid ) {
$this->db->update( 'user',
array( 'user_touched' => $this->db->timestamp() ),
array( 'user_id' => $userid ),
'MakebotStewardForm::touchUser' );
global $wgMemc;
$key = "$database:user:id:$userid";
$wgMemc->delete( $key );
}
}
}
} // End of invocation guard
?>

File Metadata

Mime Type
text/x-php
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1993
Default Alt Text
SpecialMakebot.php (12 KB)

Event Timeline