MusikAnimal has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/373165 )

Change subject: Rename IP::isValidBlock to isValidRange, depracating the former
......................................................................

Rename IP::isValidBlock to isValidRange, depracating the former

This is to remove confusion with the MediaWiki Block class.

All instances of isValidBlock within MediaWiki core have been updated.

Usage of this function will be more widespread with this patch:
https://gerrit.wikimedia.org/r/#/c/349457/

Change-Id: Ice1bdae3d16cf365da14c6df0e8d91d2b914e067
---
M RELEASE-NOTES-1.30
M includes/Block.php
M includes/libs/IP.php
M tests/phpunit/includes/libs/IPTest.php
4 files changed, 38 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/65/373165/1

diff --git a/RELEASE-NOTES-1.30 b/RELEASE-NOTES-1.30
index 13a9f4e..ec256ce 100644
--- a/RELEASE-NOTES-1.30
+++ b/RELEASE-NOTES-1.30
@@ -157,6 +157,7 @@
 * Article::viewRedirect() is deprecated.
 * DeprecatedGlobal no longer supports passing in a direct value, it requires a
   callable factory function or a class name.
+* IP::isValidBlock() is deprecated. Use the equivalent IP::isValidRange().
 * The $parserMemc global, wfGetParserCacheStorage(), and 
ParserCache::singleton()
   are all deprecated. The main ParserCache instance should be obtained from
   MediaWikiServices instead. Access to the underlying BagOStuff is possible
diff --git a/includes/Block.php b/includes/Block.php
index 5066038..05e97b9 100644
--- a/includes/Block.php
+++ b/includes/Block.php
@@ -1354,7 +1354,7 @@
                                self::TYPE_IP
                        ];
 
-               } elseif ( IP::isValidBlock( $target ) ) {
+               } elseif ( IP::isValidRange( $target ) ) {
                        # Can't create a User from an IP range
                        return [ IP::sanitizeRange( $target ), self::TYPE_RANGE 
];
                }
diff --git a/includes/libs/IP.php b/includes/libs/IP.php
index b22f06d..96b247a 100644
--- a/includes/libs/IP.php
+++ b/includes/libs/IP.php
@@ -23,14 +23,14 @@
 
 use IPSet\IPSet;
 
-// Some regex definition to "play" with IP address and IP address blocks
+// Some regex definition to "play" with IP address and IP address ranges
 
 // An IPv4 address is made of 4 bytes from x00 to xFF which is d0 to d255
 define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])' );
 define( 'RE_IP_ADD', RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' 
. RE_IP_BYTE );
-// An IPv4 block is an IP address and a prefix (d1 to d32)
+// An IPv4 range is an IP address and a prefix (d1 to d32)
 define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)' );
-define( 'RE_IP_BLOCK', RE_IP_ADD . '\/' . RE_IP_PREFIX );
+define( 'RE_IP_RANGE', RE_IP_ADD . '\/' . RE_IP_PREFIX );
 
 // An IPv6 address is made up of 8 words (each x0000 to xFFFF).
 // However, the "::" abbreviation can be used on consecutive x0000 words.
@@ -47,8 +47,8 @@
                RE_IPV6_WORD . '(?::' . RE_IPV6_WORD . '){7}' .
        ')'
 );
-// An IPv6 block is an IP address and a prefix (d1 to d128)
-define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
+// An IPv6 range is an IP address and a prefix (d1 to d128)
+define( 'RE_IPV6_RANGE', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
 // For IPv6 canonicalization (NOT for strict validation; these are quite lax!)
 define( 'RE_IPV6_GAP', ':(?:0+:)*(?::(?:0+:)*)?' );
 define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
@@ -64,7 +64,7 @@
 
 /**
  * A collection of public static functions to play with IP address
- * and IP blocks.
+ * and IP ranges.
  */
 class IP {
        /** @var IPSet */
@@ -118,16 +118,30 @@
        }
 
        /**
-        * Validate an IP Block (valid address WITH a valid prefix).
+        * Validate an IP range (valid address with a valid CIDR prefix).
         * SIIT IPv4-translated addresses are rejected.
         * @note canonicalize() tries to convert translated addresses to IPv4.
         *
-        * @param string $ipblock
+        * @deprecated since 1.30. Use the equivalent IP::isValidRange().
+        * @param string $ipRange
         * @return bool True if it is valid
         */
-       public static function isValidBlock( $ipblock ) {
-               return ( preg_match( '/^' . RE_IPV6_BLOCK . '$/', $ipblock )
-                       || preg_match( '/^' . RE_IP_BLOCK . '$/', $ipblock ) );
+       public static function isValidBlock( $ipRange ) {
+               return self::isValidRange( $ipRange );
+       }
+
+       /**
+        * Validate an IP range (valid address with a valid CIDR prefix).
+        * SIIT IPv4-translated addresses are rejected.
+        * @note canonicalize() tries to convert translated addresses to IPv4.
+        *
+        * @param string $ipRange
+        * @return bool True if it is valid
+        * @since 1.30
+        */
+       public static function isValidRange( $ipRange ) {
+               return ( preg_match( '/^' . RE_IPV6_RANGE . '$/', $ipRange )
+                       || preg_match( '/^' . RE_IP_RANGE . '$/', $ipRange ) );
        }
 
        /**
diff --git a/tests/phpunit/includes/libs/IPTest.php 
b/tests/phpunit/includes/libs/IPTest.php
index 307652d..7462756 100644
--- a/tests/phpunit/includes/libs/IPTest.php
+++ b/tests/phpunit/includes/libs/IPTest.php
@@ -237,7 +237,7 @@
                ];
                foreach ( $ipCIDRs as $i ) {
                        $this->assertFalse( IP::isValid( $i ),
-                               "$i is an invalid IP address because it is a 
block" );
+                               "$i is an invalid IP address because it is a 
range" );
                }
                // Incomplete/garbage
                $invalid = [
@@ -254,9 +254,9 @@
        }
 
        /**
-        * Provide some valid IP blocks
+        * Provide some valid IP ranges
         */
-       public function provideValidBlocks() {
+       public function provideValidRanges() {
                return [
                        [ '116.17.184.5/32' ],
                        [ '0.17.184.5/30' ],
@@ -274,22 +274,22 @@
        }
 
        /**
-        * @covers IP::isValidBlock
-        * @dataProvider provideValidBlocks
+        * @covers IP::isValidRange
+        * @dataProvider provideValidRanges
         */
-       public function testValidBlocks( $block ) {
-               $this->assertTrue( IP::isValidBlock( $block ), "$block is a 
valid IP block" );
+       public function testValidRanges( $range ) {
+               $this->assertTrue( IP::isValidRange( $range ), "$range is a 
valid IP range" );
        }
 
        /**
         * @covers IP::isValidBlock
-        * @dataProvider provideInvalidBlocks
+        * @dataProvider provideInvalidRanges
         */
-       public function testInvalidBlocks( $invalid ) {
-               $this->assertFalse( IP::isValidBlock( $invalid ), "$invalid is 
not a valid IP block" );
+       public function testInvalidRanges( $invalid ) {
+               $this->assertFalse( IP::isValidRange( $invalid ), "$invalid is 
not a valid IP range" );
        }
 
-       public function provideInvalidBlocks() {
+       public function provideInvalidRanges() {
                return [
                        [ '116.17.184.5/33' ],
                        [ '0.17.184.5/130' ],

-- 
To view, visit https://gerrit.wikimedia.org/r/373165
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ice1bdae3d16cf365da14c6df0e8d91d2b914e067
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MusikAnimal <musikani...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to