Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PurgeExpiredGlobalRights
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
20
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 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21$IP = getenv( 'MW_INSTALL_PATH' );
22if ( $IP === false ) {
23    $IP = __DIR__ . '/../../..';
24}
25require_once "$IP/maintenance/Maintenance.php";
26
27use MediaWiki\Extension\CentralAuth\CentralAuthServices;
28use Wikimedia\Rdbms\OrExpressionGroup;
29
30/**
31 * @author Taavi "Majavah" Väänänen <hi@taavi.wtf>
32 */
33class PurgeExpiredGlobalRights extends Maintenance {
34    public function __construct() {
35        parent::__construct();
36        $this->requireExtension( 'CentralAuth' );
37        $this->setBatchSize( 50 );
38    }
39
40    public function execute() {
41        $databaseManager = CentralAuthServices::getDatabaseManager();
42        $dbw = $databaseManager->getCentralPrimaryDB();
43
44        $counter = 0;
45
46        while ( true ) {
47            $rows = $dbw->newSelectQueryBuilder()
48                ->select( [ 'gug_user', 'gug_group' ] )
49                ->from( 'global_user_groups' )
50                ->where( [ $dbw->expr( 'gug_expiry', '<', $dbw->timestamp() ), ] )
51                ->orderBy( 'gug_expiry' )
52                ->limit( $this->getBatchSize() )
53                ->caller( __METHOD__ )
54                ->fetchResultSet();
55
56            if ( $rows->numRows() === 0 ) {
57                break;
58            }
59
60            $conds = [];
61            foreach ( $rows as $row ) {
62                $conds[] = $dbw->expr( 'gug_user', '=', (int)$row->gug_user )
63                    ->and( 'gug_group', '=', $row->gug_group );
64            }
65
66            $dbw->delete(
67                'global_user_groups',
68                [ new OrExpressionGroup( ...$conds ) ],
69                __METHOD__
70            );
71
72            $counter += $dbw->affectedRows();
73
74            $this->output( "Purged $counter expired group memberships so far... waiting for replication to catch up." );
75            $databaseManager->waitForReplication();
76            $this->output( ".. done.\n" );
77        }
78
79        $this->output( "All done, purged $counter rows total.\n" );
80    }
81}
82
83$maintClass = PurgeExpiredGlobalRights::class;
84require_once RUN_MAINTENANCE_IF_MAIN;