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