Jump to content

User:Martijn Hoekstra/checkrange.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Martijn Hoekstra (talk | contribs) at 00:53, 22 February 2012 (trying to fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
//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.0", "ca-rangeChecker", "Check range 2.0")
  },

  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){
        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){
        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){
        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){
        add_prefix(octet1.toString() + '.' + octet2.toString() + '.' + octet3.toString() + '.' + current_octet4.toString(), prefixes)
        current_octet4++
      }
    }
    return prefixes
  }

}

jQuery(checkrange.addlink)