User:EpochFail/nice util.js

UPDATETIMEOUT = 100; NOOBLIMIT = 100; REQUESTTIMEOUT = 10000;

REVERTED = new Object; REVERTED.revisionId = null; REVERTED.username  = null; REVERTED.userRevs  = null; REVERTED.timestamp = null;

INTERFACE = new Object; INTERFACE.warn = true; INTERFACE.box = true;

LOGGER = new Logger("http://www-users.cs.umn.edu/~halfak/wpInterfaceLogger.cgi")

String.prototype.trim = function { return this.replace(/^\s+|\s+$/g,""); }

function importCSS(page) { var uri = mw.config.get('wgScript') + '?title=' + encodeURIComponent(page.replace(/ /g,'_')).replace('%2F','/').replace('%3A',':') + '&action=raw&ctype=text/css'; cssLink = document.createElement("link"); cssLink.rel = "stylesheet"; cssLink.type = "text/css"; cssLink.href = uri document.getElementsByTagName("head")[0].appendChild(cssLink); return cssLink; }

function checkForPreviousEdits(result, args){ try{ res = eval("(" + result + ")"); }	catch(e){ LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Searching for username using revisionId", "Unable to process JSON result" + result) showErrorWhenReady("The Wikipedia server returned an invalid result while asking for user information."); }	page = null try{ for(thing in res.query.pages){ page = res.query.pages[thing] }		username = page.revisions[0].user }	catch(e){ LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Looking for username in result", "Invalid result structure: " + result) showErrorWhenReady("The Wikipedia server returned an invalid result while asking for user information."); }	REVERTED.username = username params = new Object; params.action = 'query' params.list = 'users' params.ususers = username params.usprop = 'editcount' params.format = 'json' WPAPIHandler.performGET(		params, 		processEditCountResult, 		{},		function(message){			LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, message)			showErrorWhenReady(message);		}	) }

function processEditCountResult(result, args){ try{ res = eval("(" + result + ")"); }	catch(e){ LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Searching for editcount using username", "Unable to process JSON result: " + result) showErrorWhenReady("The Wikipedia server returned an invalid result while asking for user information.") return }	try{ REVERTED.userRevs = res.query.users[0].editcount }	catch(e){ LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Looking for editcount in result", "Invalid result structure: " + result) showErrorWhenReady("The Wikipedia server returned an invalid result while asking for user information.") return } }

function getEditToken(result, args){ try{ res = eval("(" + result + ")"); }	catch(e){ LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Searching for edit token for user talk page", "Unable to process JSON result: " + result) if(confirm('Unable to post to ' + REVERTED.username + '\'s talk page. Continue with revert?')){ document.getElementById("editform").submit }		return }

page = null try{ for(thing in res.query.pages){ page = res.query.pages[thing] }		editToken = page.edittoken }	catch(e){ LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Looking for edit token in result", "Invalid result structure: " + result) if(confirm('Unable to post to ' + REVERTED.username + '\'s talk page. Continue with revert?')){ document.getElementById("editform").submit }		return }	params = new Object; params.action = 'edit' params.title = 'User_talk:' + args.username params.section = 'new' params.summary = args.header params.text = args.message params.token = editToken params.format = 'json' WPAPIHandler.performPOST(		params, 		function(result, args){			try{				res = eval("(" + result + ")");			}catch(e){				LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Posting to user's talk page", "Unable to process JSON result: " + result)				if(confirm('Unable to post to ' + REVERTED.username + '\'s talk page. Continue with revert?')){					LOGGER.revert( INTERFACE.warn, INTERFACE.box, REVERTED.username, REVERTED.userRevs, wgUserName, REVERTED.revisionId, null, null )					document.getElementById("editform").submit				}				return			}			try{				newRevId = null				oldRevId = null				newRevId = res.edit.newrevid,				oldRevId = res.edit.oldrevid			}catch(e){				LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Logging a successful revert", "Invalid result structure: " + result)			}			LOGGER.revert( INTERFACE.warn, INTERFACE.box, REVERTED.username, REVERTED.userRevs, wgUserName, REVERTED.revisionId, newRevId, oldRevId )			document.getElementById("editform").submit		}, 		{},		function(message){			LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Posting on user's talk page", message)			if(confirm('Unable to post to ' + REVERTED.username + '\'s talk page. Continue with revert?')){				document.getElementById("editform").submit			}		}	); }

