r84285 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84284‎ | r84285 | r84286 >
Date:23:28, 18 March 2011
Author:happy-melon
Status:ok
Tags:
Comment:
Move Block::normaliseRange() to IP.php, then reduce it to 4 lines by using the existing functionality in that class.
Modified paths:
  • /trunk/phase3/includes/Block.php (modified) (history)
  • /trunk/phase3/includes/IP.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Block.php
@@ -813,35 +813,10 @@
814814 * For example, 127.111.113.151/24 -> 127.111.113.0/24
815815 * @param $range String: IP address to normalize
816816 * @return string
 817+ * @deprecated since 1.18, call IP::sanitizeRange() directly
817818 */
818819 public static function normaliseRange( $range ) {
819 - $parts = explode( '/', $range );
820 - if ( count( $parts ) == 2 ) {
821 - // IPv6
822 - if ( IP::isIPv6( $range ) && $parts[1] >= 64 && $parts[1] <= 128 ) {
823 - $bits = $parts[1];
824 - $ipint = IP::toUnsigned( $parts[0] );
825 - # Native 32 bit functions WON'T work here!!!
826 - # Convert to a padded binary number
827 - $network = wfBaseConvert( $ipint, 10, 2, 128 );
828 - # Truncate the last (128-$bits) bits and replace them with zeros
829 - $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
830 - # Convert back to an integer
831 - $network = wfBaseConvert( $network, 2, 10 );
832 - # Reform octet address
833 - $newip = IP::toOctet( $network );
834 - $range = "$newip/{$parts[1]}";
835 - } // IPv4
836 - elseif ( IP::isIPv4( $range ) && $parts[1] >= 16 && $parts[1] <= 32 ) {
837 - $shift = 32 - $parts[1];
838 - $ipint = IP::toUnsigned( $parts[0] );
839 - $ipint = $ipint >> $shift << $shift;
840 - $newip = long2ip( $ipint );
841 - $range = "$newip/{$parts[1]}";
842 - }
843 - }
844 -
845 - return $range;
 820+ return IP::sanitizeRange( $range );
846821 }
847822
848823 /**
Index: trunk/phase3/includes/IP.php
@@ -614,4 +614,17 @@
615615
616616 return null; // give up
617617 }
 618+
 619+ /**
 620+ * Gets rid of uneeded numbers in quad-dotted/octet IP strings
 621+ * For example, 127.111.113.151/24 -> 127.111.113.0/24
 622+ * @param $range String: IP address to normalize
 623+ * @return string
 624+ */
 625+ public static function sanitizeRange( $range ){
 626+ list( /*...*/, $bits ) = self::parseCIDR( $range );
 627+ list( $start, /*...*/ ) = self::parseRange( $range );
 628+ $start = self::formatHex( $start );
 629+ return "$start/$bits";
 630+ }
618631 }

Status & tagging log