MediaWiki:Gadget-afdnominator.js

From RationalWiki
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/*<nowiki>
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 = '<div id="afdnominator-dialog" style="display: none; left: 0; right: 0;  margin-left: auto; margin-right: auto;"> ' +
		'<p>Welcome to the AFD nominator menu. Introduce the reason for nominating this article below</p>' +
		'<label for="afdnominator-shortreason">Short reason</label><br>' + 
		'<input id="afdnominator-shortreason" style="width: 700px;" placeholder="Explain in a few words why this article should be deleted. This will be displayed in the article and in the notice to the article creator"></input><br>' +
		'<label for="afdnominator-reason">Reason</label><br>' +
		'<textarea id="afdnominator-reason" placeholder="Your reason for nominating this article. This will be the reason displayed at this article' + "'s " + 'entry at RW:AFD"></textarea><br>' +
		'<input type="checkbox" id="afdnominator-alertauthor" checked>Leave a notice on the article creator' + "'s " +  'talkpage?</input><br>' +
		'<button id="afdnominator-button">Nominate</button></div>' + 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 {{Article for deletion}} template.
		var params = {
			action: "edit",
			pageid: mw.config.get("wgArticleId"),
			prependtext: "{{Article for deletion|" + document.getElementById("afdnominator-shortreason").value + "}}\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{{" + afdtitle + "/" + mw.config.get("wgPageName") + "}}",
					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{{" + afdtitle + "/" + mw.config.get("wgPageName") + "}}" + 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 = '{{subst:afd2|pg={{subst:#titleparts: {{subst:FULLPAGENAME}} | | 2}}|text=' + document.getElementById("afdnominator-reason").value + '}}';
		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: "{{Afdnotice|" + mw.config.get("wgPageName") + "|" + document.getElementById("afdnominator-shortreason").value + "}} ~~~~",
					summary: "AFD notice (Performed using the AFD nominator)"
				};
				api.postWithToken('csrf', params7).done(function(){});
			});
		}
		document.getElementById("afdnominator-dialog").innerHTML += '<br><p>Done. You may see the AFD entry at <a href=https://rationalwiki.org/wiki/' + afdtitle + '/' + mw.config.get("wgPageName") + '>here</a></p>';
	};
}//</nowiki>