Skip to content

Commit a367d69

Browse files
authored
Added a comand for checking if user email exist in api and MW user tables (#1031)
Bug: T410856
1 parent b790ee1 commit a367d69

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Console\Commands\User;
4+
5+
use App\Services\WikiUserEmailChecker;
6+
use App\User;
7+
use Illuminate\Console\Command;
8+
9+
class CheckUserEmailExist extends Command {
10+
protected $signature = 'wbs-user:check-email {emails*}';
11+
12+
protected $description = 'Check if emails exist in apidb.users or any MediaWiki user table';
13+
14+
public function handle(WikiUserEmailChecker $emailChecker): int {
15+
$emails = $this->argument('emails');
16+
foreach ($emails as $email) {
17+
$found = false;
18+
19+
if (User::whereEmail($email)->exists()) {
20+
$this->line("FOUND: {$email} in apidb.users");
21+
$found = true;
22+
}
23+
24+
$mwResults = $emailChecker->findEmail($email);
25+
26+
foreach ($mwResults as $location) {
27+
$this->line("FOUND: {$email} in {$location}");
28+
$found = true;
29+
}
30+
31+
if (!$found) {
32+
$this->line("NOT FOUND: {$email}");
33+
}
34+
35+
$this->line('-------------------------------------------------');
36+
}
37+
38+
return 0;
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Illuminate\Database\DatabaseManager;
6+
use PDO;
7+
8+
class WikiUserEmailChecker {
9+
public function __construct(private DatabaseManager $db) {}
10+
11+
public function findEmail(string $email): array {
12+
$this->db->purge('mw');
13+
$pdo = $this->db->connection('mw')->getPdo();
14+
15+
$mwDatabases = $pdo
16+
->query("SHOW DATABASES LIKE 'mwdb_%'")
17+
->fetchAll(PDO::FETCH_COLUMN);
18+
19+
$foundIn = [];
20+
21+
foreach ($mwDatabases as $dbName) {
22+
$userTable = $this->findUserTable($pdo, $dbName);
23+
24+
if (!$userTable) {
25+
continue;
26+
}
27+
28+
if ($this->emailExists($pdo, $dbName, $userTable, $email)) {
29+
$foundIn[] = "{$dbName}.{$userTable}";
30+
}
31+
}
32+
33+
return $foundIn;
34+
}
35+
36+
private function findUserTable(PDO $pdo, string $dbName): ?string {
37+
$stmt = $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+
45+
$stmt->execute(['db' => $dbName]);
46+
47+
return $stmt->fetchColumn() ?: null;
48+
}
49+
50+
private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool {
51+
$stmt = $pdo->prepare("
52+
SELECT 1
53+
FROM {$dbName}.{$table}
54+
WHERE user_email = :email
55+
LIMIT 1
56+
");
57+
58+
$stmt->execute(['email' => $email]);
59+
60+
return (bool) $stmt->fetch();
61+
}
62+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Tests\Commands;
4+
5+
use App\Services\WikiUserEmailChecker;
6+
use App\User;
7+
use Mockery;
8+
use Tests\TestCase;
9+
10+
class CheckUserEmailExistTest extends TestCase {
11+
public function testItFindsEmailInApiUsersTable() {
12+
User::factory()->create([
13+
'email' => 'user@example.com',
14+
]);
15+
16+
// Act & Assert
17+
$this->artisan('wbs-user:check-email', ['emails' => ['user@example.com']])
18+
->expectsOutput('FOUND: user@example.com in apidb.users')
19+
->assertExitCode(0);
20+
}
21+
22+
public function testItReturnsNotFoundIfEmailDoesNotExist() {
23+
$this->artisan('wbs-user:check-email', ['emails' => ['nonexistent@example.com']])
24+
->expectsOutput('NOT FOUND: nonexistent@example.com')
25+
->assertExitCode(0);
26+
}
27+
28+
public function testItChecksMultipleEmails() {
29+
User::factory()->create(['email' => 'user1@example.com']);
30+
31+
$emails = ['user1@example.com', 'other@example.com'];
32+
33+
$this->artisan('wbs-user:check-email', ['emails' => $emails])
34+
->expectsOutput('FOUND: user1@example.com in apidb.users')
35+
->expectsOutput('NOT FOUND: other@example.com')
36+
->assertExitCode(0);
37+
}
38+
39+
public function testEmailFoundInWikiDb() {
40+
$checker = Mockery::mock(WikiUserEmailChecker::class);
41+
42+
$checker->shouldReceive('findEmail')
43+
->with('test@example.com')
44+
->andReturn(['mwdb_test.mwdb_test_user']);
45+
46+
$this->app->instance(WikiUserEmailChecker::class, $checker);
47+
48+
$this->artisan('wbs-user:check-email', [
49+
'emails' => ['test@example.com'],
50+
])
51+
->expectsOutput('FOUND: test@example.com in mwdb_test.mwdb_test_user')
52+
->assertExitCode(0);
53+
}
54+
}

0 commit comments

Comments
 (0)