Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / nukePage.php
blob1870273eb798515eb0d714b29b6c33bbaed014ae
1 <?php
2 /**
3 * Erase a page record from the database
4 * Irreversible (can't use standard undelete) and does not update link tables
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.
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.
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
21 * @file
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that erases a page record from the database.
31 * @ingroup Maintenance
33 class NukePage extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Remove a page record from the database";
37 $this->addOption( 'delete', "Actually delete the page" );
38 $this->addArg( 'title', 'Title to delete' );
41 public function execute() {
43 $name = $this->getArg();
44 $delete = $this->getOption( 'delete', false );
46 $dbw = wfGetDB( DB_MASTER );
47 $dbw->begin( __METHOD__ );
49 $tbl_pag = $dbw->tableName( 'page' );
50 $tbl_rec = $dbw->tableName( 'recentchanges' );
51 $tbl_rev = $dbw->tableName( 'revision' );
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" );
62 # Get corresponding revisions
63 $this->output( "Searching for revisions..." );
64 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
65 $revs = array();
66 foreach ( $res as $row ) {
67 $revs[] = $row->rev_id;
69 $count = count( $revs );
70 $this->output( "found $count.\n" );
72 # Delete the page record and associated recent changes entries
73 if ( $delete ) {
74 $this->output( "Deleting page record..." );
75 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
76 $this->output( "done.\n" );
77 $this->output( "Cleaning up recent changes..." );
78 $dbw->query( "DELETE FROM $tbl_rec WHERE rc_cur_id = $id" );
79 $this->output( "done.\n" );
82 $dbw->commit( __METHOD__ );
84 # Delete revisions as appropriate
85 if ( $delete && $count ) {
86 $this->output( "Deleting revisions..." );
87 $this->deleteRevisions( $revs );
88 $this->output( "done.\n" );
89 $this->purgeRedundantText( true );
92 # Update stats as appropriate
93 if ( $delete ) {
94 $this->output( "Updating site stats..." );
95 $ga = $isGoodArticle ? -1 : 0; // if it was good, decrement that too
96 $stats = new SiteStatsUpdate( 0, -$count, $ga, -1 );
97 $stats->doUpdate();
98 $this->output( "done.\n" );
100 } else {
101 $this->output( "not found in database.\n" );
102 $dbw->commit( __METHOD__ );
106 public function deleteRevisions( $ids ) {
107 $dbw = wfGetDB( DB_MASTER );
108 $dbw->begin( __METHOD__ );
110 $tbl_rev = $dbw->tableName( 'revision' );
112 $set = implode( ', ', $ids );
113 $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
115 $dbw->commit( __METHOD__ );
119 $maintClass = "NukePage";
120 require_once RUN_MAINTENANCE_IF_MAIN;