User:Void/userRights.js

// // Script for changing userrights on meta, or other wikis // TODO: target user must be steward to recieve CU, OS, or Steward if( mw.config.get('wgGlobalGroups').includes('steward') ) { mw.loader.using(['mediawiki.util', 'oojs-ui']).then( function {	var link = mw.util.addPortletLink( 'p-personal', '#',		'UserRights', 'pt-userrights', 'Edit local or interwiki user rights', null, '#pt-preferences' );	$(link).click(function{ //Make interface $('#bodyContent').replaceWith(' '); var userBox = new OO.ui.TextInputWidget({ value: mw.config.get('wgUserName'), id: 'ur-username' }); $('#ur-interface').append('Enter a username:'); $('#ur-interface').append(userBox.$element.css('margin', '1em 0')); var wikiSelect = new OO.ui.ComboBoxInputWidget({			value: mw.config.get('wgDBname'),			options: [				{ data: 'metawiki' },				{ data: 'testwiki' },				{ data: 'loginwiki' },			],			id: 'ur-wiki',		}); $('#ur-interface').append('Select which wiki you want to modify rights on:'); $('#ur-interface').append(wikiSelect.$element.css('margin', '1em 0')); var stdexpiry = {value: 'infinity', options: [{ data: '1 hour' }, { data: '1 week' }, { data: 'infinity' },],}; var rightsField = new OO.ui.FieldsetLayout({}); var sysopcb = new OO.ui.CheckboxInputWidget({ id: 'ur-sysop' }); var sysopexp = new OO.ui.ComboBoxInputWidget(stdexpiry); var cratcb = new OO.ui.CheckboxInputWidget({ id: 'ur-bureaucrat' }); var cratexp = new OO.ui.ComboBoxInputWidget(stdexpiry); var oscb = new OO.ui.CheckboxInputWidget({ id: 'ur-oversight' }); var osexp = new OO.ui.ComboBoxInputWidget(stdexpiry); var cucb = new OO.ui.CheckboxInputWidget({ id: 'ur-checkuser' }); var cuexp = new OO.ui.ComboBoxInputWidget(stdexpiry); var stewardcb = new OO.ui.CheckboxInputWidget({ id: 'ur-steward' }); var stewardexp = new OO.ui.ComboBoxInputWidget(stdexpiry); rightsField.addItems([			new OO.ui.FieldLayout( sysopcb, { label: 'Administrator; expiry:', align: 'inline' }, sysopexp ),			new OO.ui.FieldLayout( cratcb, { label: 'Bureaucrat; expiry:', align: 'inline' }, cratexp ),			new OO.ui.FieldLayout( oscb, { label: 'Oversight; expiry:', align: 'inline' }, osexp ),			new OO.ui.FieldLayout( cucb, { label: 'CheckUser; expiry:', align: 'inline' }, cuexp ),			new OO.ui.FieldLayout( stewardcb, { label: 'Steward; expiry:', align: 'inline' }, stewardexp )		]); $('#ur-interface').append(rightsField.$element.css('margin', '1em 0')); var summaryInput = new OO.ui.ComboBoxInputWidget({			options: [				{ data: 'Redacting information' },				{ data: 'Investigating possible abuse' },				{ data: 'Revoking due to abuse' },				{ data: 'Assigning local permissions' },			],			id: 'ur-summary'		}); $('#ur-interface').append('Enter a reason for changing usergroups:'); $('#ur-interface').append(summaryInput.$element.css('margin', '1em 0')); var addButton = new OO.ui.ButtonWidget({ label: 'Add groups', flags: ['progressive', 'primary'] }); var removeButton = new OO.ui.ButtonWidget({ label: 'Remove groups', flags: 'destructive'}); $('#ur-interface').append(addButton.$element.click(function{ submit(true); })); $('#ur-interface').append(removeButton.$element.click(function{ submit(false); })); function submit(adding) { link.remove; addButton.setDisabled(true); removeButton.setDisabled(true); var newlink = mw.util.addPortletLink(				'p-personal',				'#',				'UserRights',				'pt-userrights',				'Edit local or interwiki user rights',				null,				'#pt-preferences'			); $(newlink).click(function{ addButton.setDisabled(false); removeButton.setDisabled(false); newlink.remove; }); $.getJSON(				'https://meta.miraheze.org/w/api.php',				{					format: 'json',					action: 'query',					meta: 'tokens',					type: 'userrights'				}			).success(function(data){ modify(adding, data.query.tokens.userrightstoken); }) .fail(function{ alert('Failed to fetch userrights token'); }); }		function modify(adding, token) { var username = userBox.value; var wiki = wikiSelect.value; var sysop = sysopcb.isSelected; var crat = cratcb.isSelected; var os = oscb.isSelected; var cu = cucb.isSelected; var steward = stewardcb.isSelected; var summary = summaryInput.value; if( wiki.lastIndexOf('wiki') !== wiki.length - 4 ) wiki += 'wiki'; if( !adding && wiki === 'metawiki' && steward ) return alert('Please remove steward manually.'); // Prevent stupid mistakes var selectedGroups = ''; var expiry = ''; if(sysop) { selectedGroups += 'sysop'; expiry += (sysopexp.value.length === 0 ? 'infinity' : sysopexp.value); }			if(crat) { selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'bureaucrat'; expiry += (expiry.length === 0 ? '' : '|') + (cratexp.value.length === 0 ? 'infinity' : cratexp.value); }			if(os) { selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'oversight'; expiry += (expiry.length === 0 ? '' : '|') + (osexp.value.length === 0 ? 'infinity' : osexp.value); }			if(cu) { selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'checkuser'; expiry += (expiry.length === 0 ? '' : '|') + (cuexp.value.length === 0 ? 'infinity' : cuexp.value); }			if(steward) { selectedGroups += (selectedGroups.length === 0 ? '' : '|') + 'steward'; expiry += (expiry.length === 0 ? '' : '|') + (stewardexp.value.length === 0 ? 'infinity' : stewardexp.value); }			if( wiki !== 'metawiki' ) username += '@' + wiki; var query = {}; if(adding) { query = { format: 'json', action: 'userrights', user: username, add: selectedGroups, expiry: expiry, reason: summary, token: token };			} else { query = { format: 'json', action: 'userrights', user: username, remove: selectedGroups, reason: summary, token: token };			}			$.ajax({				url: 'https://meta.miraheze.org/w/api.php',				type: 'POST',				dataType: 'json',				data: query			}).success(function{ alert((adding ? 'Added ' : 'Removed ') + selectedGroups.replace(/\|/g, ', ') + (adding ? ' to ' : ' from ') + username); }) .fail(function{ alert('Could not modify groups of ' + username); }); }	}); }); } //