User:Hoo man/spi helper.js

/* SPI helper by User:Hoo_man revision 2; 2010-07-19

Should run in both monobook and vector using a current version of IE or Firefox

Attention: still needs testing!!

//config var loadMessage = 'Retrieving data...'; //message shown while data gets transfered to the server //error messages: var error = []; error['retrieve'] = 'Error: unable to retrieve source'; error['changePage'] = 'The following error occurred while updating the page: $1'; //other var changeWindow = []; changeWindow['title'] = 'Set the status of the SPI template to:'; changeWindow['button'] = 'Change Status'; var menuTitle = 'Change Status'; var editConflict = 'Edit conflict: Please reload the page and try again'; var editSummary = 'Changed Status to: $1';

//do not change things below here, unless you know what you are doing! var layout = []; var source = ''; var status = ''; var lastChange = ''; var template = ''; var newStatus = ''; var editToken = ''; if (window.XMLHttpRequest) { var http_request = new XMLHttpRequest; } else if (window.ActiveXObject) { var http_request = new ActiveXObject("Microsoft.XMLHTTP"); } window.onload = init; //main function, checks whether the page is a Sockpuppet page or not, and gets the raw code function init { //are we on a subpage of Wikipedia:Sockpuppet investigations? if(/Wikipedia:Sockpuppet_investigations\/.*/i.test(wgPageName) && wgAction == 'view') { //get the raw source var http = http_request; http.open("GET", mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + "&action=raw&oldid=" + wgCurRevisionId, true); http.send(null); http.onreadystatechange = function { if(http.readyState == 4) { if(http.status == 200) { // succes ;-)					mw.util.addPortletLink('p-cactions', 'javascript:openChangeStatusWindow;', menuTitle, 'ca-spiStatus');					source = http.responseText;					http = null;					getToken;					analyse;				}			}		}	} } //analyses the current template on the page function analyse {	//remove html comments to make sure we wont find template in there	tmp = source.replace(/<!(?:--[\s\S]*?--\s*)?>\s*/g, '');	//test for existing templates...	template = tmp.match(/\{\{SPI case status\|?(.*)\}\}/ig);	if(!template) {		//template is fully missing, we have to add it		template = 'no';	}else{		//there already is a template, which we can replace...		status = /\|(.*)}}/i.exec(template[0]);		oldTemplate = template[0];		template = 'yes';		if(!status[1]) {			//template exists, but without arguments			status = 'open';		}else{			status = status[1];		}	} } //opens the change status window function openChangeStatusWindow { if(!document.getElementById('statusWindow')) { layout['statusWindow'] = document.createElement('div'); layout['statusWindow'].id = 'statusWindow'; layout['statusWindow'].style.position = 'fixed'; layout['statusWindow'].style.left = '50%'; layout['statusWindow'].style.top = '150px'; layout['statusWindow'].style.zIndex = '2'; layout['statusWindow'].style.background = 'white'; layout['statusWindow'].style.width = '250px'; layout['statusWindow'].style.fontSize = '12px'; layout['statusWindow'].style.border = 'lightblue 2px solid'; var tmp = ' ' + changeWindow['title'] + '   ' layout['statusWindow'].innerHTML = tmp; document.getElementsByTagName('body')[0].appendChild(layout['statusWindow']); }else{ layout['statusWindow'].style.display = 'block'; } } //closes the change status window function closeChangeStatusWindow { layout['statusWindow'].style.display = 'none'; } //get called after a user clicked on Change Status... generates the new source code function prepare { //get choosen Status for (i=0; i<document.changeStatus.newStatus.length; i++) { if (document.changeStatus.newStatus[i].checked) { newStatus = document.changeStatus.newStatus[i].value; }	}	if(template == 'yes') { //there is already a template, which we just have to replace if(newStatus != 'open') { var newSource = source.replace(oldTemplate, '\{\{SPI case status|' + newStatus + '}}') }else{ var newSource = source.replace(oldTemplate, '\{\{SPI case status}}') }	}else{ //we add a new templte to the top of the page if(newStatus != 'open') { var newSource = '\{\{SPI case status|' + newStatus + '}}\n' + source; }else{ var newSource = '\{\{SPI case status}}\n' + source; }	}	applyChanges(newSource); } //applys the changes to the page function applyChanges(text) { in_process('start', loadMessage); var summary = editSummary.replace('$1', newStatus); //build the (post) request var params = 'action=edit&title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(text) + '&format=xml&minor=1&token=' + encodeURIComponent(editToken) + '&basetimestamp=' + lastChange; http_request.open("POST", mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php', false); http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); http_request.setRequestHeader("Content-length", params.length); http_request.setRequestHeader("Connection", "close"); //send it	http_request.send(params); if(http_request.readyState == 4) { if(http_request.status == 200) { // success!? in_process('stop'); response = http_request.responseXML; if(response.getElementsByTagName('edit')[0]) { //succes! var result = http_request.responseXML.getElementsByTagName('edit')[0].getAttribute('result'); }else{ //something went wrong :(				var result = http_request.responseXML.getElementsByTagName('error')[0].getAttribute('info');			}			if(result == "Success") {				//success, so we reload the page				window.location.href = window.location.href;			}else if(result == "Edit conflict detected") {				alert(editConflict);			}else{				alert(error['changePage'].replace('$1', result));			}		}else{			alert(error['retrieve']);		}	} } //gets the current edit Token and the timestamp of the last change and stores it into the global var editToken/ lastChange function getToken {	if(editToken == '') {		http_request.open("GET", mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&prop=info|revisions&intoken=edit&titles=' + encodeURIComponent(mw.config.get('wgPageName')) + '&format=xml', false);		http_request.send(null);		if(http_request.readyState == 4) {			if(http_request.status == 200) { editToken = http_request.responseXML.getElementsByTagName('page')[0].getAttribute('edittoken'); lastChange = http_request.responseXML.getElementsByTagName('rev')[0].getAttribute('timestamp'); }else{ alert(error['retrieve']); }		} 	} } //will show an animation while data gets send function in_process(startStop, message) { if(startStop == 'start') { if(!document.getElementById('in_process')) { layout['in_process'] = document.createElement('div'); layout['in_process'].id = 'in_process'; layout['in_process'].innerHTML = message; layout['in_process'].style.position = 'fixed'; layout['in_process'].style.left = '50%'; layout['in_process'].style.top = '300px'; layout['in_process'].style.border = '2px solid red'; layout['in_process'].style.background = 'white'; layout['in_process'].style.zIndex = '4'; layout['in_process'].style.width = '300px'; layout['in_process'].style.height = '75px'; layout['in_process'].style.fontSize = '15px'; document.getElementsByTagName('body')[0].appendChild(layout['in_process']); }else{ layout['in_process'].innerHTML = message; layout['in_process'].style.display = 'block'; }	}else{ layout['in_process'].style.display = 'none'; } }