User:MGA73/movetocommonsreview.js

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.
//<source lang=javascript>
/**
 * MoveToCommonsReview tagger
 * URL: http://en.wikipedia.org/wiki/User:MGA73/movetocommonsreview.js
 * Credits: 
 * MGA73, ZooFari, Krinkle
 *
 * Based on http://commons.wikimedia.org/wiki/User:ZooFari/licensereviewer.js by Patstuart and 
 * http://en.wikipedia.org/wiki/User:MGA73/nowcommonsreview.js
 */
window.movetocommonsreview = "0.01 (2012-06-29)";

// MoveToCommons review passed
function MoveToCommonsReviewOK(autosave){
	var needAppend=document.editform.wpTextbox1.value;
	document.editform.wpTextbox1.value = needAppend.replace(/({{([Mm][Tt][Cc]|([Cc]opy|[Mm]ove) *to( *Wikimedia|) *(C|c)ommons)([^\}]*?))}}/g, '$1|human=' + mw.config.get('wgUserName') + '}}');

	document.editform.wpSummary.value = '[[Template:Move to Commons|Move to Commons review]] passed (using [[User:MGA73/movetocommonsreview.js|movetocommonsreviewer]])';
	document.editform.wpMinoredit.checked = true;

	if(autosave){ document.editform.submit(); }
}

// MoveToCommons review failed
function MoveToCommonsReviewNotOK(autosave){
	var needAppend=document.editform.wpTextbox1.value;

	var reason = window.prompt("Reason for failed MoveToCommons review:","");
	if (reason===null) {return;} /* on cancel button, actually cancel */
	document.editform.wpTextbox1.value = needAppend.replace(/({{([Mm][Tt][Cc]|([Cc]opy|[Mm]ove) *to( *Wikimedia|) *(C|c)ommons)([^\}]*?))}}/g, '$1|reason=' + reason + '}}').replace(/\{\{([Mm][Tt][Cc]|([Cc]opy|[Mm]ove) *to( *Wikimedia|) *(C|c)ommons)/g, '{{NotMovedToCommons');

	document.editform.wpSummary.value = '[[Template:Move to Commons|Move to Commons review]] failed (using [[User:MGA73/movetocommonsreview.js|movetocommonsreviewer]])';
	document.editform.wpMinoredit.checked = true;

	if(autosave){ document.editform.submit(); }
}

// [review] link inserter
$(function(){

	var targets = document.querySelectorAll('div.movetocommons-reviewme');
	if(targets.length === 0) return true; // MoveToCommons is not on this page, return the function.
	
	targets[0].style.textAlign = 'center'; // center the text

	var catstring = mw.config.get('wgCategories').join("|");

	if(catstring.match(/Wikipedia files reviewed on Wikimedia Commons/)){
		// This page is in a reviewed category. Dont display links.
		targets[0].innerHTML = '<small>Already reviewed</small>';
		return true; 

	} else { // This MoveToCommons page isn't reviewed yet. Show links.
		var passlink = mw.config.get('wgScript')+"?title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=edit&functionName=MoveToCommonsReviewOK";
		var faillink = mw.config.get('wgScript')+"?title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=edit&functionName=MoveToCommonsReviewNotOK";
	
		targets[0].innerHTML = '[<a href="'+passlink+'">pass review</a>] [<a href="'+faillink+'">fail review</a>]';
		return true;
	}	
});

// Link/Button maker
function addFunction(functionNameString, buttonDisplayName, buttonID){

	if (mw.util.getParamValue('functionName')==functionNameString){
		addOnloadHook(function(){
			eval(functionNameString+"(true)");
		});
	}

	var _href;
	if (mw.config.get('wgAction')=="edit"){ 
		_href = "javascript:"+functionNameString+"(true);";
	} else {
		_href = mw.config.get('wgScript')+"?title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=edit&functionName="+functionNameString;
	}

	// Add buttons
	addOnloadHook(function(){ mw.util.addPortletLink('p-cactions', _href, buttonDisplayName, 'ca-'+buttonID, '', null, 0); });
	addOnloadHook(function(){ mw.util.addPortletLink('p-tb', _href, buttonDisplayName, 'tb-'+buttonID, '', null, 0); });
}

// Fire it off:
addFunction("MoveToCommonsReviewOK", "Mtc OK", "movetocommonsreview");
addFunction("MoveToCommonsReviewNotOK", "Mtc Not OK", "movetocommonsreview");

//</source>