Skip to content
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

DeleteWikis: rewrite to improve robustness and merge DeleteWiki into it #705

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
46 changes: 0 additions & 46 deletions maintenance/DeleteWiki.php

This file was deleted.

129 changes: 111 additions & 18 deletions maintenance/DeleteWikis.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,116 @@

class DeleteWikis extends Maintenance {

private array $deletedWikis = [];
private bool $notified = false;

public function __construct() {
parent::__construct();

$this->addDescription(
'Allows complete deletion of wikis with args controlling ' .
'deletion levels. Will never DROP a database!'
'Deletes wikis. If the --deletewiki option is provided, deletes a single wiki specified by database. ' .
'Otherwise, lists or deletes all wikis marked as deleted (will never DROP a database!). ' .
'A notification is always sent regardless of mode.'

Check warning on line 18 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L16-L18

Added lines #L16 - L18 were not covered by tests
);

$this->addOption( 'delete', 'Actually performs deletions and not outputs wikis to be deleted', false );
$this->addArg( 'user', 'Username or reference name of the person running this script. ' .
$this->addOption( 'deletewiki', 'Specify the database name to delete (single deletion mode).', false, true );
$this->addOption( 'delete', 'Actually performs deletion and not just outputs what would be deleted.' );

Check warning on line 22 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L21-L22

Added lines #L21 - L22 were not covered by tests

$this->addOption( 'user',
'Username or reference name of the person running this script. ' .

Check warning on line 25 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L24-L25

Added lines #L24 - L25 were not covered by tests
'Will be used in tracking and notification internally.',
true );
true, true );

Check warning on line 27 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L27

Added line #L27 was not covered by tests

$this->requireExtension( 'CreateWiki' );
}

private function log( string $msg, bool $output ): void {
$logger = $this->getServiceContainer()->get( 'CreateWikiLogger' );
$logger->debug( "DeleteWikis: $msg" );
if ( $output ) {
$this->output( "$msg\n" );

Check warning on line 36 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L32-L36

Added lines #L32 - L36 were not covered by tests
}
}

public function execute(): void {
$user = $this->getOption( 'user' );
if ( !$user ) {
$this->fatalError( 'Please specify the username of the user executing this script.' );

Check warning on line 43 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L41-L43

Added lines #L41 - L43 were not covered by tests
}

$this->deletedWikis = [];
$this->notified = false;

Check warning on line 47 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L46-L47

Added lines #L46 - L47 were not covered by tests

register_shutdown_function( [ $this, 'shutdownHandler' ] );

Check warning on line 49 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L49

Added line #L49 was not covered by tests

try {
$dbname = $this->getOption( 'deletewiki' );
if ( $dbname ) {
$this->processSingleDeletion( $dbname );
return;

Check warning on line 55 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L52-L55

Added lines #L52 - L55 were not covered by tests
}

$this->processMultipleDeletions();
$this->output( "Done.\n" );

Check warning on line 59 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L58-L59

Added lines #L58 - L59 were not covered by tests
} finally {
// Make sure we notify deletions regardless even
// if an exception occurred, we always want to notify which
// ones have already been deleted.
$this->notifyDeletions();

Check warning on line 64 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L64

Added line #L64 was not covered by tests
}
}

private function processSingleDeletion( string $dbname ): void {
if ( $this->hasOption( 'delete' ) ) {
$this->output(
"You are about to delete $dbname from CreateWiki. " .
"This will not DROP the database. If this is wrong, Ctrl-C now!\n"
);

Check warning on line 73 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L68-L73

Added lines #L68 - L73 were not covered by tests

// Let's count down JUST to be safe!
$this->countDown( 10 );

Check warning on line 76 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L76

Added line #L76 was not covered by tests

$wikiManager = $this->getServiceContainer()->get( 'WikiManagerFactory' )
->newInstance( $dbname );
$delete = $wikiManager->delete( force: true );

Check warning on line 80 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L78-L80

Added lines #L78 - L80 were not covered by tests

if ( $delete ) {
$this->fatalError( $delete );

Check warning on line 83 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L82-L83

Added lines #L82 - L83 were not covered by tests
}

$this->log( "Wiki $dbname deleted.", output: true );
$this->deletedWikis[] = $dbname;

Check warning on line 87 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L86-L87

Added lines #L86 - L87 were not covered by tests
} else {
$this->output( "Wiki $dbname would be deleted. Use --delete to actually perform deletion.\n" );
$this->deletedWikis[] = $dbname;

Check warning on line 90 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L89-L90

Added lines #L89 - L90 were not covered by tests
}
}

private function processMultipleDeletions(): void {
if ( $this->hasOption( 'delete' ) ) {
$this->output(
"You are about to delete all wikis that are marked as deleted from CreateWiki. " .
"This will not DROP any databases. If this is wrong, Ctrl-C now!\n"
);

Check warning on line 99 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L94-L99

Added lines #L94 - L99 were not covered by tests

// Let's count down JUST to be safe!
$this->countDown( 10 );

Check warning on line 102 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L102

Added line #L102 was not covered by tests
}

$databaseUtils = $this->getServiceContainer()->get( 'CreateWikiDatabaseUtils' );
$wikiManagerFactory = $this->getServiceContainer()->get( 'WikiManagerFactory' );

$dbr = $databaseUtils->getGlobalReplicaDB();

$res = $dbr->newSelectQueryBuilder()
->select( '*' )
->from( 'cw_wikis' )
->table( 'cw_wikis' )
->fields( [
'wiki_dbcluster',
'wiki_dbname',
] )

Check warning on line 114 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L110-L114

Added lines #L110 - L114 were not covered by tests
->where( [ 'wiki_deleted' => 1 ] )
->caller( __METHOD__ )
->fetchResultSet();

$deletedWikis = [];

foreach ( $res as $row ) {
$wiki = $row->wiki_dbname;
$dbCluster = $row->wiki_dbcluster;
Expand All @@ -46,25 +125,37 @@
$delete = $wikiManager->delete( force: false );

if ( $delete ) {
$this->output( "{$wiki}: {$delete}\n" );
$this->log( "$wiki: $delete", output: true );

Check warning on line 128 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L128

Added line #L128 was not covered by tests
continue;
}

$this->output( "$dbCluster: DROP DATABASE {$wiki};\n" );
$deletedWikis[] = $wiki;
$this->log( "Wiki $wiki deleted from $dbCluster.", output: false );
$this->output( "$dbCluster: DROP DATABASE $wiki;\n" );
$this->deletedWikis[] = $wiki;

Check warning on line 134 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L132-L134

Added lines #L132 - L134 were not covered by tests
} else {
$this->output( "$wiki: $dbCluster\n" );
$this->deletedWikis[] = $wiki;

Check warning on line 137 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L137

Added line #L137 was not covered by tests
}
}
}

