User:Writ Keeper/Scripts/massRevdel.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.hook("wikipage.content").add( function()
{
	mw.loader.using(["mediawiki.api"]).then(function ()
	{
		var apiLimit = 500;
		var api = new mw.Api();
		if(mw.config.get("wgCanonicalSpecialPageName") == "Contributions")
		{
			wkContribsCheckboxInit = true;
			$("ul.mw-contributions-list:first").before("<div style='display:inline-block;' id='revdelCP'><span id='revdelLabel' style='cursor:pointer;'>Revision deletion\
	<img id='revdelLabelImg' src='/w/load.php?modules=oojs-ui.styles.icons-movement&image=expand&format=rasterized&lang=en' style='width:12px; transform:rotate(270deg);'/></span>\
	<span id='revdelCPBody' style='display:none;'><input type='button' id='revdelSelectAll' value='Select all'><input type='button' id='revdelSelectNone' value='Select none'>\
	<input type='button' id='revdelSelectInv' value='Invert selection'>\
	<span style='white-space:nowrap;'><input type='checkbox' id='revdelContent' name='revdelOptions' value='content'> delete content</span> \
	<span style='white-space:nowrap;'><input type='checkbox' id='revdelName' name='revdelOptions' value='userName'> delete user name</span> \
	<span style='white-space:nowrap;'><input type='checkbox' id='revdelComment' name='revdelOptions' value='editSummary'> delete edit summary</span> \
	<span style='white-space:nowrap;'><input type='checkbox' id='undelContent' name='revdelOptions' value='content'> undelete content</span> \
	<span style='white-space:nowrap;'><input type='checkbox' id='undelName' name='revdelOptions' value='userName'> undelete user name</span> \
	<span style='white-space:nowrap;'><input type='checkbox' id='undelComment' name='revdelOptions' value='editSummary'> undelete edit summary</span> \
	<br/><select id='wpRevDeleteReasonList'><option value='other'>Other reason</option></select><input name='wpReason' size='60' id='wpReason' maxlength='100'>\
	<input type='button' class='revdelSubmit' id='revdelSubmit' value='Revdel selected entries'> \
	<input type='button' class='revdelSubmit' id='oversightSubmit' value='Oversight selected entries'></span></div>");
			$("ul.mw-contributions-list .mw-revdelundel-link").each(function(ind,el){
				if($(this).children("a").length > 0)
				{
					var revId = /ids=(\d+)/.exec($(this).children("a").attr("href"))[1];
					var pageTitle = /target=([^&]+)/.exec($(this).children("a").attr("href"))[1];
					el.innerHTML = "<input type='checkbox' name='"+decodeURIComponent(pageTitle)+"' class='revdelIds' value='"+revId+"'>";
					$(el).children(".revdelIds").data("index", ind);
				}
			});
			$("ul.mw-contributions-list").after("</form>");
			
			$("#revdelCP").data("revdelToolbarShowing", false);
			$("#revdelCP").data("lastSelectedIndex", -1);

			//load canned summaries
			$.get("/w/index.php?title=MediaWiki:Revdelete-reason-dropdown&action=raw",function(data)
			{
				reasons = data.replace(/\*\* ([^\*]+)/g, '<option value="$1">$1</option>');
				reasons = reasons.replace(/\* ([^<]+)([^\*]+)/g, '<optgroup label="$1">$2</optgroup>');
				$('#wpRevDeleteReasonList').append(reasons);
			}).done(function()
			{
				$.get("/w/index.php?title=MediaWiki:Revdelete-reason-dropdown-suppress&action=raw", function(data)
				{
					reasons = data.replace(/\*\* ([^\*]+)/g, '<option value="$1">$1</option>');
					reasons = reasons.replace(/\* ([^<]+)([^\*]+)/g, '<optgroup label="$1">$2</optgroup>');
					$('#wpRevDeleteReasonList').append(reasons);
				});
			});
			
			//attach handlers
			$("#revdelLabel").click(
				function()
				{
					if($("#revdelCP").data("revdelToolbarShowing"))
					{
						$("#revdelCP").data("revdelToolbarShowing", false);
						$("#revdelLabelImg").css("transform","rotate(270deg)");
						$("#revdelCPBody").fadeOut();
					}
					else
					{
						$("#revdelCP").data("revdelToolbarShowing", true);
						$("#revdelLabelImg").css("transform","none");
						$("#revdelCPBody").fadeIn();
					}
				}
			);
			$("#revdelSelectAll").click(
				function()
				{
					$('input.revdelIds').each(function()
					{
						$(this).prop("checked", true);
					});
					$("#revdelCP").data("lastSelectedIndex", -1);
				}
			);
			$("#revdelSelectNone").click(
				function()
				{
					$('input.revdelIds').each(function()
					{
						$(this).prop("checked", false);
					});
					$("#revdelCP").data("lastSelectedIndex", -1);
				}
			);

			$("#revdelSelectInv").click(
				function()
				{
					$('input.revdelIds').each(function()
					{
						$(this).prop("checked", !($(this).prop("checked")));
					});
					$("#revdelCP").data("lastSelectedIndex", -1);
				}
			);
			$("input.revdelIds").off("click").click(
				function(ev)
				{
					var lastSelectedRevdel = $("#revdelCP").data("lastSelectedIndex");
					var newIndex = $(this).data("index")
					if(ev.shiftKey && lastSelectedRevdel >= 0)
					{
						var checkboxArray = $("input.revdelIds");
						var start = lastSelectedRevdel;
						var stop = newIndex;
						if(start < stop)
						{
							for(var i = start; i < stop; i++)
							{
								if(i != lastSelectedRevdel)
								{
									$(checkboxArray[i]).prop("checked", !($(checkboxArray[i]).prop("checked")));
								}
							}
						}
						else
						{
							for(var i = start; i > stop; i--)
							{
								if(i != lastSelectedRevdel)
								{
									$(checkboxArray[i]).prop("checked", !($(checkboxArray[i]).prop("checked")));
								}
							}
						}
					}
					 $("#revdelCP").data("lastSelectedIndex", newIndex);
				}
			);
			$("#revdelSubmit").click(
				function()
				{
					//figure out which revisions and pages we're working on.
					var revCount = 0;
					var numTitles = 0;
					var pageTitles = {};
					$("input.revdelIds:checked").each(function(ind)
					{
						revCount = ind + 1;
						if(ind > apiLimit - 1)
						{
							mw.notify("You can't do more than " + apiLimit + " revdels at once! Canceling...");
							return false;
						}
						if(typeof pageTitles[$(this).attr("name")] == "undefined")
						{
							pageTitles[$(this).attr("name")] = $(this).val();
							numTitles++;
						}
						else
						{
							pageTitles[$(this).attr("name")] = pageTitles[$(this).attr("name")] + "|" + $(this).val();
						}
					});
					if(numTitles == 0)
					{
						mw.notify("You didn't select any revisions to delete!");
						return false;
					}
					var confirmString = "You are attempting to modify " + revCount + " revisions.\n\nThe following revision attributes will be changed:\n";
					
					//figure out what we're doing to each revision. This is pretty clunky, but whatever.
					var deleteString = "";
					var revealString = "";
					var typeString = "revdel";
					
					if($("#revdelContent").prop("checked") == $("#undelContent").prop("checked"))
					{
						confirmString = confirmString + "-Content visibility won't change.\n";
					}
					else if($("#revdelContent").prop("checked"))
					{
						deleteString = "content";
						confirmString = confirmString + "-Content will be deleted.\n";
					}
					else
					{
						revealString = "content";
						confirmString = confirmString + "-Content will be revealed.\n";
					}
					if($("#revdelComment").prop("checked") == $("#undelComment").prop("checked"))
					{
						confirmString = confirmString + "-Edit summary visibility won't change.\n";
					}
					else if($("#revdelComment").prop("checked"))
					{
						if(deleteString != "")
						{
							deleteString = deleteString + "|";
						}
						deleteString = deleteString + "comment";
						confirmString = confirmString + "-Edit summary will be deleted.\n";
					}
					else
					{
						if(revealString != "")
						{
							revealString = revealString + "|";
						}
						revealString = revealString + "comment";
						confirmString = confirmString + "-Edit summary will be revealed.\n";
					}
					if($("#revdelName").prop("checked") == $("#undelName").prop("checked"))
					{
						confirmString = confirmString + "-User name visibility won't change.\n";
					}
					else if($("#revdelName").prop("checked"))
					{
						if(deleteString != "")
						{
							deleteString = deleteString + "|";
						}
						deleteString = deleteString + "user";
						confirmString = confirmString + "-User name will be deleted.\n";
					}
					else
					{
						if(revealString != "")
						{
							revealString = revealString + "|";
						}
						revealString = revealString + "user";
						confirmString = confirmString + "-User name will be revealed.\n";
					}
					if(deleteString == "" && revealString == "")
					{
						mw.notify("You didn't select any properties of the revisions to change!");
						return false;
					}
					
					var summary = "";
					
					//construct the revdel summary
					if($("#wpRevDeleteReasonList").val() == "other")
					{
						if($("#wpReason").val() == "")
						{
							mw.notify("You didn't select or write in an edit summary for the logs!");
							return false;
						}
						summary = $("#wpReason").val();
					}
					else
					{
						summary = $("#wpRevDeleteReasonList").val();
						if($("#wpReason").val() != "")
						{
							summary = summary + ": " +  $("#wpReason").val();
						}
					}
					confirmString = confirmString + "\nYour revdel summary is: "+ summary +"\n\nAre you sure you want to do this?";
					
					if(confirm(confirmString))
					{
						var numCompleted = 0;
						for (title in pageTitles)
						{
							var ajaxData;
							ajaxData = {action:"revisiondelete",type:"revision",ids:pageTitles[title],reason:summary};
							if(deleteString != "")
							{
								ajaxData.hide=deleteString;
							}
							if(revealString != "")
							{
								ajaxData.show=revealString;
							}
							api.postWithToken('csrf', ajaxData).done(function()
							{
								numCompleted++;
								if(numCompleted == numTitles)
								{
									mw.notify("modified " + revCount + " revision(s) successfully!");
									return false;
								}
							});
						}
					}
					return false;
				}
			)
			$("#oversightSubmit").click(
				function()
				{
					//figure out which revisions and pages we're working on.
					var revCount = 0;
					var numTitles = 0;
					var pageTitles = {};
					$("input.revdelIds:checked").each(function(ind)
					{
						revCount = ind + 1;
						if(ind > apiLimit - 1)
						{
							mw.notify("You can't do more than " + apiLimit + " revdels at once! Canceling...");
							return false;
						}
						if(typeof pageTitles[$(this).attr("name")] == "undefined")
						{
							pageTitles[$(this).attr("name")] = $(this).val();
							numTitles++;
						}
						else
						{
							pageTitles[$(this).attr("name")] = pageTitles[$(this).attr("name")] + "|" + $(this).val();
						}
					});
					if(numTitles == 0)
					{
						mw.notify("You didn't select any revisions to delete!");
						return false;
					}
					var confirmString = "You are attempting to modify " + revCount + " revisions.\n\nThe following revision attributes will be changed:\n";
					
					//figure out what we're doing to each revision. This is pretty clunky, but whatever.
					var deleteString = "";
					var oversighting = -1;
					
					if($("#revdelContent").prop("checked") == $("#undelContent").prop("checked"))
					{
						confirmString = confirmString + "-Content visibility won't change.\n";
					}
					else if($("#revdelContent").prop("checked"))
					{
						deleteString = "content";
						confirmString = confirmString + "-Content will be oversighted.\n";
						oversighting = 1;
					}
					else
					{
						if(oversighting == 1)
						{
							mw.notify("we can't oversight and un-oversight edits at the same time!");
							return false;
						}
						deleteString = "content";
						confirmString = confirmString + "-Content will be returned to normal (admin-only) revision deletion.\n";
						oversighting = 0;
					}
					if($("#revdelComment").prop("checked") == $("#undelComment").prop("checked"))
					{
						confirmString = confirmString + "-Edit summary visibility won't change.\n";
					}
					else if($("#revdelComment").prop("checked"))
					{
						if(oversighting == 0)
						{
							mw.notify("we can't oversight and un-oversight edits at the same time!");
							return false;
						}
						if(deleteString != "")
						{
							deleteString = deleteString + "|";
						}
						deleteString = deleteString + "comment";
						confirmString = confirmString + "-Edit summary will be oversighted.\n";
						oversighting = 1;
					}
					else
					{
						if(oversighting == 1)
						{
							mw.notify("we can't oversight and un-oversight edits at the same time!");
							return false;
						}
						if(deleteString != "")
						{
							deleteString = deleteString + "|";
						}
						deleteString = deleteString + "comment";
						confirmString = confirmString + "-Edit summary will be returned to normal (admin-only) revision deletion.\n";
						oversighting = 0;
					}
					if($("#revdelName").prop("checked") == $("#undelName").prop("checked"))
					{
						confirmString = confirmString + "-User name visibility won't change.\n";
					}
					else if($("#revdelName").prop("checked"))
					{
						if(oversighting == 0)
						{
							mw.notify("we can't oversight and un-oversight edits at the same time!");
							return false;
						}
						if(deleteString != "")
						{
							deleteString = deleteString + "|";
						}
						deleteString = deleteString + "user";
						confirmString = confirmString + "-User name will be oversighted.\n";
						oversighting = 1;
					}
					else
					{
						if(oversighting == 1)
						{
							mw.notify("we can't oversight and un-oversight edits at the same time!");
							return false;
						}
						if(deleteString != "")
						{
							deleteString = deleteString + "|";
						}
						deleteString = deleteString + "user";
						confirmString = confirmString + "-User name will be returned to normal (admin-only) revision deletion.\n";
						oversighting = 0;
					}
					if(deleteString == "")
					{
						mw.notify("You didn't select any properties of the revisions to change!");
						return false;
					}
					
					var summary = "";
					
					//construct the revdel summary
					if($("#wpRevDeleteReasonList").val() == "other")
					{
						if($("#wpReason").val() == "")
						{
							mw.notify("You didn't select or write in an edit summary for the logs!");
							return false;
						}
						summary = $("#wpReason").val();
					}
					else
					{
						summary = $("#wpRevDeleteReasonList").val();
						if($("#wpReason").val() != "")
						{
							summary = summary + ": " +  $("#wpReason").val();
						}
					}
					confirmString = confirmString + "\nYour oversight summary is: "+ summary +"\n\nAre you sure you want to do this?";
					
					if(confirm(confirmString))
					{
						var numCompleted = 0;
						for (title in pageTitles)
						{
							var ajaxData;
							ajaxData = {action:"revisiondelete",type:"revision",ids:pageTitles[title],reason:summary,hide:deleteString};
							if(oversighting == 1)
							{
								ajaxData.suppress = "yes";
							}
							else if(oversighting == 0)
							{
								ajaxData.suppress = "no";
							}
							else
							{
								mw.notify("Something went wrong, so we're going to abort without doing anything.");
							}
							api.postWithToken('csrf', ajaxData).done(function()
							{
								numCompleted++;
								if(numCompleted == numTitles)
								{
									mw.notify("modified " + revCount + " revision(s) successfully!");
									return false;
								}
							});
						}
					}
					return false;
				}
			)
		}
	});
});