User:X/grantAdmin.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
+
mNo edit summary
Line 1: Line 1:
// <nowiki>
//Script to add sysop to a user on one click
// Script to add sysop to a user on one click.
//To do: add toggles for different skins
// To do: add toggles for different skins.
if( mw.config.get("wgRelevantUserName") ) {
if( mw.config.get("wgRelevantUserName") ) {
var username = mw.config.get("wgRelevantUserName");
var username = mw.config.get("wgRelevantUserName");
Line 22: Line 23:
'Grant Admin',
'Grant Admin',
'pt-grantadminlink',
'pt-grantadminlink',
'Grant admin privleges to this user'
'Grant administrator privileges to this user'
);
);
Line 54: Line 55:
if(data.query.users[0].groups.indexOf('sysop') >= 0) {/* Do nothing */}
if(data.query.users[0].groups.indexOf('sysop') >= 0) {/* Do nothing */}
else {
else {
check = confirm("Do you want to give " + username + " administrator privleges on this wiki?");
check = confirm("Do you want to give " + username + " administrator privileges on this wiki?");
if (check) {
if (check) {
grantAdmin(username);
grantAdmin(username);
alertUser(username);
alertUser(username);
alert(username + " is now an administrator");
alert(username + " is now an administrator.");
}
}
}
}
Line 94: Line 95:
user: username,
user: username,
add: 'sysop',
add: 'sysop',
reason: '+[[Test Wiki:Administrators|sysop]]; Requested',
reason: '+[[Special:MyLanguage/Test Wiki:Administrators|sysop]]; requested.',
token: rightsToken,
token: rightsToken,
}
}
Line 125: Line 126:
nocreate: 1,
nocreate: 1,
header: 'Administrator permissions granted',
header: 'Administrator permissions granted',
appendtext: '\n\n{{subst:' + 'Admin granted}}',
appendtext: '\n\n{{subst:' + 'admin granted}} ~~~~',
token: mw.user.tokens.get( 'csrfToken' )
token: mw.user.tokens.get( 'csrfToken' )
}
}
Line 147: Line 148:
createonly: 1,
createonly: 1,
header: 'Administrator permissions granted',
header: 'Administrator permissions granted',
text: '{{subst:' + 'Admin granted}}',
text: '{{subst:' + 'admin granted}} ~~~~',
token: mw.user.tokens.get( 'csrfToken' )
token: mw.user.tokens.get( 'csrfToken' )
}
}
Line 157: Line 158:
});
});
}
}
// </nowiki>

Revision as of 02:41, 11 May 2024

// <nowiki>
// Script to add sysop to 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('sysop') >= 0) {/* Do nothing */}
    	else {
			var link = mw.util.addPortletLink(
	    		'p-cactions',
	    		'#',
	    		'Grant Admin',
		    	'pt-grantadminlink',
			 	'Grant administrator privileges to this user'
			);
		
			$(link).click(function() {
				checkAndPromote(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 checkAndPromote(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('sysop') >= 0) {/* Do nothing */}
    	else {
    		check = confirm("Do you want to give " + username + " administrator privileges on this wiki?");
			if (check) {
				grantAdmin(username);
				alertUser(username);
				alert(username + " is now an administrator.");
			}
    	}
    }
    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 grantAdmin(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;
    	//Grant admin
    	$.ajax( {
	        url: mw.util.wikiScript( 'api' ),
    	    type: 'POST',
        	dataType: 'json',
        	data: {
	            format: 'json',
	            action: 'userrights',
	            user: username,
	            add: 'sysop',
	            reason: '+[[Special:MyLanguage/Test Wiki:Administrators|sysop]]; requested.',
	            token: rightsToken,
	        }
	    } ).done(console.log( "Granted sysop 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 alertUser(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: 'Administrator permissions granted',
            nocreate: 1,
            header: 'Administrator permissions granted',
            appendtext: '\n\n{{subst:' + 'admin 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: 'Administrator permissions granted',
            createonly: 1,
            header: 'Administrator permissions granted',
            text: '{{subst:' + 'admin 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 ) );
    });
}
// </nowiki>