MediaWiki:Gadget-VFHcloser.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

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

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
//<nowiki>
//Code to execute on the starting page

//First make sure we have something to do
if (document.getElementById("buttons") || document.getElementsByClassName("featurecode")[0]) {
	//If we're on the nomination page...
	if (document.getElementById("buttons")) {
		//create and style feature button
		var fl = document.getElementById("buttons");
		var button = document.createElement("input");
		fl.appendChild(button);
		button.setAttribute("type", "submit");
		button.setAttribute("value", "Feature this article");
		button.setAttribute("style", "background-color: #cfc; font-weight: bold; margin: .2em");
		button.setAttribute("id", "featureb1");
		var fl = document.getElementById("buttons");
		var article = fl.getAttribute("data-article");

	//If we're on VFHS...
	} else if (document.getElementsByClassName("featurecode")[0]) {
		var featurecode = document.getElementsByClassName("featurecode");
		for (i = 0; i < featurecode.length; i++) {
			featurecode[i].removeAttribute("style");
			var links = featurecode[i].getElementsByTagName("a");
			links[0].setAttribute("id", "featureb1");
			links[1].setAttribute("id", "failb1");
		}
		var article = featurecode[0].parentNode.getElementsByClassName("vfharticlelink")[0].getElementsByTagName("a")[0].innerHTML;
	}


	//Now add the function
	$("#featureb1").click(function(e) {

		//Prevent the link from opening
		e.preventDefault();

 		//Create popup to input image and article text
		var popup = document.createElement("div");
		popup.setAttribute("id", "vfhpopup");
		popup.setAttribute("style", "background: #cfc; border: 1px solid #090; padding: 1em; position: absolute; top: 0; margin: 20%; min-width: 50%");
		popup.innerHTML = "<p>Image name (without <code>File:</code> prefix):</p>"
		+ '<textarea id="ibox"></textarea>'
		+ "<p>First few (roughly 2-3) paragraphs of article; link its name at the beginning and remove any &lt;ref> tags:</p>"
		+ '<textarea id="abox" rows="10"></textarea>'
		+ "<p>Optional comment:</p>"
		+ '<textarea id="cbox"></textarea>'
		+ '<input id="featureb2" type="submit" value="Feature now">';
		document.getElementsByTagName("body")[0].appendChild(popup);

		$("#featureb2").click(function(e) {
			//Find popup
			var popup = document.getElementById("vfhpopup");

			//Retrieve input
			var itext = document.getElementById("ibox").value;
			var atext = document.getElementById("abox").value;
			var ctext = document.getElementById("cbox").value;

			//Text to append
			var append = "\n{{vfh featured\n|sig="
			+ (typeof(threetildes) == "undefined" ? "~~" + "~~" : "~~" + "~") //obfuscated because it automatically expands otherwise, even on a JS page
			+ "\n|stamp={{s" + "ubst:CURRENTDAY}}"
			+ " {{s" + "ubst:CURRENTMONTHNAME}}"
			+ " {{s" + "ubst:CURRENTYEAR}}\n|comment="
			+ ctext
			+ "\n}}";
			//Make API edit request to close nomination
			popup.innerHTML += "<p>Closing nomination as featured...</p>";
			var api = new mw.Api();
			api.post ({
				action: "edit",
				title: mw.config.get("wgPageName"),
				summary: "Closing nomination as featured ([[MediaWiki:Gadget-VFHcloser.js|VFHcloser]])",
				notminor: "1",
				appendtext: append,
				token: mw.user.tokens.get('csrfToken')
			}).done(function() {
				//popup.innerHTML += "<p>Nomination closed</p>";
			}).fail(function() {
				//popup.innerHTML += "<p>Something went wrong with closing the nomination</p>";
			});

			//find "recently featured"
			var api1 = new mw.Api();
			api1.get ({
				action: "query",
				prop: "revisions",
				titles: "Template:Featuredarticle",
				rvprop: "content",
				indexpageids: 1
			}).done(function(data) {
				var id = data.query.pageids;
				var oldtext = data.query.pages[id].revisions[0]["*"];
				//Get name, text and image of last featured article
				var lastart = oldtext.indexOf('article = ') + 10;
				var lastarticle = oldtext.slice(lastart, oldtext.indexOf('\n', lastart));
				var laistart = oldtext.indexOf("image = ") + 8;
				var laimage = oldtext.slice(laistart, oldtext.indexOf("\n", laistart));
				var latstart = oldtext.indexOf("text =") + 7;
				var latext = oldtext.slice(latstart, oldtext.indexOf("\n", latstart));
				latext = latext.trim();
				//Get names of recently featured articles
				var start = oldtext.indexOf('recently featured|') + 18;
				var start1 = oldtext.indexOf('|', start) + 1;
				var start2 = oldtext.indexOf('|', start1);
				var article1 = oldtext.slice(start, start1 - 1);
				var article2 = oldtext.slice(start1, start2);
				//Text to overwrite {Featuredarticle} with
				var text = "{{featuredarticle/framework"
				+ "\n |image = " + itext
				+ "\n |article = " + article
				+ "\n |text ="
				+ "\n" + atext
				+ "\n |rf = {{featuredarticle/recently featured|"
				+ lastarticle + "|" + article1 + "|" + article2 + "}}"
				+ "\n |image2 = " + laimage
				+ "\n |article2 = " + lastarticle
				+ "\n |text2 ="
				+ "\n" + latext
				+ "\n}}";
				//API edit request for {Featuredarticle}
				popup.innerHTML += "<p>Adding article content to main page...</p>";
				var api2 = new mw.Api();
				api2.post ({
					action: "edit",
					title: "Template:Featuredarticle",
					summary: "Featuring article ([[MediaWiki:Gadget-VFHcloser.js|VFHcloser]])",
					notminor: "1",
					text: text,
					token: mw.user.tokens.get('csrfToken')
				}).done(function() {
					//popup.innerHTML += "<p>Article content added</p>";
				});
			});
			//Remove VFH template from article and add FA
			popup.innerHTML += "<p>Adding feature template to article...</p>";
			var api3 = new mw.Api();
			api3.get ({
				action: "query",
				indexpageids: 1,
				prop: "revisions",
				rvprop: "content|ids",
				rvslots: "main",
				titles: article
			}).done(function(data) {
				var id = data.query.pageids;
				var revid = data.query.pages[id].revisions[0].revid;
				var oldtext = data.query.pages[id].revisions[0].slots.main["*"];
				var newtext = oldtext.replace(/{{VFH([^}]+)?}}/i, "");
				if (newtext.indexOf("\n") === 0) {
					newtext = newtext.replace("\n", "");
				}
				newtext += "\n{{F" + "A|date={{s" + "ubst:#time:d F Y|{{s"
				+ "ubst:CURRENTTIMESTAMP}}}}|revision=" + revid + "}}";
				var api4 = new mw.Api();
				api4.post ({
					action: "edit",
					title: article,
					summary: "Featuring article ([[MediaWiki:Gadget-VFHcloser.js|VFHcloser]])",
					notminor: "1",
					text: newtext,
					token: mw.user.tokens.get('csrfToken')
				}).done(function() {
					//popup.innerHTML += "<p>Feature template added</p>";
					//Semiprotect the article. Check if it's already protected first.
					popup.innerHTML += "<p>Protecting article...</p>";
					var api5 = new mw.Api();
					api5.get ({
						action: "query",
						prop: "info",
						inprop: "protection",
						titles: article,
						indexpageids: "1"
					}).done(function(data) {
						var id = data.query.pageids;
						if(data.query.pages[id].protection[0]) {
							popup.innerHTML += "<p>Article seems to be already protected</p>";
							//Reload page
							location.reload();
						} else {
							var api6 = new mw.Api();
							api6.post ({
								action: "protect",
								title: article,
								protections: "edit=autoconfirmed|move=autoconfirmed",
								reason: "Featured article",
								token: mw.user.tokens.get('csrfToken')
							}).done(function(data) {
								popup.innerHTML += "<p>Article is protected</p>";
								location.reload();
							});
						}
					});

				});
			});
		});
	});

