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 16:00, 2 June 2021 (Fix error stopping reply save). 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 COIResponse = [
["ntmp", "speechBubbleAdd", "Comment", "Comment (No template)", ""],
["d", "checkAll", "Done", "Done", "|A"],
["pd", "check", "Partly done", "Partly done:", "|P"],
["a", "clock", "Already Done", "Already Done", "|A"],
["q", "helpNotice", "Question", "Question:", ""],
["g", "edit", "Go ahead", "Go ahead: I have reviewed these proposed changes and suggest that you go ahead and make the proposed changes to the page.", "|G"],
["note", "ellipsis", "Note", "Note:", ""],
["n", "notice", "Not done", "Not done:", "|G"],
["nfn", "notice", "Not done for now", "Not done for now:", "|D"],
["c", "userGroup", "Not done – Needs consensus", "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.", "|D|C"],
["udp","undo", "Partly undone", "Undone: This request has been partially undone.", "|D|The requested edit has been partially undone"], 
["ud", "undo", "Undone", "Undone: This request has been undone.", "|D|The requested edit has been undone"]];

function executeCOI(currentBox, selectedReply, 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 api = new mw.Api();
			api.get( {
				action: "parse",
				page: mw.config.values.wgPageName,
				prop: "sections",
			}).done(function(data) {
				infoBox.setLabel("Processing request — Making changes to the edit request");
				var sections = data.parse.sections;
				var sectionNum = -1;
				var offset = 0;
				for (var j = 0; j < sections.length; j++) {
					if (sections[j].anchor == header) {
						sectionNum = j + 1;
						sectionNum -= offset;
					}
					if (sections[j].fromtitle != mw.config.values.wgPageName) {
						offset++;
					}
				}
				infoBox.setLabel("Processing request — Getting information from section: " + sectionNum + ".");
				api.get( {
					action: "parse",
					page: mw.config.values.wgPageName,
					prop: "wikitext",
					section: sectionNum
				}).done( function(data) {
					infoBox.setLabel("Processing request — Making changes to the edit request.");
					var wikitext = data.parse.wikitext["*"];
					var tempStr = "";
					if (selectedReply == "CLOSEREQ") {
						tempStr = "|A";
					} else {
						tempStr = COIResponse[selectedReply][4];
					}
					var tempType = "{{request edit";
					var upperWT = wikitext.toUpperCase();
					var templateLoc = upperWT.indexOf("{{REQ");
					if (templateLoc == -1) {
						templateLoc = upperWT.indexOf("{{EDIT");
					}
					if (templateLoc == -1) {
						templateLoc = upperWT.indexOf("{{COI");
					}
					if (templateLoc == -1) {
						infoBox.setLabel('Failed to find {{Request edit}} template, please report to "User talk:Terasail/COI Request Tool"');
						infoBox.setType("error");
					} else {
						var templateEnd = upperWT.indexOf("}}", templateLoc) + 2;
						if (tempStr != "" || answered == false) {
							if (answered == false) {
								tempStr = "";
							}
							wikitext = wikitext.slice(0, templateLoc) + "{{Request edit" + tempStr + "}}" + wikitext.slice(templateEnd);
						}
						var editSummary = "/* " + header.replaceAll("_", " ") + " */ ";
						if (selectedReply == "REMOVE") {
							wikitext = "";
							editSummary = "Removed edit request";
							selectedReply = -1;
						} else if (selectedReply == "CLOSEREQ") {
							editSummary += "Closed edit request";
							selectedReply = -1;
						} else {
							if (selectedReply == 0) { //Do not add response template
								wikitext += "\n:" + inputText.value;
							} else {
								wikitext += "\n:{{subst:ECOI|" + COIResponse[selectedReply][0] + "}}";
								if (inputText != "") {
									wikitext += " " + inputText.value;
								}
							}
							wikitext += " ~~~~";
							editSummary += COIResponse[selectedReply][2];
						}
						editSummary += " ([[User:Terasail/COI_Request_Tool|COI Request Tool]])";
						if (selectedReply != -1 && infoBox.type != "error") {
							infoBox.setType("success");
							infoBox.setLabel("Processing '" + COIResponse[selectedReply][2] + "' request — Saving changes to the talk page. (This may take a few seconds)");
						}
						api.postWithEditToken({
							action: 'edit',
							title: mw.config.values.wgPageName,
							text: wikitext,
							section: sectionNum,
							summary: editSummary
						}).done(function(result) {
							window.location = "https" + "://en.wikipedia.org/w/index.php?title=" + mw.config.values.wgPageName + "&type=revision&diff=cur&oldid=prev";
						});
					}
				});
			});
		}
	});
}

function loadButtons(currentBox) {
	$(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: []}
	});
	COIResponse.forEach(function(item) {
		var newOption = new OO.ui.MenuOptionWidget({
			label: item[3],
			icon: item[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: "'~~~~' is automatically added"});
	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") {
			for (var k = 0; k < COIResponse.length; k++) {
				if (COIResponse[k][3] == dropMenu.getLabel()) {
					executeCOI(currentBox, k, inputText, answerCB.selected);
				}
			}
		} else {
			executeCOI(currentBox, 1, inputText, answerCB.selected);
		}
	});
	hzCLayout.addItems([doneCB]);
	//Go ahead button
	var gaB = new OO.ui.ButtonWidget( { 
		icon: "edit",
		label: "Go ahead",
		title: "Approve to make change themselves"
	});
	hzCLayout.addItems([gaB]);
	gaB.on("click", function() {
		executeCOI(currentBox, 5, inputText, answerCB.selected);
	});
	//Consensus button
	var conB = new OO.ui.ButtonWidget( { 
		icon: "userGroup",
		label: "WP:Consensus",
		title: "Change needs consensus"
	});
	hzCLayout.addItems([conB]);
	conB.on("click", function() {
		executeCOI(currentBox, 9, inputText, answerCB.selected);
	});
	//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, "CLOSEREQ", "", answerCB.selected)
	});
	//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";
	$(currentBox.children[0].children[0]).append(removeB.$element);
	removeB.on("click", function() {
		executeCOI(currentBox, "REMOVE", "", answerCB.selected);
	});
}

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>