HTMLForm: throw a warning when no callback is set
[mediawiki.git] / maintenance / nukeNS.php
blob5d4f374c9c29c9b83084638e1d8c238de5f2e36a
1 <?php
3 /**
4 * Remove pages with only 1 revision from the MediaWiki namespace, without
5 * flooding recent changes, delete logs, etc.
6 * Irreversible (can't use standard undelete) and does not update link tables
8 * This is mainly useful to run before maintenance/update.php when upgrading
9 * to 1.9, to prevent flooding recent changes/deletion logs. It's intended
10 * to be conservative, so it's possible that a few entries will be left for
11 * deletion by the upgrade script. It's also possible that it hasn't been
12 * tested thouroughly enough, and will delete something it shouldn't; so
13 * back up your DB if there's anything in the MediaWiki that is important to
14 * you.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 * http://www.gnu.org/copyleft/gpl.html
31 * @ingroup Maintenance
32 * @author Steve Sanbeg
33 * based on nukePage by Rob Church
36 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
38 class NukeNS extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->mDescription = "Remove pages with only 1 revision from any namespace";
42 $this->addOption( 'delete', "Actually delete the page" );
43 $this->addOption( 'ns', 'Namespace to delete from, default NS_MEDIAWIKI', false, true );
44 $this->addOption( 'all', 'Delete everything regardless of revision count' );
47 public function execute() {
48 $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
49 $delete = $this->getOption( 'delete', false );
50 $all = $this->getOption( 'all', false );
51 $dbw = wfGetDB( DB_MASTER );
52 $dbw->begin( __METHOD__ );
54 $tbl_pag = $dbw->tableName( 'page' );
55 $tbl_rev = $dbw->tableName( 'revision' );
56 $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
58 $n_deleted = 0;
60 foreach ( $res as $row ) {
61 // echo "$ns_name:".$row->page_title, "\n";
62 $title = Title::makeTitle( $ns, $row->page_title );
63 $id = $title->getArticleID();
65 // Get corresponding revisions
66 $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
67 $revs = array();
69 foreach ( $res2 as $row2 ) {
70 $revs[] = $row2->rev_id;
72 $count = count( $revs );
74 // skip anything that looks modified (i.e. multiple revs)
75 if ( $all || $count == 1 ) {
76 # echo $title->getPrefixedText(), "\t", $count, "\n";
77 $this->output( "delete: " . $title->getPrefixedText() . "\n" );
79 // as much as I hate to cut & paste this, it's a little different, and
80 // I already have the id & revs
81 if ( $delete ) {
82 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
83 $dbw->commit( __METHOD__ );
84 // Delete revisions as appropriate
85 $child = $this->runChild( 'NukePage', 'nukePage.php' );
86 $child->deleteRevisions( $revs );
87 $this->purgeRedundantText( true );
88 $n_deleted ++;
90 } else {
91 $this->output( "skip: " . $title->getPrefixedText() . "\n" );
94 $dbw->commit( __METHOD__ );
96 if ( $n_deleted > 0 ) {
97 # update statistics - better to decrement existing count, or just count
98 # the page table?
99 $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
100 $pages -= $n_deleted;
101 $dbw->update(
102 'site_stats',
103 array( 'ss_total_pages' => $pages ),
104 array( 'ss_row_id' => 1 ),
105 __METHOD__
109 if ( !$delete ) {
110 $this->output( "To update the database, run the script with the --delete option.\n" );
115 $maintClass = "NukeNS";
116 require_once( RUN_MAINTENANCE_IF_MAIN );