User:Martijn Hoekstra/checkrange.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Martijn Hoekstra/checkrange. |
//stuff for sanity-checking rangeblocks
var checkrange = {
check_months : 1,
check_days : 0,
check_years : 0,
min_edits : 1,
ip_prefixes : {},
range : "92.41.0.0/21",
ips : {},
num_ips : 0,
startdate: new Object(),
addlink : function() {
addPortletLink("p-cactions", "javascript:checkrange.init()", "Check range 2.1", "ca-rangeChecker", "Check range 2.1")
},
init : function() {
this.num_ips = 0;
var range=prompt("Please enter the blocked range","");
if (range!=null) {
if (found = range.match(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}/)){
this.range = found[0];
this.get_recently_active_ips()
} else {
alert("misformatted range")
}
}
},
showbox : function() {
var message = "<h1>getting active IP's</h1>"
message += "prefixes:<ul id='prefixlist'></ul>"
message += "unique IP's <span id='IPs'>0</span>"
jsMsg(message)
},
add_prefix : function(prefix){
this.ip_prefixes.push(prefix)
var msg_prefixes = document.getElementById('prefixlist')
var listitem = document.createElement('li')
listitem.appendChild(document.createTextNode(prefix + ' ' ))
var s = document.createElement("span")
s.id = prefix
s.innerText = 'fetching'
listitem.appendChild(s)
msg_prefixes.appendChild(listitem)
},
get_recently_active_ips : function (){
console.log('get recently active ips');
var used_ips = {};
this.startdate = new Date();
this.startdate.setFullYear(this.startdate.getFullYear()- this.check_years);
this.startdate.setMonth(this.startdate.getMonth() - this.check_months);
this.startdate.setDate(this.startdate.getDate() - this.check_days);
this.showbox();
this.get_active_ips();
},
get_active_ips: function(){
console.log('get active ips');
var calls = this.construct_api_calls()
for (var next_call in calls){
this.get_ips(calls[next_call], calls[next_call])
}
},
get_ips : function(base_url, call_url){
var prefix = call_url.match(/ucuserprefix=([^&]*)/)[1]
$.getJSON(call_url, function(data) {
var usercontribs = data.query["usercontribs"]
for(var item in usercontribs){
var realitem=usercontribs[item]
console.log('check for useritem');
if (this.ips.hasOwnProperty(realitem["user"])){
this.ips[realitem["user"]] ++
} else {
this.ips[realitem["user"]] = 1
}
if (this.ips[realitem["user"]] == min_edits) {
numIPs++
document.getElementById("IPs").innerText=numIPs
}
}
console.log('check for query-continue')
if (data.hasOwnProperty("query-continue")){
var next_call_postfix = data["query-continue"].usercontribs["uccontinue"]
if (next_call_postfix){
get_ips(base_url, base_url + "&uccontinue="+next_call_postfix, ips, prefix)
}
document.getElementById(prefix).innerText='fetching more'
} else {
document.getElementById(prefix).innerHTML='<b>done</b>'
}
});
},
construct_api_calls : function(){
var api_call_base_url = "https://en.wikipedia.org/w/api.php?action=query&list=usercontribs&format=json&ucuserprefix="
var api_calls = new Array()
var prefixes = this.get_prefixes()
for (var prefix in prefixes){
api_calls.push(api_call_base_url + prefixes[prefix])
}
return api_calls
},
get_prefixes : function(){
var prefixes = new Array();
var parts = this.range.split('/');
var start = parts[0];
var size = parseInt(parts[1]);
var octets = $.map(start.split('.'), function(n){
return parseInt(n);
});
var octet1 = parseInt(this.range.split('.')[0])
var octet2 = parseInt(this.range.split('.')[1])
var octet3 = parseInt(this.range.split('.')[2])
var octet4 = parseInt(this.range.split('.')[3])
//now the annoying part
if (size <= 8) {
var factor = 8 - size
var num_blocks = 1 << factor
max_octet = octets[0] + num_blocks - 1
var current_octet = octets[0]
while(current_octet1 <= max_octet1){
this.add_prefix(current_octet.toString() + '.', prefixes)
current_octet++
}
} else if (size > 8 && size <= 16){
var factor = 16 - size
var num_blocks = 1 << factor
max_octet2 = octet2 + num_blocks - 1
var current_octet2 = octet2
while(current_octet2 <= max_octet2){
this.add_prefix(octet1.toString() + '.' + current_octet2.toString() + '.', prefixes)
current_octet2++
}
} else if (size > 16 && size <= 24){
var factor = 24 - size
var num_blocks = 1 << factor
max_octet3 = octet3 + num_blocks - 1
var current_octet3 = octet3
while(current_octet3 <= max_octet3){
this.add_prefix(octet1.toString() + '.' + octet2.toString() + '.' + current_octet3.toString() + '.', prefixes)
current_octet3++
}
} else {
var factor = 32 - size
var num_d = 1 << factor
max_octet4 = octet4 + num_d - 1
while(current_octet4 <= max_octet4){
this.add_prefix(octet1.toString() + '.' + octet2.toString() + '.' + octet3.toString() + '.' + current_octet4.toString(), prefixes)
current_octet4++
}
}
return prefixes
}
}
jQuery(checkrange.addlink)