MediaWiki REL1_41
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->newSelectQueryBuilder()
63 ->select( 'page_title' )
64 ->from( 'page' )
65 ->where( [ 'page_namespace' => $ns ] )
66 ->caller( __METHOD__ )->fetchResultSet();
67
68 $n_deleted = 0;
69
70 foreach ( $res as $row ) {
71 // echo "$ns_name:".$row->page_title, "\n";
72 $title = Title::makeTitle( $ns, $row->page_title );
73 $id = $title->getArticleID();
74
75 // Get corresponding revisions
76 $revs = $dbw->newSelectQueryBuilder()
77 ->select( 'rev_id' )
78 ->from( 'revision' )
79 ->where( [ 'rev_page' => $id ] )
80 ->caller( __METHOD__ )->fetchFieldValues();
81 $count = count( $revs );
82
83 // skip anything that looks modified (i.e. multiple revs)
84 if ( $all || $count == 1 ) {
85 # echo $title->getPrefixedText(), "\t", $count, "\n";
86 $this->output( "delete: " . $title->getPrefixedText() . "\n" );
87
88 // as much as I hate to cut & paste this, it's a little different, and
89 // I already have the id & revs
90 if ( $delete ) {
91 $dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
92 $this->commitTransaction( $dbw, __METHOD__ );
93 // Delete revisions as appropriate
95 $child = $this->runChild( NukePage::class, 'nukePage.php' );
96 '@phan-var NukePage $child';
97 $child->deleteRevisions( $revs );
98 $n_deleted++;
99 }
100 } else {
101 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
102 }
103 }
104 $this->commitTransaction( $dbw, __METHOD__ );
105
106 if ( $n_deleted > 0 ) {
107 $this->purgeRedundantText( true );
108
109 # update statistics - better to decrement existing count, or just count
110 # the page table?
111 $pages = $dbw->newSelectQueryBuilder()
112 ->select( 'ss_total_pages' )
113 ->from( 'site_stats' )
114 ->caller( __METHOD__ )->fetchField();
115 $pages -= $n_deleted;
116 $dbw->update(
117 'site_stats',
118 [ 'ss_total_pages' => $pages ],
119 [ 'ss_row_id' => 1 ],
120 __METHOD__
121 );
122 }
123
124 if ( !$delete ) {
125 $this->output( "To update the database, run the script with the --delete option.\n" );
126 }
127 }
128}
129
130$maintClass = NukeNS::class;
131require_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:76
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:130
const DB_PRIMARY
Definition defines.php:28