diff --git a/app/Console/Commands/User/CheckUserEmailExist.php b/app/Console/Commands/User/CheckUserEmailExist.php new file mode 100644 index 00000000..5b33f180 --- /dev/null +++ b/app/Console/Commands/User/CheckUserEmailExist.php @@ -0,0 +1,40 @@ +argument('emails'); + foreach ($emails as $email) { + $found = false; + + if (User::whereEmail($email)->exists()) { + $this->line("FOUND: {$email} in apidb.users"); + $found = true; + } + + $mwResults = $emailChecker->findEmail($email); + + foreach ($mwResults as $location) { + $this->line("FOUND: {$email} in {$location}"); + $found = true; + } + + if (!$found) { + $this->line("NOT FOUND: {$email}"); + } + + $this->line('-------------------------------------------------'); + } + + return 0; + } +} diff --git a/app/Services/WikiUserEmailChecker.php b/app/Services/WikiUserEmailChecker.php new file mode 100644 index 00000000..fff4c9ed --- /dev/null +++ b/app/Services/WikiUserEmailChecker.php @@ -0,0 +1,62 @@ +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(); + } +} diff --git a/tests/Commands/User/CheckUserEmailExistTest.php b/tests/Commands/User/CheckUserEmailExistTest.php new file mode 100644 index 00000000..57d74867 --- /dev/null +++ b/tests/Commands/User/CheckUserEmailExistTest.php @@ -0,0 +1,54 @@ +create([ + 'email' => 'user@example.com', + ]); + + // Act & Assert + $this->artisan('wbs-user:check-email', ['emails' => ['user@example.com']]) + ->expectsOutput('FOUND: user@example.com in apidb.users') + ->assertExitCode(0); + } + + public function testItReturnsNotFoundIfEmailDoesNotExist() { + $this->artisan('wbs-user:check-email', ['emails' => ['nonexistent@example.com']]) + ->expectsOutput('NOT FOUND: nonexistent@example.com') + ->assertExitCode(0); + } + + public function testItChecksMultipleEmails() { + User::factory()->create(['email' => 'user1@example.com']); + + $emails = ['user1@example.com', 'other@example.com']; + + $this->artisan('wbs-user:check-email', ['emails' => $emails]) + ->expectsOutput('FOUND: user1@example.com in apidb.users') + ->expectsOutput('NOT FOUND: other@example.com') + ->assertExitCode(0); + } + + public function testEmailFoundInWikiDb() { + $checker = Mockery::mock(WikiUserEmailChecker::class); + + $checker->shouldReceive('findEmail') + ->with('test@example.com') + ->andReturn(['mwdb_test.mwdb_test_user']); + + $this->app->instance(WikiUserEmailChecker::class, $checker); + + $this->artisan('wbs-user:check-email', [ + 'emails' => ['test@example.com'], + ]) + ->expectsOutput('FOUND: test@example.com in mwdb_test.mwdb_test_user') + ->assertExitCode(0); + } +}