[go: nahoru, domu]

User:Splarka/contribsrange.js

This is an old revision of this page, as edited by Splarka (talk | contribs) at 01:45, 26 February 2008 (+crude timestamp, tweak a bit). 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.
/* Special:Contributions CIDR lookup, version [0.0.5]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/contribsrange.js

Notes:
* This uses the API, which is assloads faster than most other CIDR. 
* Currently uses a GET via script src to avoid ajax problems.
* Only currently works with /24.
* Only currently works if submitted.
* Due to URI length restrictions at Wikimedia, it has to split long queries (123.123.123.0/24 will show in two results).

To do:
* Pagination via timestamp of last listed contribution. Should be possible?
** Sortable by address? (not useful until pagination is available).
* More like normal Special:Recentchanges (hist)(diff) blah blah blah (except talk page existence for red/blue link)?
* Option to have /23 /22 /21 etc. Require more flexible regex and smarter start/end handling.
* Option to refresh on page? (not really needed, but might be more handy than onload action only).
* Anything else?
*/

if(wgCanonicalSpecialPageName == 'Contributions') { 
  addOnloadHook(contribsCIDRinit);
  var cidrlimit = 500;
}

function contribsCIDRinit() {
  var frm = document.getElementsByTagName('form')[0];
  var res = document.createElement('div');
  res.setAttribute('id','results-from-CIDR');
  frm.parentNode.appendChild(res);
  var pattern = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.0\/24/i ;
  if(frm.target.value.search(pattern) == 0) {
    res.style.border = '1px solid black';
    res.style.padding = '.5em';
    var cidr = frm.target.value.match(pattern)[0].replace(/0\/24/,'');
    var namespace = (parseInt(frm.namespace[frm.namespace.selectedIndex].value) > -1) ? '&ucnamespace=' + frm.namespace[frm.namespace.selectedIndex].value : '';
    if(cidr.length < 12) {
      contribsCIDRscript(cidr,0,255,namespace);
    } else {
      //this would be a long-ass URL (over 4000 characters).
      contribsCIDRscript(cidr,0,127,namespace);
      contribsCIDRscript(cidr,128,255,namespace);
    }
  }
}

function contribsCIDRscript(cidr,cidrStart,cidrEnd,namespace) {
  var url = wgScriptPath + '/api.php?action=query&format=json&callback=contribsCIDR&list=usercontribs' + namespace + '&uclimit=' + parseInt(cidrlimit) + '&ucuser=';
  for(var i=cidrStart;i<=cidrEnd;i++) {
    url += '' + cidr + i;
    if(i != cidrEnd) url += '|'
  }
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',url);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

function contribsCIDR(obj) {
  if(!obj['query'] || !obj['query']['usercontribs']) return
  cidr = obj['query']['usercontribs'];
  var res = document.getElementById('results-from-CIDR');
  res.appendChild(document.createElement('hr'));
  if(cidr.length == 0) {
    res.appendChild(document.createTextNode('No changes were found for this CIDR range.'));
    return;
  }
  res.appendChild(document.createTextNode(cidr.length + ' matches in the CIDR range specified (chronologically): '));
  if(cidr.length == parseInt(cidrlimit)) {
    res.appendChild(document.createElement('br'));
    res.appendChild(document.createTextNode('NOTE: There are more results than the ' + cidrlimit + ' shown. You may not be seeing ALL contributors in this CIDR range.'));
  }
  res.appendChild(document.createElement('hr'));
  for(var i=0;i<cidr.length;i++) {
    res.appendChild(document.createTextNode('\u2022 ' + cidr[i].timestamp.replace(/T[\d:]*Z/,' ')));
    addlinkchild(res, wgScript + '?title=Special:Contributions/' + cidr[i].user, cidr[i].user);
    res.appendChild(document.createTextNode(' edited ('));
    addlinkchild(res, wgScript + '?title=-&curid=' + cidr[i].pageid + '&diff=' + cidr[i].revid , 'diff');
    res.appendChild(document.createTextNode(') '));
    addlinkchild(res, wgScript + '?title=-&curid=' + cidr[i].pageid, cidr[i].title);
    if(cidr[i].comment) res.appendChild(document.createTextNode(' (' + cidr[i].comment + ')'));
    res.appendChild(document.createElement('br'));
  }
}

function addlinkchild(obj,href,text,id,classes) {
  if(!obj || !href || !text) return false;
  var a = document.createElement('a');
  a.setAttribute('href',href);
  a.appendChild(document.createTextNode(text));
  if(id) a.setAttribute('id',id);
  if(classes) a.setAttribute('class',classes);
  obj.appendChild(a);
}