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