MediaWiki REL1_40
nukePage.php
Go to the documentation of this file.
1<?php
26require_once __DIR__ . '/Maintenance.php';
27
29
35class NukePage extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Remove a page record from the database' );
39 $this->addOption( 'delete', "Actually delete the page" );
40 $this->addArg( 'title', 'Title to delete' );
41 }
42
43 public function execute() {
44 $name = $this->getArg( 0 );
45 $delete = $this->hasOption( 'delete' );
46
47 $dbw = $this->getDB( DB_PRIMARY );
48 $this->beginTransaction( $dbw, __METHOD__ );
49
50 # Get page ID
51 $this->output( "Searching for \"$name\"..." );
52 $title = Title::newFromText( $name );
53 if ( $title ) {
54 $id = $title->getArticleID();
55 $real = $title->getPrefixedText();
56 $isGoodArticle = $title->isContentPage();
57 $this->output( "found \"$real\" with ID $id.\n" );
58
59 # Get corresponding revisions
60 $this->output( "Searching for revisions..." );
61
62 $revs = $dbw->selectFieldValues(
63 'revision',
64 'rev_id',
65 [ 'rev_page' => $id ],
66 __METHOD__
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->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
75 $this->output( "done.\n" );
76 $this->output( "Cleaning up recent changes..." );
77 $dbw->delete( 'recentchanges', [ 'rc_cur_id' => $id ], __METHOD__ );
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 // if it was good, decrement that too
95 $ga = $isGoodArticle ? -1 : 0;
96 $stats = SiteStatsUpdate::factory( [
97 'edits' => -$count,
98 'articles' => $ga,
99 'pages' => -1
100 ] );
101 $stats->doUpdate();
102 $this->output( "done.\n" );
103 }
104 } else {
105 $this->output( "not found in database.\n" );
106 $this->commitTransaction( $dbw, __METHOD__ );
107 }
108 }
109
110 public function deleteRevisions( $ids ) {
111 $dbw = $this->getDB( DB_PRIMARY );
112 $this->beginTransaction( $dbw, __METHOD__ );
113
114 $dbw->delete( 'revision', [ 'rev_id' => $ids ], __METHOD__ );
115
116 $this->commitTransaction( $dbw, __METHOD__ );
117 }
118}
119
120$maintClass = NukePage::class;
121require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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.
Represents a title within MediaWiki.
Definition Title.php:82
Maintenance script that erases a page record from the database.
Definition nukePage.php:35
deleteRevisions( $ids)
Definition nukePage.php:110
__construct()
Default constructor.
Definition nukePage.php:36
execute()
Do the actual work.
Definition nukePage.php:43
$maintClass
Definition nukePage.php:120
const DB_PRIMARY
Definition defines.php:28