MediaWiki  master
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  # Get page ID
49  $this->output( "Searching for \"$name\"..." );
50  $title = Title::newFromText( $name );
51  if ( $title ) {
52  $id = $title->getArticleID();
53  $real = $title->getPrefixedText();
54  $isGoodArticle = $title->isContentPage();
55  $this->output( "found \"$real\" with ID $id.\n" );
56 
57  # Get corresponding revisions
58  $this->output( "Searching for revisions..." );
59 
60  $revs = $dbw->selectFieldValues(
61  'revision',
62  'rev_id',
63  [ 'rev_page' => $id ],
64  __METHOD__
65  );
66  $count = count( $revs );
67  $this->output( "found $count.\n" );
68 
69  # Delete the page record and associated recent changes entries
70  if ( $delete ) {
71  $this->output( "Deleting page record..." );
72  $dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
73  $this->output( "done.\n" );
74  $this->output( "Cleaning up recent changes..." );
75  $dbw->delete( 'recentchanges', [ 'rc_cur_id' => $id ], __METHOD__ );
76  $this->output( "done.\n" );
77  }
78 
79  $this->commitTransaction( $dbw, __METHOD__ );
80 
81  # Delete revisions as appropriate
82  if ( $delete && $count ) {
83  $this->output( "Deleting revisions..." );
84  $this->deleteRevisions( $revs );
85  $this->output( "done.\n" );
86  $this->purgeRedundantText( true );
87  }
88 
89  # Update stats as appropriate
90  if ( $delete ) {
91  $this->output( "Updating site stats..." );
92  // if it was good, decrement that too
93  $ga = $isGoodArticle ? -1 : 0;
94  $stats = SiteStatsUpdate::factory( [
95  'edits' => -$count,
96  'articles' => $ga,
97  'pages' => -1
98  ] );
99  $stats->doUpdate();
100  $this->output( "done.\n" );
101  }
102  } else {
103  $this->output( "not found in database.\n" );
104  $this->commitTransaction( $dbw, __METHOD__ );
105  }
106  }
107 
108  public function deleteRevisions( $ids ) {
109  $dbw = $this->getDB( DB_MASTER );
110  $this->beginTransaction( $dbw, __METHOD__ );
111 
112  $dbw->delete( 'revision', [ 'rev_id' => $ids ], __METHOD__ );
113 
114  $this->commitTransaction( $dbw, __METHOD__ );
115  }
116 }
117 
118 $maintClass = NukePage::class;
119 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
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:329
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:323
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:53
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1380
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:241
$title
$title
Definition: testCompression.php:38
SiteStatsUpdate\factory
static factory(array $deltas)
Definition: SiteStatsUpdate.php:71
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: nukePage.php:118
NukePage\deleteRevisions
deleteRevisions( $ids)
Definition: nukePage.php:108
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1395
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1350
Maintenance\purgeRedundantText
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
Definition: Maintenance.php:1287
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:294
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:262
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:347