Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.77% covered (warning)
80.77%
21 / 26
54.55% covered (warning)
54.55%
6 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Counter
80.77% covered (warning)
80.77%
21 / 26
54.55% covered (warning)
54.55%
6 / 11
17.82
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 onEditSuccess
n/a
0 / 0
n/a
0 / 0
0
 onRevert
n/a
0 / 0
n/a
0 / 0
0
 isRevertCountingEnabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEditCountForLang
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 setEditCountForLang
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 incrementEditCountForLang
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 decrementEditCountForLang
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateEditStreak
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 getEditStreak
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 incrementRevertCountForLang
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRevertCountForLang
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * @file
18 */
19
20namespace MediaWiki\Extension\WikimediaEditorTasks;
21
22use MediaWiki\Request\WebRequest;
23use MediaWiki\Revision\RevisionRecord;
24
25/**
26 * Base counter class containing most of the logic for interacting with the DAO.  Subclasses must
27 * implement onEditSuccess and onRevert methods containing any custom filtering logic according to
28 * the counts they are meant to maintain (e.g., in-app description edits).  It is expected that
29 * subclasses will call this class' DAO interaction methods (e.g., incrementForLang) after any
30 * required filtering.
31 */
32abstract class Counter {
33
34    /** @var CounterDao */
35    private $dao;
36
37    /** @var int */
38    private $keyId;
39
40    /** @var bool */
41    private $editStreaksEnabled;
42
43    /** @var bool */
44    private $revertCountsEnabled;
45
46    /**
47     * @param int $keyId edit counter key ID
48     * @param CounterDao $dao
49     * @param bool $editStreaksEnabled
50     * @param bool $revertCountsEnabled
51     */
52    public function __construct(
53        int $keyId,
54        CounterDao $dao,
55        bool $editStreaksEnabled,
56        bool $revertCountsEnabled
57    ) {
58        $this->keyId = $keyId;
59        $this->dao = $dao;
60        $this->editStreaksEnabled = $editStreaksEnabled;
61        $this->revertCountsEnabled = $revertCountsEnabled;
62    }
63
64    /**
65     * Specifies the action to take when a successful edit is made.
66     * E.g., increment a counter if the edit is an in-app Wikidata description edit.
67     * @param int $centralId central ID user who edited
68     * @param WebRequest $request the request object
69     * @param RevisionRecord $revision revision representing the successful edit
70     */
71    abstract public function onEditSuccess( int $centralId, WebRequest $request,
72        RevisionRecord $revision ): void;
73
74    /**
75     * Specifies the action to take when a revert is performed.
76     * E.g., increment revert counter.
77     * Note: this is currently called specifically in response to undo and rollback actions,
78     * although in principle this class is agnostic with respect to the definition of "revert"
79     * used.
80     * @param int $centralId central ID of the user who was reverted
81     * @param int $revisionId revision ID of the reverted edit
82     * @param RevisionRecord $revision RevisionRecord corresponding with $revisionID
83     */
84    abstract public function onRevert(
85        int $centralId,
86        int $revisionId,
87        RevisionRecord $revision
88    ): void;
89
90    /** @return bool */
91    protected function isRevertCountingEnabled(): bool {
92        return $this->revertCountsEnabled;
93    }
94
95    /**
96     * Get count for lang for user
97     * @param int $centralId central ID of the user
98     * @param string $lang language code
99     * @return int|bool value of counter, or false if row does not exist
100     */
101    protected function getEditCountForLang( $centralId, $lang ) {
102        $count = $this->dao->getEditCountForKeyAndLang( $centralId, $this->keyId, $lang );
103        if ( $count ) {
104            return (int)$count;
105        }
106        return false;
107    }
108
109    /**
110     * Set count for lang for user
111     * @param int $centralId
112     * @param string $lang language code
113     * @param int $count value to set
114     */
115    protected function setEditCountForLang( $centralId, $lang, $count ) {
116        $this->dao->setEditCountForKeyAndLang( $centralId, $this->keyId, $lang, $count );
117    }
118
119    /**
120     * Increment count for lang and user
121     * @param int $centralId central ID of the user
122     * @param string $lang language code
123     */
124    protected function incrementEditCountForLang( $centralId, $lang ) {
125        if ( $this->getEditCountForLang( $centralId, $lang ) ) {
126            $this->dao->incrementEditCountForKeyAndLang( $centralId, $this->keyId, $lang );
127        } else {
128            $this->setEditCountForLang( $centralId, $lang, 1 );
129        }
130    }
131
132    /**
133     * Decrement count for user
134     * @param int $centralId central ID of the user
135     * @param string $lang language code
136     */
137    protected function decrementEditCountForLang( $centralId, $lang ) {
138        $this->dao->decrementEditCountForKeyAndLang( $centralId, $this->keyId, $lang );
139    }
140
141    /**
142     * Reset count for user
143     * @param int $centralId central ID of the user
144     */
145    protected function reset( $centralId ) {
146        $this->dao->deleteAllCountsForKey( $centralId, $this->keyId );
147    }
148
149    /**
150     * Update the edit streak length and last edit time for user
151     * @param int $centralId central ID of the user
152     */
153    protected function updateEditStreak( $centralId ) {
154        if ( !$this->editStreaksEnabled ) {
155            return;
156        }
157        $this->dao->setEditStreak( $centralId );
158    }
159
160    /**
161     * Get the edit streak length and last edit time for user
162     * @param int $centralId central ID of the user
163     * @return array[]|false An array contains current streak length and last edit time
164     */
165    protected function getEditStreak( $centralId ) {
166        if ( !$this->editStreaksEnabled ) {
167            return false;
168        }
169        return $this->dao->getEditStreak( $centralId );
170    }
171
172    /** Increment revert count for lang and user
173     * @param int $centralId central ID of the user
174     * @param string $lang language code
175     */
176    protected function incrementRevertCountForLang( $centralId, $lang ) {
177        $this->dao->incrementRevertCountForKeyAndLang( $centralId, $this->keyId, $lang );
178    }
179
180    /**
181     * Get revert count for lang for user
182     * @param int $centralId central ID of the user
183     * @param string $lang language code
184     * @return int|bool value of revert counter, or false if row does not exist
185     */
186    protected function getRevertCountForLang( $centralId, $lang ) {
187        $count = $this->dao->getRevertCountForKeyAndLang( $centralId, $this->keyId, $lang );
188        if ( $count ) {
189            return (int)$count;
190        }
191        return false;
192    }
193}