Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
36 / 44
57.14% covered (warning)
57.14%
8 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpoofUser
81.82% covered (warning)
81.82%
36 / 44
57.14% covered (warning)
57.14%
8 / 14
23.65
0.00% covered (danger)
0.00%
0 / 1
 __construct
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
2.06
 isLegal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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%
9 / 9
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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 batchRecord
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 update
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 remove
0.00% covered (danger)
0.00%
0 / 2
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 Status;
22use Wikimedia\Rdbms\IDatabase;
23
24class SpoofUser {
25    /** @var bool */
26    private $legal;
27
28    /** @var string */
29    private $name;
30
31    /** @var string|null */
32    private $normalized;
33
34    /** @var null|Status */
35    private $error;
36
37    /**
38     * @param string $name
39     */
40    public function __construct( $name ) {
41        $this->name = strval( $name );
42        $status = AntiSpoof::checkUnicodeStringStatus( $this->name );
43        $this->legal = $status->isOK();
44        if ( $this->legal ) {
45            $this->normalized = $status->getValue();
46            $this->error = null;
47        } else {
48            $this->normalized = null;
49            $this->error = $status;
50        }
51    }
52
53    /**
54     * Does the username pass Unicode legality and script-mixing checks?
55     * @return bool
56     */
57    public function isLegal() {
58        return $this->legal;
59    }
60
61    /**
62     * Describe the error.
63     * @return null|Status
64     * @since 1.32
65     */
66    public function getErrorStatus() {
67        return $this->error;
68    }
69
70    /**
71     * Get the normalized key form
72     * @return string|null
73     */
74    public function getNormalized() {
75        return $this->normalized;
76    }
77
78    /**
79     * @return string
80     */
81    protected function getTableName() {
82        return 'user';
83    }
84
85    /**
86     * @return string
87     */
88    protected function getUserColumn() {
89        return 'user_name';
90    }
91
92    /**
93     * Does the username pass Unicode legality and script-mixing checks?
94     *
95     * @return array empty if no conflict, or array containing conflicting usernames
96     */
97    public function getConflicts() {
98        $dbr = $this->getDBReplica();
99
100        // Join against the user table to ensure that we skip stray
101        // entries left after an account is renamed or otherwise munged.
102        $spoofedUsers = $dbr->select(
103            [ 'spoofuser', $this->getTableName() ],
104            [ 'su_name' ], // Same thing due to the join. Saves extra variableness
105            [
106                'su_normalized' => $this->normalized,
107                'su_name = ' . $this->getUserColumn(),
108            ],
109            __METHOD__,
110            [
111                'LIMIT' => 5
112            ] );
113
114        $spoofs = [];
115        foreach ( $spoofedUsers as $row ) {
116            array_push( $spoofs, $row->su_name );
117        }
118        return $spoofs;
119    }
120
121    /**
122     * Record the username's normalized form into the database
123     * for later comparison of future names...
124     * @return bool
125     */
126    public function record() {
127        return self::batchRecord( $this->getDBPrimary(), [ $this ] );
128    }
129
130    /**
131     * @return array
132     */
133    private function insertFields() {
134        return [
135            'su_name'       => $this->name,
136            'su_normalized' => $this->normalized,
137            'su_legal'      => $this->legal ? 1 : 0,
138            'su_error'      => $this->error ? $this->error->getMessage()->text() : null,
139        ];
140    }
141
142    /**
143     * Insert a batch of spoof normalization records into the database.
144     * @param IDatabase $dbw
145     * @param SpoofUser[] $items
146     * @return bool
147     */
148    public static function batchRecord( IDatabase $dbw, $items ) {
149        if ( !count( $items ) ) {
150            return false;
151        }
152        $fields = [];
153        /**
154         * @var $item SpoofUser
155         */
156        foreach ( $items as $item ) {
157            $fields[] = $item->insertFields();
158        }
159        $dbw->replace(
160            'spoofuser',
161            'su_name',
162            $fields,
163            __METHOD__
164        );
165        return true;
166    }
167
168    /**
169     * @param string $oldName
170     */
171    public function update( $oldName ) {
172        $method = __METHOD__;
173        $dbw = $this->getDBPrimary();
174        // Avoid user rename triggered deadlocks
175        $dbw->onTransactionPreCommitOrIdle(
176            function () use ( $dbw, $method, $oldName ) {
177                if ( $this->record() ) {
178                    $dbw->delete(
179                        'spoofuser',
180                        [ 'su_name' => $oldName ],
181                        $method
182                    );
183                }
184            },
185            $method
186        );
187    }
188
189    /**
190     * Remove a user from the spoofuser table
191     */
192    public function remove() {
193        $this->getDBPrimary()->delete(
194            'spoofuser',
195            [ 'su_name' => $this->name ],
196            __METHOD__
197        );
198    }
199
200    /**
201     * @return IDatabase
202     */
203    protected function getDBReplica() {
204        return wfGetDB( DB_REPLICA );
205    }
206
207    /**
208     * @return IDatabase
209     */
210    protected function getDBPrimary() {
211        return wfGetDB( DB_PRIMARY );
212    }
213}