Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / nukeNS.php
blobb1ce6f40af3c405b38b676f77ba5fc19155bc9ab
1 <?php
2 /**
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
13 * you.
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
30 * @file
31 * @ingroup Maintenance
32 * @author Steve Sanbeg
33 * based on nukePage by Rob Church
36 // @codeCoverageIgnoreStart
37 require_once __DIR__ . '/Maintenance.php';
38 // @codeCoverageIgnoreEnd
40 use MediaWiki\Maintenance\Maintenance;
41 use MediaWiki\Title\Title;
43 /**
44 * Maintenance script that removes pages with only one revision from the
45 * MediaWiki namespace.
47 * @ingroup Maintenance
49 class 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' );
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__ );
65 $res = $dbw->newSelectQueryBuilder()
66 ->select( 'page_title' )
67 ->from( 'page' )
68 ->where( [ 'page_namespace' => $ns ] )
69 ->caller( __METHOD__ )->fetchResultSet();
71 $n_deleted = 0;
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();
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 );
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" );
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
100 /** @var NukePage $child */
101 $child = $this->runChild( NukePage::class, 'nukePage.php' );
102 '@phan-var NukePage $child';
103 $child->deleteRevisions( $revs );
104 $n_deleted++;
106 } else {
107 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
110 $this->commitTransaction( $dbw, __METHOD__ );
112 if ( $n_deleted > 0 ) {
113 $this->purgeRedundantText( true );
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();
130 if ( !$delete ) {
131 $this->output( "To update the database, run the script with the --delete option.\n" );
136 // @codeCoverageIgnoreStart
137 $maintClass = NukeNS::class;
138 require_once RUN_MAINTENANCE_IF_MAIN;
139 // @codeCoverageIgnoreEnd