function postMessageOnUserTalk(username, header, message){ params = new Object; params.action = 'query'; params.prop = 'info'; params.intoken = "edit"; params.titles = 'User_talk:' + username params.format = 'json'; args = new Object; args.username = username; args.header = header; args.message = message; WPAPIHandler.performGET(		params, 		getEditToken, 		args, 		function(message){			LOGGER.error(mw.config.get('wgUserName'), REVERTED.revisionId, "Asking for edit token", message)			if(confirm('Unable to post to ' + REVERTED.username + '\'s talk page. Continue with revert?')){				document.getElementById("editform").submit			}		}	); }

function saveAndPostMessage(e){ if(saveForm.messageBoxDisplayed && document.getElementById("niceExplaination").value.trim.length > 0){ username = REVERTED.username; header = document.getElementById("niceHeader").value; message = document.getElementById("niceExplaination").value; postMessageOnUserTalk(username, header, message); //Don't do anything ever. e.stopPropagation; e.preventDefault; e.returnValue = false; } }

function SaveForm(editOptions){ this.messageBoxDisplayed = false; this.editOptions = editOptions; this.errorMessage = document.createElement("div") errorHeader = document.createElement("h2") errorHeader.innerHTML = "The NO BITING interface failed to load." this.errorBody = document.createElement("div") this.errorMessage.appendChild(errorHeader) this.errorMessage.appendChild(this.errorBody) this.errorMessage.id = "errorMessage" this.warnDiv = document.createElement("div") this.warnDiv.id = "warning"; this.warnDiv.innerHTML = '' + ' \n' + '	 Note: You are reverting a new editor. Don&apos;t bite.\n' + ' \n' + ' \n' + '	Analysis suggests that reverting this editor is likely to \n' + '	discourage them from making future contributions.\n' + ' \n'; this.saveOptions = document.createElement("div"); this.saveOptions.id = "saveOptions"; this.normalSelector = document.createElement("a"); this.normalSelector.innerHTML = "Normal"; this.normalSelector.id = "normalSelector"; this.normalSelector.className="selected"; this.normalSelector.onclick = function(a){ saveForm.hideMessageBox }	this.saveOptions.appendChild(this.normalSelector); this.niceSelector = document.createElement("a"); this.niceSelector.innerHTML = "Be Very Nice"; this.niceSelector.id = "niceSelector"; this.niceSelector.className=""; this.niceSelector.onclick = function(a){ saveForm.showMessageBox }	this.saveOptions.appendChild(this.niceSelector); this.oldForm = document.createElement("div"); this.oldForm.id="oldForm"; this.messageForm = document.createElement("div"); this.messageForm.id = "messageForm"; this.messageForm.style.display="none"; this.messageForm.style.width="48%"; this.formMessage = document.createElement("p"); this.formMessage.style.display="none"; this.finalBreak = document.createElement("br"); this.finalBreak.style.clear="both"; }

SaveForm.prototype.showError = function(message){ this.errorBody.innerHTML = message; if(!this.errorMessage.parent){ //Load style importCSS(ACCOUNT_NAME + '/error_message.css') if(this.saveOptions.parentNode){ this.editOptions.parentNode.insertBefore(this.errorMessage, this.saveOptions) }			else{ this.editOptions.parentNode.insertBefore(this.errorMessage, this.editOptions) }		}	}	SaveForm.prototype.showWarning = function{ //Load style importCSS(ACCOUNT_NAME + '/warn.css'); this.copyWarn = document.getElementById("editpage-copywarn") this.copyWarn.style.display = "none"; if(this.errorMessage.parentNode){ this.editOptions.parentNode.insertBefore(this.warnDiv, this.errorMessage); }		else if(this.saveOptions.parentNode){ this.editOptions.parentNode.insertBefore(this.warnDiv, this.saveOptions); }		else{ this.editOptions.parentNode.insertBefore(this.warnDiv, this.editOptions); }	}	SaveForm.prototype.initMessageBox = function(revertedId, revertedUser){ //Load style importCSS(ACCOUNT_NAME + '/message_box.css'); this.editOptions.parentNode.insertBefore(this.saveOptions, this.editOptions); this.editOptions.appendChild(this.oldForm); this.oldForm.innerHTML = this.editOptions.innerHTML; this.editOptions.innerHTML = ""; this.formMessage.innerHTML= '' + 'The form below will allow you to both save your changes to ' + '' + mw.config.get('wgTitle') + ' and post a breif message to ' + '' + revertedUser + '&apos;s talk page explaining the revert. ' + 			'This is your opportunity to help a new user learn the ropes.' this.editOptions.appendChild(this.formMessage); this.messageForm.innerHTML = '' + ' ' + 		'	' + '		 ' + 		'			Message header' + '			 (This will be posted on ' + revertedUser + '&apos;s talk page) ' + '		 ' + 		'	 ' + 		' ' + 		' ' + 		'' + ' ' + 		' ' + 		'	' + '		 ' + 		'			Explaination' + '			 (Consider explaining the appropriate Wikipedia Policy) ' + '		 ' + 		'	 ' + 		' ' + 		' '; this.editOptions.appendChild(this.messageForm); this.editOptions.appendChild(this.oldForm); this.editOptions.appendChild(this.finalBreak); document.getElementById('editform').addEventListener("submit", saveAndPostMessage, false); }	SaveForm.prototype.showMessageBox = function{ if(this.messageBoxDisplayed){ return true }		this.normalSelector.className=""; this.niceSelector.className="selected"; this.oldForm.style.width="48%"; this.messageForm.style.display="block"; this.formMessage.style.display="block"; this.messageBoxDisplayed = true; }	SaveForm.prototype.hideMessageBox = function{ if(!this.messageBoxDisplayed){ return true; }		this.normalSelector.className     = "selected"; this.niceSelector.className       = ""; this.oldForm.style.width      = "96%"; this.messageForm.style.display = "none"; this.formMessage.style.display = "none"; this.messageBoxDisplayed = false; }

function Logger(url){ this.url = url; }

Logger.prototype.revert = function(warn, box, revertedUser, revertedUserRevs, revertingUser, revisionReverted, newTalkRevision, prevTalkRevision){ this.log(			{				"action":            "revert",				"timestamp":          REVERTED.timestamp,				"warn":               warn,				"box":                box,				"user_reverted":      revertedUser,				"user_reverted_revs": revertedUserRevs,				"user_reverting":     revertingUser,				"revision_reverted":  revisionReverted,				"new_talk_revision":  newTalkRevision,				"prev_talk_revision": prevTalkRevision			}		) }

Logger.prototype.undo = function(warn, box, userToBeReverted, userRevs, userReverting, revisionToBeReverted){ this.log(			{				"action":                  "undo",				"warn":                     warn,				"box":                      box,				"user_to_be_reverted":      userToBeReverted,				"user_to_be_reverted_revs": userRevs,				"user_reverting":           userReverting,				"revision_to_be_reverted":  revisionToBeReverted			}		) }

Logger.prototype.error = function(revertingUser, revisionToBeReverted, operation, message){ this.log(			{				"action":                  "error",				"timestamp":                REVERTED.timestamp,				"user_reverting":           revertingUser,				"revision_to_be_reverted":  revisionToBeReverted,				"operation":                operation,				"message":                  message			}		) }	Logger.prototype.log = function(obj){ if(obj.action){ queryString = "?logging=logging" for(key in obj){ queryString += "&" + key + "=" + this.escape(obj[key]) }			//alert("logging: " + queryString); this.sendRequest(queryString); }	}	Logger.prototype.sendRequest = function(queryString){ var tag = document.createElement("script"); tag.setAttribute("src", this.url + queryString); tag.setAttribute('type','text/javascript'); document.getElementsByTagName('head')[0].appendChild(tag); }	Logger.prototype.escape = function(thing){ if(thing == null){ return "/n" }		else{ return escape(thing) }	}

function createSaveFormWhenReady{ if(document.getElementById("editform")){ editOptions = document.getElementsByClassName("editOptions")[0]; editForm = document.getElementById("editform"); saveForm = new SaveForm(editOptions); updateSaveFormWhenReady }		else{ setTimeout('createSaveFormWhenReady', UPDATETIMEOUT); } }

function updateSaveFormWhenReady{ if(		REVERTED.username != null && 		REVERTED.revisionId != null && 		REVERTED.userRevs != null	) {		LOGGER.undo(			INTERFACE.box, 			INTERFACE.warn, 			REVERTED.username, 			REVERTED.userRevs, 			wgUserName, 			REVERTED.revisionId		) if(INTERFACE.warn && REVERTED.userRevs <= NOOBLIMIT){ saveForm.showWarning; saveForm.showMessageBox; }		if(INTERFACE.box){ saveForm.initMessageBox(REVERTED.revisionId, REVERTED.username) saveForm.showMessageBox; }	}	else{ setTimeout('updateSaveFormWhenReady', UPDATETIMEOUT); } }

function showErrorWhenReady(message){ if(window.saveForm){ saveForm.showError(message) }	else{ setTimeout('showErrorWhenReady("' + message + '")', UPDATETIMEOUT) } }