MediaWiki master
nukePage.php
Go to the documentation of this file.
1<?php
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/Maintenance.php';
14// @codeCoverageIgnoreEnd
15
19
25class NukePage extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription( 'Remove a page record from the database' );
29 $this->addOption( 'delete', "Actually delete the page" );
30 $this->addArg( 'title', 'Title to delete' );
31 }
32
33 public function execute() {
34 $name = $this->getArg( 0 );
35 $delete = $this->hasOption( 'delete' );
36
37 $dbw = $this->getPrimaryDB();
38 $this->beginTransactionRound( __METHOD__ );
39
40 # Get page ID
41 $this->output( "Searching for \"$name\"..." );
42 $title = Title::newFromText( $name );
43 if ( $title && $title->exists() ) {
44 $id = $title->getArticleID();
45 $real = $title->getPrefixedText();
46 $isGoodArticle = $title->isContentPage();
47 $this->output( "found \"$real\" with ID $id.\n" );
48
49 # Get corresponding revisions
50 $this->output( "Searching for revisions..." );
51
52 $revs = $dbw->newSelectQueryBuilder()
53 ->select( 'rev_id' )
54 ->from( 'revision' )
55 ->where( [ 'rev_page' => $id ] )
56 ->caller( __METHOD__ )->fetchFieldValues();
57 $count = count( $revs );
58 $this->output( "found $count.\n" );
59
60 # Delete the page record and associated recent changes entries
61 if ( $delete ) {
62 $this->output( "Deleting page record..." );
63 $dbw->newDeleteQueryBuilder()
64 ->deleteFrom( 'page' )
65 ->where( [ 'page_id' => $id ] )
66 ->caller( __METHOD__ )->execute();
67 $this->output( "done.\n" );
68 $this->output( "Cleaning up recent changes..." );
69 $dbw->newDeleteQueryBuilder()
70 ->deleteFrom( 'recentchanges' )
71 ->where( [ 'rc_cur_id' => $id ] )
72 ->caller( __METHOD__ )->execute();
73 $this->output( "done.\n" );
74 }
75
76 $this->commitTransactionRound( __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 // if it was good, decrement that too
90 $ga = $isGoodArticle ? -1 : 0;
91 $stats = SiteStatsUpdate::factory( [
92 'edits' => -$count,
93 'articles' => $ga,
94 'pages' => -1
95 ] );
96 $stats->doUpdate();
97 $this->output( "done.\n" );
98 }
99 } else {
100 $this->output( "not found in database.\n" );
101 $this->commitTransactionRound( __METHOD__ );
102 }
103 }
104
105 public function deleteRevisions( array $ids ) {
106 $dbw = $this->getPrimaryDB();
107 $this->beginTransactionRound( __METHOD__ );
108
109 $dbw->newDeleteQueryBuilder()
110 ->deleteFrom( 'revision' )
111 ->where( [ 'rev_id' => $ids ] )
112 ->caller( __METHOD__ )->execute();
113
114 $this->commitTransactionRound( __METHOD__ );
115 }
116}
117
118// @codeCoverageIgnoreStart
119$maintClass = NukePage::class;
120require_once RUN_MAINTENANCE_IF_MAIN;
121// @codeCoverageIgnoreEnd
Class for handling updates to the site_stats table.
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.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
Maintenance script that erases a page record from the database.
Definition nukePage.php:25
deleteRevisions(array $ids)
Definition nukePage.php:105
__construct()
Default constructor.
Definition nukePage.php:26
execute()
Do the actual work.
Definition nukePage.php:33
$maintClass
Definition nukePage.php:119