Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.22% |
13 / 18 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| StaticUserOptionsLookup | |
76.47% |
13 / 17 |
|
80.00% |
4 / 5 |
10.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOption | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getOptions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getOptionBatchForUserNames | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\User\Options; |
| 4 | |
| 5 | use MediaWiki\User\UserIdentity; |
| 6 | use Wikimedia\Rdbms\IDBAccessObject; |
| 7 | |
| 8 | /** |
| 9 | * A UserOptionsLookup that's just an array. Useful for testing and creating staging environments. |
| 10 | * Note that unlike UserOptionsManager, no attempt is made to canonicalize user names. |
| 11 | * @since 1.36 |
| 12 | */ |
| 13 | class StaticUserOptionsLookup extends UserOptionsLookup { |
| 14 | |
| 15 | /** @var array[] */ |
| 16 | private $userMap; |
| 17 | |
| 18 | /** @var mixed[] */ |
| 19 | private $defaults; |
| 20 | |
| 21 | /** |
| 22 | * @param array[] $userMap User options, username => [ option name => value ] |
| 23 | * @param mixed[] $defaults Defaults for each option, option name => value |
| 24 | */ |
| 25 | public function __construct( array $userMap, array $defaults = [] ) { |
| 26 | $this->userMap = $userMap; |
| 27 | $this->defaults = $defaults; |
| 28 | } |
| 29 | |
| 30 | /** @inheritDoc */ |
| 31 | public function getDefaultOptions( ?UserIdentity $userIdentity = null ): array { |
| 32 | return $this->defaults; |
| 33 | } |
| 34 | |
| 35 | /** @inheritDoc */ |
| 36 | public function getOption( |
| 37 | UserIdentity $user, |
| 38 | string $oname, |
| 39 | $defaultOverride = null, |
| 40 | bool $ignoreHidden = false, |
| 41 | int $queryFlags = IDBAccessObject::READ_NORMAL |
| 42 | ) { |
| 43 | $userOptions = $this->getOptions( $user ); |
| 44 | if ( array_key_exists( $oname, $userOptions ) ) { |
| 45 | return $userOptions[$oname]; |
| 46 | } else { |
| 47 | return $defaultOverride; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** @inheritDoc */ |
| 52 | public function getOptions( |
| 53 | UserIdentity $user, |
| 54 | int $flags = 0, |
| 55 | int $queryFlags = IDBAccessObject::READ_NORMAL |
| 56 | ): array { |
| 57 | $userOptions = []; |
| 58 | if ( $user->isRegistered() ) { |
| 59 | $userOptions = $this->userMap[$user->getName()] ?? []; |
| 60 | } |
| 61 | if ( !( $flags & self::EXCLUDE_DEFAULTS ) ) { |
| 62 | $userOptions += $this->defaults; |
| 63 | } |
| 64 | return $userOptions; |
| 65 | } |
| 66 | |
| 67 | /** @inheritDoc */ |
| 68 | public function getOptionBatchForUserNames( array $users, string $key ) { |
| 69 | $options = []; |
| 70 | foreach ( $users as $name ) { |
| 71 | $options[$name] = $this->userMap[$name][$key] ?? $this->defaults[$key] ?? ''; |
| 72 | } |
| 73 | return $options; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** @deprecated class alias since 1.42 */ |
| 78 | class_alias( StaticUserOptionsLookup::class, 'MediaWiki\User\StaticUserOptionsLookup' ); |