Skip to content
Draft
Changes from 1 commit
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
44 changes: 40 additions & 4 deletions app/Console/Commands/Wiki/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!';
Expand All @@ -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);
Expand All @@ -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 {
Comment thread
deer-wmde marked this conversation as resolved.
Outdated
$wikiDb = $wiki->wikidb;
$prefix = $wikiDb->prefix;

// Replaces current mw database connection config with scoped wiki credentials
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.

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?

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.

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:

  1. scope the db access to only the affected wiki
  2. use the connection settings usually used to connect to the mw dbs
  3. allow to run this command on any API pods (as only the queue deployment gets the root(!) credentials for the mw dbs)

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.

refactored to not directly use the mw connection config, but to create a new one by copying and altering it at runtime

Comment thread
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 = ''");
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.

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 ?

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.

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

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.

refactored to drop the whole wiki database instead of blanking certain fields


return $statement->execute();
}
}
Loading