User:Dreamy Jazz/historymergerhelper.js

/* History merge helper script for use dealing with reports at Wikipedia:WikiProject History Merge

Written by Dreamy Jazz, with inspiration in code design from User:Joeytje50/JWB.js and User:GeneralNotability/spihelper.js


 * version: pre-0.0.0 */

/* Global constants */ //How many history entries should be displayed for the source and desination pages which were edits after the diff. const historymerger_afterdiff = 3;

/* mediawiki API object for this script */ const historymerger_api = new mw.Api;

var historymerger_currententrygenerator;

const historymerger_interface = `  History merger script  Choose the page you want to look over  Go!       Skip Merge `;

$(function {	if (mw.config.get("wgPageName") !== "User:Dreamy_Jazz/historymerger") {		return;	}	historymerger_init; });

async function historymerger_init { pagelist = await historymerger_loadPageList; console.log(pagelist); $('body').html(historymerger_interface); var i;	for (i = 0; i < pagelist.length; i++) { $('#historymerger_pageslist').append($(' ').val(pagelist[i]).text(pagelist[i])); }	// Show interface $("#historymerger_interface").css("display", ""); $("#historymerger_startShowingEntriesButton").click(await async function {		historymerger_currententrygenerator = await historymerger_generateEntriesFromPage($("#historymerger_pageslist").find(":selected").text);		console.log(historymerger_currententrygenerator)		$("#historymerger_startscreen").addClass("hidden");		$("#historymerger_leftcolumn, #historymerger_rightcolumn, #historymerger_bottomline").removeClass("hidden");		$("#historymerger_skipbutton").click(function{ historymerger_showNextEntryFromGenerator; });		$("#historymeger_mergebutton").click(function{ historymerger_performHistoryMerge; historymerger_showNextEntryFromGenerator; });		historymerger_showNextEntryFromGenerator;	}); historymerger_loadEntry(1, 19); // Testing generator = await historymerger_generateEntriesFromPage(19); while ((value = await generator.next) !== undefined) { console.log(value); break; } }

/* Show the next entry from the current generator async function historymerger_showNextEntryFromGenerator { console.log(historymerger_currententrygenerator.next); console.log(historymerger_currententrygenerator.value); $("#historymerger_sourcehistory").attr("src", "https://en.wikipedia.org/w/index.php?title=" + historymerger_currententrygenerator.value.source + "&date-range-to=" + (await historymerger_getDateFromDiff(historymerger_currententrygenerator.value.source_rvid))) }

async function historymerger_getDateFromDiff(diff) { request = { "action": "query", "format": "json", "prop": "revisions", "revids": diff.toString, "rvprop": "timestamp" };	try { response = await historymerger_api.get(request); console.log(response) return response.query.pages[diff.toString].revisions.timestamp; } catch (error) { console.error("ERROR: Error encountered while trying to get timestamp for diff: " + error); return false; } }

function historymerger_performHistoryMerge { console.log("unwritten"); }

async function historymerger_loadPageList { pagelist = [] request = { "action": "query", "format": "json", "list": "allpages", "apprefix": "WikiProject History Merge/", "apnamespace": "4", "aplimit": "max" };	try { response = await historymerger_api.get(request); subpages = response.query.allpages; var i;		for (i = 0; i < subpages.length; i++) { console.log(subpages[i].title.split("WikiProject History Merge/")[1]); if (subpages[i].title.split("WikiProject History Merge/")[1].match(/^[0-9]+$/)) { console.log(true); pagelist.push(subpages[i].title.split("WikiProject History Merge/")[1]); }		}		return pagelist; } catch (error) { console.error("ERROR: Error encountered while trying to get list of subpages for WikiProject History Merge: " + error); return false; } }

/*
 * param index: The number of the entry in the list
 * param page: The subpage number to read from

async function historymerger_loadEntry(index, page) { entries = await historymerger_splitPageIntoEntries(page); }

function* historymerger_splitGenerator(text, delimiter) { var lastmatchi = 0; while (text.indexOf(delimiter, lastmatchi) !== -1) { yield text.slice(lastmatchi, text.indexOf(delimiter, lastmatchi)); lastmatchi = text.indexOf(delimiter, lastmatchi) + delimiter.length; } }

async function historymerger_generateEntriesFromPage(page) { content = await historymerger_getPageText("Wikipedia:WikiProject History Merge/" + page.toString); console.log(content); content = content.split("{|")[1]; content_generator = historymerger_splitGenerator(content, "|-"); skip = 2; var currentvalue; var currententry; var entry; return { next: function { while ((currentvalue = content_generator.next.value) !== undefined) { entry = {}; console.log(currentvalue); if (skip !== 0) { // Skip the header of the table skip--; console.log(true); continue; }				split_entry = currentvalue.split("||"); // Split on the column seperators for the row entry.index = split_entry[0].split("\n|")[1].trim; entry.source = split_entry[1].split("")[1].split("")[0]; entry.source_rvid = split_entry[2].split("[")[1].split(" ")[1].split("]")[0]; entry.destination = split_entry[4].split("")[1].split("")[0]; entry.destination_rvid = split_entry[5].split("[")[1].split(" ")[1].split("]")[0]; currententry = entry; return entry; }		},		value: function { return currententry; }	} }

async function historymerger_splitPageIntoEntries(page) { entries = {}; content = await historymerger_getPageText("Wikipedia:WikiProject History Merge/" + page.toString); console.log(content); content = content.split("{|")[1].split("|-").slice(2); var i;	for (i = 0; i < content.length; i++) { split_entry = content[i].split("||"); // The below code will likely fail if the table has been modified in some way. Perhaps a rewrite to make it more robust is needed. entries.index = split_entry[0].split("\n|"); entries.source = split_entry[1].split("")[1].split("")[0]; entries.source_rvid = split_entry[2].split("[")[1].split(" ")[1].split("]")[0]; entries.destination = split_entry[4].split("")[1].split("")[0]; entries.destination_rvid = split_entry[5].split("[")[1].split(" ")[1].split("]")[0]; }	//console.log(entries); return entries; }

/* Adapted from spiHelper_getPageText */ async function historymerger_getPageText(page) { request = { action: 'query', prop: 'revisions', rvprop: 'content', rvslots: 'main', indexpageids: true, titles: page.toString };

try { response = await historymerger_api.get(request); pageid = response.query.pageids[0];

if (pageid === '-1') { console.error("ERROR: Page " + page.toString + " does not exist."); return false; }		return response.query.pages[pageid].revisions[0].slots.main['*']; } catch (error) { console.error("ERROR: Error encountered while trying to get page text for " + page.toString + ": " + error); return false } }