//Same thing for failing

	//If we're on the nomination page...
	if (document.getElementById("buttons")) {

		//create and style fail button
		var button1 = document.createElement("input");
		fl.appendChild(button1);
		button1.setAttribute("type", "submit");
		button1.setAttribute("value", "Fail this article");
		button1.setAttribute("style", "background-color: #fcc; font-weight: bold; margin: .2em");
		button1.setAttribute("id", "failb1");
		var fl = document.getElementById("buttons");
		var article = fl.getAttribute("data-article");

	//If we're on VFHS...
	} else if (document.getElementsByClassName("featurecode")[0]) {
		//We already did the bulk of this earlier, so we don't need much here.
		var article = featurecode[0].parentNode.getElementsByClassName("vfharticlelink")[0].getElementsByTagName("a")[0].innerHTML;
	}

	$("#failb1").click(function(e) {

		//Prevent the link from opening
		e.preventDefault();

		//Create popup
		var popup = document.createElement("div");
		popup.setAttribute("id", "vfhpopup");
		popup.setAttribute("style", "background: #fcc; border: 1px solid #900; padding: 1em; position: absolute; top: 0; margin: 20%; min-width: 50%");
		popup.innerHTML = "<p>Optional comment:</p>"
		+ '<textarea id="cbox"></textarea>'
		+ '<input id="failb2" type="submit" value="Fail now">';
		document.getElementsByTagName("body")[0].appendChild(popup);
		$("#failb2").click(function(e) {
			//Find popup
			var popup = document.getElementById("vfhpopup");

			//Text to append
			var append = "\n{{vfh failed\n|sig="
			+ (typeof(threetildes) == "undefined" ? "~~" + "~~" : "~~" + "~")
			+ "\n|stamp={{s" + "ubst:CURRENTDAY}}"
			+ " {{s" + "ubst:CURRENTMONTHNAME}}"
			+ " {{s" + "ubst:CURRENTYEAR}}\n|comment="
			+ document.getElementById("cbox").value
			+ "\n}}";
			//Make API edit request to close nomination
			popup.innerHTML += "<p>Closing nomination as failed...</p>";
			var api7 = new mw.Api();
			api7.post ({
				action: "edit",
				title: mw.config.get("wgPageName"),
				summary: "Closing nomination as failed ([[MediaWiki:Gadget-VFHcloser.js|VFHcloser]])",
				notminor: "1",
				appendtext: append,
				token: mw.user.tokens.get('csrfToken')
			}).done(function() {
				//popup.innerHTML += "<p>Nomination closed</p>";
			}).fail(function() {
				//popup.innerHTML += "<p>Something went wrong with closing the nomination</p>";
			});
		
			//Remove VFH template from article
			popup.innerHTML += "<p>Removing VFH template from article...</p>";
			var api8 = new mw.Api();
			api8.get ({
				action: "query",
				indexpageids: 1,
				prop: "revisions",
				rvprop: "content",
				rvslots: "main",
				titles: article
			}).done(function(data) {
				var id = data.query.pageids;
				var oldtext = data.query.pages[id].revisions[0].slots.main["*"];
				var newtext = oldtext.replace(/{{VFH([^}]+)?}}/i, "");
				if (newtext.indexOf("\n") == 0) {
					newtext = newtext.replace("\n", "");
				}

			var api9 = new mw.Api();
				api9.post({
					action: "edit",
					title: article,
					summary: "Failing article ([[MediaWiki:Gadget-VFHcloser.js|VFHcloser]])",
					notminor: "1",
					text: newtext,
					token: mw.user.tokens.get('csrfToken')
				}).done(function() {
					//popup.innerHTML += "<p>VFH template removed</p>";

					//Reload page
					location.reload();
				}).fail(function() {
					//popup.innerHTML += "<p>Something went wrong with removing the VFH template</p>";
				});
			});
		});
	});
}
//</nowiki>