User:Terasail/COI Request Tool.js: Difference between revisions

Content deleted Content added
Test
Update to support new parser format
Line 37:
}
}
 
if (COIRequests.length > 0) {
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-editing-core", "oojs-ui.styles.icons-editing-advanced", "oojs-ui.styles.icons-user"]);
loadCOITool();
//Get page watchers, visitors and user watch status.
let watchStatus = [];
let pageID = mw.config.get("wgArticleId");
new mw.Api().get({
action: "query",
prop: "info",
pageids: pageID,
inprop: "watchers|visitingwatchers|watched",
format: "json"
}).done(function (data) {
data = data.query.pages[pageID];
let watched = data.watched;
let expiry = data.watchlistexpiry;
if (expiry) {
watched = Math.ceil((new Date(expiry).getTime() - Date.now()) / 1000 / 60 / 60 / 24) + " days";
}
watchStatus.push(data.watchers || "less than 30", data.visitingwatchers || "<30", watched);
});
//Increment through all COI requests & add respond button
for (let i = 0; i < COIRequests.length; i++) {
let respondButton = new OO.ui.ButtonWidget({
icon: "edit",
label: "Respond",
flags: "progressive",
title: "Open the response menu for this request"
}).on("click", function() {
loadCOIResponseBox(COIRequests[i], respondButton, watchStatus);
respondButton.setDisabled(true);
});
respondButton.$element[0].style = "margin:5px";
$($('.response-cell')[i]).append(respondButton.$element);
}
});
}
 
async function loadCOITool() {
function loadCOIResponseBox(COIRequest, respondButton, watchStatus) {
// Get page watchers, visitors and user watch status.
let watchStatus = [];
let watchQuery = await ApiGetCOI({
action: "query",
prop: "info",
pageids: mw.config.get("wgArticleId"),
inprop: "watchers|visitingwatchers|watched",
format: "json"
});
let watchData = watchQuery.query.pages[mw.config.get("wgArticleId")];
let watched = watchData.watched;
let expiry = watchData.watchlistexpiry;
if (expiry) {
watched = Math.ceil((new Date(expiry).getTime() - Date.now()) / 1000 / 60 / 60 / 24) + " days";
}
watchStatus.push(watchData.watchers || "less than 30", watchData.visitingwatchers || "<30", watched);
//Increment through all COI requests & add respond button
for (let i = 0; i < COIRequests.length; i++) {
let respondButton = new OO.ui.ButtonWidget({
icon: "edit",
label: "Respond",
flags: "progressive",
title: "Open the response menu for this request"
}).on("click", function() {
loadCOIResponse(COIRequests[i], respondButton, watchStatus);
respondButton.setDisabled(true);
});
respondButton.$element[0].style = "margin:5px";
$($('.response-cell')[i]).append(respondButton.$element);
}
}
 
function loadCOIResponse(COIRequest, respondButton, watchStatus) {
let responseBoxHTML = '<table class="response-box" style="border:1px solid #A2A9B1; border-radius:2px; padding:10px 16px 0; margin:auto; max-width:55em; width:100%; clear:both;"><tr><td style="color:#808080"><div style="font-style:italic; margin-left:1em;">There are currently ' + watchStatus[0] + ' users watching this page (' + watchStatus[1] + ' have viewed recent edits).</div><div>Quick options:</div></td></tr><tr style="display: flex; justify-content: center;"><td class="response-quick"></td></tr><tr><td style="color:#808080">Custom response:</td></tr><tr style="text-align:center;"><td class="response-custom"></td></tr><tr style="background:#F6F6F6;"><td class="response-preview" style="display:none;"><div style="color:#808080">Preview:</div><div></div></td></tr><tr style="display: flex; justify-content: right;"><td class="response-controls"></td></tr></table>';
$(responseBoxHTML).insertAfter(COIRequest, respondButton);
Line 103 ⟶ 106:
invisibleLabel: true
}).on("click", function () {
executeCOIResponsesaveResponseCOI([COIRequest, responseQuick, responsePreview, responseControls], nonResponseCOI[tempVal], "", "nochange", undefined);
});
quickLayout.addItems([tempButton]);
Line 115 ⟶ 118:
title: responseCOI[tempVal].title
}).on("click", function () {
executeCOIResponsesaveResponseCOI([COIRequest, responseQuick, responsePreview, responseControls], responseCOI[tempVal], "", "nochange", undefined);
});
quickLayout.addItems([tempButton]);
Line 126 ⟶ 129:
}).on("labelChange", function () {
submitButton.setDisabled(false);
peviewCOIResponsepreviewCOI(responseDropdown.menu.findSelectedItem().getData(), responseText.value, responsePreview);
});
for (let i = 0; i < responseCOI.length; i++) {
Line 143 ⟶ 146:
}).on("change", function () {
if (responseDropdown.menu.findSelectedItem()) {
peviewCOIResponsepreviewCOI(responseDropdown.menu.findSelectedItem().getData(), responseText.value, responsePreview);
}
});
Line 197 ⟶ 200:
disabled: true
}).on("click", function () {
executeCOIResponsesaveResponseCOI([COIRequest, responseQuick, responsePreview, responseControls], responseDropdown.menu.findSelectedItem().getData(), responseText.value, watchlistCheckbox.selected, watchlistDropdown.value);
});
controlsLayout.addItems([cancelButton, watchlistCheckbox, watchlistLabel, watchlistLayout, submitButton]);
}
 