$this->output( "Done.\n" );
/**
* Shutdown handler to catch termination of the script.
*/
public function shutdownHandler(): void {
if ( !$this->notified ) {
$this->notifyDeletions();

Check warning on line 147 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L145-L147

Added lines #L145 - L147 were not covered by tests
}
}

$user = $this->getArg( 0 );
$deletedWikis = implode( ', ', $deletedWikis );
private function notifyDeletions(): void {
$user = $this->getOption( 'user' );
$deletedWikisList = implode( ', ', $this->deletedWikis );
$action = $this->hasOption( 'delete' ) ? 'has deleted' : 'is about to delete';

Check warning on line 154 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L151-L154

Added lines #L151 - L154 were not covered by tests

$message = "Hello!\nThis is an automatic notification from CreateWiki notifying you that " .
"just now {$user} has deleted the following wikis from the CreateWiki and " .
"associated extensions:\n{$deletedWikis}";
"just now that $user $action the following wiki(s) from CreateWiki and " .
"associated extensions:\n$deletedWikisList";

Check warning on line 158 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L157-L158

Added lines #L157 - L158 were not covered by tests

$notificationData = [
'type' => 'deletion',
Expand All @@ -78,6 +169,8 @@
// No receivers, it will send to configured email
receivers: []
);

$this->notified = true;

Check warning on line 173 in maintenance/DeleteWikis.php

View check run for this annotation

Codecov / codecov/patch

maintenance/DeleteWikis.php#L173

Added line #L173 was not covered by tests
}
}

Expand Down
Loading