MediaWiki  master
nukeNS.php
Go to the documentation of this file.
1 <?php
36 require_once __DIR__ . '/Maintenance.php';
37 
44 class NukeNS extends Maintenance {
45  public function __construct() {
46  parent::__construct();
47  $this->addDescription( 'Remove pages with only 1 revision from any namespace' );
48  $this->addOption( 'delete', "Actually delete the page" );
49  $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
50  $this->addOption( 'all', 'Delete everything regardless of revision count' );
51  }
52 
53  public function execute() {
54  $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
55  $delete = $this->hasOption( 'delete' );
56  $all = $this->hasOption( 'all' );
57  $dbw = $this->getDB( DB_MASTER );
58  $this->beginTransaction( $dbw, __METHOD__ );
59 
60  $res = $dbw->select( 'page', 'page_title', [ 'page_namespace' => $ns ], __METHOD__ );
61 
62  $n_deleted = 0;
63 
64  foreach ( $res as $row ) {
65  // echo "$ns_name:".$row->page_title, "\n";
66  $title = Title::makeTitle( $ns, $row->page_title );
67  $id = $title->getArticleID();
68 
69  // Get corresponding revisions
70  $revs = $dbw->selectFieldValues(
71  'revision',
72  'rev_id',
73  [ 'rev_page' => $id ],
74  __METHOD__
75  );
76  $count = count( $revs );
77 
78  // skip anything that looks modified (i.e. multiple revs)
79  if ( $all || $count == 1 ) {
80  # echo $title->getPrefixedText(), "\t", $count, "\n";
81  $this->output( "delete: " . $title->getPrefixedText() . "\n" );
82 
83  // as much as I hate to cut & paste this, it's a little different, and
84  // I already have the id & revs
85  if ( $delete ) {
86  $dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
87  $this->commitTransaction( $dbw, __METHOD__ );
88  // Delete revisions as appropriate
90  $child = $this->runChild( NukePage::class, 'nukePage.php' );
91  '@phan-var NukePage $child';
92  $child->deleteRevisions( $revs );
93  $this->purgeRedundantText( true );
94  $n_deleted++;
95  }
96  } else {
97  $this->output( "skip: " . $title->getPrefixedText() . "\n" );
98  }
99  }
100  $this->commitTransaction( $dbw, __METHOD__ );
101 
102  if ( $n_deleted > 0 ) {
103  # update statistics - better to decrement existing count, or just count
104  # the page table?
105  $pages = $dbw->selectField( 'site_stats', 'ss_total_pages', [], __METHOD__ );
106  $pages -= $n_deleted;
107  $dbw->update(
108  'site_stats',
109  [ 'ss_total_pages' => $pages ],
110  [ 'ss_row_id' => 1 ],
111  __METHOD__
112  );
113  }
114 
115  if ( !$delete ) {
116  $this->output( "To update the database, run the script with the --delete option.\n" );
117  }
118  }
119 }
120 
121 $maintClass = NukeNS::class;
122 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
$maintClass
$maintClass
Definition: nukeNS.php:121
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
Maintenance\runChild
runChild( $maintClass, $classFile=null)
Run a child maintenance script.
Definition: Maintenance.php:701
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
$res
$res
Definition: testCompression.php:57
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
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
DB_MASTER
const DB_MASTER
Definition: defines.php:26
NukeNS\execute
execute()
Do the actual work.
Definition: nukeNS.php:53
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
NukeNS\__construct
__construct()
Default constructor.
Definition: nukeNS.php:45
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
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:277
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:77
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:262
NukeNS
Maintenance script that removes pages with only one revision from the MediaWiki namespace.
Definition: nukeNS.php:44