Jump to content

User:JJPMaster/rfi.js

From Wikibooks, open books for an open world
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
//<nowiki>

// If you're testing this script, or if you're an interface admin who needs to shut it off in an emergency, set production = false. Otherwise, don't touch it.
var production = true;

var contentContent = document.getElementById("bodyContent");
mw.util.addPortletLink("p-tb", `/wiki/Special:MassRFI`, "Mass RFI", "p-massrfi", "Mass RFI request maker");
// Adapted from User:Ahecht/Scripts/massmove.js

function prod() {
	if (production) return "Wikibooks:Requests_for_import";
	return "User:JJPMaster/rfi/sandbox";
}

function massRFIGetArticles() {
	var articles = document.getElementById("wpMassRFIPages").value.split("\n");
	var ret = [];
	var i, len;
	for (i = 0, len = articles.length; i < len; i++) {
		var s = articles[i];
		s = s.trim();
		if (s) {
			ret.push(s);
		}
	}
	return ret;
}
async function parseRFI(merge) {
    var params = {
        action: 'parse',
        page: prod(),
        prop: 'sections',
        format: 'json'
    };
    var api = new mw.Api();
    var arr1;
    var arr2;
    const data = await api.get(params);
    if (merge) return data.parse.sections.find((e) => e.line == "Import and Merge").index;
    return data.parse.sections.find((e) => e.line == "Simple Imports").index; 
}

function massRFIGetNewArticles(prefix) {
	var newArts = [];
	massRFIGetArticles().forEach((e) => {
		regex = new RegExp('^' + document.getElementById("wpPipeTrick").value);
		newArts.push(e.replace(regex, ""));
	});
	return newArts;
}

function massRFIPrepare(e) {
	e.preventDefault();
	if (!wpPipeTrick.value) {
		alert("You must add a prefix to remove.");
		return;
	}
	var MCarts = confirm("Your articles are these, right?\n" + massRFIGetArticles());
	if (MCarts) MCcheck = confirm("And you want them to be imported to these titles, right?" + massRFIGetNewArticles($("#wpPipeTrick").val()));
	if (MCcheck) MCfinal = confirm("Are you sure you want to make this request?");
	if (MCfinal) doMassRFI();
}

function massRFIGenerateRequest(reason, xml) {
	var req = "";
	req = `\n== Request from {{SUBST:REVISIONUSER}} ({{SUBST:#time: d F Y}}) ==\n`;
	for (var i = 0; i < massRFIGetArticles().length; i++) {
        req = req + (`* [[${massRFIGetArticles()[i]}]] → [[${massRFIGetNewArticles()[i]}]]\n`);
    }
    req = req + reason + "\n\n";
    if (xml) req = req + "This request requires an import uploader or administrator. ";
    req = req + "~~~~";
    return req;
}

async function doMassRFI() {
	var params = {
		action: 'edit',
		title: prod(),
		section: await parseRFI(getCheck(document.getElementById("wpMassRFICheck"))),
		appendtext: massRFIGenerateRequest(document.getElementById("wpReason").value, getCheck(document.getElementById("wpMassRFICheckXML"))),
		summary: 'Added import request using [[User:JJPMaster/rfi.js|script]]',
		format: 'json'
	},
	api = new mw.Api();

	const data = await api.postWithToken( 'csrf', params ).catch(e => handle(e));
	console.log( data );
	setTimeout(() => {
		window.location.href = mw.config.get("wgArticlePath").replace("$1", "") + getTitle(production);
	}, 2000);
}

function getCheck(check) {
	if(check.checked) return true;
	return false;
}

function getTitle() {
	title = prod();
	var parsedVal = parseRFI(getCheck(document.getElementById("wpMassRFICheck")));
	if (parsedVal == 3) {
		title += "#Import_and_Merge";
	}
	else {
		title += "#Simple_Imports";
	}
	return title;
}

$(function() {
	if(mw.config.get("wgPageName").toLowerCase() == "special:massrfi") {
		document.title = "Mass RFI - " + mw.config.get("wgSiteName");
		document.getElementById("firstHeading").innerText = "Mass RFI";	
		// Adapted from [[User:Ahecht/Scripts/massmove.js]]
		contentContent.innerHTML = `<div style="display: flex;"><form id="wpMassRFI" name="wpMassRFI">
			<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>
			<div id="wpMassRFIFailedContainer"></div>
			<br />
				Pages to import (one on each line, please):<br />
					<textarea tabindex="1" accesskey="," name="wpMassRFIPages" id="wpMassRFIPages" rows="10" cols="80"></textarea>
				<input type="text" id="wpPipeTrick" name="wpPipeTrick" />
				<label for="wpPipeTrick">Prefix to remove from pages (e.g. "w:es:")</label>
				<br /><input type="text" id="wpReason" name="wpReason" />
				<label for="wpReason">Reason for importation</label>
				<br />
				<input type="checkbox" id="wpMassRFICheck" name="wpMassRFICheck" />
				<label for="wpMassRFICheck">Requires a history merge</label>
				<input type="checkbox" id="wpMassRFICheckXML" name="wpMassRFICheckXML" />
				<label for="wpMassRFICheckXML">Requires XML import</label>
				<br />
			<input type="submit"/>
			</form></div>`;

	document.getElementById("wpMassRFI").addEventListener("submit", massRFIPrepare);	
	}
});
//</nowiki>