r88750 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88749‎ | r88750 | r88751 >
Date:21:04, 24 May 2011
Author:brion
Status:ok
Tags:
Comment:
* (bug 29116) Fix regression breaking CheckUser extension

Fixes regression from r84475 and friends which made Block->load() and its new front-end Block::newFromTarget() fail when an empty string was passed in as the IP / $vagueTarget parameter to indicate skipping IP-based lookups.
Added phpunit test cases to confirm that both Block->load() and Block::newFromTarget() work when given null (already ok), '' (as done from CheckUser), or false (not seen, but perfectly legit sounding).
Adjusted comparisons to work as expected.
Modified paths:
  • /trunk/phase3/includes/Block.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/BlockTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/BlockTest.php
@@ -48,5 +48,38 @@
4949
5050 }
5151
 52+ /**
 53+ * This is the method previously used to load block info in CheckUser etc
 54+ * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
 55+ *
 56+ * This stopped working with r84475 and friends: regression being fixed for bug 29116.
 57+ *
 58+ * @dataProvider dataBug29116
 59+ */
 60+ function testBug29116LoadWithEmptyIp( $vagueTarget ) {
 61+ $block = new Block();
 62+ $block->load( $vagueTarget, 'UTBlockee' );
 63+ $this->assertTrue( $this->block->equals( Block::newFromTarget('UTBlockee', $vagueTarget) ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) );
 64+ }
 65+
 66+ /**
 67+ * CheckUser since being changed to use Block::newFromTarget started failing
 68+ * because the new function didn't accept empty strings like Block::load()
 69+ * had. Regression bug 29116.
 70+ *
 71+ * @dataProvider dataBug29116
 72+ */
 73+ function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
 74+ $block = Block::newFromTarget('UTBlockee', $vagueTarget);
 75+ $this->assertTrue( $this->block->equals( $block ), "newFromTarget() returns the same block as the one that was made when given empty vagueTarget param " . var_export( $vagueTarget, true ) );
 76+ }
 77+
 78+ function dataBug29116() {
 79+ return array(
 80+ array( null ),
 81+ array( '' ),
 82+ array( false )
 83+ );
 84+ }
5285 }
5386
Index: trunk/phase3/includes/Block.php
@@ -184,7 +184,7 @@
185185 * 2) A rangeblock encompasing the given IP (smallest first)
186186 * 3) An autoblock on the given IP
187187 * @param $vagueTarget User|String also search for blocks affecting this target. Doesn't
188 - * make any sense to use TYPE_AUTO / TYPE_ID here
 188+ * make any sense to use TYPE_AUTO / TYPE_ID here. Leave blank to skip IP lookups.
189189 * @return Bool whether a relevant block was found
190190 */
191191 protected function newLoad( $vagueTarget = null ) {
@@ -198,7 +198,8 @@
199199 $conds = array( 'ipb_address' => array() );
200200 }
201201
202 - if( $vagueTarget !== null ){
 202+ # Be aware that the != '' check is explicit, since empty values will be passed by some callers.
 203+ if( $vagueTarget != ''){
203204 list( $target, $type ) = self::parseTarget( $vagueTarget );
204205 switch( $type ) {
205206 case self::TYPE_USER:
@@ -962,7 +963,7 @@
963964 * 1.2.3.4 will not select 1.2.0.0/16 or even 1.2.3.4/32)
964965 * @param $vagueTarget String|User|Int as above, but we will search for *any* block which
965966 * affects that target (so for an IP address, get ranges containing that IP; and also
966 - * get any relevant autoblocks)
 967+ * get any relevant autoblocks). Leave empty or blank to skip IP-based lookups.
967968 * @param $fromMaster Bool whether to use the DB_MASTER database
968969 * @return Block|null (null if no relevant block could be found). The target and type
969970 * of the returned Block will refer to the actual block which was found, which might
@@ -979,8 +980,9 @@
980981 if( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ){
981982 return Block::newFromID( $target );
982983
983 - } elseif( $target === null && $vagueTarget === null ){
 984+ } elseif( $target === null && $vagueTarget == '' ){
984985 # We're not going to find anything useful here
 986+ # Be aware that the == '' check is explicit, since empty values will be passed by some callers.
985987 return null;
986988
987989 } elseif( in_array( $type, array( Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ) ) ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r90874Follow-up r88738: this check is no longer needed since r88750.happy-melon13:48, 27 June 2011
r92330REL1_18 MFT r88750, r88870, r88871, r89003, r89005, r89114, r89115, r89129, r...reedy22:56, 15 July 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r84475Blame hashar for this giant commit; he teased me for making so many smaller o...happy-melon19:12, 21 March 2011
r88738(bug 29116) follow-up r84475: normalise the empty string to null in Block::ne...happy-melon19:03, 24 May 2011

Status & tagging log