r88730 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88729‎ | r88730 | r88731 >
Date:18:12, 24 May 2011
Author:brion
Status:ok
Tags:
Comment:
JS fixes & a little cleanup/modernization on CheckUser

Was throwing errors due to trying to use addOnloadHook() from what's now loaded by ResourceLoader before wikibits classic stuff.
Switched a few things to use jQuery, and factored out a couple common bits of output formatting code.
Also tried to fix some bogus undeclared globals and duplicate var declarations.
Modified paths:
  • /trunk/extensions/CheckUser/CheckUser_body.php (modified) (history)
  • /trunk/extensions/CheckUser/checkuser.js (modified) (history)

Diff [purge]

Index: trunk/extensions/CheckUser/CheckUser_body.php
@@ -224,7 +224,7 @@
225225 global $wgOut;
226226 $s = '<fieldset id="mw-checkuser-cidrform" style="display:none; clear:both;">' .
227227 '<legend>' . wfMsgHtml( 'checkuser-cidr-label' ) . '</legend>';
228 - $s .= '<textarea id="mw-checkuser-iplist" rows="5" cols="50" onkeyup="updateCIDRresult()" onclick="updateCIDRresult()"></textarea><br />';
 228+ $s .= '<textarea id="mw-checkuser-iplist" rows="5" cols="50"></textarea><br />';
229229 $s .= wfMsgHtml( 'checkuser-cidr-res' ) . '&#160;' .
230230 Xml::input( 'mw-checkuser-cidr-res', 35, '', array( 'id' => 'mw-checkuser-cidr-res' ) ) .
231231 '&#160;<strong id="mw-checkuser-ipnote"></strong>';
Index: trunk/extensions/CheckUser/checkuser.js
@@ -2,11 +2,16 @@
33
44 /* Every time you change this JS please bump $wgCheckUserStyleVersion in CheckUser.php */
55
 6+var showResults = function(size, cidr) {
 7+ $( '#mw-checkuser-cidr-res' ).val( size );
 8+ $( '#mw-checkuser-ipnote' ).text( cidr );
 9+};
 10+
