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