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
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
19namespace MediaWiki\Extension\AntiSpoof;
20
21use MediaWiki\MediaWikiServices;
22use MediaWiki\Status\Status;
23use Wikimedia\Rdbms\IDatabase;
24use Wikimedia\Rdbms\IReadableDatabase;
25
26class SpoofUser {
27    private bool $legal;
28
29    private ?string $normalized;
30
31    private ?Status $error;
32
33    public function __construct(
34        private readonly string $name
35    ) {
36        $status = AntiSpoof::checkUnicodeStringStatus( $this->name );
37        $this->legal = $status->isOK();
38        if ( $this->legal ) {
39            $this->normalized = $status->getValue();
40            $this->error = null;
41        } else {
42            $this->normalized = null;
43            $this->error = $status;
44        }
45    }
46
47    /**
48     * Does the username pass Unicode legality and script-mixing checks?
49     */
50    public function isLegal(): bool {
51        return $this->legal;
52    }
53
54    /**
55     * Describe the error.
56     * @since 1.32
57     */
58    public function getErrorStatus(): ?Status {
59        return $this->error;
60    }
61
62    /**
63     * Get the normalized key form
64     */
65    public function getNormalized(): ?string {
66        return $this->normalized;
67    }
68
69    protected function getTableName(): string {
70        return 'user';
71    }
72
73    protected function getUserColumn(): string {
74        return 'user_name';
75    }
76
77    /**
78     * Does the username pass Unicode legality and script-mixing checks?
79     *
80     * @return string[] empty if no conflict, or array containing conflicting usernames
81     */
82    public function getConflicts(): array {
83        if ( !$this->isLegal() ) {
84            return [];
85        }
86
87        $dbr = $this->getDBReplica();
88
89        // Join against the user table to ensure that we skip stray
90        // entries left after an account is renamed or otherwise munged.
91        return $dbr->newSelectQueryBuilder()
92            ->select( [ 'su_name' ] )
93            ->from( 'spoofuser' )
94            ->join( $this->getTableName(), null, 'su_name = ' . $this->getUserColumn() )
95            ->where( [ 'su_normalized' => $this->normalized ] )
96            ->limit( 5 )
97            ->caller( __METHOD__ )
98            ->fetchFieldValues();
99    }
100
101    /**
102     * Record the username's normalized form into the database
103     * for later comparison of future names...
104     */
105    public function record(): bool {
106        return self::batchRecord( $this->getDBPrimary(), [ $this ] );
107    }
108
109    private function insertFields(): array {
110        return [
111            'su_name'       => $this->name,
112            'su_normalized' => $this->normalized,
113            'su_legal'      => $this->legal ? 1 : 0,
114            'su_error'      => $this->error?->getMessage()->text(),
115        ];
116    }
117
118    /**
119     * Insert a batch of spoof normalization records into the database.
120     * @param IDatabase $dbw
121     * @param self[] $items
122     * @return bool
123     */
124    public static function batchRecord( IDatabase $dbw, array $items ): bool {
125        if ( !count( $items ) ) {
126            return false;
127        }
128
129        $rqb = $dbw->newReplaceQueryBuilder()
130            ->replaceInto( 'spoofuser' );
131        foreach ( $items as $item ) {
132            $rqb->row( $item->insertFields() );
133        }
134        $rqb->uniqueIndexFields( 'su_name' )
135            ->caller( __METHOD__ )->execute();
136        return true;
137    }
138
139    public function update( string $oldName ): void {
140        $method = __METHOD__;
141        $dbw = $this->getDBPrimary();
142        // Avoid user rename triggered deadlocks
143        $dbw->onTransactionPreCommitOrIdle(
144            function () use ( $dbw, $method, $oldName ) {
145                if ( $this->record() ) {
146                    $dbw->newDeleteQueryBuilder()
147                        ->deleteFrom( 'spoofuser' )
148                        ->where( [ 'su_name' => $oldName ] )
149                        ->caller( $method )->execute();
150                }
151            },
152            $method
153        );
154    }
155
156    /**
157     * Remove a user from the spoofuser table
158     */
159    public function remove(): void {
160        $this->getDBPrimary()
161            ->newDeleteQueryBuilder()
162            ->deleteFrom( 'spoofuser' )
163            ->where( [ 'su_name' => $this->name ] )
164            ->caller( __METHOD__ )->execute();
165    }
166
167    /**
168     * Allows overriding the database connection in sub-classes.
169     */
170    protected function getDBReplica(): IReadableDatabase {
171        return MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
172    }
173
174    /**
175     * Allows overriding database connection in sub-classes.
176     */
177    protected function getDBPrimary(): IDatabase {
178        return MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
179    }
180}