Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.56% covered (warning)
73.56%
64 / 87
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateCulComment
79.01% covered (warning)
79.01%
64 / 81
33.33% covered (danger)
33.33%
1 / 3
12.12
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
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
77.46% covered (warning)
77.46%
55 / 71
0.00% covered (danger)
0.00%
0 / 1
9.93
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
21namespace MediaWiki\CheckUser\Maintenance;
22
23use LoggedUpdateMaintenance;
24use MediaWiki\CheckUser\Services\CheckUserLogService;
25use MediaWiki\MediaWikiServices;
26use Psr\Log\NullLogger;
27use Wikimedia\Services\NoSuchServiceException;
28
29$IP = getenv( 'MW_INSTALL_PATH' );
30if ( $IP === false ) {
31    $IP = __DIR__ . '/../../..';
32}
33require_once "$IP/maintenance/Maintenance.php";
34
35/**
36 * Maintenance script for filling up cul_reason_id
37 *  and cul_reason_plaintext_id
38 *
39 * @author Modified version of populateCucComment by Zabe
40 */
41class PopulateCulComment extends LoggedUpdateMaintenance {
42
43    public function __construct() {
44        parent::__construct();
45        $this->requireExtension( 'CheckUser' );
46        $this->addDescription( 'Populate the cul_reason_id and cul_reason_plaintext_id columns.' );
47        $this->addOption(
48            'sleep',
49            'Sleep time (in seconds) between every batch. Default: 0',
50            false,
51            true
52        );
53    }
54
55    /**
56     * @inheritDoc
57     */
58    protected function getUpdateKey() {
59        return 'PopulateCulComment-2';
60    }
61
62    /**
63     * @inheritDoc
64     */
65    protected function doDBUpdates() {
66        $services = MediaWikiServices::getInstance();
67        $commentStore = $services->getCommentStore();
68        try {
69            /** @var CheckUserLogService $checkUserLogService */
70            $checkUserLogService = $services->get( 'CheckUserLogService' );
71        } catch ( NoSuchServiceException $ex ) {
72            # CheckUser ServiceWiring files may not loaded until
73            #  postDatabaseUpdateMaintenance is run.
74            # If this is the case, manually get the service.
75            $checkUserLogService = new CheckUserLogService(
76                $services->getDBLoadBalancerFactory(),
77                $services->getCommentStore(),
78                $services->getCommentFormatter(),
79                // No need to log as this maintenance script does not use any methods
80                //  that use the logger.
81                new NullLogger(),
82                $services->getActorStore(),
83                $services->getUserIdentityLookup()
84            );
85        }
86        $mainLb = $services->getDBLoadBalancerFactory()->getMainLB();
87        $dbr = $mainLb->getConnection( DB_REPLICA, 'vslow' );
88        $dbw = $mainLb->getMaintenanceConnectionRef( DB_PRIMARY );
89        $batchSize = $this->getBatchSize();
90
91        $prevId = 1;
92        $curId = $prevId + $batchSize;
93        $maxId = (int)$dbr->newSelectQueryBuilder()
94            ->field( 'MAX(cul_id)' )
95            ->table( 'cu_log' )
96            ->caller( __METHOD__ )
97            ->fetchField();
98
99        if ( !$maxId ) {
100            $this->output( "The cu_log table seems to be empty.\n" );
101            return true;
102        }
103
104        if ( !$dbw->fieldExists( 'cu_log', 'cul_reason' ) ) {
105            $this->output( "The cul_reason field does not exist which is needed for migration.\n" );
106            return true;
107        }
108
109        $this->output( "Populating the cul_reason_id and cul_reason_plaintext_id columns...\n" );
110
111        $diff = $maxId - $prevId;
112        if ( $batchSize > $diff ) {
113            $batchSize = $diff;
114        }
115        $failed = 0;
116        $sleep = (int)$this->getOption( 'sleep', 0 );
117
118        do {
119            $res = $dbr->newSelectQueryBuilder()
120                ->fields( [ 'cul_id', 'cul_reason' ] )
121                ->table( 'cu_log' )
122                ->conds( [
123                    'cul_reason_id' => [ 0, null ],
124                    $dbr->expr( 'cul_id', '>=', $prevId ),
125                    $dbr->expr( 'cul_id', '<=', $curId ),
126                ] )
127                ->caller( __METHOD__ )
128                ->fetchResultSet();
129
130            foreach ( $res as $row ) {
131                $culReasonId = $commentStore->createComment( $dbw, $row->cul_reason )->id;
132                $culReasonPlaintextId = $commentStore->createComment(
133                    $dbw, $checkUserLogService->getPlaintextReason( $row->cul_reason )
134                )->id;
135
136                if ( !$culReasonId || !$culReasonPlaintextId ) {
137                    $failed++;
138                    continue;
139                }
140
141                $dbw->newUpdateQueryBuilder()
142                    ->update( 'cu_log' )
143                    ->set( [
144                        'cul_reason_id' => $culReasonId,
145                        'cul_reason_plaintext_id' => $culReasonPlaintextId,
146                    ] )
147                    ->where( [ 'cul_id' => $row->cul_id ] )
148                    ->caller( __METHOD__ )
149                    ->execute();
150            }
151
152            $this->waitForReplication();
153
154            if ( $sleep > 0 ) {
155                sleep( $sleep );
156            }
157
158            $this->output( "Processed $batchSize rows out of $diff.\n" );
159
160            $prevId = $curId;
161            $curId += $batchSize;
162        } while ( $prevId <= $maxId );
163
164        $this->output( "Done. Migration failed for $failed row(s).\n" );
165        return true;
166    }
167}
168
169$maintClass = PopulateCulComment::class;
170require_once RUN_MAINTENANCE_IF_MAIN;