611 /*
712 * This function calculates the common range of a list of
813 * IPs. It should be set to update on keyUp.
914 */
10 -window.updateCIDRresult = function() {
 15+var updateCIDRresult = function() {
1116 var form = document.getElementById( 'mw-checkuser-cidrform' );
1217 if( !form ) {
1318 return; // no JS form
@@ -16,27 +21,28 @@
1722 if( !iplist ) {
1823 return; // no JS form
1924 }
20 - var text = iplist.value;
 25+ var text = iplist.value, ips;
2126 // Each line should have one IP or range
2227 if( text.indexOf("\n") != -1 ) {
23 - var ips = text.split("\n");
 28+ ips = text.split("\n");
2429 // Try some other delimiters too...
2530 } else if( text.indexOf("\t") != -1 ) {
26 - var ips = text.split("\t");
 31+ ips = text.split("\t");
2732 } else if( text.indexOf(",") != -1 ) {
28 - var ips = text.split(",");
 33+ ips = text.split(",");
2934 } else if( text.indexOf("-") != -1 ) {
30 - var ips = text.split("-");
 35+ ips = text.split("-");
3136 } else if( text.indexOf(" ") != -1 ) {
32 - var ips = text.split(" ");
 37+ ips = text.split(" ");
3338 } else {
34 - var ips = text.split(";");
 39+ ips = text.split(";");
3540 }
3641 var bin_prefix = 0;
3742 var prefix_cidr = 0;
3843 var prefix = new String( '' );
3944 var foundV4 = false;
4045 var foundV6 = false;
 46+ var ip_count;
4147 // Go through each IP in the list, get its binary form, and
4248 // track the largest binary prefix among them...
4349 for( var i = 0; i < ips.length; i++ ) {
@@ -63,7 +69,7 @@
6470 if( blocs[0] <= 2 ) continue;
6571 for( var x = 0; x < blocs.length; x++ ) {
6672 bloc = parseInt( blocs[x], 10 );
67 - bin_block = bloc.toString( 2 ); // concat bin with binary form of bloc
 73+ var bin_block = bloc.toString( 2 ); // concat bin with binary form of bloc
6874 while( bin_block.length < 8 ) {
6975 bin_block = '0' + bin_block; // pad out as needed
7076 }
@@ -88,12 +94,10 @@
8995 }
9096 }
9197 // Build the IP in CIDR form
92 - var prefix_cidr = bin_prefix.length;
 98+ prefix_cidr = bin_prefix.length;
9399 // CIDR too small?
94100 if( prefix_cidr < 16 ) {
95 - document.getElementById( 'mw-checkuser-cidr-res' ).value = '!';
96 - document.getElementById( 'mw-checkuser-ipnote' ).innerHTML = '&gt;' +
97 - Math.pow( 2, 32 - prefix_cidr );
 101+ showResults( '!', '>' + Math.pow( 2, 32 - prefix_cidr ) );
98102 return; // too big
99103 }
100104 // Build the IP in dotted-quad form
@@ -145,7 +149,7 @@
146150 var blocs = ip.split(':');
147151 for( var x = 0; x <= 7; x++ ) {
148152 bloc = blocs[x] ? blocs[x] : '0';
149 - int_block = hex2int( bloc ); // convert hex -> int
 153+ var int_block = hex2int( bloc ); // convert hex -> int
150154 bin_block = int_block.toString( 2 ); // concat bin with binary form of bloc
151155 while( bin_block.length < 16 ) {
152156 bin_block = '0' + bin_block; // pad out as needed
@@ -174,9 +178,7 @@
175179 var prefix_cidr = bin_prefix.length;
176180 // CIDR too small?
177181 if( prefix_cidr < 96 ) {
178 - document.getElementById( 'mw-checkuser-cidr-res' ).value = '!';
179 - document.getElementById( 'mw-checkuser-ipnote' ).innerHTML = '&gt;'
180 - + Math.pow( 2, 128 - prefix_cidr );
 182+ showResults('!', '>' + Math.pow( 2, 128 - prefix_cidr ) );
181183 return; // too big
182184 }
183185 // Build the IP in dotted-quad form
@@ -203,22 +205,19 @@
204206 }
205207 // Update form
206208 if( prefix != '' ) {
 209+ var full = prefix;
207210 if( prefix_cidr != false ) {
208 - document.getElementById( 'mw-checkuser-cidr-res' ).value = prefix + '/' + prefix_cidr;
209 - } else {
210 - document.getElementById( 'mw-checkuser-cidr-res' ).value = prefix;
 211+ full += '/' + prefix_cidr;
211212 }
212 - document.getElementById( 'mw-checkuser-ipnote' ).innerHTML = '~' + ip_count;
 213+ showResults( '~' + ip_count, full );
213214 } else {
214 - document.getElementById( 'mw-checkuser-cidr-res' ).value = '?';
215 - document.getElementById( 'mw-checkuser-ipnote' ).innerHTML = '';
 215+ showResults( '?', '' );
216216 }
217217
218218 };
219 -addOnloadHook( updateCIDRresult );
220219
221220 // Utility function to convert hex to integers
222 -window.hex2int = function( hex ) {
 221+var hex2int = function( hex ) {
223222 hex = new String( hex );
224223 hex = hex.toLowerCase();
225224 var intform = 0;
@@ -251,3 +250,10 @@
252251 }
253252 return intform;
254253 };
 254+
 255+$( function() {
 256+ updateCIDRresult();
 257+ $('#mw-checkuser-iplist').bind('keyup click', function() {
 258+ updateCIDRresult();
 259+ });
 260+});

Follow-up revisions

RevisionCommit summaryAuthorDate
r99668(bug 31662) - 'CIDR calculator now has results reversed', regression from r88730aaron05:00, 13 October 2011

Status & tagging log