Jump to content

User:2aprilboy/historyReviewer.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.
// only for firefox for now.. :P

var hr_debugInfo = ''	// type  javascript:alert(hr_debugInfo);  in adressbar to see debug info for current article
var hr_status = []	// class = 'hr_' + hr_status
var hr_limit = 5	// limit of edit count to work with
var hr_recentEditCriterium = 7*24*60*60*1000	// time difference in miliseconds (7 days)
var hr_editWarCriterium = 30*60*1000		// time difference in miliseconds ()
var hr_questionableEditorCriterium = 5		// minimal # of contributions for user not to be considered questionableEditor
var hr_userInfoLoaded = 0

hr_debugInfo += 'hr_limit = ' + hr_limit+ '\n'
hr_debugInfo += 'hr_recentEditCriterium  = ' + hr_recentEditCriterium /1000 + ' s\n'
hr_debugInfo += 'hr_editWarCriterium = ' + hr_editWarCriterium/1000 + ' s\n'
hr_debugInfo += 'hr_questionableEditorCriterium = ' + hr_questionableEditorCriterium + '\n\n'

/*called using Wikipedia API (JSON format), example:
hr_getUserInfo(	
{"query":
 {"users":
  [{"name":"Ash","editcount":16565,"registration":"2006-09-26T12:44:06Z","groups":["rollbacker"]}
]}})*/
function hr_getUserInfo(data){
	last_user = data['query']['users'][0]
	hr_debugInfo += 'Last edit by ' + last_user['name'] + ' (' + last_user['editcount'] + ' contributions)\n'
	if(last_user['invalid'] == "" || last_user['editcount'] < hr_questionableEditorCriterium){
		hr_status.push('questionableEditor')
	}
	hr_userInfoLoaded = 1
}

// Wikipedia API call to function hr_getUserInfo()
function hr_reviewUsers(users){
	var api = document.createElement('script')
	var url = 'http://en.wikipedia.org/w/api.php?action=query&format=json&callback=hr_getUserInfo&list=users&usprop=blockinfo|groups|editcount|registration&ususers='+users
	api.setAttribute('src', url)
	api.setAttribute('type','text/javascript')
	document.getElementsByTagName('head')[0].appendChild(api)
}

/*called using Wikipedia API (JSON format), exapmle:
hr_getInfo(
{'query':
 {'pages':
  {'25959529':{'pageid':25959529,'ns':2,'title':'User:2aprilboy\/historyReviewer.js','revisions':[{'user':'2aprilboy','timestamp':'2010-01-26T21:34:17Z'},{'user':'2aprilboy','timestamp':'2010-01-26T21:27:34Z'},{'user':'2aprilboy','timestamp':'2010-01-26T21:00:24Z'},{'user':'2aprilboy','timestamp':'2010-01-26T20:49:29Z'},{'user':'2aprilboy','timestamp':'2010-01-26T20:37:01Z'}]}
}}})*/
function hr_getInfo(data){
	if(data['query']['pages']['-1']){
		hr_userInfoLoaded = -1
		hr_status.push('internalScriptError')
		return
	}
	var revisions = []
	for(var id in data['query']['pages']){
		revisions = data['query']['pages'][id]['revisions']
		break
	}
	var last = revisions.shift()
	var last_date = getDateFromTimestamp(last['timestamp']).valueOf()
	var now = new Date()
	now = now.valueOf() + now.getTimezoneOffset()*60*1000
	var last_diff = (now - last_date) // difference in miliseconds
	var nr_of_edits = revisions.length
	if(nr_of_edits == 0){
		hr_status.push('new')
	} else {
		var first = revisions.pop()
		var first_date = getDateFromTimestamp(first['timestamp']).valueOf()
		var average_diff = (last_date - first_date)/nr_of_edits // simple mean used for now.. might be median in future..
		hr_debugInfo += last_diff/1000		+ ' s since last edit (last_diff)\n'
		hr_debugInfo += average_diff/1000		+ ' s between edits (average_diff)\n'
		if(last_diff < hr_recentEditCriterium && last_diff < average_diff+hr_editWarCriterium){
			hr_status.push('recentlyChanged')
		}
		if(average_diff < hr_editWarCriterium && last_diff < hr_editWarCriterium){
			hr_status.push('editWar')
		}
	}
	hr_reviewUsers(last['user'])
}

// __main__ - Wikipedia API call to function hr_getInfo()
function hr_review(){
	if(wgPageName.indexOf('Special:') != -1){
		return
	}
	var api = document.createElement('script')
	var url = 'http://en.wikipedia.org/w/api.php?action=query&format=json&callback=hr_getInfo&prop=revisions&rvprop=timestamp|user&rvlimit='+hr_limit+'&titles='+escape(wgPageName)
	api.setAttribute('src', url)
	api.setAttribute('type','text/javascript')
	document.getElementsByTagName('head')[0].appendChild(api)
}

// sets class after page loads
var hr_infiniteLoopPrevention = 0
function hr_setClassName(){
	var h = document.getElementById('ca-history')
	if(!h){
		hr_debugInfo += '\n"ca-history" not found\n'
		return
	}
	// Chrome fires load event prematurely, so this is a validation that all info is loaded
	if(!hr_userInfoLoaded && hr_infiniteLoopPrevention++ < 50){
		setTimeout('hr_setClassName()',100)
		return
	}
	if(hr_status.length == 0){
		hr_status.push('ok')
	}
	for(var i in hr_status){
		h.className  += ' hr_' + hr_status[i]
	}
	//this is for css debug, if needed
	//h.className = 'collapsible hr_editWar'
	hr_debugInfo += '\n<li id="ca-history" class="' + h.className + '"...>\n'
}

// assigns CSS file
function hr_assignCSS(url){
	if(document.createStyleSheet){
		// for Internet Explorer
		document.createStyleSheet(url)
		document.createStyleSheet(url.replace('.css','_ie.css'))
	} else {
		var s = document.createElement('style')
		s.type = 'text/css'
		s.rel = 'stylesheet'
		s.appendChild(document.createTextNode('@import "' + url + '";'))
		document.getElementsByTagName('head')[0].appendChild(s)
	}
}

// run it..
hr_review()
hr_assignCSS('http://en.wikipedia.org/w/index.php?action=raw&ctype=text/css&title=User:2aprilboy/historyReviewer.css')
hookEvent('load', hr_setClassName)