MediaWiki:Gadget-afdnominator.js

/* A gadget that takes care of the technical aspects of nominating articles for AFD.

Maintained by Rabbitseatcarrots, for complaining please post to their talkpage, or FIY if you're a tech.

Note: This gadget injects the menu at the top of the article by putting the menu first, then concatenating the contents of the page. It works, but it's ugly.

//Configuration var afdlog = 164152; var afdtitle = "RationalWiki:Articles for deletion"; var afd = 164147; var namespaces = [0, 2, 112, 106]; //Main, User:, Draft:, Fun:

if (namespaces.includes(mw.config.get("wgNamespaceNumber"))) { var button = mw.util.addPortletLink("p-cactions", null, "Nominate for deletion", "pb-afdnominator"); button.children[0].attributes.removeNamedItem("href");//We're going to modify it manually and remove the link, as we don't actually want a link, just the button document.getElementById("mw-content-text").innerHTML = ' ' + ' Welcome to the AFD nominator menu. Introduce the reason for nominating this article below ' + 'Short reason ' + ' ' + 'Reason ' + ' ' + 'Leave a notice on the article creator' + "'s " + 'talkpage? ' +		'Nominate ' + document.getElementById("mw-content-text").innerHTML; var dialog = document.getElementById("afdnominator-dialog"); button.children[0].onclick = function { if (dialog.style.display ==="none") { dialog.style.display = "block"; } else { dialog.style.display = "none"; }	};	document.getElementById("afdnominator-button").onclick = function { //TODO: We're supposed to check for edit conflicts. var api = new mw.Api; //First, edit the article and add the template. var params = { action: "edit", pageid: mw.config.get("wgArticleId"), prependtext: "\n", summary: "Nominate article for deletion (Performed using the AFD Nominator)" };		api.postWithToken('csrf', params).done(function {}); //Next, edit RW:AFD/Log. The instructions say to add it to the top, so we'll first download the page, check where that comment is, then add our entry below it		var params2 = { action: 'parse', pageid: afdlog, prop: 'wikitext', format: 'json' };		api.get(params2).done(function(data) {			console.log(data);			var afdloglist = data.parse.wikitext['*'];			console.log(afdloglist);			var htmlcommentendregex = /([(!!!)-->]$)/; //With this regex, we can find the position of the comment			var afdloglistcommentposition = afdloglist.match(htmlcommentendregex).index;			if (afdloglist.length - afdloglistcommentposition === 1) {				//There are no other articles nominated				var params3 = {					action: "edit",					pageid: afdlog,					appendtext: "\n",					summary: "Nominate " + mw.config.get("wgPageName") + " for deletion (Performed using the AFD Nominator)"				};				api.postWithToken('csrf', params3).done( function {});			} else {				//There's at least one other nomination at the log. Not as easy as just using appendtext in the API, but nothing some substring magic can't solve.				var top = afdloglist.substring(0, afdloglistcommentposition); var othernominations = afdloglist.substring(afdloglistcommentposition); //If ommited, the second parameter will be the string's length //We can put our nomination right in the middle var edittext = top + "\n" + othernominations; var params4 = { action: "edit", pageid: afdlog, text: edittext, summary: "Nominate " + mw.config.get("wgPageName") + " for deletion (Performed using the AFD Nominator)" };				api.postWithToken('csrf', params4).done( function {}); }		});//api.get(params2)		//Next up on the list is the actual AFD page.		//TODO: The article name may be in use by another, previous AFD, we need to check for this situation		var afdedittext = '';		var params5 = {			action: "edit",			title: afdtitle + "/" + mw.config.get("wgPageName"),			text: afdedittext,			summary: "Nominate " + mw.config.get("wgPageName") + " for deletion (Performed using the AFD Nominator)"		};		api.postWithToken('csrf', params5).done(function{});		//And finally, the author alert		if (document.getElementById("afdnominator-authoralert").checked) {			var params6 = {				action: 'query',				prop: 'revisions',				titles: mw.config.get("wgPageName"),				rvprop: 'user',				rvdir: 'newer',				rvlimit: 1,				rvslots: 'main',				formatversion: '2',				format: 'json'			};			api.get(params6).done(function (data2) { var creator = data2.query.pages[0].revisions[0].user; console.log(creator); console.log(data2); var params7 = { action: "edit", title: "User talk:" + creator, section: "new", sectiontitle: "AFD notice", text: " ~", summary: "AFD notice (Performed using the AFD nominator)" };				api.postWithToken('csrf', params7).done(function{}); });		}		document.getElementById("afdnominator-dialog").innerHTML += ' Done. You may see the AFD entry at here ';	}; }//