Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
49.25% covered (danger)
49.25%
33 / 67
33.33% covered (danger)
33.33%
3 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
SecurePollLogPager
49.25% covered (danger)
49.25%
33 / 67
33.33% covered (danger)
33.33%
3 / 9
66.18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getQueryInfo
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultQuery
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getFilterConds
50.00% covered (danger)
50.00%
11 / 22
0.00% covered (danger)
0.00%
0 / 1
16.00
 getIndexField
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 formatRow
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 getStartBody
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getEndBody
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getEmptyBody
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\SecurePoll;
4
5use MediaWiki\Extension\SecurePoll\Pages\ActionPage;
6use MediaWiki\Html\Html;
7use MediaWiki\Linker\Linker;
8use MediaWiki\Pager\ReverseChronologicalPager;
9use MediaWiki\User\UserFactory;
10
11class SecurePollLogPager extends ReverseChronologicalPager {
12    /** @var Context */
13    private $context;
14
15    /** @var UserFactory */
16    private $userFactory;
17
18    /** @var string */
19    private $type;
20
21    /** @var string */
22    private $performer;
23
24    /** @var string */
25    private $target;
26
27    /** @var string */
28    private $electionName;
29
30    /** @var int[] */
31    private $actions;
32
33    /**
34     * @param Context $context
35     * @param UserFactory $userFactory
36     * @param string $type
37     * @param string $performer
38     * @param string $target
39     * @param string $electionName
40     * @param int $year
41     * @param int $month
42     * @param int $day
43     * @param int[] $actions
44     */
45    public function __construct(
46        Context $context,
47        UserFactory $userFactory,
48        string $type,
49        string $performer,
50        string $target,
51        string $electionName,
52        int $year,
53        int $month,
54        int $day,
55        array $actions
56    ) {
57        parent::__construct();
58        $this->context = $context;
59        $this->userFactory = $userFactory;
60        $this->type = $type;
61        $this->performer = $performer;
62        $this->target = $target;
63        $this->electionName = $electionName;
64        $this->actions = $actions;
65
66        $this->getDateCond( $year, $month, $day );
67    }
68
69    /**
70     * @inheritDoc
71     */
72    public function getQueryInfo() {
73        return [
74            'tables' => [ 'securepoll_log' ],
75            'fields' => [
76                'spl_id',
77                'spl_timestamp',
78                'spl_election_id',
79                'spl_user',
80                'spl_type',
81                'spl_target',
82            ],
83            'conds' => $this->getFilterConds(),
84        ];
85    }
86
87    /**
88     * @inheritDoc
89     */
90    public function getDefaultQuery() {
91        parent::getDefaultQuery();
92        unset( $this->mDefaultQuery['date'] );
93        return $this->mDefaultQuery;
94    }
95
96    private function getFilterConds() {
97        $conds = [];
98
99        switch ( $this->type ) {
100            case 'voter':
101                $type = ActionPage::LOG_TYPE_VIEWVOTES;
102                break;
103            case 'admin':
104                $type = $this->actions;
105                break;
106            default:
107                $type = null;
108                break;
109        }
110        if ( $type ) {
111            $conds['spl_type'] = $type;
112        }
113
114        if ( $this->performer ) {
115            $performer = $this->userFactory->newFromName( $this->performer )->getId();
116            $conds['spl_user'] = $performer;
117        }
118
119        if ( $this->target ) {
120            $target = $this->userFactory->newFromName( $this->target )->getId();
121            $conds['spl_target'] = $target;
122        }
123
124        if ( $this->electionName ) {
125            $electionId = $this->context->getElectionByTitle( $this->electionName )->getId();
126            $conds['spl_election_id'] = $electionId;
127        }
128
129        return $conds;
130    }
131
132    /**
133     * @inheritDoc
134     */
135    public function getIndexField() {
136        return [ [ 'spl_timestamp', 'spl_id' ] ];
137    }
138
139    /**
140     * @inheritDoc
141     */
142    public function formatRow( $row ) {
143        $timestamp = $this->getLanguage()->timeanddate(
144            wfTimestamp( TS_MW, $row->spl_timestamp ),
145            true
146        );
147
148        $user = $this->userFactory->newFromId( $row->spl_user );
149        $userLink = Linker::userLink( $user->getId(), $user->getName() );
150
151        $election = $this->context->getElection( $row->spl_election_id );
152        // TODO: this is double escaped
153        $electionTitle = htmlspecialchars( $election->title );
154
155        $message = $this->msg(
156            'securepoll-log-action-type-' . $row->spl_type, $timestamp );
157
158        $message->Rawparams( $userLink );
159        $message->Rawparams( $electionTitle );
160
161        if ( $row->spl_target ) {
162            $target = $this->userFactory->newFromId( $row->spl_target );
163            $targetLink = Linker::userLink( $target->getId(), $target->getName() );
164
165            $message->RawParams( $targetLink );
166        }
167
168        return Html::RawElement( 'li', [], $message->parse() );
169    }
170
171    /**
172     * @inheritDoc
173     */
174    protected function getStartBody() {
175        return $this->getNumRows() ? '<ul>' : '';
176    }
177
178    /**
179     * @inheritDoc
180     */
181    protected function getEndBody() {
182        return $this->getNumRows() ? '</ul>' : '';
183    }
184
185    /**
186     * @inheritDoc
187     */
188    protected function getEmptyBody() {
189        return Html::element( 'p', [], $this->msg( 'securepoll-log-empty' )->text() );
190    }
191}