Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateCaUserColumn
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
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
56
1<?php
2/**
3 * Maintenance script to populate the ca_user column in the code_authors table.
4 * Somewhat copypasted from repopulateCodePaths.php.
5 *
6 * @file
7 * @ingroup Maintenance
8 */
9
10use MediaWiki\Extension\CodeReview\Backend\CodeRepository;
11
12$IP = getenv( 'MW_INSTALL_PATH' );
13if ( $IP === false ) {
14    $IP = __DIR__ . '/../../..';
15}
16require_once "$IP/maintenance/Maintenance.php";
17
18class PopulateCaUserColumn extends Maintenance {
19    public function __construct() {
20        parent::__construct();
21        $this->addDescription( 'Populates the code_authors.ca_user column so that Renameuser ' .
22            'doesn\'t get confused.' );
23        $this->addOption( 'repo', 'The name of the repository. Cannot be all.', true, true );
24
25        $this->requireExtension( 'CodeReview' );
26    }
27
28    public function execute() {
29        $repoName = $this->getOption( 'repo' );
30        if ( !$repoName ) {
31            $this->fatalError( 'Need a repository name to do something!' );
32        }
33
34        // Oh come on!
35        if ( $repoName == 'all' ) {
36            $this->fatalError( "Cannot use the 'all' repo" );
37        }
38
39        $repo = CodeRepository::newFromName( $repoName );
40        if ( !$repo ) {
41            $this->fatalError( "Repo '{$repoName}' is not a valid repository" );
42        }
43
44        $dbr = wfGetDB( DB_REPLICA );
45
46        $res = $dbr->select(
47            'code_authors',
48            'ca_user_text',
49            [
50                'ca_repo_id' => $repo->getId(),
51                'ca_user' => 0
52            ],
53            __METHOD__
54        );
55
56        $numRows = $res->numRows();
57
58        // No results == nothing to do, so bail out
59        if ( $numRows == 0 ) {
60            $this->fatalError( "Nothing to do here, Captain!\n" );
61        }
62
63        // Show some information to the user so that they're aware that we're
64        // actually doing stuff, too, instead of goofing off
65        $this->output( "{$numRows} authors with ca_user = 0 in the table...\n" );
66
67        $dbw = wfGetDB( DB_PRIMARY );
68        $this->beginTransaction( $dbw, __METHOD__ );
69
70        foreach ( $res as $row ) {
71            $userId = User::idFromName( $row->ca_user_text );
72
73            // It's not paranoia if they're out to get you!
74            if ( $userId === null ) {
75                $this->output(
76                    "Unable to get the user ID for the user named: {$row->ca_user_text}\n"
77                );
78                continue;
79            }
80
81            $dbw->update(
82                'code_authors',
83                [ 'ca_user' => $userId ],
84                [
85                    'ca_repo_id' => $repo->getId(),
86                    'ca_user_text' => $row->ca_user_text
87                ],
88                __METHOD__
89            );
90        }
91
92        $this->commitTransaction( $dbw, __METHOD__ );
93
94        $this->output( "Done!\n" );
95    }
96}
97
98$maintClass = PopulateCaUserColumn::class;
99require_once RUN_MAINTENANCE_IF_MAIN;