Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
BatchAntiSpoof
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 7
110
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
 batchRecord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTableName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserColumn
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPrimaryKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeSpoofUser
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 24
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
19use MediaWiki\Extension\AntiSpoof\SpoofUser;
20use MediaWiki\MediaWikiServices;
21
22$IP = getenv( 'MW_INSTALL_PATH' );
23if ( $IP === false ) {
24    $IP = __DIR__ . '/../../..';
25}
26require_once "$IP/maintenance/Maintenance.php";
27
28/**
29 * Go through all usernames and calculate and record spoof thingies
30 */
31class BatchAntiSpoof extends Maintenance {
32
33    public function __construct() {
34        parent::__construct();
35
36        $this->requireExtension( 'AntiSpoof' );
37        $this->setBatchSize( 1000 );
38    }
39
40    /**
41     * @param array $items
42     */
43    protected function batchRecord( $items ) {
44        SpoofUser::batchRecord( $this->getDB( DB_PRIMARY ), $items );
45    }
46
47    /**
48     * @return string
49     */
50    protected function getTableName() {
51        return 'user';
52    }
53
54    /**
55     * @return string
56     */
57    protected function getUserColumn() {
58        return 'user_name';
59    }
60
61    /**
62     * @return string Primary key of the table returned by getTableName()
63     */
64    protected function getPrimaryKey() {
65        return 'user_id';
66    }
67
68    /**
69     * @param string $name
70     * @return SpoofUser
71     */
72    protected function makeSpoofUser( $name ) {
73        return new SpoofUser( $name );
74    }
75
76    /**
77     * Do the actual work. All child classes will need to implement this
78     */
79    public function execute() {
80        $this->output( "Creating username spoofs...\n" );
81
82        $userCol = $this->getUserColumn();
83        $iterator = new BatchRowIterator( $this->getDB( DB_PRIMARY ),
84            $this->getTableName(),
85            $this->getPrimaryKey(),
86            $this->getBatchSize()
87        );
88        $iterator->setFetchColumns( [ $userCol ] );
89        $iterator->setCaller( __METHOD__ );
90        $services = MediaWikiServices::getInstance();
91        $lbFactory = $services->getDBLoadBalancerFactory();
92        $tempUserConfig = $services->getTempUserConfig();
93
94        $n = 0;
95        foreach ( $iterator as $batch ) {
96            $items = [];
97            foreach ( $batch as $row ) {
98                if ( $tempUserConfig->isTempName( $row->$userCol ) ) {
99                    continue;
100                }
101                $items[] = $this->makeSpoofUser( $row->$userCol );
102            }
103
104            $n += count( $items );
105            $this->output( "...$n\n" );
106            $this->batchRecord( $items );
107            $lbFactory->waitForReplication();
108        }
109
110        $this->output( "$n user(s) done.\n" );
111    }
112}