Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.29% |
29 / 34 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
WatchedTopicItems | |
85.29% |
29 / 34 |
|
20.00% |
1 / 5 |
12.46 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addOverrideWatched | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getWatchStatus | |
93.10% |
27 / 29 |
|
0.00% |
0 / 1 |
8.02 | |||
getUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getWatchlistDb | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Flow; |
4 | |
5 | use Flow\Exception\DataModelException; |
6 | use MediaWiki\Title\Title; |
7 | use MediaWiki\User\User; |
8 | use Wikimedia\Rdbms\IReadableDatabase; |
9 | |
10 | /** |
11 | * Is there a core object for retrieving multiple watchlist items? |
12 | */ |
13 | class WatchedTopicItems { |
14 | |
15 | /** @var User */ |
16 | protected $user; |
17 | /** @var IReadableDatabase */ |
18 | protected $watchListDb; |
19 | /** @var true[][] */ |
20 | protected $overrides = []; |
21 | |
22 | public function __construct( User $user, IReadableDatabase $watchListDb ) { |
23 | $this->user = $user; |
24 | $this->watchListDb = $watchListDb; |
25 | } |
26 | |
27 | /** |
28 | * Helps prevent reading our own writes. If we have explicitly |
29 | * watched this title in this request set it here instead of |
30 | * querying a replica and possibly not noticing due to replica lag. |
31 | * @param Title $title |
32 | */ |
33 | public function addOverrideWatched( Title $title ) { |
34 | $this->overrides[$title->getNamespace()][$title->getDBkey()] = true; |
35 | } |
36 | |
37 | /** |
38 | * @param string[] $titles Array of UUID strings |
39 | * @return array |
40 | * @throws \Flow\Exception\DataModelException |
41 | */ |
42 | public function getWatchStatus( array $titles ) { |
43 | $titles = array_unique( $titles ); |
44 | $result = array_fill_keys( $titles, false ); |
45 | |
46 | if ( !$this->user->getId() ) { |
47 | return $result; |
48 | } |
49 | |
50 | $queryTitles = []; |
51 | foreach ( $titles as $id ) { |
52 | $obj = Title::makeTitleSafe( NS_TOPIC, $id ); |
53 | if ( $obj ) { |
54 | $key = $obj->getDBkey(); |
55 | if ( isset( $this->overrides[$obj->getNamespace()][$key] ) ) { |
56 | $result[strtolower( $key )] = true; |
57 | } else { |
58 | $queryTitles[$key] = $obj->getDBkey(); |
59 | } |
60 | } |
61 | } |
62 | |
63 | if ( !$queryTitles ) { |
64 | return $result; |
65 | } |
66 | |
67 | $res = $this->watchListDb->select( |
68 | [ 'watchlist' ], |
69 | [ 'wl_title' ], |
70 | [ |
71 | 'wl_user' => $this->user->getId(), |
72 | 'wl_namespace' => NS_TOPIC, |
73 | 'wl_title' => $queryTitles |
74 | ], |
75 | __METHOD__ |
76 | ); |
77 | if ( !$res ) { |
78 | throw new DataModelException( 'query failure', 'process-data' ); |
79 | } |
80 | foreach ( $res as $row ) { |
81 | $result[strtolower( $row->wl_title )] = true; |
82 | } |
83 | return $result; |
84 | } |
85 | |
86 | /** |
87 | * @return User |
88 | */ |
89 | public function getUser() { |
90 | return $this->user; |
91 | } |
92 | |
93 | /** |
94 | * @return IReadableDatabase |
95 | */ |
96 | public function getWatchlistDb() { |
97 | return $this->watchListDb; |
98 | } |
99 | } |