Jump to content

User:Ale jrb/Scripts/userhist.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ale jrb (talk | contribs) at 20:05, 8 September 2009 (change button name). 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 userHistMain() {
		var me = this;
		
		this.displayBox = function() {
			var box = document.createElement('input');
			box.setAttribute('id', 'userhist-isolate');
			box.setAttribute('type', 'text');
			box.setAttribute('width', '200px');
			
			var button = document.createElement('input');
			button.setAttribute('type', 'button');
			button.setAttribute('value', 'isolate user');
			
			if (button.addEventListener) { 
				button.addEventListener('click', function() { userHist.getUserHist(document.getElementById('userhist-isolate').value); }, false); 
			} 
			else 
			{ 
				button.attachEvent('onclick', function() { userHist.getUserHist(document.getElementById('userhist-isolate').value); }); 
			}
			
			
			var span = document.createElement('span');
			span.setAttribute('style','margin-left: 20px;');
			span.appendChild(box);
			span.appendChild(button);
			
			document.getElementById('mw-history-search').appendChild(span);
		}
		
		this.getUserHist = function(user) {
			var api = 'http://en.wikipedia.org/w/api.php';
			if (typeof user == 'undefined') return false;
			if (!user) return false;
			
			user = user.replace(/ /g, '_');
			user = user.replace(/User:/g, '');
			
			// remove useless interface
			var histPar = document.getElementById('mw-history-compare');
			histPar.innerHTML = 'loading edits...';
			
			var apiLink = '?action=query&format=xml&prop=revisions&titles='+wgPageName+'&rvprop=ids|timestamp|flags|comment|user|size&rvlimit=500&rvuser='+user+'';
			
			this.req 				= new wa_ajaxcall();
			this.req.requestUrl		= api + apiLink;
			this.req.get			(function() {
										userHist.data = userHist.req.response;
										userHist.showUserHist();
										return true;
									});
		}
		
		this.showUserHist = function() {
			var data = this.data;
			
			if (data.getElementsByTagName('rev').length <= 0) {
				this.showError('That user has not edited this page');
				return false;
			}
			
			// get output
			var output = [];
			for (var i = 0; i < data.getElementsByTagName('rev').length; i ++) {
				var dataset = data.getElementsByTagName('rev')[i];
				
				output[i] = [];
				output[i][0] = dataset.getAttribute('revid'); // oldid
				output[i][1] = dataset.getAttribute('user'); // user
				output[i][2] = dataset.getAttribute('timestamp'); // timestamp
				output[i][3] = dataset.getAttribute('comment'); // comment
				output[i][4] = dataset.getAttribute('size'); // size
				output[i][5] = dataset.getAttribute('minor'); // minor
			}
			
			// build our own interface
			var newInt = '<ul id="pagehistory">';
			var url = 'http://en.wikipedia.org/w/index.php?title='+wgPageName;
			for (var i = 0; i < output.length; i ++) {
				
				var timestamp = me.convertTimestamp(output[i][2]);
				var comment = me.parseComment(output[i][3]);
				if (output[i][5] != null) { var m = '<span class="minor">m</span> '; } else { var m = ''; }
				
				newInt = newInt + '<li class="">(<a href="'+url+'&oldid='+output[i][0]+'&diff=cur">cur</a>) (<a href="'+url+'&oldid='+output[i][0]+'&diff=prev">prev</a>) <span style="padding-left: 5px;"><a href="'+url+'&oldid='+output[i][0]+'">'+timestamp+'</a></span> <span class="history-user"><a href="/wiki/User:'+output[i][1]+'">'+output[i][1]+'</a></span> '+m+'<span class="history-size">('+output[i][4]+' bytes)</span> '+comment+'</li>';
			}
			newInt += '</ul>';
			
			var histPar = document.getElementById('mw-history-compare');
			histPar.innerHTML = newInt;
		}
		
		this.showError = function(error) {
			alert(error);
		}
		
		this.convertTimestamp = function(timestamp) {
			var regTest = /([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z/g;
			regTest.lastIndex = 0;
			time = regTest.exec(timestamp);
			if (time == null) return 'failed to parse timestamp';
			
			var d = new Date();
			var hourOffset = (d.getTimezoneOffset() / 60) * -1;
			var h = parseInt(time[4], 10) + hourOffset;
			if (h < 10) h = '0' + h;
			
			var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
			var month = parseInt(time[2], 10);
			
			var newStamp = h + ':' + time[5] + ', ' + time[3] + ' ' + months[month-1] + ' ' + time[1]; 
			
			return newStamp;
		}
		
		this.parseComment = function(comment) {
			if (comment == null) return '';
			
			comment = comment.replace('/*', '<span class="autocomment">→');
			comment = comment.replace('*/', '</span>');
			
			comment = comment.replace(/\[\[(.+?)(#.+?)?(?:\|(.+?))\]\]/g, "<a href=\"/wiki/$1$2\" title=\"$3\">$3</a>");
			comment = comment.replace(/\[\[(.+?)(#.+?)?\]\]/g, "<a href=\"/wiki/$1$2\" title=\"$1\">$1</a>");
			
			comment = '(<span class="comment">' + comment + '</span>)';
			
			return comment;
		}
		
		this.init = function() {
			if ( wgAction == 'history' ) {
				me.displayBox();
			}
		}
	}

	importScript('User:Ale_jrb/Scripts/waLib.js');
	var userHist = new userHistMain();
	hookEvent('load', userHist.init);