Jump to content

User:Terasail/COI Request Tool.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Terasail (talk | contribs) at 22:38, 13 February 2022 (Add WP:RS option from Template:ECOI to reply option list.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
var replyCOIT = {
"Close": ["", "Close", "Closed edit request", "|A"],
"Remove": ["", "Remove", "Removed edit request", ""],
"Comment (No template)": ["", "speechBubbleAdd", "Comment", ""],
"Done:": ["d", "checkAll", "Done", "|A"],
"Partly done:": ["pd", "check", "Partly done", "|P"],
"Already Done:": ["a", "clock", "Already Done", "|A"],
"Note:": ["note", "ellipsis", "Note", ""],
"Question:": ["q", "helpNotice", "Question", ""],
"Go ahead: I have reviewed these proposed changes and suggest that you go ahead and make the proposed changes to the page.": ["g", "edit", "Go ahead", "|G"],
"Not done:": ["n", "notice", "Not done", "|D"],
"Not done for now:": ["nfn", "notice", "Not done for now", "|D"],
"Not done for now: Please establish a consensus with editors engaged in the subject area before using the {{Request edit}} template for this proposed change.": ["c", "userGroup", "Not done – Needs consensus", "|D|C"],
"Not done: please provide reliable sources that support the change you want to be made.": ["rs", "link", "Not done - Needs reliable sources", "|D"],
"Not done for now: it's not clear what changes you want to be made. Please mention the specific changes in a 'change X to Y' format.": ["xy", "helpNotice", "Not done – Unclear request", "|D|Unclear request"],
"Undone: This request has been partially undone.": ["udp","undo", "Partly undone", "|D|The requested edit has been partially undone"], 
"Undone: This request has been undone.": ["ud", "undo", "Undone", "|D|The requested edit has been undone"]
};
var COIResponse = [];
//selectedReply
function executeCOI(currentBox, replyOption, inputText, answered) {
	OO.ui.confirm("Confirm in order to reply to this edit request.").done(function(confirmed) {
		if (confirmed) {
			currentBox.getElementsByTagName('tr')[2].remove();
			currentBox.children[0].children[0].children[1].remove();
			currentBox.children[0].children[0].children[1].remove();
			var infoBox = new OO.ui.MessageWidget( {
				icon: 'pageSettings',
				type: 'notice',
				label: 'Processing request — Edit request starting, getting section data to edit.'
			});
			var firstRowC = currentBox.children[1].children[0];
			firstRowC.style = "padding-bottom:10px";
			firstRowC.innerHTML = "";
			progressBar = new OO.ui.ProgressBarWidget( {
				progress: false
			});
			$(firstRowC).append(progressBar.$element);
			$(firstRowC).append(infoBox.$element);
			progressBar.$element[0].style = "margin:auto";
			infoBox.$element[0].style = "margin:10px auto 0px; max-width:50em; text-align:left";
			var header = "";
			var curElement = currentBox.parentNode; //Table tag
			do {
				curElement = curElement.previousElementSibling;
				if (curElement.getElementsByClassName("mw-headline").length == 1) {
					header = curElement.getElementsByClassName("mw-headline")[0].id;
				}
			}
			while (header == "");
			var conVal = mw.config.values;
			var api = new mw.Api();
			api.get( {
				action: "parse",
				page: conVal.wgPageName,
				prop: "sections|wikitext"
			}).done(function(data) {
				infoBox.setLabel("Processing request — Making changes to the edit request");
				var sections = data.parse.sections;
				var wikitext = data.parse.wikitext["*"];
				var sectionIndx = -1;
				var sectionNum = -1;
				var endSection = -1;
				for (var j = 0; j < sections.length; j++) {
					var tempSec = sections[j];
					if (tempSec.anchor == header && !isNaN(Number(tempSec.index))) {
						sectionNum = tempSec.number;
						sectionIndx = tempSec.index;
						endSection = sectionIndx;
					} else if (tempSec.number.indexOf(sectionNum) == 0) {
						endSection = tempSec.index;
					}
				}
				var offset = sections[sectionIndx - 1].byteoffset;
				if (endSection < sections.length) {
					wikitext = wikitext.slice(offset, sections[endSection].byteoffset);
				} else {
					wikitext = wikitext.slice(offset, wikitext.length);
				}
				var templateR = replyOption[3];
				var tempType = "{{request edit";
				var wikitextU = wikitext.toUpperCase();
				var templateS = wikitextU.indexOf("{{REQ");
				if (templateS == -1) {
					templateS = wikitextU.indexOf("{{EDIT");
				}
				if (templateS == -1) {
					templateS = wikitextU.indexOf("{{COI");
				}
				if (templateS == -1) {
					infoBox.setLabel('Failed to find {{Request edit}} template, please report to "User talk:Terasail/COI Request Tool"');
					infoBox.setType("error");
				} else {
					var templateE = wikitextU.indexOf("}}", templateS) + 2;
					if (answered == false) {
						templateR = "";
					}
					if (templateR != "" || answered == false) {
						wikitext = wikitext.slice(0, templateS) + "{{Request edit" + templateR + "}}" + wikitext.slice(templateE);
					}
					var editSummary = "/* " + header.replaceAll("_", " ") + " */ " + replyOption[2] +  " ([[User:Terasail/COI_Request_Tool|COI Request Tool]])";
					if (replyOption[1] != "Close") {
						var lastMsg = wikitext.lastIndexOf("\n", wikitext.lastIndexOf("(UTC)")) + 1;
						wikitext = wikitext.trim() + "\n:";
						while(lastMsg == wikitext.indexOf(":", lastMsg)) {
							lastMsg++;
							wikitext += ":";
						}
						if (replyOption[0] != "") {
							wikitext += "{{subst:ECOI|" + replyOption[0] + "}}";
						}
						inputText = inputText.replaceAll("~~~~", "");
						wikitext += " " + inputText;
						wikitext = wikitext.trim() + " ~~~~";
						if (replyOption[1] == "Remove") {
							wikitext = "";
						}
					}
					if (infoBox.type != "error") {
						infoBox.setType("success");
						infoBox.setLabel("Saving '" + replyOption[2] + "' response to the talk page. (This may take a few seconds)");
						api.postWithEditToken({
							action: 'edit',
							title: conVal.wgPageName,
							text: wikitext,
							section: sectionIndx,
							summary: editSummary
						}).done(function(result) {
							window.location = "https" + "://en.wikipedia.org/w/index.php?title=" + mw.config.values.wgPageName.replaceAll("&", "%26") + "&type=revision&diff=cur&oldid=prev";
						});
					}
				}
			});
		}
	});
}

function loadButtons(currentBox) {
	var replyListC = Object.entries(replyCOIT);
	var mainResponseC = [[replyListC[8][1], "Go ahead", "User may make the change themselves"], [replyListC[12][1], "Unclear: X-Y", "Unclear request"], [replyListC[11][1], "WP:Consensus", "Change needs consensus"]];
	$(currentBox).append('<tr><td colspan=2><div style="display: flex; justify-content: center;"></div></td></tr><tr style="display:none"><td colspan=2 style="padding-bottom:10px; text-align:center"></td></tr>');
	var firstRowC = currentBox.children[1].children[0].children[0];
	//Create dropdown menu
	var dropMenu = new OO.ui.DropdownWidget( {
		label: "Select reply option -  Add additional text below",
		menu: {items: []}
	});
	for (var count = 2; count < replyListC.length; count++) {
		var newOption = new OO.ui.MenuOptionWidget({
			label: replyListC[count][0],
			icon: replyListC[count][1][1]
		});
		dropMenu.menu.addItems([newOption]);
	}
	dropMenu.$element[0].style = "text-align:left; margin:0px";
	$(currentBox.children[2].children[0]).append(dropMenu.$element);
	//Create input box
	var inputText = new OO.ui.MultilineTextInputWidget({autosize: true, label: "Automatically signed"});
	inputText.$element[0].style = "margin:auto";
	$(currentBox.children[2].children[0]).append(inputText.$element);
	//Create horizontal layout
	var hzCLayout = new OO.ui.HorizontalLayout();
	//Toggle answer button
	var answerCB = new OO.ui.CheckboxInputWidget({selected: true});
	//Done button
	var doneCB = new OO.ui.ButtonWidget( { 
		icon: "checkAll",
		flags: ["primary", "progressive"],
		label: "Done",
		title: "Mark as done"
	});
	doneCB.on("click", function() {
		if (doneCB.getLabel() == "Submit") {
			executeCOI(currentBox, replyCOIT[dropMenu.getLabel()], inputText.value, answerCB.selected);
		} else {
			executeCOI(currentBox, replyListC[3][1], inputText.value, answerCB.selected);
		}
	});
	hzCLayout.addItems([doneCB]);
	//Quick access buttons
	mainResponseC.forEach(function(item) {
		item[3] = new OO.ui.ButtonWidget({
			label: item[1],
			title: item[2]
		});
		item[3].on("click", function() {
			executeCOI(currentBox, item[0], "", answerCB.selected);
		});
		hzCLayout.addItems([item[3]]);
	});
	//Responses button
	var respondCB = new OO.ui.ButtonWidget( { 
		icon: "add",
		label: "More",
		title: "Extra reply options"
	});
	hzCLayout.addItems([respondCB]);
	respondCB.on("click", function() {
		if (currentBox.children[2].style.display == "") {
			respondCB.setIcon("add");
			respondCB.setLabel("More");
			currentBox.children[2].style = "display:none";
			doneCB.setLabel("Done");
			doneCB.setTitle("Mark as done");
		} else {
			respondCB.setIcon("subtract");
			respondCB.setLabel("Less");
			currentBox.children[2].style = "";
			doneCB.setLabel("Submit");
			doneCB.setTitle("Submit response");
		}
	});
	hzCLayout.addItems([answerCB, new OO.ui.LabelWidget({label: "Close"})]);
	//Create first row fieldset
	var fieldsetC = new OO.ui.FieldsetLayout();
	// Add an horizontal field layout
	fieldsetC.addItems([
		new OO.ui.FieldLayout(
			new OO.ui.Widget({
				content: [hzCLayout]
			}),
		)
	]);
	$(firstRowC).append(fieldsetC.$element);
	//Close request
	var closeB = new OO.ui.ButtonWidget( { 
		icon: "unFlag",
		invisibleLabel: true,
		title: "Mark as answered"
	});
	closeB.$element[0].style = "margin:10px 10px 0px";
	$(currentBox.children[0].children[0]).append(closeB.$element);
	closeB.on("click", function() {
		executeCOI(currentBox, replyCOIT.Close, "", true)
	});
	//Remove request
	var removeB = new OO.ui.ButtonWidget( { 
		icon: "trash",
		flags: ["primary", "destructive"],
		invisibleLabel: true,
		title: "Remove entire section!"
	});
	removeB.$element[0].style = "margin:10px";
	removeB.on("click", function() {
		executeCOI(currentBox, replyCOIT.Remove, "", false);
	});
	$(currentBox.children[0].children[0]).append(removeB.$element);
}

function addReplyButton(currentBox, boxType) {
	var replyB = new OO.ui.ButtonWidget( { 
		icon: "edit",
		flags: "progressive",
		invisibleLabel: true
	});
	replyB.$element[0].style = "margin:5px 0px";
	$(currentBox.children[0].children[0]).append(replyB.$element);
	replyB.on("click", function() {
		currentBox.parentElement.setAttribute("class", "plainlinks tmbox tmbox-notice editrequest");
		var img = currentBox.children[0].children[0].children[0]
		img.width = 53;
		img.height = 53;
		img.src = "/media/wikipedia/commons/f/f4/Info_talk.png"
		img.scrset = "";
		currentBox.children[0].children[1].innerHTML = '<div style="text-align:center; font-size:125%">Please read the instructions for the parameters used by this template for accepting and declining them, and review the request below and make the edit if it is well sourced, neutral, and follows other Wikipedia guidelines and policies.</div>'
		replyB.$element.remove();
		loadButtons(currentBox);
	});
}

var COIRequestBoxes = document.getElementsByClassName("plainlinks tmbox tmbox-notice editrequest");
if (COIRequestBoxes) {
	mw.loader.using(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]).done(function() {
		mw.loader.load(["oojs-ui.styles.icons-alerts", "oojs-ui.styles.icons-interactions", "oojs-ui.styles.icons-moderation", "oojs-ui.styles.icons-user", "oojs-ui.styles.icons-editing-core"]);
		for (var i = 0; i < COIRequestBoxes.length; i++) {
			var currentBox = COIRequestBoxes[i].children[0];
			var edReqCheck = currentBox.children[0].children[1];
			if (edReqCheck.innerText.search("conflict") != -1) { //Check that it is a conflict edit request
				if (edReqCheck.innerText.search("The requested edits backlog is") != -1) { //For unanswered boxes
					loadButtons(currentBox);
				} else { //For answered boxes
					addReplyButton(currentBox);
				}
			}
		}
	});
}
//</nowiki>[[Category:Wikipedia scripts]]