MediaWiki master
nukePage.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
30
36class NukePage extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Remove a page record from the database' );
40 $this->addOption( 'delete', "Actually delete the page" );
41 $this->addArg( 'title', 'Title to delete' );
42 }
43
44 public function execute() {
45 $name = $this->getArg( 0 );
46 $delete = $this->hasOption( 'delete' );
47
48 $dbw = $this->getPrimaryDB();
49 $this->beginTransaction( $dbw, __METHOD__ );
50
51 # Get page ID
52 $this->output( "Searching for \"$name\"..." );
53 $title = Title::newFromText( $name );
54 if ( $title ) {
55 $id = $title->getArticleID();
56 $real = $title->getPrefixedText();
57 $isGoodArticle = $title->isContentPage();
58 $this->output( "found \"$real\" with ID $id.\n" );
59
60 # Get corresponding revisions
61 $this->output( "Searching for revisions..." );
62
63 $revs = $dbw->newSelectQueryBuilder()
64 ->select( 'rev_id' )
65 ->from( 'revision' )
66 ->where( [ 'rev_page' => $id ] )
67 ->caller( __METHOD__ )->fetchFieldValues();
68 $count = count( $revs );
69 $this->output( "found $count.\n" );
70
71 # Delete the page record and associated recent changes entries
72 if ( $delete ) {
73 $this->output( "Deleting page record..." );
74 $dbw->newDeleteQueryBuilder()
75 ->deleteFrom( 'page' )
76 ->where( [ 'page_id' => $id ] )
77 ->caller( __METHOD__ )->execute();
78 $this->output( "done.\n" );
79 $this->output( "Cleaning up recent changes..." );
80 $dbw->newDeleteQueryBuilder()
81 ->deleteFrom( 'recentchanges' )
82 ->where( [ 'rc_cur_id' => $id ] )
83 ->caller( __METHOD__ )->execute();
84 $this->output( "done.\n" );
85 }
86
87 $this->commitTransaction( $dbw, __METHOD__ );
88
89 # Delete revisions as appropriate
90 if ( $delete && $count ) {
91 $this->output( "Deleting revisions..." );
92 $this->deleteRevisions( $revs );
93 $this->output( "done.\n" );
94 $this->purgeRedundantText( true );
95 }
96
97 # Update stats as appropriate
98 if ( $delete ) {
99 $this->output( "Updating site stats..." );
100 // if it was good, decrement that too
101 $ga = $isGoodArticle ? -1 : 0;
102 $stats = SiteStatsUpdate::factory( [
103 'edits' => -$count,
104 'articles' => $ga,
105 'pages' => -1
106 ] );
107 $stats->doUpdate();
108 $this->output( "done.\n" );
109 }
110 } else {
111 $this->output( "not found in database.\n" );
112 $this->commitTransaction( $dbw, __METHOD__ );
113 }
114 }
115
116 public function deleteRevisions( $ids ) {
117 $dbw = $this->getPrimaryDB();
118 $this->beginTransaction( $dbw, __METHOD__ );
119
120 $dbw->newDeleteQueryBuilder()
121 ->deleteFrom( 'revision' )
122 ->where( [ 'rev_id' => $ids ] )
123 ->caller( __METHOD__ )->execute();
124
125 $this->commitTransaction( $dbw, __METHOD__ );
126 }
127}
128
129$maintClass = NukePage::class;
130require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Class for handling updates to the site_stats table.
Represents a title within MediaWiki.
Definition Title.php:78
Maintenance script that erases a page record from the database.
Definition nukePage.php:36
deleteRevisions( $ids)
Definition nukePage.php:116
__construct()
Default constructor.
Definition nukePage.php:37
execute()
Do the actual work.
Definition nukePage.php:44
$maintClass
Definition nukePage.php:129