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