Jump to content

User:The Voidwalker/histFilter.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.
//<nowiki>

// While viewing the history of a page, one may now elect to only see revisions made by blocked users

if( mw.config.get('wgAction') === 'history' ) {
	mw.loader.using('mediawiki.util').then(function() {
		var button = document.createElement('input');
		button.setAttribute('type', 'button');
		button.setAttribute('value', 'Show blocked users only');
		button.setAttribute('style', 'margin-left: 4px;');
		button.setAttribute('id', 'history-blocked-only');
		button.addEventListener('click', generateHistory);
		$('.historysubmit.mw-history-compareselectedversions-button').after(button);
		$('#history-blocked-only').click(function(){ generateHistory(); });
	});
}

function getLimit() {
	if( !/&limit=\d+/.test(window.location) ) {
		return 50;
	}
	return /\d+/.exec(/&limit=\d+/.exec(window.location)[0])[0];
}

function generateHistory(c, lim) {
	var limit;
	if( !c ) {
		$('#pagehistory')[0].innerHTML = ''; //Poof, it's gone
		$('#pagehistory').append('<span id="temp-while-fetching">Getting relevant revisions</span>');
		limit = getLimit();
	} else {
		limit = lim;
	}
	var pageID = mw.config.get('wgArticleId');
	var query = {
		format: 'json',
		action: 'query',
		pageids: pageID,
		prop: 'revisions',
		rvprop: 'ids|timestamp|flags|parsedcomment|size|user',
		rvlimit: 500
	};
	if( c ) {
		query.continue = c.continue;
		query.rvcontinue = c.rvcontinue;
	}
	$.getJSON( mw.util.wikiScript('api'), query).done(function(data){
		var revisions = data.query.pages[pageID].revisions;
		var users = [];
		for(var i = 0; i < revisions.length; i++) {
			var revuser = revisions[i].user;
			if(revuser && !users.includes(revuser))
				users.push(revuser);
		}
		var usersfull = [];
		var count = 0;
		while( users.length !== 0 ) {
			usersfull[count] = users.slice(0,50);
			users = users.slice(50);
			count++;
		}
		var apireqs = 0;
		var blocked = [];
		for(i = 0; i < usersfull.length; i++) {
			getBlockSet(usersfull[i]);
			apireqs++;
		}
		function getBlockSet(userset) {
			$.getJSON( mw.util.wikiScript('api'), {
				format: 'json',
				action: 'query',
				list: 'blocks',
				bkusers: userset.join('|'),
				bklimit: 50 // Only returns active blocks on bkusers, with max bkusers as 50
			}).done(function(data){
				var blocks = data.query.blocks;
				for(var i = 0; i < blocks.length; i++) {
					blocked.push(blocks[i].user);
				}
				if( --apireqs === 0 ) // Last request
					showResults();
			});
		}
		function showResults() {
			var appended = 0;
			for(var i = 0; i < revisions.length && appended < limit; i++) {
				if(blocked.includes(revisions[i].user)) {
					appendRev(revisions[i]);
					appended++;
				}
			}
			if(appended < limit && data.continue) {
				generateHistory(data.continue, limit - appended);
			} else {
				$("#temp-while-fetching").remove();
			}
		}
	});
	function appendRev(revision) {
		var text = '<li>(<a href="/w/index.php?diff=' + revision.revid + '">prev</a>) <a href="/w/index.php?oldid=' + revision.revid +
					'">' + revision.timestamp + '</a>&lrm; <a href="/wiki/User:' + revision.user + '">' + revision.user +
					'</a> (<a href="/wiki/User_talk:' + revision.user + '">talk</a> | <a href="/wiki/Special:Contribs/' + revision.user +
					'">contribs</a>)&lrm; ' + (revision.minor !== undefined ? '<abbr class="minoredit" title="This is a minor edit">m</abbr>&lrm; ' : '') +
					' . . (' + revision.size + ' bytes)' + (!revision.parsedcomment || revision.parsedcomment.length === 0 ? '' : ' . . <i>(' + revision.parsedcomment + ')</i>' ) + '</li>';
		$('#pagehistory').append(text);
	}
}
//</nowiki>