Jump to content

User:Writ Keeper/Scripts/peerReviewCloser.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Writ Keeper (talk | contribs) at 03:45, 19 November 2018 (fx). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
function closePeerReview()
{
	mw.loader.using("mediawiki.api.edit").done( function () 
	{
		$("#ca-closepeer>a").text("closing...");
		var talkPage = $("a:contains('Article talk')").attr("href");
		var talkPageName = "";
		if(talkPage != null)
		{
			talkPage = talkPage.match(/\/wiki\/(\w+:\w+)/);
			if(talkPage != null)
			{
				talkPageName = talkPage[1];
			}
		}
		var api = new mw.Api();
		
		api.edit(talkPageName, function(revision) 
		{ 
			var regexResult = revision.content.match(/\{\Peer review(\|archive=\d)\}\}/);
			if(regexResult != null && typeof regexResult[1] != "undefined")
			{
				return {
					text: revision.content.replace("{{peer review" + regexResult[1] + "}}", "{{Old peer review" + regexResult[1] + "}}"),
					summary: "Closing peer review"
				};
			}
			else
			{
				alert("Error updating the article talk page.");
				return null;
			}
		}).done(function() 
		{
			api.edit(mw.config.get("wgPageName"), function(revision)
			{
				var regexResult = revision.content.match(/\{\{Peer review page\|topic=\w+\}\}/);
				if(regexResult != null)
				{
					return {
						text: revision.content.replace(/\{\{Peer review page\|topic=\w+\}\}/, "{{Ombox|text='''This [[WP:PR|peer review]] discussion has been closed.'''}} <noinclude>[[Category:November 2018 peer reviews]]</noinclude>"),
						summary: "Closing peer review"
					};
				}
				else
				{
					alert("Error updating the Peer Review page.");
					return null;
				}
			}).done(function()
			{
				location.reload();
			});
		});
	});
}



mw.hook("wikipage-content").add(function() 
{
	if(mw.config.get("wgCategories").indexOf("Current peer reviews") >= 0)
	{
		mw.loader.using("mediawiki.util").done( function ()
		{
			mw.util.addPortletLink('p-cactions', '#', "close peer review", "ca-closepeer", "close this peer review");
			$("#ca-closepeer").click( function(event)
			{
				event.preventDefault();
				mw.loader.load( [ 'mediawiki.api.edit' ] ); //start loading, while the user is in the prompt
				return closePeerReview();
			});
		});
	}
});