MediaWiki:Gadget-UserRightsManager.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
fix right detection |
m Reverted edits by Justarandomamerican (talk) to last revision by Tsukushi Tag: Rollback |
||
(24 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
// <nowiki> |
// <nowiki> |
||
// Adapted from [[mhtest:MediaWiki:Gadget-userRightsManagerTW.js]], itself adapted from [[w:User:MusikAnimal/userRightsManager.js]] |
// Adapted from [[mhtest:MediaWiki:Gadget-userRightsManagerTW.js]], itself adapted from [[w:User:MusikAnimal/userRightsManager.js]], this code is therefore licensed under CC-BY-SA 4.0 |
||
(function() { |
(function() { |
||
if (mw.config.get('wgPageName') != 'Test_Wiki: |
if (mw.config.get('wgPageName') != 'Test_Wiki:Request_for_permissions' && !mw.config.get('wgPageName').includes('URMTW-TESTPAGE-RFP')) { |
||
return; |
return; |
||
} |
} |
||
Line 9: | Line 9: | ||
'sysop': 'Administrator', |
'sysop': 'Administrator', |
||
'bureaucrat': 'Bureaucrat', |
'bureaucrat': 'Bureaucrat', |
||
'interface-admin': 'Interface |
'interface-admin': 'Interface administrator', |
||
'non-stewardsuppress': 'Suppressor', |
|||
'abusefilter-admin': 'Abuse filter administrator', |
|||
} |
} |
||
var cannedResponses = { |
var cannedResponses = { |
||
'sysop': '{{ |
'sysop': '{{administrator granted}} ~~~~', |
||
'bureaucrat': '{{ |
'bureaucrat': '{{bureaucrat granted}} ~~~~', |
||
'interface-admin': '{{ |
'interface-admin': '{{interface administrator granted}} ~~~~', |
||
'non-stewardsuppress': '{{done}}. ~~~~', |
|||
'abusefilter-admin': '{{done}}. ~~~~', |
|||
}; |
}; |
||
var listKeys = { |
|||
'sysop': 'A', |
|||
'bureaucrat': 'B', |
|||
'interface-admin': 'F' |
|||
} |
|||
var api, |
var api, |
||
permission/* = mw.config.get('wgTitle').split('/').slice(-1)[0]*/, |
permission, perms/* = mw.config.get('wgTitle').split('/').slice(-1)[0]*/, |
||
revisionId = mw.config.get('wgRevisionId'), |
revisionId = mw.config.get('wgRevisionId'), |
||
permaLink, userName, sectionId, dialog; |
permaLink, userName, sectionId, dialog; |
||
Line 47: | Line 45: | ||
OO.inheritClass(Dialog, OO.ui.ProcessDialog); |
OO.inheritClass(Dialog, OO.ui.ProcessDialog); |
||
Dialog.static.name = 'user-rights-manager'; |
Dialog.static.name = 'user-rights-manager'; |
||
Dialog.static.title = ' |
Dialog.static.title = 'Granting rights'; |
||
Dialog.static.actions = [ |
Dialog.static.actions = [ |
||
{ action: 'submit', label: 'Grant', flags: ['primary', ' |
{ action: 'submit', label: 'Grant', flags: ['primary', 'progressive'] }, |
||
{ label: 'Cancel', flags: 'safe' } |
{ label: 'Cancel', flags: 'safe' } |
||
]; |
]; |
||
Line 56: | Line 54: | ||
}; |
}; |
||
Dialog.prototype.getBodyHeight = function() { |
Dialog.prototype.getBodyHeight = function() { |
||
return |
return 393; |
||
}; |
}; |
||
Dialog.prototype.initialize = function() { |
Dialog.prototype.initialize = function() { |
||
Line 67: | Line 65: | ||
}); |
}); |
||
this.editPanel.$element.append( this.editFieldset.$element ); |
this.editPanel.$element.append( this.editFieldset.$element ); |
||
this.groupsSysopInput = new OO.ui.CheckboxInputWidget({ |
|||
selected: permissionText.includes('sysop') || (permissionText.includes('admin') && (!permissionText.includes('interface') || permissionText.split('admin').length > 2)) || (!permissionText.includes('interface') && !permissionText.includes('crat')), |
|||
⚫ | |||
this.groupsBureaucratInput = new OO.ui.CheckboxInputWidget({ |
|||
selected: permissionText.includes('crat') |
|||
⚫ | |||
this.groupsInterfaceAdminInput = new OO.ui.CheckboxInputWidget({ |
|||
selected: permissionText.includes('interface') && mw.config.get('wgUserGroups').includes('steward'), |
|||
disabled: !mw.config.get('wgUserGroups').includes('steward') |
|||
⚫ | |||
this.groupsNonStewardSuppressInput = new OO.ui.CheckboxInputWidget({ |
|||
selected: permissionText.includes('suppress') && mw.config.get('wgUserGroups').includes('steward'), |
|||
disabled: !mw.config.get('wgUserGroups').includes('steward') |
|||
}); |
|||
this.groupsAbuseFilterInput = new OO.ui.CheckboxInputWidget({ |
|||
selected: permissionText.includes('abusefilter-admin') && mw.config.get('wgUserGroups').includes('steward'), |
|||
disabled: !mw.config.get('wgUserGroups').includes('steward') |
|||
}); |
|||
this.userNameInput = new OO.ui.TextInputWidget({ |
|||
value: userName.replace(/_/g, ' ') |
|||
}); |
|||
this.closingRemarksInput = new OO.ui.MultilineTextInputWidget({ |
this.closingRemarksInput = new OO.ui.MultilineTextInputWidget({ |
||
value: cannedResponses[permission], |
value: cannedResponses[permission], |
||
Line 72: | Line 91: | ||
}); |
}); |
||
var formElements = [ |
var formElements = [ |
||
new OO.ui.FieldLayout(new OO.ui.Widget({ |
|||
content: [ |
|||
new OO.ui.FieldsetLayout({ |
|||
content: [ |
|||
new OO.ui.FieldLayout(this.groupsSysopInput, {label: 'Administrator', align: 'inline'}), |
|||
new OO.ui.FieldLayout(this.groupsBureaucratInput, {label: 'Bureaucrat', align: 'inline'}), |
|||
new OO.ui.FieldLayout(this.groupsInterfaceAdminInput, {label: 'Interface administrator', align: 'inline'}), |
|||
new OO.ui.FieldLayout(this.groupsNonStewardSuppressInput, {label: 'Suppressor', align: 'inline'}), |
|||
new OO.ui.FieldLayout(this.groupsAbuseFilterInput, {label: 'Abuse filter administrator', align: 'inline'}), |
|||
] |
|||
}) |
|||
] |
|||
}), { |
|||
label: 'Rights', |
|||
}), |
|||
new OO.ui.FieldLayout(this.userNameInput, { |
|||
label: 'Target user', |
|||
}), |
|||
new OO.ui.FieldLayout(this.closingRemarksInput, { |
new OO.ui.FieldLayout(this.closingRemarksInput, { |
||
label: 'Closing remarks', |
label: 'Closing remarks', |
||
Line 87: | Line 124: | ||
this.changeRightsProgressLabel = new OO.ui.LabelWidget(); |
this.changeRightsProgressLabel = new OO.ui.LabelWidget(); |
||
this.changeRightsProgressField = new OO.ui.FieldLayout( this.changeRightsProgressLabel ); |
this.changeRightsProgressField = new OO.ui.FieldLayout( this.changeRightsProgressLabel ); |
||
this.updateJsonProgressLabel = new OO.ui.LabelWidget(); |
|||
this.updateJsonProgressField = new OO.ui.FieldLayout( this.updateJsonProgressLabel ); |
|||
this.markAsDoneProgressLabel = new OO.ui.LabelWidget(); |
this.markAsDoneProgressLabel = new OO.ui.LabelWidget(); |
||
this.markAsDoneProgressField = new OO.ui.FieldLayout( this.markAsDoneProgressLabel ); |
this.markAsDoneProgressField = new OO.ui.FieldLayout( this.markAsDoneProgressLabel ); |
||
Line 99: | Line 134: | ||
Dialog.prototype.onSubmit = function() { |
Dialog.prototype.onSubmit = function() { |
||
userName = this.userNameInput.getValue(); |
|||
perms = []; |
|||
if(this.groupsSysopInput.isSelected()) perms.push('sysop'); |
|||
if(this.groupsBureaucratInput.isSelected()) perms.push('bureaucrat'); |
|||
if(this.groupsInterfaceAdminInput.isSelected()) perms.push('interface-admin'); |
|||
if(this.groupsNonStewardSuppressInput.isSelected()) perms.push('non-stewardsuppress'); |
|||
if(this.groupsNonStewardSuppressInput.isSelected()) perms.push('abusefilter-admin'); |
|||
var self = this, promiseCount = 2; |
|||
self.actions.setAbilities( { submit: false } ); |
self.actions.setAbilities( { submit: false } ); |
||
Line 138: | Line 181: | ||
self.markAsDoneProgressField.setLabel( 'Marking request as done...' ); |
self.markAsDoneProgressField.setLabel( 'Marking request as done...' ); |
||
self.submitFieldset.addItems( [self.markAsDoneProgressField] ); |
self.submitFieldset.addItems( [self.markAsDoneProgressField] ); |
||
self.updateJsonProgressField.setLabel( 'Updating markadmins.json' ); |
|||
self.submitFieldset.addItems( [self.updateJsonProgressField] ); |
|||
self.changeRightsProgressField.setLabel( 'Assigning rights...' ); |
self.changeRightsProgressField.setLabel( 'Assigning rights...' ); |
||
self.submitFieldset.addItems( [self.changeRightsProgressField] ); |
self.submitFieldset.addItems( [self.changeRightsProgressField] ); |
||
Line 147: | Line 188: | ||
markAsDone('\n:' + this.closingRemarksInput.getValue()) |
markAsDone('\n:' + this.closingRemarksInput.getValue()) |
||
).then(function(data) { |
).then(function(data) { |
||
addPromise( |
|||
self.updateJsonProgressField, |
|||
updateJson(data.edit.newrevid) |
|||
⚫ | |||
addPromise( |
addPromise( |
||
self.changeRightsProgressField, |
self.changeRightsProgressField, |
||
Line 186: | Line 223: | ||
format: 'json', |
format: 'json', |
||
user: userName.replace(/ /g, '_'), |
user: userName.replace(/ /g, '_'), |
||
add: |
add: perms.join('|'), |
||
reason: '+' + |
reason: '+' + perms.join(', +') + '; ' + permaLink + ' at [[TW:RfP]]', |
||
expiry: 'infinity', |
expiry: 'infinity', |
||
tags: ' |
tags: 'userRightsManager' |
||
⚫ | |||
} |
|||
function updateJson(revId) { |
|||
return api.get({ |
|||
'action': 'query', |
|||
'titles': 'MediaWiki:Gadget-markadmins.json', |
|||
'prop': 'revisions', |
|||
'rvslots': '*', |
|||
'rvlimit': 1, |
|||
'rvprop': 'content' |
|||
}).then(function(data){ |
|||
var newContent = JSON.parse(data['query']['pages'][Object.keys(data['query']['pages'])[0]]['revisions'][0]['slots']['main']['*']); |
|||
newContent['userSet'][listKeys[permission]].push(userName.replace(/_/g, ' ')) |
|||
newContent['userSet'][listKeys[permission]].sort() |
|||
newContent = JSON.stringify(newContent, null, 4) |
|||
return api.postWithToken( 'edit', { |
|||
'action': 'edit', |
|||
'title': 'MediaWiki:Gadget-markadmins.json', |
|||
'text': newContent, |
|||
'tags': 'userRightsManagerTW', |
|||
summary: 'Adding [[User:' + userName + '|' + userName.replace(/_/g, ' ') + ']] to ' + names[permission] + 's ' + '([[Special:Diff/' + revId + '|per request]])' |
|||
⚫ | |||
}); |
}); |
||
} |
} |
||
Line 232: | Line 246: | ||
'section': sectionId, |
'section': sectionId, |
||
'text': newContent, |
'text': newContent, |
||
'tags': ' |
'tags': 'userRightsManager', |
||
summary: '/* User:' + userName + ' */ done' |
summary: '/* User:' + userName + ' */ done' |
||
}) |
}) |
Latest revision as of 13:30, 15 September 2024
// <nowiki>
// Adapted from [[mhtest:MediaWiki:Gadget-userRightsManagerTW.js]], itself adapted from [[w:User:MusikAnimal/userRightsManager.js]], this code is therefore licensed under CC-BY-SA 4.0
(function() {
if (mw.config.get('wgPageName') != 'Test_Wiki:Request_for_permissions' && !mw.config.get('wgPageName').includes('URMTW-TESTPAGE-RFP')) {
return;
}
var names = {
'sysop': 'Administrator',
'bureaucrat': 'Bureaucrat',
'interface-admin': 'Interface administrator',
'non-stewardsuppress': 'Suppressor',
'abusefilter-admin': 'Abuse filter administrator',
}
var cannedResponses = {
'sysop': '{{administrator granted}} ~~~~',
'bureaucrat': '{{bureaucrat granted}} ~~~~',
'interface-admin': '{{interface administrator granted}} ~~~~',
'non-stewardsuppress': '{{done}}. ~~~~',
'abusefilter-admin': '{{done}}. ~~~~',
};
var api,
permission, perms/* = mw.config.get('wgTitle').split('/').slice(-1)[0]*/,
revisionId = mw.config.get('wgRevisionId'),
permaLink, userName, sectionId, dialog;
mw.loader.using(['oojs-ui', 'mediawiki.api'], function() {
api = new mw.Api();
$('.assign-permissions-link').on('click', function(e) {
e.preventDefault();
permissionText = $(this).parent().parent().next().text().split(':')[1].trim().toLowerCase();
permission = permissionText.includes('interface') ? 'interface-admin' : (permissionText.includes('crat') ? 'bureaucrat' : 'sysop');
userName = $(this).siblings().eq(0).text();
sectionId = $(this).parent().parent().prev().find(".mw-editsection a:not('.mw-editsection-visualeditor')").prop('href').match(/section=(\d+)/)[1];
showDialog();
});
});
function showDialog() {
Dialog = function(config) {
Dialog.super.call(this, config);
};
OO.inheritClass(Dialog, OO.ui.ProcessDialog);
Dialog.static.name = 'user-rights-manager';
Dialog.static.title = 'Granting rights';
Dialog.static.actions = [
{ action: 'submit', label: 'Grant', flags: ['primary', 'progressive'] },
{ label: 'Cancel', flags: 'safe' }
];
Dialog.prototype.getApiManager = function() {
return this.apiManager;
};
Dialog.prototype.getBodyHeight = function() {
return 393;
};
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.groupsSysopInput = new OO.ui.CheckboxInputWidget({
selected: permissionText.includes('sysop') || (permissionText.includes('admin') && (!permissionText.includes('interface') || permissionText.split('admin').length > 2)) || (!permissionText.includes('interface') && !permissionText.includes('crat')),
});
this.groupsBureaucratInput = new OO.ui.CheckboxInputWidget({
selected: permissionText.includes('crat')
});
this.groupsInterfaceAdminInput = new OO.ui.CheckboxInputWidget({
selected: permissionText.includes('interface') && mw.config.get('wgUserGroups').includes('steward'),
disabled: !mw.config.get('wgUserGroups').includes('steward')
});
this.groupsNonStewardSuppressInput = new OO.ui.CheckboxInputWidget({
selected: permissionText.includes('suppress') && mw.config.get('wgUserGroups').includes('steward'),
disabled: !mw.config.get('wgUserGroups').includes('steward')
});
this.groupsAbuseFilterInput = new OO.ui.CheckboxInputWidget({
selected: permissionText.includes('abusefilter-admin') && mw.config.get('wgUserGroups').includes('steward'),
disabled: !mw.config.get('wgUserGroups').includes('steward')
});
this.userNameInput = new OO.ui.TextInputWidget({
value: userName.replace(/_/g, ' ')
});
this.closingRemarksInput = new OO.ui.MultilineTextInputWidget({
value: cannedResponses[permission],
rows: 9
});
var formElements = [
new OO.ui.FieldLayout(new OO.ui.Widget({
content: [
new OO.ui.FieldsetLayout({
content: [
new OO.ui.FieldLayout(this.groupsSysopInput, {label: 'Administrator', align: 'inline'}),
new OO.ui.FieldLayout(this.groupsBureaucratInput, {label: 'Bureaucrat', align: 'inline'}),
new OO.ui.FieldLayout(this.groupsInterfaceAdminInput, {label: 'Interface administrator', align: 'inline'}),
new OO.ui.FieldLayout(this.groupsNonStewardSuppressInput, {label: 'Suppressor', align: 'inline'}),
new OO.ui.FieldLayout(this.groupsAbuseFilterInput, {label: 'Abuse filter administrator', align: 'inline'}),
]
})
]
}), {
label: 'Rights',
}),
new OO.ui.FieldLayout(this.userNameInput, {
label: 'Target user',
}),
new OO.ui.FieldLayout(this.closingRemarksInput, {
label: 'Closing remarks',
})
];
this.editFieldset.addItems(formElements);
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.stackLayout = new OO.ui.StackLayout( {
items: [this.editPanel, this.submitPanel],
padded: true
} );
this.$body.append( this.stackLayout.$element );
};
Dialog.prototype.onSubmit = function() {
userName = this.userNameInput.getValue();
perms = [];
if(this.groupsSysopInput.isSelected()) perms.push('sysop');
if(this.groupsBureaucratInput.isSelected()) perms.push('bureaucrat');
if(this.groupsInterfaceAdminInput.isSelected()) perms.push('interface-admin');
if(this.groupsNonStewardSuppressInput.isSelected()) perms.push('non-stewardsuppress');
if(this.groupsNonStewardSuppressInput.isSelected()) perms.push('abusefilter-admin');
var self = this, promiseCount = 2;
self.actions.setAbilities( { submit: false } );
addPromise = function( field, promise ) {
self.pushPending();
promise.done(function() {
field.$field.append( $( '<span>' )
.text( 'Complete!' )
.prop('style', 'position:relative; top:0.5em; color: #009000; font-weight: bold')
);
}).fail(function(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( $( '<span>' )
.text('An unknown error occurred.')
.prop('style', 'position:relative; top:0.5em; color: #cc0000; font-weight: bold')
);
}
}).always( function() {
promiseCount--; // FIXME: maybe we could use a self.isPending() or something
self.popPending();
if (promiseCount === 0) {
setTimeout(function() {
location.reload(true);
}, 1000);
}
});
return promise;
};
self.markAsDoneProgressField.setLabel( 'Marking request as done...' );
self.submitFieldset.addItems( [self.markAsDoneProgressField] );
self.changeRightsProgressField.setLabel( 'Assigning rights...' );
self.submitFieldset.addItems( [self.changeRightsProgressField] );
addPromise(
self.markAsDoneProgressField,
markAsDone('\n:' + this.closingRemarksInput.getValue())
).then(function(data) {
addPromise(
self.changeRightsProgressField,
assignPermission(data.edit.newrevid)
);
}.bind(this));
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(revId) {
permaLink = '[[Special:Diff/' + revId + '|Requested]]';
return api.postWithToken( 'userrights', {
action: 'userrights',
format: 'json',
user: userName.replace(/ /g, '_'),
add: perms.join('|'),
reason: '+' + perms.join(', +') + '; ' + permaLink + ' at [[TW:RfP]]',
expiry: 'infinity',
tags: 'userRightsManager'
});
}
function markAsDone(closingRemarks) {
return api.get({
'action': 'query',
'pageids': mw.config.get('wgArticleId'),
'prop': 'revisions',
'rvslots': '*',
'rvlimit': 1,
'rvprop': 'content',
'rvsection': sectionId
}).then(function(data){
var newContent = data['query']['pages'][mw.config.get('wgArticleId')]['revisions'][0]['slots']['main']['*'] + closingRemarks
return api.postWithToken( 'edit', {
'action': 'edit',
'pageid': mw.config.get('wgArticleId'),
'section': sectionId,
'text': newContent,
'tags': 'userRightsManager',
summary: '/* User:' + userName + ' */ done'
})
});
}
})();
// </nowiki>