Jump to content

User:JJPMaster/rpp.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>

const rppParsedTitle = mw.config.get("wgPageName").replace(/_/g, " ");
// Don't touch this unless the script is being disabled altogether
const rppPage = "Wikibooks:Reading room/Administrative Assistance";

async function rppPreliminaryQuery() {
	var params = {
		action: 'parse',
		page: rppPage,
		prop: "wikitext",
		format: 'json'
	},
	api = new mw.Api();
	const data = await api.get(params);
	let content = data.parse.wikitext["*"];
	return content.includes(`<!-- PAGEREQUESTED:/${mw.config.get("wgPageName")}/ -->`);
}

function getLevelAndDuration(full, indef) {
	var myArr = [];
	myArr.push($(full).prop("checked"));
	myArr.push($(indef).prop("checked"));
	return myArr;
}

async function makeMetaRPP(theAd, title, rzn, level, duration) {
	await mw.loader.using( 'mediawiki.ForeignApi' );
	const api = new mw.ForeignApi( 'https://meta.wikimedia.org/w/api.php' );
	await api.postWithToken( 'options', {
	    action: 'edit',
        title: 'Global sysops/Requests',
        summary: `Adding protection request from ${mw.config.get("wgWikiID")}${theAd}`,
        appendtext: `\n* Please ${duration == false ? "temporarily" : "indefinitely"} ${level == false ? "semi-" : "fully "}protect [[:b:en:${title}]]${rzn ? `: ${rzn}` : "."} ~~~~`,
        format: 'json'
} ).done((data) => console.log(data));}

$(() => {
	var porty;
	$("#bodyContent").append(`
	<div id="rppDialog">
		Why would you like to request protection for this page?<br />
		Note: By default, this form will request temporary semi-protection.<br />
		<textarea id="rppReason" name="rppReason"></textarea>
		<input type="checkbox" id="fullProt" name="fullProt" /><label for="fullProt">Request full protection</label><br />
		<input type="checkbox" id="indef" name="indef" /><label for="indef">Request indefinite protection</label><br />
	    <button type="button" id="rppBtn">Request protection</button></div>
	`);
	$("#rppDialog").hide();
	if (mw.config.get("wgArticleId") > 0) {
		porty = mw.util.addPortletLink('luna-actions', '#', 'RPP', 'luna-rpp', 'Request page protection');
	}
	else return;
	$(porty).click(() => $("#rppDialog").dialog({ title: "Request page protection"} ));
	$("#rppBtn").click(function() {
		const lnd = getLevelAndDuration($("#fullProt"), $("#indef"));
		var reason = document.getElementById("rppReason").value;
		var isAlreadyReported = (async () => { return await rppPreliminaryQuery() })();
		isAlreadyReported.then(r => {
			if (!reason) {
				alert("You must provide a reason for protecting a page.");
			}
			else if (r) {
				alert(`Protection appears to have already been requested for this page. If this is a mistake, you can manually request protection at the reading room.`);
			}
			else {
				params = {
					action: 'edit',
					appendtext: `\n{{subst:User:JJPMaster/rpp/request|1=${rppParsedTitle}|full=${lnd[0]}|indef=${lnd[1]}|reason=${reason}|nosig=true}} <!-- PAGEREQUESTED:/${mw.config.get("wgPageName")}/ --> ~~~~`,
					title: rppPage,
					summary: `Requesting protection on [[${rppParsedTitle}]]` + summaryAd,
					format: 'json'
				},
				api = new mw.Api();
				
				if(confirm("Would you like to request that a global sysop protect this page? Note: This request will be made in addition to the request at the reading room.")) {
					makeMetaRPP(summaryAd, rppParsedTitle, reason, lnd[0], lnd[1]);	
				}
				
				api.postWithToken( 'csrf', params ).done( function ( data ) {
					console.log( data );
				} );
				mw.notify("Luna:\nThe request has been added. Redirecting to the request page...");
				setTimeout(() => window.location.href = window.location.href.replace(mw.config.get("wgPageName") + "#", `${rppPage}#Request for protection of ${mw.config.get("wgPageName")}`.replace(/ /g, "_")) , 2000);
			}
		}).catch(e => console.error(e));
	});
});
// </nowiki>