Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / nukeNS.php
blob479dcf7611aa671f2398ff6b51d64748c1fb090e
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 require_once __DIR__ . '/Maintenance.php';
38 /**
39 * Maintenance script that removes pages with only one revision from the
40 * MediaWiki namespace.
42 * @ingroup Maintenance
44 class NukeNS extends Maintenance {
45 public function __construct() {
46 parent::__construct();
47 $this->mDescription = "Remove pages with only 1 revision from any namespace";
48 $this->addOption( 'delete', "Actually delete the page" );
49 $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
50 $this->addOption( 'all', 'Delete everything regardless of revision count' );
53 public function execute() {
54 $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
55 $delete = $this->getOption( 'delete', false );
56 $all = $this->getOption( 'all', false );
57 $dbw = wfGetDB( DB_MASTER );
58 $dbw->begin( __METHOD__ );
60 $tbl_pag = $dbw->tableName( 'page' );
61 $tbl_rev = $dbw->tableName( 'revision' );
62 $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
64 $n_deleted = 0;
66 foreach ( $res as $row ) {
67 // echo "$ns_name:".$row->page_title, "\n";
68 $title = Title::makeTitle( $ns, $row->page_title );
69 $id = $title->getArticleID();
71 // Get corresponding revisions
72 $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
73 $revs = array();
75 foreach ( $res2 as $row2 ) {
76 $revs[] = $row2->rev_id;
78 $count = count( $revs );
80 // skip anything that looks modified (i.e. multiple revs)
81 if ( $all || $count == 1 ) {
82 # echo $title->getPrefixedText(), "\t", $count, "\n";
83 $this->output( "delete: " . $title->getPrefixedText() . "\n" );
85 // as much as I hate to cut & paste this, it's a little different, and
86 // I already have the id & revs
87 if ( $delete ) {
88 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
89 $dbw->commit( __METHOD__ );
90 // Delete revisions as appropriate
91 $child = $this->runChild( 'NukePage', 'nukePage.php' );
92 $child->deleteRevisions( $revs );
93 $this->purgeRedundantText( true );
94 $n_deleted ++;
96 } else {
97 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
100 $dbw->commit( __METHOD__ );
102 if ( $n_deleted > 0 ) {
103 # update statistics - better to decrement existing count, or just count
104 # the page table?
105 $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
106 $pages -= $n_deleted;
107 $dbw->update(
108 'site_stats',
109 array( 'ss_total_pages' => $pages ),
110 array( 'ss_row_id' => 1 ),
111 __METHOD__
115 if ( !$delete ) {
116 $this->output( "To update the database, run the script with the --delete option.\n" );
121 $maintClass = "NukeNS";
122 require_once RUN_MAINTENANCE_IF_MAIN;