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