-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserRightsManager.js
More file actions
300 lines (276 loc) · 10 KB
/
Copy pathuserRightsManager.js
File metadata and controls
300 lines (276 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//Taken from https://en.wikipedia.org/w/index.php?title=User:MusikAnimal/userRightsManager.js&oldid=831137097
//Add a few custom templates [[User:MusikAnimal/userRightsManager.js]]
// <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',
'Event coordinator': 'eventcoordinator',
'Extended confirmed': 'extendedconfirmed',
'File mover': 'filemover',
'Mass message sender': 'massmessage-sender',
'New page reviewer': 'patroller',
'Page mover': 'extendedmover',
'Pending changes reviewer': 'reviewer',
'Rollback': 'rollbacker',
'Template editor': 'templateeditor',
'Temporary account IP viewer': 'temporary-account-viewer'
};
var templates = {
'Account creator': 'Account creator granted',
'Autopatrolled': 'Autopatrolledgiven',
'AutoWikiBrowser': '',
'Confirmed': 'User:Amorymeltzer/perm/Confirmed granted',
'Event coordinator': 'Event coordinator granted',
'Extended confirmed': 'User:Amorymeltzer/perm/Extended confirmed granted',
'File mover': 'Filemovergiven',
'Mass message sender': 'Mass message sender granted',
'New page reviewer': 'New Page Reviewer granted',
'Page mover': 'Page mover granted',
'Pending changes reviewer': 'Pending changes reviewer granted',
'Rollback': 'Rollback granted 3',
'Template editor': 'Template editor granted',
'Temporary account IP viewer': 'Temporary account IP viewer granted'
};
var api,
permission = mw.config.get('wgTitle').split('/').slice(-1)[0],
revisionId = mw.config.get('wgRevisionId'),
tagLine = ' (using [[User:MusikAnimal/userRightsManager|userRightsManager]])',
permaLink, userName, dialog;
mw.loader.using(['oojs-ui', 'mediawiki.api', 'mediawiki.widgets.DateInputWidget'], function() {
api = new mw.Api();
$('.perm-assign-permissions a').on('click', function(e) {
if (permission === 'AutoWikiBrowser') return true;
e.preventDefault();
userName = $(this).parents('.plainlinks').find('a').eq(0).text();
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 = '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 208;
};
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]]'
});
this.expiryInput = new mw.widgets.DateInputWidget({
$overlay: $('.oo-ui-window')
});
this.closingRemarksInput = new OO.ui.TextInputWidget({
value: '{{done}} ~~~~'
});
this.watchTalkPageCheckbox = new OO.ui.CheckboxInputWidget({
selected: false
});
var formElements = [
new OO.ui.FieldLayout(this.rightsChangeSummaryInput, {
label: 'Summary'
}),
new OO.ui.FieldLayout(this.expiryInput, {
label: 'Expiry (optional)'
}),
new OO.ui.FieldLayout(this.closingRemarksInput, {
label: 'Closing remarks'
})
];
if (!!templates[permission]) {
formElements.push(
new OO.ui.FieldLayout(this.watchTalkPageCheckbox, {
label: 'Watch user talk page'
})
);
}
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.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 );
$( '.mw-widget-dateInputWidget' ).css( 'width', '100%' );
};
Dialog.prototype.onSubmit = function() {
var self = this, promiseCount = !!templates[permission] ? 3 : 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] );
if (!!templates[permission]) {
self.issueTemplateProgressField.setLabel( 'Issuing template...' );
self.submitFieldset.addItems( [self.issueTemplateProgressField] );
}
addPromise(
self.markAsDoneProgressField,
markAsDone('\n:' + this.closingRemarksInput.getValue())
).then(function(data) {
addPromise(
self.changeRightsProgressField,
assignPermission(
this.rightsChangeSummaryInput.getValue(),
data.edit.newrevid,
this.expiryInput.getValue()
)
).then(function() {
// silently add user to MMS list
if (permission === 'New page reviewer') {
addToMMSList();
} else if (permission === 'Autopatrolled') {
updateWhiteList();
}
if (!!templates[permission]) {
addPromise(
self.issueTemplateProgressField,
issueTemplate(this.watchTalkPageCheckbox.isSelected(), this.expiryInput.getValue())
);
}
}.bind(this));
}.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(summary, revId, expiry) {
permaLink = '[[Special:PermaLink/' + revId + '#User:' + userName + '|permalink]]';
return api.postWithToken( 'userrights', {
action: 'userrights',
format: 'json',
user: userName.replace(/ /g, '_'),
add: permissionNames[permission],
reason: '+' + permissionNames[permission] + '; ' + summary + '; ' + permaLink + tagLine,
expiry: expiry === '' ? 'infinity' : expiry
});
}
function markAsDone(closingRemarks) {
var sectionNode = document.getElementById('User:' + userName.replace(/"/g, '.22').replace(/ /g, '_')),
sectionNumber = $(sectionNode).siblings('.mw-editsection').find("a:not('.mw-editsection-visualeditor')").prop('href').match(/section=(\d+)/)[1];
return api.postWithToken( 'edit', {
format: 'json',
action: 'edit',
title: mw.config.get('wgPageName'),
section: sectionNumber,
summary: '/* User:' + userName + ' */ done' + tagLine,
appendtext: closingRemarks
});
}
function issueTemplate(watch, expiry) {
var talkPage = 'User talk:' + userName.replace(/ /g, '_');
return api.postWithToken( 'edit', {
format: 'json',
action: 'edit',
title: talkPage,
section: 'new',
summary: permission + ' granted per ' + permaLink + tagLine,
text: '{{subst:' + templates[permission] + (expiry === '' ? '' : '|expiry=' + expiry) + '}}',
sectiontitle: permission + ' granted',
watchlist: watch ? 'watch' : 'unwatch'
});
}
function addToMMSList() {
api.postWithToken( 'csrf', {
format: 'json',
action: 'editmassmessagelist',
spamlist: 'Wikipedia:New pages patrol/Reviewers/Newsletter list',
add: 'User talk:' + userName
});
}
function updateWhiteList() {
api.edit( 'Wikipedia:New pages patrol/Redirect autopatrol list', function (revision) {
var newContent = revision.content.replace(
new RegExp('\\*\\s*{{\\s*user2\\s*\\|\\s*' + userName + '\\s*}}\\n'),
''
);
return {
text: newContent,
summary: 'Removing ' + userName + ' who is now autopatrolled' + tagLine,
minor: true
};
});
}
})();
// </nowiki>