Jump to content

User:Writ Keeper/Scripts/userHistory.js

From Wikipedia, the free encyclopedia
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.
	if ( histlimit === undefined ) var histlimit = 40;
	if ( typeof histwidth === 'undefined' ) var histwidth = 200;
	
	diffRequestLocked = "f";
	
	function userHistMain() {
		var me = this;
		
		this.displayBox = function ( user ) {
			// if user is set, this name will automatically be displayed on load
			if ( user == null ) { user = ''; } else {
				user = user.replace ( /(_|%20)/gi, ' ' );
				user = user.replace ( /(%3A)/gi, ':' );
			}
			
			var box = document.createElement( 'input' );
			box.setAttribute ( 'id', 'userhist-isolate' );
			box.setAttribute ( 'type', 'text' );
			box.setAttribute ( 'value', user );
			box.setAttribute ( 'style', 'width: ' + histwidth + 'px' );
			
			var button = document.createElement( 'input' );
			button.setAttribute ( 'type', 'button' );
			button.setAttribute ( 'value', 'Isolate history' );
			button.setAttribute ( 'style', 'margin-left: 4px;' );
			
			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 = '/w/api.php';
			if ( typeof user == 'undefined' ) return false;
			if ( ! user ) return false
			
			// remove useless interface
			var histPar = document.getElementById ( 'mw-history-compare' );
			histPar.innerHTML = '<span style="padding: 4px;">isolating edits by <strong>' + user + '</strong> - please wait...</span>';
			
			var apiLink = '?action=query&format=xml&prop=revisions&titles='+encodeURIComponent(mw.config.get("wgPageName"))+'&rvprop=ids|timestamp|flags|comment|user|size&rvlimit=500&rvuser='+encodeURIComponent(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 never 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 = document.createElement("ul");
			newInt.id = "pagehistory";
			var url = '/w/index.php?title='+mw.config.get("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 = ''; }
				
				newEntry = document.createElement("li");
				newEntry.className='""';
				newEntry.id = "rev" + output[i][0];
				newEntry.innerHTML = '(<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+' ';
				
				         if(typeof inlineDiffSmallUI != "undefined")
                                        { 
                                              inlineDiffButton = document.createElement("a");
					      inlineDiffButton.href = "#";
					      inlineDiffButton.innerHTML = '<b><span style="color:black;">[</span><span style="color:MidnightBlue;">inspect diff</span><span style="color:black;">]</span></b>';
                                        }
                                        else
                                        {
                                              inlineDiffButton = document.createElement("input");
					      inlineDiffButton.type = "button";
					      inlineDiffButton.value = "Inspect edit";
                                        }
				inlineDiffButton.id = output[i][0];
				$(inlineDiffButton).click(function(){ return userHist.inspectDiff(this);});
				newEntry.appendChild(inlineDiffButton);
				newInt.appendChild(newEntry);
			}
			
			var histPar = document.getElementById( 'mw-history-compare' );
			histPar.innerHTML = "";
			histPar.appendChild(newInt);
                        mw.loader.load('mediawiki.action.history.diff')
		}
		
		this.inspectDiff = function( button)
		{
			if(diffRequestLocked === "t")
			{
				alert("An old request is still being processed, please wait...");
				return;
			}
			else
			{
				diffRequestLocked = "t";
				
				$.getJSON("/w/api.php?action=query&prop=revisions&format=json&rvprop=timestamp&revids="+button.id+"&rvdiffto=prev", function(response, status){

				var diffString = response.query.pages[mw.config.get("wgArticleId")].revisions[0].diff["*"];
				
                                if(diffString == null)
                                {
                                     alert("Request failed!");
                                     diffRequestLocked = "f";
                                     return;
                                }

				var newTable = document.createElement("table");
                                newTable.className = "diff";
                                
                                var colGroup = document.createElement("colgroup");
                                var diffCol = document.createElement("col");
                                diffCol.className = "diff-marker";
                                colGroup.appendChild(diffCol);
                                diffCol = document.createElement("col");
                                diffCol.className = "diff-content";
                                colGroup.appendChild(diffCol);
                                diffCol = document.createElement("col");
                                diffCol.className = "diff-marker";
                                colGroup.appendChild(diffCol);
                                diffCol = document.createElement("col");
                                diffCol.className = "diff-content";
                                colGroup.appendChild(diffCol);
                                newTable.appendChild(colGroup);

				$(newTable).append(diffString);
				$(newTable).insertAfter("#"+ button.id);
                                newTable.id = button.id + "display";

                                $(button).unbind("click");
                                if(typeof inlineDiffSmallUI != "undefined")
                                {
				    $(button).html('<b><span style="color:black;">[</span><span style="color:MidnightBlue;">hide diff</span><span style="color:black;">]</span></b>');
				    $(button).click(function(){ return userHist.hideSmallEditInspection(this);});
                                }
                                else
                                {
                                    $(button).attr("value","Hide edit");
                                    $(button).click(function(){ return userHist.hideEdit(this);});
                                }

                                //$(button).attr("disabled", "disabled");
                                //$(button).click(function(){return false;});

				diffRequestLocked = "f";
                                });
                                
			}
                        return false;
		}
		
                this.showEdit = function( button)
                {
                    $("#"+button.id+"display").css("display", "");
                    $(button).attr("value","Hide edit");
                    $(button).unbind("click");
                    $(button).click(function(){ userHist.hideEdit(this);});
                }

                this.hideEdit = function( button)
                {
                    $("#"+button.id+"display").css("display", "none");
                    $(button).attr("value","Show edit");
                    $(button).unbind("click");
                    $(button).click(function(){ userHist.showEdit(this);});
                }
		this.showSmallEditInspection = function( button)
        	{
       	        	$("#"+button.id+"display").css("display", "");
			$(button).html('<b><span style="color:black;">[</span><span style="color:MidnightBlue;">hide diff</span><span style="color:black;">]</span></b>');
			$(button).unbind("click");
			$(button).click(function(){ return userHist.hideSmallEditInspection(this);});
               	 	return false;
      	  	}
 
        	this.hideSmallEditInspection = function( button)
        	{
			$("#"+button.id+"display").css("display", "none");
			$(button).html('<b><span style="color:black;">[</span><span style="color:MidnightBlue;">show diff</span><span style="color:black;">]</span></b>');
			$(button).unbind("click");
			$(button).click(function(){ return userHist.showSmallEditInspection(this);});
               	 	return false;
        	}
 
		this.showError = function( errorMessage ) {
			var container = document.getElementById( 'mw-history-compare' );
			container.innerHTML = '<span style="padding: 4px; color: #885555; font-weight: bold;">userhist error: ' + errorMessage + '</span>';
			
			return true;
		}
		
		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.manageSize = function () {
			var regTest = /class="history-size">\(([,0-9]+?) +?bytes\)<\/span>/ig, regMatch;
			var col = { 'add': '#006400', 'remove': '#8b0000' }, results = [], i = 0;
			
			while ( regMatch = regTest.exec ( document.getElementById ( 'pagehistory' ).innerHTML ) ) {
				results [ i ++ ] = parseInt ( regMatch [1].replace ( ',', '' ), 10 );
				
				if ( i > histlimit ) break;
			}
			
			for ( var i = 0, l = results.length; i < ( l - 1 ); i ++ ) {
				var addition = results [i] - results [i + 1];
				if ( addition === 0 ) { var rep = '<span style="color: #555555; ">0</span>'; } else
				if ( addition < 0 ) { var rep = '<span style="color: ' + col ['remove'] + '; font-weight: bold;">' + addition + '</span>'; } else { var rep = '<span style="color: ' + col ['add'] + '; font-weight: bold;">+' + addition + '</span>'; }
				
				document.getElementById ( 'pagehistory' ).innerHTML = document.getElementById ( 'pagehistory' ).innerHTML.replace ( /class="history-size">\(([,0-9]+?) +?bytes\)<\/span>/i, "class=\"historysize\">(" + rep + ", $1 bytes)</span>" );
			}
		}
		
		this.init = function () {
			if ( mw.config.get("wgAction") == 'history' ) {
				if ( ( window.location.href.indexOf ( '&isolate=' ) > -1 ) && ( window.location.href.indexOf ( '&offset=' ) == -1 ) && ( window.location.href.indexOf ( '&limit=' ) == -1 ) ) {
					var user = window.location.href.substr ( window.location.href.indexOf( '&isolate=' ) + 9 );
					me.getUserHist ( user );
				} else {
					//me.manageSize (); - FIXME: prevents the history diff selectors from working correctly
					var user = '';
				}
				
				me.displayBox ( user );
			}
			
			else if ( ( mw.config.get("wgAction") == 'view' ) && ( mw.config.get("wgPageName") == 'Special:Contributions' ) ) {
				var regContent = document.getElementById ( 'contentSub' ).innerHTML;
				var regTest = /for .*?(?:title="(.+?)">|([\.0-9]+?) \()/i;
				var regMatch = regTest.exec ( regContent );
				if ( regMatch[1] === undefined ) {
					var user = regMatch[2];
				} else {
					var user = regMatch[1];
				}
				
				var bodyContent = document.getElementById ( 'bodyContent' );
				regTest.lastIndex = 0;
				regTest = /(title="([^<]+?)">hist<\/a>)\)/ig;
				bodyContent.innerHTML = bodyContent.innerHTML.replace ( regTest, "$1 | <a href=\"/w/index.php?title=$2&action=history&isolate=" + user + "\">all</a>\)" );
			}
		}
	}

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