MediaWiki:Gadget-show-activity.js

Fra Wikipedia, den frie encyklopedi

Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.

  • Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
  • Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
  • Internet Explorer / Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
  • Opera: Ttrykk Ctrl+F5.
//
// This script is used by Gadget-show-patrol-activity ([[MediaWiki:Gadget-show-patrol-activity.js]]) and
// Gadget-show-sysop-activity ([[MediaWiki:Gadget-show-sysop-activity.js]]).
// Check the history of those scripts for the complete history.
//
// This is a big nasty javascript which does a *lot* of callbacks. It will do 12*numberofsysops calls...
//
// By [[:no:Bruker:Stigmj]] - 2008
// Contribution by [[:m:b:pt:User:Helder.wiki]] - 2012 (http://no.wikipedia.org/w/index.php?title=MediaWiki:Gadget-show-sysop-activity.js&oldid=8758912&diff=10106625)
//
//
Date.prototype.setISO8601 = function (string) {
	var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
	             "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\\.([0-9]+))?)?" +
	             "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
	var d = string.match(new RegExp(regexp));
 
	var offset = 0;
	var date = new Date(d[1], 0, 1);
 
	if (d[3]) { date.setMonth(d[3] - 1); }
	if (d[5]) { date.setDate(d[5]); }
	if (d[7]) { date.setHours(d[7]); }
	if (d[8]) { date.setMinutes(d[8]); }
	if (d[10]) { date.setSeconds(d[10]); }
	if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
	if (d[14]) {
		offset = (Number(d[16]) * 60) + Number(d[17]);
		offset *= ((d[15] == '-') ? 1 : -1);
	}
 
	offset -= date.getTimezoneOffset();
	time = (Number(date) + (offset * 60 * 1000));
	this.setTime(Number(time));
};

function getList(divName, userGroup, pageName, processCallback) {
	var numa = mw.util.$content.find( divName );
	if (numa.length > 0) {
		for(var i=0;i<numa.length;i++) {
			var username = _getText(numa[i]);
			numa[i].style.display = 'block';
			_remText(numa[i]);
			var userlist = username.split('|');
			var querystring = {
				query: {
					allusers: []
				},
				special: {
					search: {
						name: username
					}
				}
			};
			for(var j=0;j<userlist.length;j++) {
				uname = userlist[j].replace(/\s*(=|\(.*?\))\s*/g, '');
				querystring.query.allusers.push({ name: uname });
			}
			processCallback(querystring);
		}
	}
	if ( mw.config.get( 'wgPageName' ) == pageName ) {
		$.getJSON( mw.util.wikiScript( 'api' ), {
			action:'query',
			format:'json',
			list:'allusers',
			augroup: userGroup,
			aulimit:'500'
 
		}, processCallback );
	}
}

function _getText(obj) {
	if(obj.nodeType == 3) {
		return obj.nodeValue;
	}
	var txt = [];
	var i = 0;
	while(obj.childNodes[i]) {
		txt[txt.length] = _getText(obj.childNodes[i]);
		i++;
	}
	return txt.join('');
}

function _remText(obj) {
	while(obj.firstChild) {
		obj.removeChild(obj.firstChild);
	}
}

function processAll(header, types, itemlist, docobj, processUser) {
	// Get the bodyContent-container to construct the table
	var tc, i;
	// Create a table object
	var table = document.createElement('table');
	table.setAttribute('class', 'wikitable');
	var thead = document.createElement('thead');
	var tfoot = document.createElement('tfoot');
	var tbody = document.createElement('tbody');
	var hr = document.createElement('tr');
	for (i=0; i<header.length; i++) {
		tc = document.createElement('th');
		tc.appendChild(document.createTextNode(header[i]));
		hr.appendChild(tc);
	}
	thead.appendChild(hr);
	table.appendChild(thead);
	hr = document.createElement('tr');
	tc = document.createElement('td');
	tc.setAttribute('style', 'font-size:x-small');
	tc.appendChild(document.createTextNode('Alle kolonner viser tidspunktet for siste utførelse av handlingen'));
	tc.setAttribute('colSpan', header.length);
	hr.appendChild(tc);
	tfoot.appendChild(hr);
	table.appendChild(tfoot);
	table.appendChild(tbody);
	docobj.appendChild(table);
	for(i=0;i<itemlist.length;i++) {
		hr = document.createElement('tr');
		for (var j=0; j<types.length; j++) {
			tc = document.createElement('td');
			tc.setAttribute('id', 'td-'+types[j]+'-' + itemlist[i].name);
			tc.style.backgroundColor = '#cccccc';
			tc.appendChild(document.createTextNode((j===0?itemlist[i].name:'n/a')));
			hr.appendChild(tc);
		}
		tbody.appendChild(hr);
		processUser(itemlist[i].name);
	}
}

