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