Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| UpdateBetaFeatureUserCountsJob | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
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 MediaWiki\JobQueue\Job; |
| 29 | use Wikimedia\Rdbms\IConnectionProvider; |
| 30 | |
| 31 | class UpdateBetaFeatureUserCountsJob extends Job { |
| 32 | |
| 33 | /** @inheritDoc */ |
| 34 | public function __construct( |
| 35 | array $params, |
| 36 | private readonly IConnectionProvider $dbProvider, |
| 37 | ) { |
| 38 | parent::__construct( 'updateBetaFeaturesUserCounts', $params ); |
| 39 | $this->removeDuplicates = true; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Execute the job |
| 44 | * |
| 45 | * @return bool |
| 46 | */ |
| 47 | public function run() { |
| 48 | $dbw = $this->dbProvider->getPrimaryDatabase(); |
| 49 | |
| 50 | $res = $dbw->newSelectQueryBuilder() |
| 51 | ->select( [ |
| 52 | 'number' => 'COUNT(up_property)', |
| 53 | 'feature' => 'up_property' |
| 54 | ] ) |
| 55 | ->from( 'user_properties' ) |
| 56 | ->where( [ |
| 57 | // Database would convert true to '1' |
| 58 | 'up_value' => '1', |
| 59 | 'up_property' => $this->params['prefs'] |
| 60 | ] ) |
| 61 | ->groupBy( 'up_property' ) |
| 62 | ->caller( __METHOD__ )->fetchResultSet(); |
| 63 | |
| 64 | $rows = []; |
| 65 | foreach ( $res as $row ) { |
| 66 | $rows[] = [ |
| 67 | 'feature' => $row->feature, |
| 68 | 'number' => $row->number, |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | // Save 0 when there are no more users in this beta feature (T362017) |
| 73 | if ( count( $this->params['prefs'] ) > count( $rows ) ) { |
| 74 | $updatedRows = array_map( static function ( $v ) { |
| 75 | return $v['feature']; |
| 76 | }, $rows ); |
| 77 | foreach ( array_diff( $this->params['prefs'], $updatedRows ) as $pref ) { |
| 78 | $rows[] = [ |
| 79 | 'feature' => $pref, |
| 80 | 'number' => 0, |
| 81 | ]; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if ( $rows ) { |
| 86 | $dbw->newReplaceQueryBuilder() |
| 87 | ->replaceInto( 'betafeatures_user_counts' ) |
| 88 | ->uniqueIndexFields( 'feature' ) |
| 89 | ->rows( $rows ) |
| 90 | ->caller( __METHOD__ ) |
| 91 | ->execute(); |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | } |