MediaWiki  1.34.0
nukePage.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class NukePage extends Maintenance {
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Remove a page record from the database' );
37  $this->addOption( 'delete', "Actually delete the page" );
38  $this->addArg( 'title', 'Title to delete' );
39  }
40 
41  public function execute() {
42  $name = $this->getArg( 0 );
43  $delete = $this->hasOption( 'delete' );
44 
45  $dbw = $this->getDB( DB_MASTER );
46  $this->beginTransaction( $dbw, __METHOD__ );
47 
48  $tbl_pag = $dbw->tableName( 'page' );
49  $tbl_rec = $dbw->tableName( 'recentchanges' );
50  $tbl_rev = $dbw->tableName( 'revision' );
51 
52  # Get page ID
53  $this->output( "Searching for \"$name\"..." );
54  $title = Title::newFromText( $name );
55  if ( $title ) {
56  $id = $title->getArticleID();
57  $real = $title->getPrefixedText();
58  $isGoodArticle = $title->isContentPage();
59  $this->output( "found \"$real\" with ID $id.\n" );
60 
61  # Get corresponding revisions
62  $this->output( "Searching for revisions..." );
63  $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
64  $revs = [];
65  foreach ( $res as $row ) {
66  $revs[] = $row->rev_id;
67  }
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->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
75  $this->output( "done.\n" );
76  $this->output( "Cleaning up recent changes..." );
77  $dbw->query( "DELETE FROM $tbl_rec WHERE rc_cur_id = $id" );
78  $this->output( "done.\n" );
79  }
80 
81  $this->commitTransaction( $dbw, __METHOD__ );
82 
83  # Delete revisions as appropriate
84  if ( $delete && $count ) {
85  $this->output( "Deleting revisions..." );
86  $this->deleteRevisions( $revs );
87  $this->output( "done.\n" );
88  $this->purgeRedundantText( true );
89  }
90 
91  # Update stats as appropriate
92  if ( $delete ) {
93  $this->output( "Updating site stats..." );
94  $ga = $isGoodArticle ? -1 : 0; // if it was good, decrement that too
95  $stats = SiteStatsUpdate::factory( [
96  'edits' => -$count,
97  'articles' => $ga,
98  'pages' => -1
99  ] );
100  $stats->doUpdate();
101  $this->output( "done.\n" );
102  }
103  } else {
104  $this->output( "not found in database.\n" );
105  $this->commitTransaction( $dbw, __METHOD__ );
106  }
107  }
108 
109  public function deleteRevisions( $ids ) {
110  $dbw = $this->getDB( DB_MASTER );
111  $this->beginTransaction( $dbw, __METHOD__ );
112 
113  $tbl_rev = $dbw->tableName( 'revision' );
114 
115  $set = implode( ', ', $ids );
116  $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
117 
118  $this->commitTransaction( $dbw, __METHOD__ );
119  }
120 }
121 
122 $maintClass = NukePage::class;
123 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
NukePage
Maintenance script that erases a page record from the database.
Definition: nukePage.php:33
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
NukePage\execute
execute()
Do the actual work.
Definition: nukePage.php:41
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$res
$res
Definition: testCompression.php:52
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1426
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$title
$title
Definition: testCompression.php:34
SiteStatsUpdate\factory
static factory(array $deltas)
Definition: SiteStatsUpdate.php:71
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: nukePage.php:122
NukePage\deleteRevisions
deleteRevisions( $ids)
Definition: nukePage.php:109
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1441
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\purgeRedundantText
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
Definition: Maintenance.php:1308
NukePage\__construct
__construct()
Default constructor.
Definition: nukePage.php:34
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:319
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:371