Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteInactiveNewsletters
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3use MediaWiki\Maintenance\Maintenance;
4
5$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ?
6    getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../../..';
7
8require_once $basePath . '/maintenance/Maintenance.php';
9
10/**
11 * @author Addshore
12 */
13class DeleteInactiveNewsletters extends Maintenance {
14
15    public function __construct() {
16        parent::__construct();
17        $this->addDescription(
18            "Deletes all inactive newsletters\nThese newsletters will no longer be restorable" );
19        $this->addOption( 'delete', 'Performs the deletion' );
20        $this->requireExtension( 'Newsletter' );
21    }
22
23    public function execute() {
24        $dbw = $this->getDB( DB_PRIMARY );
25
26        if ( !$this->hasOption( 'delete' ) ) {
27            $count = $dbw->newSelectQueryBuilder()
28                ->select( 'COUNT(*)' )
29                ->from( 'nl_newsletters' )
30                ->where( [ 'nl_active' => 0 ] )
31                ->caller( __METHOD__ )
32                ->fetchField();
33            $this->output( "Found $count inactive newsletters to delete.\n" );
34            $this->output( "Please run the script again with the --delete option "
35                . "to really delete the revisions.\n" );
36            return;
37        }
38
39        $this->output( "Getting inactive newsletters...\n" );
40        $idsToDelete = $dbw->newSelectQueryBuilder()
41            ->select( 'nl_id' )
42            ->from( 'nl_newsletters' )
43            ->where( [ 'nl_active' => 0 ] )
44            ->caller( __METHOD__ )
45            ->fetchFieldValues();
46
47        if ( !$idsToDelete ) {
48            $this->output( "No newsletters found to be deleted" );
49            return;
50        }
51
52        $dbw->newDeleteQueryBuilder()
53            ->deleteFrom( 'nl_newsletters' )
54            ->where( [ 'nl_id' => $idsToDelete ] )
55            ->caller( __METHOD__ )
56            ->execute();
57        $count = $dbw->affectedRows();
58        $this->output( "Deleted $count inactive newsletters.\n" );
59
60        $dbw->newDeleteQueryBuilder()
61            ->deleteFrom( 'nl_issues' )
62            ->where( [ 'nli_newsletter_id' => $idsToDelete ] )
63            ->caller( __METHOD__ )
64            ->execute();
65        $count = $dbw->affectedRows();
66        $this->output( "Deleted $count inactive newsletter issues.\n" );
67
68        $dbw->newDeleteQueryBuilder()
69            ->deleteFrom( 'nl_publishers' )
70            ->where( [ 'nlp_newsletter_id' => $idsToDelete ] )
71            ->caller( __METHOD__ )
72            ->execute();
73        $count = $dbw->affectedRows();
74        $this->output( "Deleted $count inactive newsletter publishers.\n" );
75
76        $dbw->newDeleteQueryBuilder()
77            ->deleteFrom( 'nl_subscriptions' )
78            ->where( [ 'nls_newsletter_id' => $idsToDelete ] )
79            ->caller( __METHOD__ )
80            ->execute();
81        $count = $dbw->affectedRows();
82        $this->output( "Deleted $count inactive newsletter subscriptions.\n" );
83
84        $this->output( "Done!\n" );
85    }
86
87}
88
89$maintClass = DeleteInactiveNewsletters::class;
90require_once RUN_MAINTENANCE_IF_MAIN;