Merge "CSSMin: Clean up $remote trailing slash fix"
[lhc/web/wiklou.git] / maintenance / nukePage.php
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 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class NukePage extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Remove a page record from the database";
31 $this->addOption( 'delete', "Actually delete the page" );
32 $this->addArg( 'title', 'Title to delete' );
33 }
34
35 public function execute() {
36
37 $name = $this->getArg();
38 $delete = $this->getOption( 'delete', false );
39
40 $dbw = wfGetDB( DB_MASTER );
41 $dbw->begin( __METHOD__ );
42
43 $tbl_pag = $dbw->tableName( 'page' );
44 $tbl_rec = $dbw->tableName( 'recentchanges' );
45 $tbl_rev = $dbw->tableName( 'revision' );
46
47 # Get page ID
48 $this->output( "Searching for \"$name\"..." );
49 $title = Title::newFromText( $name );
50 if ( $title ) {
51 $id = $title->getArticleID();
52 $real = $title->getPrefixedText();
53 $isGoodArticle = $title->isContentPage();
54 $this->output( "found \"$real\" with ID $id.\n" );
55
56 # Get corresponding revisions
57 $this->output( "Searching for revisions..." );
58 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
59 $revs = array();
60 foreach ( $res as $row ) {
61 $revs[] = $row->rev_id;
62 }
63 $count = count( $revs );
64 $this->output( "found $count.\n" );
65
66 # Delete the page record and associated recent changes entries
67 if ( $delete ) {
68 $this->output( "Deleting page record..." );
69 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
70 $this->output( "done.\n" );
71 $this->output( "Cleaning up recent changes..." );
72 $dbw->query( "DELETE FROM $tbl_rec WHERE rc_cur_id = $id" );
73 $this->output( "done.\n" );
74 }
75
76 $dbw->commit( __METHOD__ );
77
78 # Delete revisions as appropriate
79 if ( $delete && $count ) {
80 $this->output( "Deleting revisions..." );
81 $this->deleteRevisions( $revs );
82 $this->output( "done.\n" );
83 $this->purgeRedundantText( true );
84 }
85
86 # Update stats as appropriate
87 if ( $delete ) {
88 $this->output( "Updating site stats..." );
89 $ga = $isGoodArticle ? -1 : 0; // if it was good, decrement that too
90 $stats = new SiteStatsUpdate( 0, -$count, $ga, -1 );
91 $stats->doUpdate();
92 $this->output( "done.\n" );
93 }
94 } else {
95 $this->output( "not found in database.\n" );
96 $dbw->commit( __METHOD__ );
97 }
98 }
99
100 public function deleteRevisions( $ids ) {
101 $dbw = wfGetDB( DB_MASTER );
102 $dbw->begin( __METHOD__ );
103
104 $tbl_rev = $dbw->tableName( 'revision' );
105
106 $set = implode( ', ', $ids );
107 $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
108
109 $dbw->commit( __METHOD__ );
110 }
111 }
112
113 $maintClass = "NukePage";
114 require_once( RUN_MAINTENANCE_IF_MAIN );