v50 Steam/Premium information for editors
- v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
- Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.
Difference between revisions of "User:Lethosor/rater 0.1.js"
Jump to navigation
Jump to search
(New rating script (which does little to no actual work)) |
m (This should do for now) |
||
Line 4: | Line 4: | ||
Improved version -- now uses jQuery, extra options (existence not guaranteed) | Improved version -- now uses jQuery, extra options (existence not guaranteed) | ||
*/ | */ | ||
+ | |||
addOnloadHook(function(){jQuery(function($){ | addOnloadHook(function(){jQuery(function($){ | ||
Line 15: | Line 16: | ||
rater.page = { | rater.page = { | ||
name: wgPageName, | name: wgPageName, | ||
− | url: wgScript+' | + | url: wgScript+'/'+wgPageName |
} | } | ||
− | + | ||
+ | rater.is_valid_page = function(page){ | ||
+ | ns=page.split(':')[0]; | ||
+ | if('DF2012 v0.31 40d 23a'.split(' ').indexOf(ns)+1) return true | ||
+ | if($('#norate').length) return false | ||
+ | return false | ||
+ | }; | ||
+ | |||
+ | rater.error_invalid_page=function(){ | ||
+ | rater.box.clear().append('<span class="error">Invalid page</span>') | ||
+ | setTimeout(rater.cancel,1000) | ||
+ | }; | ||
+ | |||
// Set up UI | // Set up UI | ||
rater.overlay = $('<div>').css({width:'100%', height:'100%', top:0, left:0, | rater.overlay = $('<div>').css({width:'100%', height:'100%', top:0, left:0, | ||
Line 31: | Line 44: | ||
rater.box.append(rater.cancel_link) | rater.box.append(rater.cancel_link) | ||
.append($('<h2>').text('Rating '+wgPageName)); | .append($('<h2>').text('Rating '+wgPageName)); | ||
+ | return rater.box; | ||
}; | }; | ||
rater.box.clear(); | rater.box.clear(); | ||
Line 47: | Line 61: | ||
$("#left-navigation #p-namespaces ul:nth(0)").append(rater.show_link) | $("#left-navigation #p-namespaces ul:nth(0)").append(rater.show_link) | ||
− | rater.invoke = function(e){ | + | rater.invoke = function(e, force){ |
PD(e); | PD(e); | ||
rater.overlay.stop(1,1).fadeIn(500); | rater.overlay.stop(1,1).fadeIn(500); | ||
rater.box.stop(1,1).fadeIn(500); | rater.box.stop(1,1).fadeIn(500); | ||
+ | if(!rater.is_valid_page(wgPageName) && !force) | ||
+ | return rater.error_invalid_page(); | ||
rater.box.clear() | rater.box.clear() | ||
− | rater.box.append('<p>Performing automatic tests, please wait...</p>'); | + | rater.box.append('<p>Performing automatic tests (<span class="rater-tests-left"></span> remaining), please wait...</p>'); |
rater.begin_tests(); | rater.begin_tests(); | ||
}; | }; | ||
Line 83: | Line 99: | ||
tests.list[name].result = tests.results[name] = result | tests.list[name].result = tests.results[name] = result | ||
tests.num_waiting-- | tests.num_waiting-- | ||
+ | $(".rater-tests-left").text(tests.num_waiting) | ||
if(tests.num_waiting <= 0){ | if(tests.num_waiting <= 0){ | ||
setTimeout(tests.all_complete, 1); //avoid stacking | setTimeout(tests.all_complete, 1); //avoid stacking | ||
Line 99: | Line 116: | ||
rater.begin_tests = function(){ | rater.begin_tests = function(){ | ||
− | render_url=rater.page.url+' | + | render_url=rater.page.url+'?action=render' |
+ | raw_url=rater.page.url+'?action=raw' | ||
tests.add('redlinks', render_url, function(data){ | tests.add('redlinks', render_url, function(data){ | ||
all_links = data.match(/<a .*<\/a>/g); | all_links = data.match(/<a .*<\/a>/g); | ||
Line 109: | Line 127: | ||
return total_redlinks | return total_redlinks | ||
}); | }); | ||
− | tests.add('editors', rater.page.url+' | + | tests.add('editors', rater.page.url+'?action=history&limit=100', function(data){ |
all_editors = data.match(/<li>.*<\/li>/g) | all_editors = data.match(/<li>.*<\/li>/g) | ||
if(!all_editors) return 0; //no editors | if(!all_editors) return 0; //no editors | ||
Line 125: | Line 143: | ||
return num; | return num; | ||
}); | }); | ||
− | tests.add(' | + | tests.add('linkshere', wgScript+'/Special:WhatLinksHere/'+wgPageName, function(data){ |
− | return | + | return $(data).find('#mw-whatlinkshere-list').length; |
}); | }); | ||
− | tests.add(' | + | tests.add('links', render_url, function(data){ |
− | return | + | return $(data).find('a[href*="'+wgScript+'"]').length; |
+ | }); | ||
+ | tests.add('length', raw_url, function(data){ | ||
+ | var a={ | ||
+ | 'full':data.length, | ||
+ | 'nospace':data.replace(/\s/g,'').length, | ||
+ | 'notemplate':data.replace(/{{[^}]*?}}/g,'').length, | ||
+ | 'raw':data.replace(/\s/g,'').replace(/{{[^}]*?}}/g,'').length | ||
+ | }; | ||
+ | a.average=.1 * a.full + .2*a.nospace + .3*a.notemplate + .4*a.raw | ||
+ | return a | ||
}); | }); | ||
tests.add_callback(function(){ | tests.add_callback(function(){ | ||
Line 143: | Line 171: | ||
if(!base) base=0; | if(!base) base=0; | ||
return v*weight+base; | return v*weight+base; | ||
+ | }; | ||
+ | rater.metadata = { | ||
+ | |||
}; | }; | ||
Line 155: | Line 186: | ||
rater.score = 0 | rater.score = 0 | ||
− | +rater.score_bool(data. | + | +rater.score_bool(!data.links,-25) //orphaned |
− | +rater.score_bool(data. | + | +rater.score_bool(!data.linkshere,-30) //dead end |
− | +rater.score_int(data.redlinks,-5) | + | +rater.score_int(data.links,0.5) |
− | +rater.score_int(data.editors, | + | +rater.score_int(data.linkshere,0.75) |
+ | +rater.score_int(data.redlinks,-5,10) | ||
+ | +rater.score_int(data.editors,20,-15) | ||
+ | |||
; | ; | ||
rater.box.append($("<p>").text("Score: "+rater.score)) | rater.box.append($("<p>").text("Score: "+rater.score)) |
Revision as of 01:25, 14 April 2013
/*
Rating Script (DF Wiki)
Improved version -- now uses jQuery, extra options (existence not guaranteed)
*/
addOnloadHook(function(){jQuery(function($){
SCOPE = this
var rater = {}
function PD(e){//preventDefault
if(e && 'preventDefault' in e && 'call' in e.preventDefault)
e.preventDefault()
}
rater.page = {
name: wgPageName,
url: wgScript+'/'+wgPageName
}
rater.is_valid_page = function(page){
ns=page.split(':')[0];
if('DF2012 v0.31 40d 23a'.split(' ').indexOf(ns)+1) return true
if($('#norate').length) return false
return false
};
rater.error_invalid_page=function(){
rater.box.clear().append('<span class="error">Invalid page</span>')
setTimeout(rater.cancel,1000)
};
// Set up UI
rater.overlay = $('<div>').css({width:'100%', height:'100%', top:0, left:0,
position:'fixed', 'background-color':'rgba(128,128,128,0.5)', 'z-index':9999})
.hide().appendTo('body');
rater.box = $('<div>').css({width:'80%', height:'80%', top:'10%', left:'10%',
position:'fixed', 'background-color':'white', 'z-index':10000, padding:'1em'})
.hide().appendTo('body');
rater.cancel_link = $('<a>').text('Cancel').attr('href','#rater-cancel').css({color:'red',
'float':'right'}).appendTo(rater.box);
rater.box.clear = function(){
rater.box.html('')
rater.box.append(rater.cancel_link)
.append($('<h2>').text('Rating '+wgPageName));
return rater.box;
};
rater.box.clear();
rater.cancel = function(e){
PD(e)
rater.overlay.stop(1,1).fadeOut(500);
rater.box.stop(1,1).fadeOut(500);
};
$('body').on('click','a[href=#rater-cancel]',rater.cancel);
// Set up link
rater.show_link = $("<li>").append($('<span>').append(
$("<a href='#rater-invoke'>").text('Rate')
));
$("#left-navigation #p-namespaces ul:nth(0)").append(rater.show_link)
rater.invoke = function(e, force){
PD(e);
rater.overlay.stop(1,1).fadeIn(500);
rater.box.stop(1,1).fadeIn(500);
if(!rater.is_valid_page(wgPageName) && !force)
return rater.error_invalid_page();
rater.box.clear()
rater.box.append('<p>Performing automatic tests (<span class="rater-tests-left"></span> remaining), please wait...</p>');
rater.begin_tests();
};
$('body').on('click', 'a[href=#rater-invoke]', rater.invoke)
/*
Tests
Usage: Specify a URL to get data from and `processor` function to extract the
needed data
*/
var tests={};
tests.list={}; //list of all tests
tests.results={}; //shortcut: data[x]==lists[x].data
tests.num_waiting = 0;
tests.add = function(name, data_url, processor){
//Note: jQuery caches Ajax requests by default, no need to here
tests.list[name] = {data_url:data_url, processor:processor};
//console.log('retrieving')
$.get(data_url, function(data){
tests.process(name, data);
});
tests.num_waiting++
};
tests.process = function(name,raw_data){
result = tests.list[name].processor(raw_data);
tests.list[name].result = tests.results[name] = result
tests.num_waiting--
$(".rater-tests-left").text(tests.num_waiting)
if(tests.num_waiting <= 0){
setTimeout(tests.all_complete, 1); //avoid stacking
}
};
tests.all_callbacks=[]; //when all tests are done
tests.all_complete = function(){
for(i=0; i<tests.all_callbacks.length; i++){
tests.all_callbacks[i]();
}
};
tests.add_callback=function(func){
tests.all_callbacks.push(func);
};
rater.begin_tests = function(){
render_url=rater.page.url+'?action=render'
raw_url=rater.page.url+'?action=raw'
tests.add('redlinks', render_url, function(data){
all_links = data.match(/<a .*<\/a>/g);
if(!all_links) return 0; //no links
total_redlinks=0;
$.each(all_links, function(i,link){
if(link.match(/href=.*redlink=1/)) total_redlinks++;
});
return total_redlinks
});
tests.add('editors', rater.page.url+'?action=history&limit=100', function(data){
all_editors = data.match(/<li>.*<\/li>/g)
if(!all_editors) return 0; //no editors
editors={};
$.each(all_editors, function(i,li){
ed = $(li).find('.mw-userlink:nth(0)').text()
if(ed in editors) editors[ed]=0;
editors[ed]++
});
num=0;
for(i in editors){
if(i in {}) continue;
num++
}
return num;
});
tests.add('linkshere', wgScript+'/Special:WhatLinksHere/'+wgPageName, function(data){
return $(data).find('#mw-whatlinkshere-list').length;
});
tests.add('links', render_url, function(data){
return $(data).find('a[href*="'+wgScript+'"]').length;
});
tests.add('length', raw_url, function(data){
var a={
'full':data.length,
'nospace':data.replace(/\s/g,'').length,
'notemplate':data.replace(/{{[^}]*?}}/g,'').length,
'raw':data.replace(/\s/g,'').replace(/{{[^}]*?}}/g,'').length
};
a.average=.1 * a.full + .2*a.nospace + .3*a.notemplate + .4*a.raw
return a
});
tests.add_callback(function(){
rater.display_test_results();
});
};
rater.score_bool=function(v,y,n){
if(isNaN(Number(n))) n=-y;
return Number(v?y:n);
};
rater.score_int=function(v,weight,base){
if(!base) base=0;
return v*weight+base;
};
rater.metadata = {
};
rater.display_test_results=function(){
rater.box.clear();
data=tests.results;
for(i in data){
if(i in {}) continue;
rater.box.append($("<p>"+i+": "+data[i]+"</p>"));
}
rater.score = 0
+rater.score_bool(!data.links,-25) //orphaned
+rater.score_bool(!data.linkshere,-30) //dead end
+rater.score_int(data.links,0.5)
+rater.score_int(data.linkshere,0.75)
+rater.score_int(data.redlinks,-5,10)
+rater.score_int(data.editors,20,-15)
;
rater.box.append($("<p>").text("Score: "+rater.score))
};
//export
rater.tests = tests;
window.rater=rater
return rater;
});});