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 $deleteQueryBuilder = $dbw->newDeleteQueryBuilder()
64 ->deleteFrom( 'page' )
65 ->where( [ 'page_id' => $id ] )
66 ->caller( __METHOD__ );
67 $deleteQueryBuilder->execute();
68 $this->getServiceContainer()->getLinkWriteDuplicator()->duplicate( $deleteQueryBuilder );
69 $this->output( "done.\n" );
70 $this->output( "Cleaning up recent changes..." );
71 $dbw->newDeleteQueryBuilder()
72 ->deleteFrom( 'recentchanges' )
73 ->where( [ 'rc_cur_id' => $id ] )
74 ->caller( __METHOD__ )->execute();
75 $this->output( "done.\n" );
76 }
77
78 $this->commitTransactionRound( __METHOD__ );
79
80 # Delete revisions as appropriate
81 if ( $delete && $count ) {
82 $this->output( "Deleting revisions..." );
83 $this->deleteRevisions( $revs );
84 $this->output( "done.\n" );
85 $this->purgeRedundantText( true );
86 }
87
88 # Update stats as appropriate
89 if ( $delete ) {
90 $this->output( "Updating site stats..." );
91 // if it was good, decrement that too
92 $ga = $isGoodArticle ? -1 : 0;
93 $stats = SiteStatsUpdate::factory( [
94 'edits' => -$count,
95 'articles' => $ga,
96 'pages' => -1
97 ] );
98 $stats->doUpdate();
99 $this->output( "done.\n" );
100 }
101 } else {
102 $this->output( "not found in database.\n" );
103 $this->commitTransactionRound( __METHOD__ );
104 }
105 }
106
107 public function deleteRevisions( array $ids ) {
108 $dbw = $this->getPrimaryDB();
109 $this->beginTransactionRound( __METHOD__ );
110
111 $dbw->newDeleteQueryBuilder()
112 ->deleteFrom( 'revision' )
113 ->where( [ 'rev_id' => $ids ] )
114 ->caller( __METHOD__ )->execute();
115
116 $this->commitTransactionRound( __METHOD__ );
117 }
118}
119
120// @codeCoverageIgnoreStart
121$maintClass = NukePage::class;
122require_once RUN_MAINTENANCE_IF_MAIN;
123// @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.
getServiceContainer()
Returns the main service container.
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:107
__construct()
Default constructor.
Definition nukePage.php:26
execute()
Do the actual work.
Definition nukePage.php:33
$maintClass
Definition nukePage.php:121