Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.67% |
20 / 30 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
GlobalUserOptionsStore | |
66.67% |
20 / 30 |
|
25.00% |
1 / 4 |
17.33 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
fetch | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
store | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
6.60 | |||
getStorage | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 |
1 | <?php |
2 | |
3 | namespace GlobalPreferences; |
4 | |
5 | use MediaWiki\Logger\LoggerFactory; |
6 | use MediaWiki\User\CentralId\CentralIdLookup; |
7 | use MediaWiki\User\Options\UserOptionsStore; |
8 | use MediaWiki\User\UserIdentity; |
9 | use Psr\Log\LoggerInterface; |
10 | use Wikimedia\Rdbms\DBAccessObjectUtils; |
11 | use Wikimedia\Rdbms\IDBAccessObject; |
12 | |
13 | /** |
14 | * An interface which allows core to update pre-existing global preferences |
15 | */ |
16 | class GlobalUserOptionsStore implements UserOptionsStore { |
17 | |
18 | /** @var CentralIdLookup */ |
19 | private $centralIdLookup; |
20 | |
21 | /** @var LoggerInterface */ |
22 | private $logger; |
23 | |
24 | public function __construct( CentralIdLookup $centralIdLookup ) { |
25 | $this->centralIdLookup = $centralIdLookup; |
26 | $this->logger = LoggerFactory::getInstance( 'preferences' ); |
27 | } |
28 | |
29 | /** |
30 | * @param UserIdentity $user |
31 | * @param int $recency |
32 | * @return array|string[] |
33 | */ |
34 | public function fetch( UserIdentity $user, int $recency ) { |
35 | $storage = $this->getStorage( $user ); |
36 | if ( !$storage ) { |
37 | return []; |
38 | } |
39 | if ( DBAccessObjectUtils::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) { |
40 | $dbType = DB_PRIMARY; |
41 | } else { |
42 | $dbType = DB_REPLICA; |
43 | } |
44 | return $storage->loadFromDB( $dbType, $recency ); |
45 | } |
46 | |
47 | /** |
48 | * @param UserIdentity $user |
49 | * @param array $updates |
50 | * @return bool |
51 | */ |
52 | public function store( UserIdentity $user, array $updates ) { |
53 | $storage = $this->getStorage( $user ); |
54 | if ( !$storage ) { |
55 | $this->logger->warning( |
56 | 'Unable to store preference for non-global user "{userName}"', |
57 | [ 'userName' => $user->getName() ] |
58 | ); |
59 | return false; |
60 | } |
61 | $replacements = []; |
62 | $deletions = []; |
63 | foreach ( $updates as $key => $value ) { |
64 | if ( $value === null ) { |
65 | $deletions[] = $key; |
66 | } else { |
67 | $replacements[$key] = $value; |
68 | } |
69 | } |
70 | $storage->replaceAndDelete( $replacements, $deletions ); |
71 | return $replacements || $deletions; |
72 | } |
73 | |
74 | private function getStorage( UserIdentity $user ): ?Storage { |
75 | if ( !$this->centralIdLookup->isOwned( $user ) ) { |
76 | return null; |
77 | } |
78 | $id = $this->centralIdLookup->centralIdFromName( $user->getName() ); |
79 | if ( !$id ) { |
80 | return null; |
81 | } |
82 | return new Storage( $id ); |
83 | } |
84 | } |