3 * Remove pages with only 1 revision from the MediaWiki namespace, without
4 * flooding recent changes, delete logs, etc.
5 * Irreversible (can't use standard undelete) and does not update link tables
7 * This is mainly useful to run before maintenance/update.php when upgrading
8 * to 1.9, to prevent flooding recent changes/deletion logs. It's intended
9 * to be conservative, so it's possible that a few entries will be left for
10 * deletion by the upgrade script. It's also possible that it hasn't been
11 * tested thouroughly enough, and will delete something it shouldn't; so
12 * back up your DB if there's anything in the MediaWiki that is important to
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
31 * @ingroup Maintenance
32 * @author Steve Sanbeg
33 * based on nukePage by Rob Church
36 require_once __DIR__
. '/Maintenance.php';
38 use MediaWiki\Title\Title
;
41 * Maintenance script that removes pages with only one revision from the
42 * MediaWiki namespace.
44 * @ingroup Maintenance
46 class 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' );
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__
);
62 $res = $dbw->newSelectQueryBuilder()
63 ->select( 'page_title' )
65 ->where( [ 'page_namespace' => $ns ] )
66 ->caller( __METHOD__
)->fetchResultSet();
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();
75 // Get corresponding revisions
76 $revs = $dbw->newSelectQueryBuilder()
79 ->where( [ 'rev_page' => $id ] )
80 ->caller( __METHOD__
)->fetchFieldValues();
81 $count = count( $revs );
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" );
88 // as much as I hate to cut & paste this, it's a little different, and
89 // I already have the id & revs
91 $dbw->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
92 $this->commitTransaction( $dbw, __METHOD__
);
93 // Delete revisions as appropriate
94 /** @var NukePage $child */
95 $child = $this->runChild( NukePage
::class, 'nukePage.php' );
96 '@phan-var NukePage $child';
97 $child->deleteRevisions( $revs );
101 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
104 $this->commitTransaction( $dbw, __METHOD__
);
106 if ( $n_deleted > 0 ) {
107 $this->purgeRedundantText( true );
109 # update statistics - better to decrement existing count, or just count
111 $pages = $dbw->newSelectQueryBuilder()
112 ->select( 'ss_total_pages' )
113 ->from( 'site_stats' )
114 ->caller( __METHOD__
)->fetchField();
115 $pages -= $n_deleted;
118 [ 'ss_total_pages' => $pages ],
119 [ 'ss_row_id' => 1 ],
125 $this->output( "To update the database, run the script with the --delete option.\n" );
130 $maintClass = NukeNS
::class;
131 require_once RUN_MAINTENANCE_IF_MAIN
;