-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDropDatabase.php
More file actions
71 lines (51 loc) · 2.05 KB
/
DropDatabase.php
File metadata and controls
71 lines (51 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace App\Console\Commands\Wiki;
use App\Wiki;
use App\WikiDb;
use Illuminate\Console\Command;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\App;
class dropDatabase extends Command {
protected $signature = 'wbs-wiki:dropDatabase {wikiDomain}';
protected $description = 'Drops the MediaWiki database of a wiki and soft-deletes it.';
public function handle(): int {
$wikiDomain = trim($this->argument('wikiDomain'));
$wiki = Wiki::with('wikidb')->firstWhere('domain', $wikiDomain);
if (!$wiki) {
$this->error("Wiki not found by domain '$wikiDomain'");
return 1;
}
if (!$this->dropWikiDb($wiki->wikidb)) {
$this->error('Failed to drop the mediawiki database of wiki.');
$this->error($wiki);
return 2;
}
$this->info('MediaWiki database dropped.');
$wiki->delete();
$this->info('Wiki soft-deleted.');
$this->info($wiki);
return 0;
}
private function dropWikiDb(WikiDb $wikiDb): bool {
$connection = $this->getWikiDbConnection($wikiDb);
if (!$connection instanceof \Illuminate\Database\Connection) {
throw new \RuntimeException('Must be run on a PDO based DB connection');
}
$mediawikiPdo = $connection->getPdo();
$statement = $mediawikiPdo->prepare('DROP DATABASE ' . $wikiDb->name);
return $statement->execute([$wikiDb->name]);
}
// Creates a temporary DB connection with wiki scoped credentials
private function getWikiDbConnection(WikiDb $wikiDb): mixed {
$wikiDbConnectionConfig = array_replace(
app()->config->get('database.connections.mw'),
[
'database' => $wikiDb->name,
'username' => $wikiDb->user,
'password' => $wikiDb->password,
]
);
app()->config->set('database.connections.wikiTemp', $wikiDbConnectionConfig);
return App::make(DatabaseManager::class)->connection('wikiTemp');
}
}