Debian Codegraph @

maintenance/nukePage.php

raw symbols SCIP
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 <[email protected]>
24 */
25
26 // @codeCoverageIgnoreStart
27 require_once __DIR__ . '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
29
30 use MediaWiki\Deferred\SiteStatsUpdate;
31 use MediaWiki\Title\Title;
32
33 /**
34 * Maintenance script that erases a page record from the database.
35 *
36 * @ingroup Maintenance
37 */
38 class NukePage extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Remove a page record from the database' );
42 $this->addOption( 'delete', "Actually delete the page" );
43 $this->addArg( 'title', 'Title to delete' );
44 }
45
46 public function execute() {
47 $name = $this->getArg( 0 );
48 $delete = $this->hasOption( 'delete' );
49
50 $dbw = $this->getPrimaryDB();
51 $this->beginTransaction( $dbw, __METHOD__ );
52
53 # Get page ID
54 $this->output( "Searching for \"$name\"..." );
55 $title = Title::newFromText( $name );
56 if ( $title ) {
57 $id = $title->getArticleID();
58 $real = $title->getPrefixedText();
59 $isGoodArticle = $title->isContentPage();
60 $this->output( "found \"$real\" with ID $id.\n" );
61
62 # Get corresponding revisions
63 $this->output( "Searching for revisions..." );
64
65 $revs = $dbw->newSelectQueryBuilder()
66 ->select( 'rev_id' )
67 ->from( 'revision' )
68 ->where( [ 'rev_page' => $id ] )
69 ->caller( __METHOD__ )->fetchFieldValues();
70 $count = count( $revs );
71 $this->output( "found $count.\n" );
72
73 # Delete the page record and associated recent changes entries
74 if ( $delete ) {
75 $this->output( "Deleting page record..." );
76 $dbw->newDeleteQueryBuilder()
77 ->deleteFrom( 'page' )
78 ->where( [ 'page_id' => $id ] )
79 ->caller( __METHOD__ )->execute();
80 $this->output( "done.\n" );
81 $this->output( "Cleaning up recent changes..." );
82 $dbw->newDeleteQueryBuilder()
83 ->deleteFrom( 'recentchanges' )
84 ->where( [ 'rc_cur_id' => $id ] )
85 ->caller( __METHOD__ )->execute();
86 $this->output( "done.\n" );
87 }
88
89 $this->commitTransaction( $dbw, __METHOD__ );
90
91 # Delete revisions as appropriate
92 if ( $delete && $count ) {
93 $this->output( "Deleting revisions..." );
94 $this->deleteRevisions( $revs );
95 $this->output( "done.\n" );
96 $this->purgeRedundantText( true );
97 }
98
99 # Update stats as appropriate
100 if ( $delete ) {
101 $this->output( "Updating site stats..." );
102 // if it was good, decrement that too
103 $ga = $isGoodArticle ? -1 : 0;
104 $stats = SiteStatsUpdate::factory( [
105 'edits' => -$count,
106 'articles' => $ga,
107 'pages' => -1
108 ] );
109 $stats->doUpdate();
110 $this->output( "done.\n" );
111 }
112 } else {
113 $this->output( "not found in database.\n" );
114 $this->commitTransaction( $dbw, __METHOD__ );
115 }
116 }
117
118 public function deleteRevisions( $ids ) {
119 $dbw = $this->getPrimaryDB();
120 $this->beginTransaction( $dbw, __METHOD__ );
121
122 $dbw->newDeleteQueryBuilder()
123 ->deleteFrom( 'revision' )
124 ->where( [ 'rev_id' => $ids ] )
125 ->caller( __METHOD__ )->execute();
126
127 $this->commitTransaction( $dbw, __METHOD__ );
128 }
129 }
130
131 // @codeCoverageIgnoreStart
132 $maintClass = NukePage::class;
133 require_once RUN_MAINTENANCE_IF_MAIN;
134 // @codeCoverageIgnoreEnd