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 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
NukePage
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
56
 deleteRevisions
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Erase a page record from the database
4 * Irreversible (can't use standard undelete) and does not update link tables
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
24 */
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
30use MediaWiki\Deferred\SiteStatsUpdate;
31use MediaWiki\Maintenance\Maintenance;
32use MediaWiki\Title\Title;
33
34/**
35 * Maintenance script that erases a page record from the database.
36 *
37 * @ingroup Maintenance
38 */
39class NukePage extends Maintenance {
40    public function __construct() {
41        parent::__construct();
42        $this->addDescription( 'Remove a page record from the database' );
43        $this->addOption( 'delete', "Actually delete the page" );
44        $this->addArg( 'title', 'Title to delete' );
45    }
46
47    public function execute() {
48        $name = $this->getArg( 0 );
49        $delete = $this->hasOption( 'delete' );
50
51        $dbw = $this->getPrimaryDB();
52        $this->beginTransaction( $dbw, __METHOD__ );
53
54        # Get page ID
55        $this->output( "Searching for \"$name\"..." );
56        $title = Title::newFromText( $name );
57        if ( $title ) {
58            $id = $title->getArticleID();
59            $real = $title->getPrefixedText();
60            $isGoodArticle = $title->isContentPage();
61            $this->output( "found \"$real\" with ID $id.\n" );
62
63            # Get corresponding revisions
64            $this->output( "Searching for revisions..." );
65
66            $revs = $dbw->newSelectQueryBuilder()
67                ->select( 'rev_id' )
68                ->from( 'revision' )
69                ->where( [ 'rev_page' => $id ] )
70                ->caller( __METHOD__ )->fetchFieldValues();
71            $count = count( $revs );
72            $this->output( "found $count.\n" );
73
74            # Delete the page record and associated recent changes entries
75            if ( $delete ) {
76                $this->output( "Deleting page record..." );
77                $dbw->newDeleteQueryBuilder()
78                    ->deleteFrom( 'page' )
79                    ->where( [ 'page_id' => $id ] )
80                    ->caller( __METHOD__ )->execute();
81                $this->output( "done.\n" );
82                $this->output( "Cleaning up recent changes..." );
83                $dbw->newDeleteQueryBuilder()
84                    ->deleteFrom( 'recentchanges' )
85                    ->where( [ 'rc_cur_id' => $id ] )
86                    ->caller( __METHOD__ )->execute();
87                $this->output( "done.\n" );
88            }
89
90            $this->commitTransaction( $dbw, __METHOD__ );
91
92            # Delete revisions as appropriate
93            if ( $delete && $count ) {
94                $this->output( "Deleting revisions..." );
95                $this->deleteRevisions( $revs );
96                $this->output( "done.\n" );
97                $this->purgeRedundantText( true );
98            }
99
100            # Update stats as appropriate
101            if ( $delete ) {
102                $this->output( "Updating site stats..." );
103                // if it was good, decrement that too
104                $ga = $isGoodArticle ? -1 : 0;
105                $stats = SiteStatsUpdate::factory( [
106                    'edits' => -$count,
107                    'articles' => $ga,
108                    'pages' => -1
109                ] );
110                $stats->doUpdate();
111                $this->output( "done.\n" );
112            }
113        } else {
114            $this->output( "not found in database.\n" );
115            $this->commitTransaction( $dbw, __METHOD__ );
116        }
117    }
118
119    public function deleteRevisions( $ids ) {
120        $dbw = $this->getPrimaryDB();
121        $this->beginTransaction( $dbw, __METHOD__ );
122
123        $dbw->newDeleteQueryBuilder()
124            ->deleteFrom( 'revision' )
125            ->where( [ 'rev_id' => $ids ] )
126            ->caller( __METHOD__ )->execute();
127
128        $this->commitTransaction( $dbw, __METHOD__ );
129    }
130}
131
132// @codeCoverageIgnoreStart
133$maintClass = NukePage::class;
134require_once RUN_MAINTENANCE_IF_MAIN;
135// @codeCoverageIgnoreEnd