MediaWiki master
nukePage.php
Go to the documentation of this file.
1<?php
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
32
38class 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;
133require_once RUN_MAINTENANCE_IF_MAIN;
134// @codeCoverageIgnoreEnd
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:38
deleteRevisions( $ids)
Definition nukePage.php:118
__construct()
Default constructor.
Definition nukePage.php:39
execute()
Do the actual work.
Definition nukePage.php:46
$maintClass
Definition nukePage.php:132