function peviewCOIResponsepreviewCOI(responseOption, responseText, tableCell) {
let restTransform = "https://en.wikipedia.org/api/rest_v1/transform/wikitext/to/html/" + encodeURI(mw.config.get("wgPageName"));
if (responseOption.response != "") {
Line 235 ⟶ 238:
}
 
async function executeCOIResponsesaveResponseCOI(requestBox, responseOption, responseText, watchPage, watchValue) {
await new Promise(function(resolve) {
OO.ui.confirm("Confirm in order to reply to this edit request.").done(function(confirmed) { if (confirmed) {
resolve();
//Create label box & remove quick actions
} else {
requestBox[1].innerHTML = "";
return;
requestBox[3].remove();
}});
let statusMessage = new OO.ui.MessageWidget({
});
icon: 'pageSettings',
//Create label box & remove quick actions
type: 'notice',
requestBox[1].innerHTML = "";
label: 'Processing request — Edit request starting, getting section data to edit.'
requestBox[3].remove();
});
let statusMessage.$element[0].style = "margin:5pxnew 0; max-width:50em";OO.ui.MessageWidget({
icon: 'pageSettings',
$(requestBox[1]).append(statusMessage.$element);
type: 'notice',
//Create progress bar
label: 'Processing request — Edit request starting, getting section data to edit.'
let progressBar = new OO.ui.ProgressBarWidget({
});
progress: false
statusMessage.$element[0].style = "margin:5px 0; max-width:50em";
});
$(requestBox[1]).append(progressBarstatusMessage.$element);
//Create progress bar
//Set preview for output
let progressBar = new OO.ui.ProgressBarWidget({
peviewCOIResponse(responseOption, responseText, requestBox[2]);
progress: false
//Find header
});
let header = "";
let tempElement = $(requestBox[01]).append(progressBar.$element);
//Set preview for output
do {
previewCOI(responseOption, responseText, requestBox[2]);
tempElement = tempElement.previousElementSibling;
// Find header
if (tempElement.getElementsByClassName("mw-headline").length == 1) {
let header = "";
let sectionIndex = 0;
let tempElement = requestBox[0];
let sectionQuery = await ApiGetCOI({
action: "parse",
page: mw.config.get("wgPageName"),
prop: "sections"
});
let sections = sectionQuery.parse.sections;
do {
tempElement = tempElement.previousElementSibling;
if (tempElement.classList.contains("mw-heading")) {
if (tempElement.parentElement.tagName == "SECTION") { //Need to support both while new parser is being implemented
header = $(tempElement).find("h1,h2,h3,h4,h5,h6")[0].id;
sectionIndex = tempElement.parentElement.dataset.mwSectionId; //This is a string and might need to be a int
} else {
header = tempElement.getElementsByClassName("mw-headline")[0].id;
for (let i = 0; i < sections.length; i++) {
}
if (sections[i].anchor == header) {
}
sectionIndex = parseInt(sections[i].index);
while (header == "");
}
let editSummary = "/* " + header.replaceAll("_", " ") + " */ " + responseOption.summary + " ([[User:Terasail/COI_Request_Tool|COI Request Tool]])";
new mw.Api().get({
action: "parse",
page: mw.config.get("wgPageName"),
prop: "sections"
}).done(function (data) {
let sections = data.parse.sections;
let sectionIndex = 0;
statusMessage.setLabel("Processing request — Making changes to the edit request");
for (let i = 0; i < sections.length; i++) {
if (sections[i].anchor == header) {
sectionIndex = parseInt(sections[i].index);
}
}
}
new mw.Api().get({
}
action: "parse",
while (header == "");
page: mw.config.get("wgPageName"),
statusMessage.setLabel("Processing request — Making changes to the edit request");
section: sectionIndex,
let editSummary = "/* " + header.replaceAll("_", " ") + " */ " + responseOption.summary + " ([[User:Terasail/COI_Request_Tool|COI Request Tool]])";
prop: "wikitext|revid"
let wikitextQuery = await ApiGetCOI({
}).done(function (data) {
action: "parse",
let wikitext = data.parse.wikitext["*"];
page: mw.config.get("wgPageName"),
let latestRevision = data.parse.revid;
section: sectionIndex,
if (responseOption.parameter != "") {
prop: "wikitext|revid"
let template = "{{Edit COI|" + responseOption.parameter + "}}";
});
wikitext = wikitext.replace(/{{ *(Edit[ _])?COI(-protected|([ _](edit|request)){2})?( *\| *([=A-Z])*)* *}}/i, template);
let wikitext = wikitextQuery.parse.wikitext["*"];
}
let latestRevision = wikitextQuery.parse.revid;
if (responseOption.response != "") {
wikitext += "\n:{{subst:ECOI|" + if (responseOption.responseparameter +!= "}}";) {
let template = "{{Edit COI|" + responseOption.parameter + "}}";
wikitext += responseText.replaceAll(/\s*~~~~\s*/g, "") + " ~~~~";
wikitext = wikitext.replace(/{{ *(Edit[ _])?COI(-protected|([ _](edit|request)){2})?( *\| *([=A-Z])*)* *}}/i, template);
}
}
if (responseOption.label == "Remove") {
if (responseOption.response != "") {
wikitext = "";
wikitext += "\n:{{subst:ECOI|" + responseOption.response + "}}";
editSummary = editSummary.replace(/[^]+\*\/ /, "");
wikitext += responseText.replaceAll(/\s*~~~~\s*/g, "") + " ~~~~";
}
}
statusMessage.setType("success");
if (responseOption.label == "Remove") {
statusMessage.setLabel("Processing request — Saving changes to the talk page.");
wikitext = "";
if (latestRevision == mw.config.values.wgRevisionId) {
postCOIResponse(wikitext,editSummary = editSummary, sectionIndex,.replace(/[^]+\*\/ watchPage/, watchValue"");
}
} else {
statusMessage.setType("success");
OO.ui.confirm("There has been a new revision to the page, do you wish to continue?").done(function (revisionConfirm) {
statusMessage.setLabel("Processing request — Saving changes to the talk page.");
if (revisionConfirm) {
if (latestRevision != mw.config.values.wgRevisionId) {
postCOIResponse(wikitext, editSummary, sectionIndex, watchPage, watchValue);
await new Promise(function(resolve) {
}
OO.ui.confirm("There has been a new revision to the page, do you wish to continue?").done(function(confirmed) { if (confirmed) {
});
}resolve();
}); else {
return;
}});
});
}});
}
 
function postCOIResponse(wikitext, editSummary, sectionIndex, watchPage, watchValue) {
if (watchPage) {
if (watchValue == "nochange") {
Line 339 ⟶ 347:
window.___location = reloadURL;
});
}
 
function ApiGetCOI(params) {
return new Promise(function(resolve) {
new mw.Api().get(params)
.done(function (data) {resolve(data);})
.fail(function (data) {console.error(data);});
});
}
//</nowiki>[[Category:Wikipedia scripts]]