Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
UpdateBetaFeatureUserCountsJob | |
0.00% |
0 / 39 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** |
3 | * This file is part of the MediaWiki extension BetaFeatures. |
4 | * |
5 | * BetaFeatures is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * BetaFeatures is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with BetaFeatures. If not, see <http://www.gnu.org/licenses/>. |
17 | * |
18 | * Job to update BetaFeatures user counts |
19 | * |
20 | * @file |
21 | * @ingroup Extensions |
22 | * @copyright 2013 Mark Holmquist and others; see AUTHORS |
23 | * @license GPL-2.0-or-later |
24 | */ |
25 | |
26 | namespace MediaWiki\Extension\BetaFeatures; |
27 | |
28 | use Job; |
29 | use Wikimedia\Rdbms\IConnectionProvider; |
30 | |
31 | class UpdateBetaFeatureUserCountsJob extends Job { |
32 | private IConnectionProvider $dbProvider; |
33 | |
34 | /** @inheritDoc */ |
35 | public function __construct( |
36 | array $params, |
37 | IConnectionProvider $dbProvider |
38 | ) { |
39 | parent::__construct( 'updateBetaFeaturesUserCounts', $params ); |
40 | $this->dbProvider = $dbProvider; |
41 | $this->removeDuplicates = true; |
42 | } |
43 | |
44 | /** |
45 | * Execute the job |
46 | * |
47 | * @return bool |
48 | */ |
49 | public function run() { |
50 | $dbw = $this->dbProvider->getPrimaryDatabase(); |
51 | |
52 | $res = $dbw->newSelectQueryBuilder() |
53 | ->select( [ |
54 | 'number' => 'COUNT(up_property)', |
55 | 'feature' => 'up_property' |
56 | ] ) |
57 | ->from( 'user_properties' ) |
58 | ->where( [ |
59 | // Database would convert true to '1' |
60 | 'up_value' => '1', |
61 | 'up_property' => $this->params['prefs'] |
62 | ] ) |
63 | ->groupBy( 'up_property' ) |
64 | ->caller( __METHOD__ )->fetchResultSet(); |
65 | |
66 | $rows = []; |
67 | foreach ( $res as $row ) { |
68 | $rows[] = [ |
69 | 'feature' => $row->feature, |
70 | 'number' => $row->number, |
71 | ]; |
72 | } |
73 | |
74 | // Save 0 when there are no more users in this beta feature (T362017) |
75 | if ( count( $this->params['prefs'] ) > count( $rows ) ) { |
76 | $updatedRows = array_map( static function ( $v ) { |
77 | return $v['feature']; |
78 | }, $rows ); |
79 | foreach ( array_diff( $this->params['prefs'], $updatedRows ) as $pref ) { |
80 | $rows[] = [ |
81 | 'feature' => $pref, |
82 | 'number' => 0, |
83 | ]; |
84 | } |
85 | } |
86 | |
87 | if ( $rows ) { |
88 | $dbw->newReplaceQueryBuilder() |
89 | ->replaceInto( 'betafeatures_user_counts' ) |
90 | ->uniqueIndexFields( 'feature' ) |
91 | ->rows( $rows ) |
92 | ->caller( __METHOD__ ) |
93 | ->execute(); |
94 | } |
95 | |
96 | return true; |
97 | } |
98 | } |