Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.21% covered (warning)
86.21%
50 / 58
71.43% covered (warning)
71.43%
10 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpoofUser
86.21% covered (warning)
86.21%
50 / 58
71.43% covered (warning)
71.43%
10 / 14
21.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 isLegal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrorStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNormalized
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTableName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserColumn
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConflicts
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 record
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 insertFields
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 batchRecord
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 update
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 remove
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getDBReplica
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDBPrimary
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare( strict_types=1 );
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20namespace MediaWiki\Extension\AntiSpoof;
21
22use MediaWiki\MediaWikiServices;
23use MediaWiki\Status\Status;
24use Wikimedia\Rdbms\IDatabase;
25use Wikimedia\Rdbms\IReadableDatabase;
26
27class SpoofUser {
28    private bool $legal;
29
30    private ?string $normalized;
31
32    private ?Status $error;
33
34    public function __construct(
35        private readonly string $name
36    ) {
37        $status = AntiSpoof::checkUnicodeStringStatus( $this->name );
38        $this->legal = $status->isOK();
39        if ( $this->legal ) {
40            $this->normalized = $status->getValue();
41            $this->error = null;
42        } else {
43            $this->normalized = null;
44            $this->error = $status;
45        }
46    }
47
48    /**
49     * Does the username pass Unicode legality and script-mixing checks?
50     */
51    public function isLegal(): bool {
52        return $this->legal;
53    }
54
55    /**
56     * Describe the error.
57     * @since 1.32
58     */
59    public function getErrorStatus(): ?Status {
60        return $this->error;
61    }
62
63    /**
64     * Get the normalized key form
65     */
66    public function getNormalized(): ?string {
67        return $this->normalized;
68    }
69
70    protected function getTableName(): string {
71        return 'user';
72    }
73
74    protected function getUserColumn(): string {
75        return 'user_name';
76    }
77
78    /**
79     * Does the username pass Unicode legality and script-mixing checks?
80     *
81     * @return string[] empty if no conflict, or array containing conflicting usernames
82     */
83    public function getConflicts(): array {
84        if ( !$this->isLegal() ) {
85            return [];
86        }
87
88        $dbr = $this->getDBReplica();
89
90        // Join against the user table to ensure that we skip stray
91        // entries left after an account is renamed or otherwise munged.
92        return $dbr->newSelectQueryBuilder()
93            ->select( [ 'su_name' ] )
94            ->from( 'spoofuser' )
95            ->join( $this->getTableName(), null, 'su_name = ' . $this->getUserColumn() )
96            ->where( [ 'su_normalized' => $this->normalized ] )
97            ->limit( 5 )
98            ->caller( __METHOD__ )
99            ->fetchFieldValues();
100    }
101
102    /**
103     * Record the username's normalized form into the database
104     * for later comparison of future names...
105     */
106    public function record(): bool {
107        return self::batchRecord( $this->getDBPrimary(), [ $this ] );
108    }
109
110    private function insertFields(): array {
111        return [
112            'su_name'       => $this->name,
113            'su_normalized' => $this->normalized,
114            'su_legal'      => $this->legal ? 1 : 0,
115            'su_error'      => $this->error?->getMessage()->text(),
116        ];
117    }
118
119    /**
120     * Insert a batch of spoof normalization records into the database.
121     * @param IDatabase $dbw
122     * @param self[] $items
123     * @return bool
124     */
125    public static function batchRecord( IDatabase $dbw, array $items ): bool {
126        if ( !count( $items ) ) {
127            return false;
128        }
129
130        $rqb = $dbw->newReplaceQueryBuilder()
131            ->replaceInto( 'spoofuser' );
132        foreach ( $items as $item ) {
133            $rqb->row( $item->insertFields() );
134        }
135        $rqb->uniqueIndexFields( 'su_name' )
136            ->caller( __METHOD__ )->execute();
137        return true;
138    }
139
140    public function update( string $oldName ): void {
141        $method = __METHOD__;
142        $dbw = $this->getDBPrimary();
143        // Avoid user rename triggered deadlocks
144        $dbw->onTransactionPreCommitOrIdle(
145            function () use ( $dbw, $method, $oldName ) {
146                if ( $this->record() ) {
147                    $dbw->newDeleteQueryBuilder()
148                        ->deleteFrom( 'spoofuser' )
149                        ->where( [ 'su_name' => $oldName ] )
150                        ->caller( $method )->execute();
151                }
152            },
153            $method
154        );
155    }
156
157    /**
158     * Remove a user from the spoofuser table
159     */
160    public function remove(): void {
161        $this->getDBPrimary()
162            ->newDeleteQueryBuilder()
163            ->deleteFrom( 'spoofuser' )
164            ->where( [ 'su_name' => $this->name ] )
165            ->caller( __METHOD__ )->execute();
166    }
167
168    /**
169     * Allows overriding the database connection in sub-classes.
170     */
171    protected function getDBReplica(): IReadableDatabase {
172        return MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
173    }
174
175    /**
176     * Allows overriding database connection in sub-classes.
177     */
178    protected function getDBPrimary(): IDatabase {
179        return MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
180    }
181}