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     */
31    public function addOverrideWatched( Title $title ) {
32        $this->overrides[$title->getNamespace()][$title->getDBkey()] = true;
33    }
34
35    /**
36     * @param string[] $titles Array of UUID strings
37     * @return array
38     */
39    public function getWatchStatus( array $titles ) {
40        $titles = array_unique( $titles );
41        $result = array_fill_keys( $titles, false );
42
43        if ( !$this->user->getId() ) {
44            return $result;
45        }
46
47        $queryTitles = [];
48        foreach ( $titles as $id ) {
49            $obj = Title::makeTitleSafe( NS_TOPIC, $id );
50            if ( $obj ) {
51                $key = $obj->getDBkey();
52                if ( isset( $this->overrides[$obj->getNamespace()][$key] ) ) {
53                    $result[strtolower( $key )] = true;
54                } else {
55                    $queryTitles[] = $obj->getDBkey();
56                }
57            }
58        }
59
60        if ( !$queryTitles ) {
61            return $result;
62        }
63
64        $res = $this->watchListDb->newSelectQueryBuilder()
65            ->select( 'wl_title' )
66            ->from( 'watchlist' )
67            ->where( [
68                'wl_user' => $this->user->getId(),
69                'wl_namespace' => NS_TOPIC,
70                'wl_title' => $queryTitles
71            ] )
72            ->caller( __METHOD__ )
73            ->fetchResultSet();
74        foreach ( $res as $row ) {
75            $result[strtolower( $row->wl_title )] = true;
76        }
77        return $result;
78    }
79
80    /**
81     * @return User
82     */
83    public function getUser() {
84        return $this->user;
85    }
86
87    /**
88     * @return IReadableDatabase
89     */
90    public function getWatchlistDb() {
91        return $this->watchListDb;
92    }
93}