User:MusikAnimal/userRightsManager.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:MusikAnimal/userRightsManager. |
// <nowiki>
// Some UI code adapted from [[User:Mr. Stradivarius/gadgets/Draftify.js]]
(function() {
if (!/Wikipedia:Requests for permissions\//.test(document.title)) {
return;
}
var permissionNames = {
'Account creator': 'accountcreator',
'Autopatrolled': 'autoreviewer',
'Confirmed': 'confirmed',
'File mover': 'filemover',
'Mass message sender': 'massmessage-sender',
'Pending changes reviewer': 'reviewer',
'Rollback': 'rollbacker',
'Template editor': 'templateeditor'
};
var templates = {
'Account creator': 'Account creator granted',
'Autopatrolled': 'Autopatrolledgiven',
'AutoWikiBrowser': '',
'Confirmed': '',
'File mover': 'Filemovergiven',
'Mass message sender': 'Mass message sender granted',
'Pending changes reviewer': 'Pending changes reviewer granted',
'Rollback': 'Rollbackgiven3',
'Template editor': 'Template editor granted'
};
var api = new mw.Api(),
permission = mw.config.get('wgTitle').split('/').slice(-1)[0],
revisionId = mw.config.get('wgRevisionId'),
tagLine = ' (using [[User:MusikAnimal/userRightsManager.js|userRightsManager]])',
permaLink, userName, dialog;
mw.loader.using(['oojs-ui'], function() {
$('.sysop-show a').on('click', function(e) {
if (permission === 'AutoWikiBrowser') return true;
e.preventDefault();
userName = $(this).parents('.plainlinks').find('a').eq(0).text();
permaLink = '[[Special:PermaLink/' + revisionId + '#User:' + userName + ']]';
showDialog();
});
});
function showDialog() {
Dialog = function(config) {
this.hasBeenSubmitted = false;
Dialog.super.call(this, config);
};
OO.inheritClass(Dialog, OO.ui.ProcessDialog);
Dialog.static.title = 'Grant ' + permission + ' to ' + userName;
Dialog.static.actions = [
{ action: 'submit', label: 'Submit', flags: ['primary', 'constructive'] },
{ label: 'Cancel', flags: 'safe' }
];
Dialog.prototype.getApiManager = function() {
return this.apiManager;
};
Dialog.prototype.getBodyHeight = function() {
return 120;
};
Dialog.prototype.initialize = function() {
Dialog.super.prototype.initialize.call( this );
this.editFieldset = new OO.ui.FieldsetLayout( {
classes: ['container']
});
this.editPanel = new OO.ui.PanelLayout({
expanded: false
});
this.editPanel.$element.append( this.editFieldset.$element );
this.rightsChangeSummaryInput = new OO.ui.TextInputWidget({
value: 'Requested at [[WP:PERM]]; ' + permaLink
});
this.closingRemarksInput = new OO.ui.TextInputWidget({
value: '{{done}} ~~~~'
});
this.editFieldset.addItems( [
new OO.ui.FieldLayout( this.rightsChangeSummaryInput, {
label: 'Summary'
} ),
new OO.ui.FieldLayout( this.closingRemarksInput, {
label: 'Closing remarks'
} )
] );
this.submitPanel = new OO.ui.PanelLayout( {
$: this.$,
expanded: false
} );
this.submitFieldset = new OO.ui.FieldsetLayout( {
classes: ['container']
} );
this.submitPanel.$element.append( this.submitFieldset.$element );
this.changeRightsProgressLabel = new OO.ui.LabelWidget();
this.changeRightsProgressField = new OO.ui.FieldLayout( this.changeRightsProgressLabel );
this.markAsDoneProgressLabel = new OO.ui.LabelWidget();
this.markAsDoneProgressField = new OO.ui.FieldLayout( this.markAsDoneProgressLabel );
this.issueTemplateProgressLabel = new OO.ui.LabelWidget();
this.issueTemplateProgressField = new OO.ui.FieldLayout( this.issueTemplateProgressLabel );
this.stackLayout = new OO.ui.StackLayout( {
items: [this.editPanel, this.submitPanel],
padded: true
} );
this.$body.append( this.stackLayout.$element );
};
Dialog.prototype.getReadyProcess = function( data ) {
data = data || {};
return Dialog.super.prototype.getReadyProcess.call( this, data ).next( function() {
if ( this.hasBeenSubmitted ) {
this.actions.setAbilities( { submit: false } );
}
}, this );
};
Dialog.prototype.onSubmit = function() {
var self = this;
self.hasBeenSubmitted = true; // FIXME: may not need this
self.actions.setAbilities( { submit: false } );
var promises = [
assignPermission(this.rightsChangeSummaryInput.getValue() + tagLine),
markAsDone('\n::' + this.closingRemarksInput.getValue()),
issueTemplate()
];
addField = function( field, label, promise ) {
if ( !promise ) return;
self.pushPending();
field.setLabel( label );
self.submitFieldset.addItems( [field] );
promise.done(function() {
field.$field.append( $( '<span>' )
.text( 'Complete!' )
.prop('style', 'position:relative; top:0.5em; color: #009000; font-weight: bold')
);
}).fail(function( id, obj ) {
if ( obj && obj.error && obj.error.info ) {
field.$field.append( $( '<span>' )
.text('Error: ' + obj.error.info)
.prop('style', 'position:relative; top:0.5em; color: #cc0000; font-weight: bold')
);
} else {
field.$field.append('An unknown error occurred.');
}
}).always( function() {
self.popPending();
});
};
addField(
self.changeRightsProgressField,
'Assigning rights...',
promises[0]
);
addField(
self.issueTemplateProgressField,
'Issuing template...',
promises[1]
);
addField(
self.markAsDoneProgressField,
'Marking request as done...',
promises[2]
);
$.when(promises).done( function() {
setTimeout(function() {
location.reload(true);
}, 2000);
});
$.when(promises).fail( function() {
self.popPending();
} );
self.stackLayout.setItem( self.submitPanel );
};
Dialog.prototype.getActionProcess = function( action ) {
return Dialog.super.prototype.getActionProcess.call( this, action ).next( function() {
if ( action === 'submit' ) {
return this.onSubmit();
} else {
return Dialog.super.prototype.getActionProcess.call( this, action );
}
}, this );
};
dialog = new Dialog({
size: 'medium'
});
var windowManager = new OO.ui.WindowManager();
$('body').append(windowManager.$element);
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);
}
function assignPermission(summary) {
return api.postWithToken( 'userrights', {
action: 'userrights',
format: 'json',
user: userName,
add: permissionNames[permission],
reason: summary
});
}
function markAsDone(closingRemarks) {
var sectionNode = document.getElementById('User:' + userName),
sectionNumber = $(sectionNode).siblings('.mw-editsection').find('a').prop('href').match(/section=(\d)/)[1];
return api.postWithToken( 'edit', {
format: 'json',
action: 'edit',
title: mw.config.get('wgPageName'),
section: sectionNumber,
summary: 'done' + tagLine,
appendtext: closingRemarks
} );
}
function issueTemplate(options) {
options = options || {};
return api.postWithToken( 'edit', {
format: 'json',
action: 'edit',
title: 'User talk:' + userName,
section: 'new',
summary: permission + ' granted per ' + permaLink + tagLine,
text: '{{subst:' + templates[permission] + '}}',
sectiontitle: permission + ' granted',
watchlist: options.watch ? 'watch' : 'unwatch'
} );
}
})();
// </nowiki>