Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions app/Console/Commands/User/CheckUserEmailExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Console\Commands\User;

use App\Services\WikiUserEmailChecker;
use App\User;
use Illuminate\Console\Command;

class CheckUserEmailExist extends Command {
protected $signature = 'wbs-user:check-email {emails*}';

protected $description = 'Check if emails exist in apidb.users or any MediaWiki user table';

public function handle(WikiUserEmailChecker $emailChecker): int {
$emails = $this->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;
}
}
62 changes: 62 additions & 0 deletions app/Services/WikiUserEmailChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
54 changes: 54 additions & 0 deletions tests/Commands/User/CheckUserEmailExistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Commands;

use App\Services\WikiUserEmailChecker;
use App\User;
use Mockery;
use Tests\TestCase;

class CheckUserEmailExistTest extends TestCase {
public function testItFindsEmailInApiUsersTable() {
User::factory()->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);
}
}
Loading