MediaWiki master
nukeNS.php
Go to the documentation of this file.
1<?php
36// @codeCoverageIgnoreStart
37require_once __DIR__ . '/Maintenance.php';
38// @codeCoverageIgnoreEnd
39
42
49class NukeNS extends Maintenance {
50 public function __construct() {
51 parent::__construct();
52 $this->addDescription( 'Remove pages with only 1 revision from any namespace' );
53 $this->addOption( 'delete', "Actually delete the page" );
54 $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
55 $this->addOption( 'all', 'Delete everything regardless of revision count' );
56 }
57
58 public function execute() {
59 $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
60 $delete = $this->hasOption( 'delete' );
61 $all = $this->hasOption( 'all' );
62 $dbw = $this->getPrimaryDB();
63 $this->beginTransaction( $dbw, __METHOD__ );
64
65 $res = $dbw->newSelectQueryBuilder()
66 ->select( 'page_title' )
67 ->from( 'page' )
68 ->where( [ 'page_namespace' => $ns ] )
69 ->caller( __METHOD__ )->fetchResultSet();
70
71 $n_deleted = 0;
72
73 foreach ( $res as $row ) {
74 // echo "$ns_name:".$row->page_title, "\n";
75 $title = Title::makeTitle( $ns, $row->page_title );
76 $id = $title->getArticleID();
77
78 // Get corresponding revisions
79 $revs = $dbw->newSelectQueryBuilder()
80 ->select( 'rev_id' )
81 ->from( 'revision' )
82 ->where( [ 'rev_page' => $id ] )
83 ->caller( __METHOD__ )->fetchFieldValues();
84 $count = count( $revs );
85
86 // skip anything that looks modified (i.e. multiple revs)
87 if ( $all || $count == 1 ) {
88 # echo $title->getPrefixedText(), "\t", $count, "\n";
89 $this->output( "delete: " . $title->getPrefixedText() . "\n" );
90
91 // as much as I hate to cut & paste this, it's a little different, and
92 // I already have the id & revs
93 if ( $delete ) {
94 $dbw->newDeleteQueryBuilder()
95 ->deleteFrom( 'page' )
96 ->where( [ 'page_id' => $id ] )
97 ->caller( __METHOD__ )->execute();
98 $this->commitTransaction( $dbw, __METHOD__ );
99 // Delete revisions as appropriate
101 $child = $this->runChild( NukePage::class, 'nukePage.php' );
102 '@phan-var NukePage $child';
103 $child->deleteRevisions( $revs );
104 $n_deleted++;
105 }
106 } else {
107 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
108 }
109 }
110 $this->commitTransaction( $dbw, __METHOD__ );
111
112 if ( $n_deleted > 0 ) {
113 $this->purgeRedundantText( true );
114
115 # update statistics - better to decrement existing count, or just count
116 # the page table?
117 $pages = $dbw->newSelectQueryBuilder()
118 ->select( 'ss_total_pages' )
119 ->from( 'site_stats' )
120 ->caller( __METHOD__ )->fetchField();
121 $pages -= $n_deleted;
122 $dbw->newUpdateQueryBuilder()
123 ->update( 'site_stats' )
124 ->set( [ 'ss_total_pages' => $pages ] )
125 ->where( [ 'ss_row_id' => 1 ] )
126 ->caller( __METHOD__ )
127 ->execute();
128 }
129
130 if ( !$delete ) {
131 $this->output( "To update the database, run the script with the --delete option.\n" );
132 }
133 }
134}
135
136// @codeCoverageIgnoreStart
137$maintClass = NukeNS::class;
138require_once RUN_MAINTENANCE_IF_MAIN;
139// @codeCoverageIgnoreEnd
const NS_MEDIAWIKI
Definition Defines.php:73
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DB servers to catch up.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
runChild( $maintClass, $classFile=null)
Returns an instance of the given maintenance script, with all of the current arguments passed to it.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB handle.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:78
Maintenance script that removes pages with only one revision from the MediaWiki namespace.
Definition nukeNS.php:49
execute()
Do the actual work.
Definition nukeNS.php:58
__construct()
Default constructor.
Definition nukeNS.php:50
$maintClass
Definition nukeNS.php:137