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