User:Krinkle/Scripts/NowCommonsReview.js

Note: After publishing, 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: Press Ctrl-F5.
//<source lang=javascript>
/**
 * NowCommonsReview tagger
 * URL: meta.wikimedia.org/wiki/User:Krinkle/Scripts/NowCommonsReview
 * Credits: [[User:MGA73]], [[User:ZooFari]], [[User:Krinkle]]
 *
 * Based on commons.wikimedia.org/wiki/User:ZooFari/licensereviewer.js by [[commons:User:Patstuart]]
 *
 * Options:
 * - Autosave (enabled by default)
 *   You can disable the automatic saving of the edits by putting <code>window.nowcommonsreviewOptAutosave=false;</code> in [[Special:Mypage/vector.js|User:You/vector.js]] *ABOVE* the importScript()
 * 
 * Calls:
 * - Versionnumber
 *   Visit javascript:alert(nowcommonsreview.version); in your addressbar to find out what version your browser has cached.
     To check if you're up to date, purge this .js page and look at the below <code>version = "...."</code>.
     If that number doesn't match the the one popping up in the javascript:alert() then [[MediaWiki:Clearyourcache|you've got an outdated cache!]]
 */

// Settings
window.nowcommonsreview = {};
nowcommonsreview.version = "0.25 (2010-07-20)";
if(window.nowcommonsreviewOptAutosave !== false){ nowcommonsreview.autosave = true; } else { nowcommonsreview.autosave = false; }

// Get value from a URL parameter
function getURLParamValue(paramName, url){
 if (typeof (url) == 'undefined'  || url === null) url = document.location.href;
 var cmdRe=RegExp( '[&?]' + paramName + '=([^&#]*)' ); // Stop at hash
 var m=cmdRe.exec(url);
 if (m && m.length > 1) return decodeURIComponent(m[1]);
 return null;
}

// NowCommons review passed
function NowCommonsReviewOK(){
	var needAppend=document.editform.wpTextbox1.value;
	document.editform.wpTextbox1.value = needAppend.replace(/({{(N|n|db-n|Db-n)ow(C|c)ommons([^\}]*?))}}/g, '$1|reviewer=' + wgUserName + '}}');

	document.editform.wpSummary.value = '[[Template:NowCommons|NowCommons]] reviewed passed (using [[m:User:Krinkle/Scripts/NowCommonsReview|NowCommonsReview.js]])';
	document.editform.wpMinoredit.checked = true;

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

// NowCommons review failed
function NowCommonsReviewNotOK(){
	var needAppend=document.editform.wpTextbox1.value;

	var reason = window.prompt("Reason for failed NowCommons review:","");
	document.editform.wpTextbox1.value = needAppend.replace(/({{(N|n|db-n|Db-n)ow(C|c)ommons([^}]*?))}}/g, '$1|reason=' + reason + '}}').replace(/\{\{(N|n|db-n|Db-n)ow(C|c)ommons/g, '{{NotMovedToCommons');

	document.editform.wpSummary.value = '[[Template:NowCommons|NowCommons]] reviewed failed (using [[m:User:Krinkle/Scripts/NowCommonsReview|NowCommonsReview.js]])';
	document.editform.wpMinoredit.checked = true;

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

// [review] link inserter
$(function(){ if(wgNamespaceNumber == 6){

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

	var catstring = wgCategories.join("|");

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

	} else { // This NowCommons page isn't reviewed yet. Show links.
		var passlink = wgScript+"?title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=edit&functionName=NowCommonsReviewOK";
		var faillink = wgScript+"?title="+encodeURIComponent(mw.config.get('wgPageName'))+"&action=edit&functionName=NowCommonsReviewNotOK";
	
		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 (getURLParamValue('functionName')==functionNameString){
		addOnloadHook(function(){
			eval(functionNameString+"(true)");
		});
	}

	var _href;
	if (wgAction=="edit"){ 
		_href = "javascript:"+functionNameString+"(true);";
	} else {
		_href = 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:
if(wgNamespaceNumber == 6){
	addFunction("NowCommonsReviewOK", "NowCommons OK", "nowcommonsreview");
	addFunction("NowCommonsReviewNotOK", "NowCommons Not OK", "nowcommonsreview");
}

//</source>