-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWikiUserEmailChecker.php
More file actions
60 lines (43 loc) · 1.62 KB
/
WikiUserEmailChecker.php
File metadata and controls
60 lines (43 loc) · 1.62 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
<?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();
$foundIn = [];
$userTables = $this->getAllMediaWikiUserTables($pdo);
foreach ($userTables as $dbName => $userTable) {
if ($this->emailExists($pdo, $dbName, $userTable, $email)) {
$foundIn[] = "{$dbName}.{$userTable}";
}
}
return $foundIn;
}
private function getAllMediaWikiUserTables(PDO $pdo): array {
$stmt = $pdo->query("
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA LIKE 'mwdb_%'
AND TABLE_NAME LIKE '%_user'
");
$tablesByDb = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$tablesByDb[$row['TABLE_SCHEMA']] = $row['TABLE_NAME'];
}
return $tablesByDb;
}
private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool {
$stmt = $pdo->prepare("
SELECT 1
FROM {$dbName}.{$table}
-- converting from tinyblob data type, see https://github.com/wbstack/api/blob/main/database/mw/new/mw1.43-wbs2.sql#L1006
WHERE LOWER(CONVERT(user_email USING utf8mb4)) = LOWER(:email)
LIMIT 1
");
$stmt->execute(['email' => $email]);
return (bool) $stmt->fetch();
}
}