Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
75 changes: 75 additions & 0 deletions app/Console/Commands/User/CheckUserEmailExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Console\Commands\User;

use App\User;
use Illuminate\Console\Command;
use PDO;

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(): int {
$emails = $this->argument('emails');

$manager = app()->db;
$manager->purge('mw');
$mwConn = $manager->connection('mw');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Tom to look up:

  • do we also need to undo this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving this open for @tarrow

$pdo = $mwConn->getPdo();

$dbStmt = $pdo->query("SHOW DATABASES LIKE 'mwdb_%'");
Comment thread
rosalieper marked this conversation as resolved.
Outdated
$mwDatabases = $dbStmt->fetchAll(PDO::FETCH_COLUMN);

foreach ($emails as $email) {
$found = false;

// Check apidb.users
if (User::whereEmail($email)->exists()) {
$this->line("FOUND: {$email} in apidb.users");
$found = true;
}

// Check MediaWiki databases
foreach ($mwDatabases as $dbName) {
// fetch user table name
$tableStmt = $pdo->prepare("
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = :db
AND TABLE_NAME LIKE '%\_user'
LIMIT 1
");
$tableStmt->execute(['db' => $dbName]);
$userTable = $tableStmt->fetchColumn();
if (!$userTable) {
continue;
}

$query = "
SELECT user_id
FROM {$dbName}.{$userTable}
WHERE user_email = :email
LIMIT 1
";

$emailStmt = $pdo->prepare($query);
$emailStmt->execute(['email' => $email]);

if ($emailStmt->fetch()) {
$this->line("FOUND: {$email} in {$dbName}.{$userTable}");
$found = true;
}
}

if (!$found) {
$this->line("NOT FOUND: {$email}");
}

$this->line('--------------------------------------------------');
}

return 0;
}
}
25 changes: 25 additions & 0 deletions tests/Commands/User/CheckUserEmailExistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Commands;

use App\User;
use Tests\TestCase;

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