User:Joepayne/grantBureaucrat.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Joepayne (talk | contribs)
Fix template
Tsukushi (talk | contribs)
m Removed protection from "User:Joepayne/grantBureaucrat.js": Unnecessary
 
(No difference)

Latest revision as of 06:58, 3 September 2023

//Script to strip rights from a user on one click
//To do: add toggles for different skins
if( mw.config.get("wgRelevantUserName") ) {
	var username = mw.config.get("wgRelevantUserName");
	$.getJSON(
	//Get user's group membership
    mw.util.wikiScript('api'),
    {
        format: 'json',
        action: 'query',
        list: 'users',
        usprop: 'groups',
        ususers: username
    }
	).done( function ( data ) {
    try {
    	if(data.query.users[0].groups.indexOf('bureaucrat') >= 0) {/* Do nothing */}
    	else {
			var link = mw.util.addPortletLink(
	    		'sidebar',
	    		'#',
	    		'Give bureaucrat',
		    	'pt-givebureaucratlink',
			 	'Add bureaucrat rights to this user'
			);
		
			$(link).click(function() {
				bureaucratPromote(username);
			});
    	}
    }
    catch ( e ) {
        console.log( "Content request error: " + e.message );
        console.log( "Content request response: " + JSON.stringify( data ) );
    }
	} ).fail( function () {
    	console.log( "While getting the userlist, there was an AJAX error." );
	} );
}

function bureaucratPromote(username) {
	$.getJSON(
	//Get user's group membership again
    mw.util.wikiScript('api'),
    {
        format: 'json',
        action: 'query',
        list: 'users',
        usprop: 'groups',
        ususers: username
    }
	).done( function ( data ) {
    try {
    	if(data.query.users[0].groups.indexOf('bureaucrat') >= 0) {/* Do nothing */}
    	else {
    		check = confirm("Do you want to give " + username + " bureaucrat rights on this wiki?");
			if (check) {
				bureaucratRights(username);
				bureaucratAlert(username);
				alert(username + " is now a bureaucrat");
			}
    	}
    }
    catch ( e ) {
        console.log( "Content request error: " + e.message );
        console.log( "Content request response: " + JSON.stringify( data ) );
    }
	} ).fail( function () {
    	console.log( "While getting the userlist, there was an AJAX error." );
	} );
}

function bureaucratRights(username) {
$.getJSON(
	//Get userrights token
    mw.util.wikiScript('api'),
    {
        format: 'json',
        action: 'query',
        meta: 'tokens',
        type: 'userrights'
    }
).done( function ( data ) {
    try {
    	var rightsToken = data.query.tokens.userrightstoken;
    	//Strip rights
    	$.ajax( {
	        url: mw.util.wikiScript( 'api' ),
    	    type: 'POST',
        	dataType: 'json',
        	data: {
	            format: 'json',
	            action: 'userrights',
	            user: username,
	            add: 'bureaucrat',
	            reason: '+bureaucrat',
	            token: rightsToken,
	        }
	    } ).done(console.log( "Added rights to: " + username )
	    ).fail( function ( e, data ){
	    	console.log( e.message );
	    	console.log( JSON.stringify( data ) );
	    });
    }
    catch ( e ) {
        console.log( "Content request error: " + e.message );
        console.log( "Content request response: " + JSON.stringify( data ) );
    }
} ).fail( function () {
    console.log( "While getting the userlist, there was an AJAX error." );
} );
}

function bureaucratAlert(username) {
	//If page already exists
	$.ajax( {
	    url: mw.util.wikiScript( 'api' ),
    	type: 'POST',
        dataType: 'json',
        data: {
	        format: 'json',
	        action: 'edit',
            title: 'User talk:' + username,
            summary: 'Rights notification',
            nocreate: 1,
            appendtext: '\n\n{{subst:' + 'Bureaucrat granted}}',
            token: mw.user.tokens.get( 'csrfToken' )
        }
    } ).done( function (data) {
    	//console.log(data);
    }).fail( function ( e, data ){
    	console.log( e.message );
    	console.log( JSON.stringify( data ) );
    });
    
    //If pages does not exist
    $.ajax( {
	    url: mw.util.wikiScript( 'api' ),
    	type: 'POST',
        dataType: 'json',
        data: {
	        format: 'json',
	        action: 'edit',
            title: 'User talk:' + username,
            summary: 'Rights notification',
            createonly: 1,
            text: '\n\n{{subst:' + 'Bureaucrat granted}}',
            token: mw.user.tokens.get( 'csrfToken' )
        }
    } ).done( function (data) {
    	//console.log(data);
    }).fail( function ( e, data ){
    	console.log( e.message );
    	console.log( JSON.stringify( data ) );
    });
}