User:Abelmoschus Esculentus/EFFPRH.js
//
//Largely based on User:Enterprisey/AFCFFU.js
var effp_advert = ' (EFFPRH)';
var effp_effpPageName = wgPageName.replace(/_/g, ' ');
var effp_effpSubmissions = new Array();
var effp_effpSections = new Array();
var effp_numTotal = 0;
var effp_AJAXnumber = 0;
var effp_Submissions = new Array();
function effp_editPage(title, newtext, summary, createonly, nopatrol) {
var edittoken = mw.user.tokens.get('editToken');
summary += effp_advert;
$("#effp_finished_wrapper").html('
');var func_id = effp_AJAXnumber;
effp_AJAXnumber++;
$('#effp_status').html($('#effp_status').html() + '
var request = {
'action': 'edit',
'title': title,
'text': newtext,
'summary': summary,
'token': edittoken
};
if (createonly) request.createonly = true;
var api = new mw.Api();
api.post(request)
.done(function ( data ) {
if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
$('#effp_edit' + jqEsc(title)).html('Saved ' + title + '');
} else {
$('#effp_edit' + jqEsc(title)).html('Edit failed on ' + title + '. Error info: ' + JSON.stringify(data));
window.console && console.error('Edit failed on %s (%s). Error info: %s', wgArticlePath.replace("$1", encodeURI(title)), title, JSON.stringify(data));
}
} )
.fail( function ( error ) {
if (createonly && error == "articleexists")
$('#effp_edit' + jqEsc(title)).html('Edit failed on ' + title + '. Error info: The article already exists!');
else
$('#effp_edit' + jqEsc(title)).html('Edit failed on ' + title + '. Error info: ' + error);
})
.always( function () {
$("#effp_AJAX_finished_" + func_id).css("display", '');
});
if (!nopatrol) {
if ($('.patrollink').length) {
var patrolhref = $('.patrollink a').attr('href');
var rcid = mw.util.getParamValue('rcid', patrolhref);
if (rcid) {
$('#effp_status').html($('#effp_status').html() + '
var patrolrequest = {
'action': 'patrol',
'format': 'json',
'token': mw.user.tokens.get('patrolToken'),
'rcid': rcid
};
api.post(patrolrequest)
.done(function ( data ) {
if ( data ) {
$('#effp_patrol' + jqEsc(title)).html('Marked ' + title + ' as patrolled');
} else {
$('#effp_patrol' + jqEsc(title)).html('Patrolling failed on ' + title + ' with an unknown error');
window.console && console.error('Patrolling failed on %s (%s) with an unknown error.', wgArticlePath.replace("$1", encodeURI(title)), title);
}
} )
.fail( function ( error ) {
$('#effp_patrol' + jqEsc(title)).html('Patrolling failed on ' + title + '. Error info: ' + error);
});
}
}
}
}
function effp_escapeHtmlChars(original) {
return original.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'");
}
function jqEsc(expression) {
return expression.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~ ]/g, '');
}
function effp_generateSelect(title, options, onchange) {
var text = '";
return text;
}
function effp_getPageText(title, show, redirectcheck, timestamp) {
if (show) $('#effp_status').html($('#effp_status').html() + '
var request = {
'action': 'query',
'prop': 'revisions',
'rvprop': 'content',
'format': 'json',
'indexpageids': true,
'titles' : title
};
if (redirectcheck) request.redirects = true;
if (timestamp) request.rvprop = 'content|timestamp';
var response = JSON.parse(
$.ajax({
url: mw.util.wikiScript('api'),
data: request,
async: false
})
.responseText
);
pageid = response['query']['pageids'][0];
if (pageid === "-1") {
if (show) $('#effp_get' +jqEsc(title)).html('The page ' + title + ' does not exist');
return '';
}
var newtext = response['query']['pages'][pageid]['revisions'][0]['*'];
if (redirectcheck && response['query']['redirects'] /* If &redirects if specified but there is no redirect, this stops us from getting an error */){
var oldusername = response['query']['redirects'][0]['from'];
var newusername = response['query']['redirects'][0]['to'];
if ((typeof(oldusername) !== 'undefined') && (typeof(newusername) !== 'undefined') && (oldusername != newusername)){
usertalkpage = newusername;
if (show) {
$('#effp_status').html($('#effp_status').html() + '
}
} else {
redirectcheck = false;
}
} else {
redirectcheck = false;
}
if (show && !redirectcheck) $('#effp_status').html($('#effp_status').html() + '
if (!timestamp) return newtext;
else return {'pagetext':newtext,'timestamp':response['query']['pages'][pageid]['revisions'][0]['timestamp']};
}
function effp_init() {
var pagetext = effp_getPageText(effp_effpPageName, false);
var section_re = /==[^=]*==/;
pagetext = pagetext.substring(pagetext.search(section_re));
section_re = /==[^=]*==/g;
var section_headers = pagetext.match(section_re);
for (var i = 0; i < section_headers.length; i++) {
var section_start = pagetext.indexOf(section_headers[i]);
var section_text = pagetext.substring(section_start);
if (i < section_headers.length - 1) {
var section_end = section_text.substring(section_headers[i].length).indexOf(section_headers[i + 1]) + section_headers[i].length;
section_text = section_text.substring(0, section_end);
}
effp_effpSections.push(section_text);
}
for (var i = 0; i < effp_effpSections.length; i++) {
var header = effp_effpSections[i].match(section_re)[0];
header = header.slice(2, (header.length - 2));
var submission = {
type: 'effp',
from: new Array(),
section: i,
blockeduser: '',
admin: '',
cmt: '',
filteruser: '',
user: '',
title: header,
action: 'none',
blockuser : false,
expiry : '',
blockreason: '',
blockacc: false,
blockemail: false,
blocktalk: false,
blockauto: false,
anononly: false,
watch: false,
comment: ''
};
effp_effpSubmissions.push(submission);
effp_numTotal++;
}
for (var k = 0; k < effp_effpSubmissions.length; k++) {
var text = '
- ';
- Response: ';
selectoptions = [{
label: 'None',
selected: true,
value: 'none'
}, {
label: 'Done (no change to filter)',
value: 'done'
}, {
label: 'Done (may need a change to filter)',
value: 'defm'
}, {
label: 'Not Done (filter working properly)',
value: 'notdone'
}, {
label: 'Not Done (may need a change to filter)',
value: 'ndefm'
}, {
label: 'Already Done',
value: 'alreadydone'
}, {
label: 'Decline (edits are vandalism)',
value: 'denied'
}, {
label: 'Checking',
value: 'checking'
}, {
label: 'User blocked',
value: 'blocked'
}, {
label: 'Request on article talk page',
value: 'talk|article'
}, {
label: 'Fixed filter',
value: 'fixed'
}, {
label: 'Question',
value: 'question'
}, {
label: 'Note',
value: 'note'
}, {
label: 'Private filter',
value: 'private'
}];
text += '
' + effp_generateSelect('effp_effp_action_'+k, selectoptions, 'effp_effp_onActionChange(' + k + ')') + ' ';
text += '
text += '
text += '';
text += '';
displayMessage_inline(text, 'effp-review-' + effp_effpSubmissions[k].section);
}
}
function effp_effp_performActions(sectionNumber) {
for (var i = 0; i < effp_effpSubmissions.length; i++) {
var action = $("#effp_effp_action_" + i).val();
effp_effpSubmissions[i].action = action;
if (action == 'none') continue;
if (action == 'blocked') {
effp_effpSubmissions[i].blockeduser = $.trim($("#effp_effp_blockeduser_" + i).val());
effp_effpSubmissions[i].admin = $.trim($("#effp_effp_admin_" + i).val());
effp_effpSubmissions[i].cmt = $.trim($("#effp_effp_cmt_" + i).val());
if (document.getElementById('effp_effp_blockuser_'+i).checked) {
effp_effpSubmissions[i].blockuser = true;
if ($('#effp_effp_blockreason_'+i).val() == 'custom') {
effp_effpSubmissions[i].blockreason = $.trim($('#effp_effp_customblockreason_'+i).val());
}
else effp_effpSubmissions[i].blockreason = $.trim($('#effp_effp_blockreason_'+i).val());
effp_effpSubmissions[i].expiry = $.trim($('#effp_effp_blockexpiry_'+i).val());
effp_effpSubmissions[i].blockacc = document.getElementById('effp_effp_blockacc_'+i).checked;
effp_effpSubmissions[i].blockemail = document.getElementById('effp_effp_blockemail_'+i).checked;
effp_effpSubmissions[i].blocktalk = document.getElementById('effp_effp_blocktalk_'+i).checked;
effp_effpSubmissions[i].anononly = document.getElementById('effp_effp_anononly_'+i).checked;
effp_effpSubmissions[i].watch = document.getElementById('effp_effp_watch_'+i).checked;
}
}
if (action == 'fixed') {
effp_effpSubmissions[i].filteruser = $.trim($("#effp_effp_filteruser_" + i).val());
effp_effpSubmissions[i].cmt = $.trim($("#effp_effp_cmt_" + i).val());
}
if (action == 'alreadydone') {
effp_effpSubmissions[i].user = $.trim($("#effp_effp_user_" + i).val());
effp_effpSubmissions[i].cmt = $.trim($("#effp_effp_cmt_" + i).val());
}
if (action != 'none' && action != 'blocked' && action != 'fixed' && action != 'alreadydone') {
effp_effpSubmissions[i].cmt = $.trim($("#effp_effp_cmt_" + i).val());
}
}
displayMessage_inline('
- Now processing...
$('#effp_finish').html('
');pagetext = effp_getPageText(effp_effpPageName, true);
var effp_total = 0;
for (var i = 0; i < effp_effpSubmissions.length; i++) {
var sub = effp_effpSubmissions[i];
if (pagetext.indexOf(effp_effpSections[sub.section]) == -1) {
$('#effp_status').html($('#effp_status').html() + '
continue;
}
var origtext = effp_effpSections[sub.section];
var text = effp_effpSections[sub.section];
var startindex = pagetext.indexOf(effp_effpSections[sub.section]);
var endindex = startindex + text.length;
if (text === origtext) {
var sub_m = effp_effpSubmissions[i];
if (sub_m.blockuser == true) {
var match = /\[\[(?:User[_ ]talk:|User:|Special:Contributions\/)([^\||\]\]]*)([^\]]*?)\]\]/i.exec(text);
if (match) {
var vandal = match[1];
var API = new mw.Api();
API.postWithToken("block", {
'action': 'block',
'expiry': sub_m.expiry,
'allowusertalk': !sub_m.blocktalk,
'noemail': sub_m.blockemail,
'anononly': sub_m.anononly,
'autoblock': sub_m.blockauto,
'nocreate': sub_m.blockacc,
'reason': sub_m.blockreason,
'watchuser': sub_m.watch,
'user': vandal
}).done(function(blockData) {
$('#effp_status').html($('#effp_status').html()+'
effp_effp_templateUser(sub_m.expiry, vandal);
}).fail(function(error) {
$("#effp_status").html(
$('#effp_status').html()+"
);
effp_effp_templateUser(sub_m.expiry, vandal);
});
}
}
if (sub_m.action == 'blocked') {
text += '\n*\{\{effp|blocked|'+sub_m.blockeduser+'|'+sub_m.admin+'\}\} '+sub_m.cmt+' \~\~\~\~\n';
effp_total++;
}
if (sub_m.action == 'fixed') {
text += '\n*\{\{effp|fixed|'+sub_m.filteruser+'\}\} '+sub_m.cmt+' \~\~\~\~\n';
effp_total++;
}
if (sub_m.action == 'alreadydone') {
text += '\n*\{\{effp|alreadydone|'+sub_m.user+'\}\} '+sub_m.cmt+' \~\~\~\~\n';
effp_total++;
}
if (sub_m.action != 'none' && sub_m.action != 'blocked' && sub_m.action != 'fixed' && sub_m.action != 'alreadydone') {
text += '\n*\{\{effp|'+sub_m.action+'\}\} '+sub_m.cmt+' \~\~\~\~\n';
effp_total++;
}
pagetext = pagetext.substring(0, startindex) + text + pagetext.substring(endindex);
}
}
var summary;
if (effp_total==1) summary = "Responding to "+effp_total+" report";
else summary = "Responding to "+effp_total+" reports";
pagetext = pagetext.replace(/[\n\r]{3,}/g,"\n\n");
pagetext = pagetext.replace(/[\n\r]+==/g,"\n\n==");
effp_editPage(effp_effpPageName, pagetext, summary, false);
$(document).ajaxStop(function () {
$("#effp_finished_main").css("display", "");
});
}
function effp_effp_templateUser(duration, vandal) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
d = new Date();
var ApI = new mw.Api();
ApI.postWithToken( "edit", {
action: "edit",
section: 'new',
watchlist: "nochange",
sectiontitle: monthNames[d.getMonth()] + ' ' + d.getFullYear(),
summary: "You have been blocked from editing for abuse of editing privileges."+effp_advert,
text: "\n{{subst:uw-block|time="+duration+"|sig=yes}}",
title: "User talk:"+vandal
}).done(function(editData) {
$('#effp_status').html($('#effp_status').html()+'
}).fail(function(error) {
$('#effp_status').html($('#effp_status').html()+'
});
}
function effp_effp_onActionChange(id) {
var extra = $("#effp_effp_extra_" + id);
var selectValue = $("#effp_effp_action_" + id).val();
if (selectValue == 'none') extra.html('');
if (selectValue == 'blocked' && !Morebits.userIsInGroup('sysop')) {
extra.html('
');
}
if (selectValue == 'fixed') {
extra.html('
');
}
if (selectValue == 'alreadydone') {
extra.html('
');
}
if (selectValue != 'none' && selectValue != 'blocked' && selectValue != 'fixed' && selectValue != 'alreadydone') {
extra.html('');
}
if (Morebits.userIsInGroup('sysop')) {
if (selectValue == 'blocked') {
extra.html('
}
}
}
function effp_effp_onActionChange2(id) {
var blockuserdiv = $("#effp_effp_blockuserdiv_"+id);
if (!document.getElementById('effp_effp_blockuser_'+id).checked) {
blockuserdiv.html('');
}
else {
var blockreasons = [{
label: 'None',
selected: true,
value: ''
}, {
label: 'Vandalism',
value: 'Vandalism'
}, {
label: 'Deliberately triggering the edit filter',
value: 'Deliberately triggering the edit filter'
}, {
label: 'Disruptive editing',
value: 'Disruptive editing'
}, {
label: 'Clearly not here to contribute to the encyclopedia',
value: 'Clearly not here to contribute to the encyclopedia'
}, {
label: 'Custom',
value: 'custom'
}];
var blockexpiration = [{
label: '31 hours',
selected: true,
value: '31 hours'
}, {
label: 'indefinite',
value: 'indefinite'
}, {
label: '24 hours',
value: '24 hours'
}, {
label: '48 hours',
value: '48 hours'
}, {
label: '72 hours',
value: '72 hours'
}, {
label: '1 week',
value: '1 week'
}, {
label: '2 weeks',
value: '2 weeks'
}, {
label: '3 weeks',
value: '3 weeks'
}, {
label: '1 month',
value: '1 month'
}, {
label: '2 months',
value: '2 months'
}, {
label: '3 months',
value: '3 months'
}, {
label: '6 months',
value: '6 months'
}, {
label: '1 year',
value: '1 year'
}, {
label: '2 years',
value: '2 years'
}];
blockuserdiv.html(''+effp_generateSelect('effp_effp_blockreason_'+id, blockreasons)+'
'+effp_generateSelect('effp_effp_blockexpiry_'+id, blockexpiration)+'
');
}
}
function displayMessage_inline(message, div, className) {
var divtitle = '#' + div;
if (message === '' || message === null) {
$(divtitle).empty().hide();
return true;
} else {
var $messageDiv = $(divtitle);
$messageDiv.attr('style', "margin:1em;padding:0.5em 2.5%;border:solid 1px #ddd;background-color:#fcfcfc");
if (!$messageDiv.length) {
if (mw.util.$content.length) {
mw.util.$content.prepend($messageDiv);
} else {
return false;
}
}
if (typeof message === 'object') {
$messageDiv.empty();
$messageDiv.append(message);
} else {
$messageDiv.html(message);
}
$messageDiv.slideDown();
return true;
}
}
function effp_links() {
var sectionHeaders = $("#mw-content-text h2");
var offset = 1;
sectionHeaders.each(function(index, element) {
var not_archived = !$(element).next().length || $(element).next().html().indexOf('This is an archived discussion.') == -1;
if (index > 0)
var idtitle = "effp-review-" + (index - 1);
$('
').insertAfter(element);var editSectionLink = $(element).children(".mw-editsection");
if ((editSectionLink.length > 0) && (not_archived)) {
editSectionLink = editSectionLink[0];
var reviewlink = document.createElement("a");
reviewlink.href = "#" + idtitle;
$(reviewlink).attr("sectionIndex", index + offset);
reviewlink.innerHTML = "Review report";
var editSectionContents = $(editSectionLink).html();
editSectionLink.innerHTML = "[";
editSectionLink.appendChild(reviewlink);
editSectionLink.innerHTML = editSectionLink.innerHTML + "] " + editSectionContents;
reviewlink.onclick = (function() {
$(reviewlink).remove();
effp_init();
});
} else {
offset = offset - 1;
}
});
$('body [sectionIndex]').click((function() {
$('body [sectionIndex]').each(function(i) {
$(this).html("Reviewing reports...").contents().unwrap();
});
effp_init();
}));
}
mw.loader.using(['ext.gadget.Twinkle', 'mediawiki.util', 'mediawiki.api', 'mediawiki.Title', 'mediawiki.RegExp'], function() {
if ( mw.config.get( "wgPageName" ) === "Wikipedia:Edit_filter/False_positives/Reports" || mw.config.get( "wgPageName" ) === "User:Abelmoschus_Esculentus/EFFPRH/sandbox" ) {
effp_links();
}
});
//