Skip to content
31 changes: 13 additions & 18 deletions app/Services/WikiUserEmailChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@ public function findEmail(string $email): array {
$this->db->purge('mw');
$pdo = $this->db->connection('mw')->getPdo();

$mwDatabases = $pdo
->query("SHOW DATABASES LIKE 'mwdb_%'")
->fetchAll(PDO::FETCH_COLUMN);

$foundIn = [];

foreach ($mwDatabases as $dbName) {
$userTable = $this->findUserTable($pdo, $dbName);

if (!$userTable) {
continue;
}
$userTables = $this->getAllMediaWikiUserTables($pdo);

foreach ($userTables as $dbName => $userTable) {
if ($this->emailExists($pdo, $dbName, $userTable, $email)) {
$foundIn[] = "{$dbName}.{$userTable}";
}
Expand All @@ -33,18 +25,21 @@ public function findEmail(string $email): array {
return $foundIn;
}

private function findUserTable(PDO $pdo, string $dbName): ?string {
$stmt = $pdo->prepare("
SELECT TABLE_NAME
private function getAllMediaWikiUserTables(PDO $pdo): array {
$stmt = $pdo->query("
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = :db
AND TABLE_NAME LIKE '%\_user'
LIMIT 1
WHERE TABLE_SCHEMA LIKE 'mwdb_%'
AND TABLE_NAME LIKE '%_user'
");

$stmt->execute(['db' => $dbName]);
$tablesByDb = [];

foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$tablesByDb[$row['TABLE_SCHEMA']] = $row['TABLE_NAME'];
}

return $stmt->fetchColumn() ?: null;
return $tablesByDb;
}

private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool {
Expand Down
1 change: 1 addition & 0 deletions tests/Commands/User/CheckUserEmailExistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testCaseInsensitive() {
User::factory()->create([
'email' => 'Test@Example.com',
]);

$exists = User::whereEmailInsensitive('tEsT@eXaMpLe.CoM')->exists();

$this->assertTrue($exists);
Expand Down
2 changes: 1 addition & 1 deletion tests/Services/WikiUserEmailCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testCorrectDatabaseFound(): void {

public function testEmailFoundInMultipleDatabases(): void {
$checker = new WikiUserEmailChecker($this->db);
$this->assertEquals(
$this->assertEqualsCanonicalizing(
['mwdb_1.db_1_user', 'mwdb_2.db_2_user'],
$checker->findEmail('user1@email.localhost')
);
Expand Down
Loading