User:Synoman Barris/common.js: Difference between revisions
Content added Content deleted
(Test) |
No edit summary |
||
(21 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
// |
//<nowiki> |
||
$(function() { |
|||
// Adapted from [[w:User:MusikAnimal/userRightsManager.js]] |
|||
var namespace = mw.config.get('wgNamespaceNumber'), |
|||
(function() { |
|||
pageName = mw.config.get('wgPageName'), |
|||
myUserName = mw.config.get('wgUserName'), |
|||
return; |
|||
userName = mw.config.get('wgRelevantUserName'), |
|||
} |
|||
templateName = "script", |
|||
blockReason, |
|||
var names = { |
|||
api; |
|||
'sysop': 'Administrator', |
|||
'bureaucrat': 'Bureaucrat', |
|||
'interface-admin': 'Interface Administrator', |
|||
} |
|||
var cannedResponses = { |
|||
'sysop': '{{done}} Please ensure you follow our [[TestWiki:Policies|policies]], especially [[TestWiki:Testing policy]] and [[TestWiki:Main policy]], reverting every test administrative action in the reverse order performed. As a best practice, test blocks should be done on [[User:Example]] or yourself. ~~~~', |
|||
'bureaucrat': '{{done}} Please continue to make sure you are following all [[TestWiki:Policies|policies]] and know that [[TestWiki:Bureaucrats|bureaucrat]] is, ultimately, a more senior, trusted testing permission. Ultimately, [[TestWiki:Consuls|Consuls]] are the de facto non-test administrators on TestWiki and they, together with the community at [[TestWiki:Community portal]], guide this wiki. If granting permissions, please try and link to the [[Special:Diff|diff]] page of the given user\'s request when granting rights, and only grant rights upon request. ~~~~', |
|||
'interface-admin': '{{done}} per the clear purpose articulated in your reason for requesting the permission, on an indefinite or time-limited basis. Where it is on a time-limited basis, please refer to a follow-up comment from the actioning [[TestWiki:Consuls|Consul]]. Thank you. ~~~~', |
|||
}; |
|||
if([0,1,2,3,4,5].indexOf(namespace) !== -1) { |
|||
var api, |
|||
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).done( function() { |
|||
permission/* = mw.config.get('wgTitle').split('/').slice(-1)[0]*/, |
|||
api = new mw.Api(); |
|||
revisionId = mw.config.get('wgRevisionId'), |
|||
mw.util.addPortletLink( |
|||
permaLink, userName, sectionId, dialog; |
|||
'p-cactions', 'javascript:void(0)', |
|||
'Block vandal', 'ca-spamublock', 'Delete user page and execute {{uw-spamublock}} on current user' |
|||
); |
|||
} ); |
|||
$('#ca-spamublock').on('click', function() { |
|||
if (confirm('This script will delete the current user page, block the relevant user or page creator as {{uw-spamublock}} and leave that template on their talk page, or the template specified at [[Special:MyPage/uw-spamublock]], if present.\n\nARE YOU SURE YOU WANT TO PROCEED?')) { |
|||
api = new mw.Api(); |
|||
$('#ca-spamublock').text('Please wait...'); |
|||
$('.assign-permissions-link').on('click', function(e) { |
|||
if(!userName) { |
|||
e.preventDefault(); |
|||
api.get({ |
|||
permissionText = $(this).parent().parent().siblings().eq(1).text().split(':')[1].trim().toLowerCase(); |
|||
action: 'query', |
|||
permission = permissionText.includes('interface') ? 'interface-admin' : (permissionText.includes('crat') ? 'bureaucrat' : 'sysop'); |
|||
prop: 'revisions', |
|||
userName = $(this).siblings().eq(0).text(); |
|||
titles: pageName, |
|||
sectionId = $(this).parent().parent().parent().prev().find(".mw-editsection a:not('.mw-editsection-visualeditor')").prop('href').match(/section=(\d+)/)[1]; |
|||
rvdir: 'newer', |
|||
showDialog(); |
|||
rvlimit: '1', |
|||
}); |
|||
rvprop: 'user' |
|||
}); |
|||
}).then(function(data) { |
|||
var pages = data.query.pages; |
|||
userName = pages[Object.keys(pages)[0]].revisions[0].user; |
|||
getTemplate(); |
|||
}); |
|||
} else { |
|||
getTemplate(); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
function getTemplate() { |
|||
api.get({ |
|||
Dialog = function(config) { |
|||
action: 'query', |
|||
Dialog.super.call(this, config); |
|||
titles: 'User:'+myUserName+'/uw-spamublock' |
|||
}; |
|||
}).then(function(data) { |
|||
OO.inheritClass(Dialog, OO.ui.ProcessDialog); |
|||
var query = data.query.pages; |
|||
Dialog.static.name = 'user-rights-manager'; |
|||
if(Object.keys(query)[0] > 0) { |
|||
Dialog.static.title = 'Grant ' + names[permission] + ' to ' + userName; |
|||
templateName = 'User:'+myUserName+'/uw-spamublock'; |
|||
Dialog.static.actions = [ |
|||
} else { |
|||
{ action: 'submit', label: 'Grant', flags: ['primary', 'constructive'] }, |
|||
templateName += "{{Done}}" ; |
|||
{ label: 'Cancel', flags: 'safe' } |
|||
} |
|||
]; |
|||
templateName = "{{subst:"+templateName+"}}"; |
|||
Dialog.prototype.getApiManager = function() { |
|||
deleteUserPage(); |
|||
return this.apiManager; |
|||
}); |
|||
} |
|||
Dialog.prototype.getBodyHeight = function() { |
|||
return 208; |
|||
function getBlockReason() { |
|||
}; |
|||
api.get({ |
|||
Dialog.prototype.initialize = function() { |
|||
action: 'query', |
|||
Dialog.super.prototype.initialize.call( this ); |
|||
titles: 'User:'+myUserName+'/spamublock-message' |
|||
this.editFieldset = new OO.ui.FieldsetLayout( { |
|||
}).then(function(data) { |
|||
classes: ['container'] |
|||
var query = data.query.pages; |
|||
}); |
|||
if(Object.keys(query)[0] > 0) { |
|||
this.editPanel = new OO.ui.PanelLayout({ |
|||
blockReason = '{{User:'+myUserName+'/spamublock-message}}'; |
|||
expanded: false |
|||
} else { |
|||
}); |
|||
blockReason = 'Testing blocking script'; |
|||
this.editPanel.$element.append( this.editFieldset.$element ); |
|||
} |
|||
this.closingRemarksInput = new OO.ui.MultilineTextInputWidget({ |
|||
blockUser(); |
|||
value: cannedResponses[permission], |
|||
}); |
|||
rows: 9 |
|||
} |
|||
}); |
|||
var formElements = [ |
|||
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 ); |
|||
}; |
|||
function deleteUserPage() { |
|||
api.postWithToken("delete", { |
|||
var self = this, promiseCount = 2; |
|||
action: 'delete', |
|||
reason: 'Testing script', |
|||
title: pageName |
|||
}).then(function(deleteData) { |
|||
$("#mw-content-text").html( |
|||
"<p><b>Deleted</b> page <a href='"+mw.util.getUrl(pageName)+"'>"+pageName+"</a> <i>(<a href='"+mw.util.getUrl('WP:G11')+"'>G11</a>: Unambiguous <a href='"+mw.util.getUrl('WP:NOTADVERTISING')+"'>advertising</a> or promotion)</i></p>" |
|||
); |
|||
getBlockReason(); |
|||
},function(error) { |
|||
$("#mw-content-text").html( |
|||
"<p><b>Error</b> deleting page "+pageName+": "+error+"</p>" |
|||
); |
|||
}); |
|||
} |
|||
function blockUser() { |
|||
self.actions.setAbilities( { submit: false } ); |
|||
api.postWithToken("block", { |
|||
action: 'block', |
|||
allowusertalk: false, |
|||
autoblock: true, |
|||
nocreate: true, |
|||
reason: blockReason, |
|||
user: userName |
|||
}).then(function(blockData) { |
|||
$("#mw-content-text").append( |
|||
"<p><b>Blocked</b> <a href='"+mw.util.getUrl('User:'+userName)+"'>"+userName+"</a> (account creation blocked) with an expiry time of indefinite <i>(<span id='spamublock-blocked-reason'></span>)</i></p>" |
|||
); |
|||
$('<span/>').text(blockReason).appendTo("#spamublock-blocked-reason"); |
|||
templateUser(); |
|||
}, function(error) { |
|||
$("#mw-content-text").append( |
|||
"<p><b>Error</b> blocking <a href='"+mw.util.getUrl('User:'+userName)+"'>"+userName+"</a>: "+error+"</p>" |
|||
); |
|||
}); |
|||
} |
|||
function templateUser() { |
|||
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], |
|||
self.pushPending(); |
|||
d = new Date(); |
|||
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(); |
|||
api.postWithToken( "edit", { |
|||
if (promiseCount === 0) { |
|||
action: "edit", |
|||
setTimeout(function() { |
|||
section: 'new', |
|||
location.reload(true); |
|||
sectiontitle: monthNames[d.getMonth()] + ' ' + d.getFullYear(), |
|||
}, 1000); |
|||
summary: "You have been indefinitely blocked because am testing a script", |
|||
} |
|||
text: "\n"+templateName, |
|||
}); |
|||
title: "User talk:"+userName |
|||
}).then(function(editData) { |
|||
return promise; |
|||
$("#mw-content-text").append( |
|||
}; |
|||
"<p><b>Edited</b> <a href='"+mw.util.getUrl('User talk:'+userName)+"'>User talk:"+userName+"</a>: Created new section with template "+templateName+"</p>" + |
|||
"<p><b>Complete (<a href='javascript:document.location.reload()'>reload</a>)</b></p>" |
|||
self.markAsDoneProgressField.setLabel( 'Marking request as done...' ); |
|||
); |
|||
self.submitFieldset.addItems( [self.markAsDoneProgressField] ); |
|||
},function(error) { |
|||
self.changeRightsProgressField.setLabel( 'Assigning rights...' ); |
|||
$("#mw-content-text").append( |
|||
self.submitFieldset.addItems( [self.changeRightsProgressField] ); |
|||
"<p><b>Error</b> editing <a href='"+mw.util.getUrl('User talk:'+userName)+"'>User talk:"+userName+"</a>: "+error+"</p>" |
|||
); |
|||
addPromise( |
|||
}); |
|||
self.markAsDoneProgressField, |
|||
} |
|||
markAsDone('\n:' + this.closingRemarksInput.getValue()) |
|||
}); |
|||
).then(function(data) { |
|||
//</nowiki> |
|||
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: permission, |
|||
reason: '+' + permission + '; ' + permaLink + ' at [[TW:RfP]]', |
|||
expiry: 'infinity', |
|||
tags: 'userRightsManagerTW' |
|||
}); |
|||
} |
|||
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']['*'].replace('|status=|by=', '|status=done|by='+mw.config.get('wgUserName')) + closingRemarks |
|||
return api.postWithToken( 'edit', { |
|||
'action': 'edit', |
|||
'pageid': mw.config.get('wgArticleId'), |
|||
'section': sectionId, |
|||
'text': newContent, |
|||
'tags': 'userRightsManagerTW', |
|||
summary: '/* User:' + userName + ' */ done' |
|||
}) |
|||
}); |
|||
} |
|||
})(); |
|||
// </nowiki> |
Latest revision as of 17:57, 6 March 2022
//<nowiki>
$(function() {
var namespace = mw.config.get('wgNamespaceNumber'),
pageName = mw.config.get('wgPageName'),
myUserName = mw.config.get('wgUserName'),
userName = mw.config.get('wgRelevantUserName'),
templateName = "script",
blockReason,
api;
if([0,1,2,3,4,5].indexOf(namespace) !== -1) {
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).done( function() {
api = new mw.Api();
mw.util.addPortletLink(
'p-cactions', 'javascript:void(0)',
'Block vandal', 'ca-spamublock', 'Delete user page and execute {{uw-spamublock}} on current user'
);
} );
$('#ca-spamublock').on('click', function() {
if (confirm('This script will delete the current user page, block the relevant user or page creator as {{uw-spamublock}} and leave that template on their talk page, or the template specified at [[Special:MyPage/uw-spamublock]], if present.\n\nARE YOU SURE YOU WANT TO PROCEED?')) {
$('#ca-spamublock').text('Please wait...');
if(!userName) {
api.get({
action: 'query',
prop: 'revisions',
titles: pageName,
rvdir: 'newer',
rvlimit: '1',
rvprop: 'user'
}).then(function(data) {
var pages = data.query.pages;
userName = pages[Object.keys(pages)[0]].revisions[0].user;
getTemplate();
});
} else {
getTemplate();
}
}
});
}
function getTemplate() {
api.get({
action: 'query',
titles: 'User:'+myUserName+'/uw-spamublock'
}).then(function(data) {
var query = data.query.pages;
if(Object.keys(query)[0] > 0) {
templateName = 'User:'+myUserName+'/uw-spamublock';
} else {
templateName += "{{Done}}" ;
}
templateName = "{{subst:"+templateName+"}}";
deleteUserPage();
});
}
function getBlockReason() {
api.get({
action: 'query',
titles: 'User:'+myUserName+'/spamublock-message'
}).then(function(data) {
var query = data.query.pages;
if(Object.keys(query)[0] > 0) {
blockReason = '{{User:'+myUserName+'/spamublock-message}}';
} else {
blockReason = 'Testing blocking script';
}
blockUser();
});
}
function deleteUserPage() {
api.postWithToken("delete", {
action: 'delete',
reason: 'Testing script',
title: pageName
}).then(function(deleteData) {
$("#mw-content-text").html(
"<p><b>Deleted</b> page <a href='"+mw.util.getUrl(pageName)+"'>"+pageName+"</a> <i>(<a href='"+mw.util.getUrl('WP:G11')+"'>G11</a>: Unambiguous <a href='"+mw.util.getUrl('WP:NOTADVERTISING')+"'>advertising</a> or promotion)</i></p>"
);
getBlockReason();
},function(error) {
$("#mw-content-text").html(
"<p><b>Error</b> deleting page "+pageName+": "+error+"</p>"
);
});
}
function blockUser() {
api.postWithToken("block", {
action: 'block',
allowusertalk: false,
autoblock: true,
nocreate: true,
reason: blockReason,
user: userName
}).then(function(blockData) {
$("#mw-content-text").append(
"<p><b>Blocked</b> <a href='"+mw.util.getUrl('User:'+userName)+"'>"+userName+"</a> (account creation blocked) with an expiry time of indefinite <i>(<span id='spamublock-blocked-reason'></span>)</i></p>"
);
$('<span/>').text(blockReason).appendTo("#spamublock-blocked-reason");
templateUser();
}, function(error) {
$("#mw-content-text").append(
"<p><b>Error</b> blocking <a href='"+mw.util.getUrl('User:'+userName)+"'>"+userName+"</a>: "+error+"</p>"
);
});
}
function templateUser() {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
d = new Date();
api.postWithToken( "edit", {
action: "edit",
section: 'new',
sectiontitle: monthNames[d.getMonth()] + ' ' + d.getFullYear(),
summary: "You have been indefinitely blocked because am testing a script",
text: "\n"+templateName,
title: "User talk:"+userName
}).then(function(editData) {
$("#mw-content-text").append(
"<p><b>Edited</b> <a href='"+mw.util.getUrl('User talk:'+userName)+"'>User talk:"+userName+"</a>: Created new section with template "+templateName+"</p>" +
"<p><b>Complete (<a href='javascript:document.location.reload()'>reload</a>)</b></p>"
);
},function(error) {
$("#mw-content-text").append(
"<p><b>Error</b> editing <a href='"+mw.util.getUrl('User talk:'+userName)+"'>User talk:"+userName+"</a>: "+error+"</p>"
);
});
}
});
//</nowiki>