-
Notifications
You must be signed in to change notification settings - Fork 3
Add Wiki/DropDatabase Command #997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
b2826b5
9a39e9f
e59e942
fd7841b
bd57703
2f5e553
0340190
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,10 @@ | |
| namespace App\Console\Commands\Wiki; | ||
|
|
||
| use App\Wiki; | ||
| use App\WikiDb; | ||
| use Illuminate\Support\Facades\App; | ||
| use Illuminate\Console\Command; | ||
| use Illuminate\Database\DatabaseManager; | ||
|
|
||
| class Delete extends Command { | ||
| public const SUCCESS = 'Success!'; | ||
|
|
@@ -12,15 +15,17 @@ class Delete extends Command { | |
|
|
||
| public const ERR_AMBIGUOUS_KEY_VALUE = 'Wiki deletion failed. Multiple wikis match the given key and value.'; | ||
|
|
||
| public const ERR_FAILED_DATA_DELETION = 'Deleting data in the wikis database failed.'; | ||
|
|
||
| protected $signature = 'wbs-wiki:delete {key} {value}'; | ||
|
|
||
| protected $description = 'Soft deletes the Wiki matching the given key and value.'; | ||
| protected $description = 'Soft deletes the Wiki matching the given key and value, while cleaning up some user data.'; | ||
|
|
||
| public function handle(): int { | ||
| $key = trim($this->argument('key')); | ||
| $value = trim($this->argument('value')); | ||
|
|
||
| $wikis = Wiki::where($key, $value); | ||
| $wikis = Wiki::with('wikidb')->where($key, $value); | ||
|
|
||
| if ($wikis->count() === 0) { | ||
| $this->error(self::ERR_WIKI_DOES_NOT_EXIST); | ||
|
|
@@ -29,13 +34,44 @@ public function handle(): int { | |
| } elseif ($wikis->count() > 1) { | ||
| $this->error(self::ERR_AMBIGUOUS_KEY_VALUE); | ||
|
|
||
| return 1; | ||
| return 2; | ||
| } | ||
|
|
||
| $wikis->delete(); | ||
| $wiki = $wikis->first(); | ||
|
|
||
| if (!$this->cleanupUserData($wiki)) { | ||
| $this->error(self::ERR_FAILED_DATA_DELETION); | ||
| $this->error($wiki); | ||
|
|
||
| return 3; | ||
| } | ||
|
|
||
| $wiki->delete(); | ||
|
|
||
| $this->info(self::SUCCESS); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private function cleanupUserData($wiki): bool { | ||
| $wikiDb = $wiki->wikidb; | ||
| $prefix = $wikiDb->prefix; | ||
|
|
||
| // Replaces current mw database connection config with scoped wiki credentials | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this work after being run? I'm not certain what this is replacing but I think this was the wikimanager type credentials right? Doesn't this mean that unless the app is restarted then this connection will no longer have sufficient powers to do work on other Wikis?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this changes this configuration at runtime - which should only affect this very instance of php running this command, not affecting other instances where Laravel runs (try scrambling the config in tinker like this for example) It serves several purposes:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored to not directly use the
deer-wmde marked this conversation as resolved.
Outdated
|
||
| app()->config->set('database.connections.mw.database', $wikiDb->name); | ||
| app()->config->set('database.connections.mw.username', $wikiDb->user); | ||
| app()->config->set('database.connections.mw.password', $wikiDb->password); | ||
|
|
||
| $manager = App::make(DatabaseManager::class); | ||
| $mwConn = $manager->connection('mw'); | ||
|
|
||
| if (!$mwConn instanceof \Illuminate\Database\Connection) { | ||
| throw new \RuntimeException('Must be run on a PDO based DB connection'); | ||
| } | ||
|
|
||
| $mediawikiPdo = $mwConn->getPdo(); | ||
| $statement = $mediawikiPdo->prepare("UPDATE ${prefix}_user SET user_real_name = '', user_email = '', user_password = ''"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO rather than poking in the raw internals of MW it would make more sense to run a MW maintenance script that does this; I think this better follows the pattern we have been following before of keeping MW writing to MW's DB. What would you think about that? I could imagine that either being run from the platform API or just as a standalone command run from a devs laptop. Maybe following the pattern like https://github.com/wmde/wbaas-deploy/blob/937b2d1a7cf16899d53ad8bd9bfa49a57c88e836/k8s/jobs/addPlatformReservedUserToBotGroup.sh but running https://github.com/wbstack/mediawiki/blob/main/dist/maintenance/deleteUserEmail.php , https://github.com/wbstack/mediawiki/blob/main/dist/maintenance/changePassword.php ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in theory yes in reality currently i want to make sure we dont mess up the db while satisfying user requests and minimizing unplanned work
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored to drop the whole wiki database instead of blanking certain fields |
||
|
|
||
| return $statement->execute(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.