Prevent loading or saving of zeros in the database.

When intval() fails, the function returns a zero. We should remove
the failures from the blacklist.

Bug: T178512
Change-Id: I89ad680a287da16c2fbd6aa4d53a725142429144
This commit is contained in:
David Barratt 2017-11-13 11:36:11 -05:00
parent f97ce8f437
commit 6593a0a427
3 changed files with 67 additions and 5 deletions

View file

@ -1252,7 +1252,7 @@ class EchoHooks {
}
if ( isset( $options['echo-notifications-blacklist'] ) ) {
$options['echo-notifications-blacklist'] = array_map( 'intval', explode( "\n", $options['echo-notifications-blacklist'] ) );
$options['echo-notifications-blacklist'] = self::mapToInt( explode( "\n", $options['echo-notifications-blacklist'] ) );
}
return true;
@ -1287,8 +1287,16 @@ class EchoHooks {
$ids = $lookup->centralIdsFromNames( $names, $user );
}
$user->setOption( 'echo-notifications-blacklist', $ids );
$options['echo-notifications-blacklist'] = implode( "\n", $user->getOption( 'echo-notifications-blacklist' ) );
$ids = self::mapToInt( $ids );
if ( count( $ids ) > 0 ) {
$user->setOption( 'echo-notifications-blacklist', $ids );
$options['echo-notifications-blacklist'] = implode( "\n", $user->getOption( 'echo-notifications-blacklist' ) );
} else {
// If the blacklist is empty, set it to null rather than an empty
// string.
$options['echo-notifications-blacklist'] = null;
}
} else {
// If the blacklist is empty, set it to null rather than an empty string.
$options['echo-notifications-blacklist'] = null;
@ -1298,6 +1306,27 @@ class EchoHooks {
return true;
}
/**
* Convert all values in an array to integers and filter out zeroes.
*
* @param array $numbers
*
* @return int[]
*/
protected static function mapToInt( array $numbers ) {
$data = [];
foreach ( $numbers as $value ) {
$int = intval( $value );
if ( $int === 0 ) {
continue;
}
$data[] = $int;
}
return $data;
}
/**
* Handler for UserClearNewTalkNotification hook.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/UserClearNewTalkNotification

View file

@ -75,8 +75,8 @@ class EchoContainmentSet {
if ( $preference ) {
$lookup = CentralIdLookup::factory();
$names = $lookup->lookupCentralIds( array_flip( $preference ), $this->recipient );
$this->addArray( array_values( $names ) );
$names = $lookup->namesFromCentralIds( $preference, $this->recipient );
$this->addArray( $names );
}
}

View file

@ -0,0 +1,33 @@
<?php
class EchoHooksTest extends MediaWikiTestCase {
/**
* Test the UserSaveOptions hook implementation.
*/
public function testOnUserSaveOptions() {
$options['echo-notifications-blacklist'] = [
'',
'0',
'abcdef',
'12345',
'54321',
];
EchoHooks::onUserSaveOptions( new User(), $options );
$this->assertSame( "12345\n54321", $options['echo-notifications-blacklist'] );
}
/**
* Test the UserLoadOptions hook implementation.
*/
public function testOnUserLoadOptions() {
$options['echo-notifications-blacklist'] = "12345\n54321";
EchoHooks::onUserLoadOptions( new User(), $options );
$this->assertSame( [ 12345, 54321 ], $options['echo-notifications-blacklist'] );
}
}