Skip to content

Commit 4da5646

Browse files
committed
Added a coomand for checking if user email exist in api and MW db
1 parent 9956e88 commit 4da5646

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Console\Commands\User;
4+
5+
use App\User;
6+
use Illuminate\Console\Command;
7+
use PDO;
8+
9+
class CheckUserEmailExist extends Command{
10+
11+
protected $signature = 'wbs-user:check-email {emails*}';
12+
protected $description = 'Check if emails exist in apidb.users or any MediaWiki user table';
13+
14+
public function handle(): int {
15+
$emails = $this->argument('emails');
16+
17+
$manager = app()->db;
18+
$manager->purge('mw');
19+
$mwConn = $manager->connection('mw');
20+
$pdo = $mwConn->getPdo();
21+
22+
$dbStmt = $pdo->query("SHOW DATABASES LIKE 'mwdb_%'");
23+
$mwDatabases = $dbStmt->fetchAll(PDO::FETCH_COLUMN);
24+
25+
foreach ($emails as $email) {
26+
$found = false;
27+
28+
//Check apidb.users
29+
if (User::whereEmail($email)->exists()) {
30+
$this->line("FOUND: {$email} in apidb.users");
31+
$found = true;
32+
}
33+
34+
//Check MediaWiki databases
35+
foreach ($mwDatabases as $dbName) {
36+
//fetch user table name
37+
$tableStmt = $pdo->prepare("
38+
SELECT TABLE_NAME
39+
FROM INFORMATION_SCHEMA.TABLES
40+
WHERE TABLE_SCHEMA = :db
41+
AND TABLE_NAME LIKE '%\_user'
42+
LIMIT 1
43+
");
44+
$tableStmt->execute(['db' => $dbName]);
45+
$userTable = $tableStmt->fetchColumn();
46+
if (!$userTable) {
47+
continue;
48+
}
49+
50+
$query = "
51+
SELECT user_id
52+
FROM {$dbName}.{$userTable}
53+
WHERE user_email = :email
54+
LIMIT 1
55+
";
56+
57+
$emailStmt = $pdo->prepare($query);
58+
$emailStmt->execute(['email' => $email]);
59+
60+
if ($emailStmt->fetch()) {
61+
$this->line("FOUND: {$email} in {$dbName}.{$userTable}");
62+
$found = true;
63+
}
64+
}
65+
66+
if (!$found) {
67+
$this->line("NOT FOUND: {$email}");
68+
}
69+
70+
$this->line('--------------------------------------------------');
71+
}
72+
73+
return 0;
74+
}
75+
76+
}

0 commit comments

Comments
 (0)