-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWikiUserEmailChecker.php
More file actions
62 lines (46 loc) · 1.53 KB
/
WikiUserEmailChecker.php
File metadata and controls
62 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace App\Services;
use Illuminate\Database\DatabaseManager;
use PDO;
class WikiUserEmailChecker {
public function __construct(private DatabaseManager $db) {}
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;
}
if ($this->emailExists($pdo, $dbName, $userTable, $email)) {
$foundIn[] = "{$dbName}.{$userTable}";
}
}
return $foundIn;
}
private function findUserTable(PDO $pdo, string $dbName): ?string {
$stmt = $pdo->prepare("
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = :db
AND TABLE_NAME LIKE '%\_user'
LIMIT 1
");
$stmt->execute(['db' => $dbName]);
return $stmt->fetchColumn() ?: null;
}
private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool {
$stmt = $pdo->prepare("
SELECT 1
FROM {$dbName}.{$table}
WHERE user_email = :email
LIMIT 1
");
$stmt->execute(['email' => $email]);
return (bool) $stmt->fetch();
}
}