function setCell(obj, otype) {
	// find the cell and write the value
	// Let's get the username
	if (obj.special) {
		return;
	}
	var name = obj.user;
	// and the timestamp
	var ts = obj.timestamp;
	// let's find the table-cell
	var tc = document.getElementById('td-last' + otype + '-' + name);
	if (!tc) {
		return;
	}
	// now calculate the color of the cell
	var tsDate = new Date();
	tsDate.setISO8601(ts);
	var date = new Date();
	var days = (date - tsDate) / 86400000;
	var a = document.createElement('a');
	if ($.inArray( otype, [ 'edit' , '50edit' , 'mwedit' ]) > -1) {
		a.setAttribute('href', mw.util.getUrl( 'Special:Contributions' ) + '?&contribs=user&limit=50&target='+ name + (otype == 'mwedit'?'&namespace=8':''));
	} else {
		a.setAttribute('href', mw.util.getUrl( 'Special:Log' ) + '?&type='+otype+'&user='+name);
	}
	if (days < 1) {
		a.appendChild(document.createTextNode('i dag'));
	} else if (days < 2) {
		a.appendChild(document.createTextNode('i går'));
	} else {
		a.appendChild(document.createTextNode(Math.ceil(days) + ' dager siden'));
	}
	tc.replaceChild(a,tc.firstChild);
	date.setDate(date.getDate()-180);
	if (tsDate>date) {
		tc.style.backgroundColor = '#00ff33';
	} else {
		date.setDate(date.getDate()-180);
		if (tsDate>date) {
			tc.style.backgroundColor = '#00ccff';
		} else {
			tc.style.backgroundColor = '#ff3300';
		}
	}
}

function processPatrolAction(obj) {
	if (obj) {
		var len = obj.length;
		for (var i=0; i<obj.length; i++) {
			if (obj[i].params.auto === undefined && obj[i].action === 'patrol') {
				setCell(obj[i], obj[i].type);
				return 1;
			}
		}
	}
	return null;
}

function patrol_handler(obj) {
	var r = obj;
	if (r.query) {
		var ret = processPatrolAction(r.query.logevents);
		if (!ret && r.continue) {
			// Put in a safeguard. Anything over 1 year is quite enough
			var tsDate = new Date();
			tsDate.setISO8601(r.query.logevents[r.query.logevents.length - 1].timestamp);
			var date = new Date();
			date.setDate(date.getDate()-365);

			if (tsDate>date) {
				$.getJSON( mw.util.wikiScript( 'api' ), { action:'query', format:'json', list:'logevents', continue: r.continue.continue, lecontinue: r.continue.lecontinue, lelimit:500, leuser: r.query.logevents[0].user, letype: 'patrol' }, patrol_handler );
			} else {
				var tc = document.getElementById('td-lastpatrol-' + r.query.logevents[0].user);	
				if (!tc) {
					return;
				}
				tc.innerHTML = '>1år til ∞';
				tc.style.backgroundColor = '#ff3300';
			}
		}
	}
}

function getLastAction(obj) {
	// Did we get something?
	if (obj.query && obj.query.logevents[0]) {
		setCell(obj.query.logevents[0], obj.query.logevents[0].type);
	}
}
 
function getLastMWedit(obj) {
	// Did we get something?
	if (obj.query && obj.query.usercontribs[0]) {
		setCell(obj.query.usercontribs[0], 'mwedit');
	}
}

function getLastEdit(obj) {
	// Did we get something?
	if (obj.query && obj.query.usercontribs[0]) {
		setCell(obj.query.usercontribs[0], 'edit');
	}
}
 
function getLast50Edit(obj) {
	// Did we get something?
	if (obj.query && obj.query.usercontribs[0]) {
		var len = obj.query.usercontribs.length;
		if (len == 50) {
			setCell(obj.query.usercontribs[len-1], '50edit');
		} else {
			setCell( {
				special: {
					nothing: {
						really: 'nothing'
					}
				}
			}, '50edit');
		}
	}
}

function getUserinfo(obj) {
	var txt, tc;
	// Did we get something?
	if (obj.query && obj.query.users[0]) {
		// find the cell and write the value
		// Let's get the username
		var name = obj.query.users[0].name;
		// let's find the table-cell for emailable
		tc = document.getElementById('td-emailable-' + name);
		if (!tc) {
			return;
		}
		if (typeof obj.query.users[0].emailable != 'undefined') {
			// Let's get the emailable-info
			var emailable = obj.query.users[0].emailable;
			var a = document.createElement('a');
			a.setAttribute('href', mw.util.getUrl( 'Special:EmailUser/' + name ) );
			a.appendChild(document.createTextNode('Ja'));
			tc.replaceChild(a,tc.firstChild);
			tc.style.backgroundColor = '#00ff33';
		} else if (typeof obj.warnings == 'undefined') {
			txt = document.createTextNode('Nei');
			tc.replaceChild(txt,tc.firstChild);
			tc.style.backgroundColor = '#ff3300';
		}
		if (obj.query.users[0].editcount) {
			// Let's get the editcount-info
			var editcount = obj.query.users[0].editcount;
			// let's find the table-cell for editcount
			tc = document.getElementById('td-editcount-' +name);
			if (!tc) {
				return;
			}
			txt = document.createTextNode(editcount);
			tc.replaceChild(txt,tc.firstChild);
			tc.style.backgroundColor = '#00ff33';
		}
	}
}