]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/nukeNS.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / nukeNS.php
1 <?php
2
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
7  *
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.
15  *
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.
20  *
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.
25  *
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
30  *
31  * @ingroup Maintenance
32  * @author Steve Sanbeg
33  * based on nukePage by Rob Church
34  */
35
36 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
37
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         }
45
46         public function execute() {
47                 $ns = $this->getOption( 'ns', NS_MEDIAWIKI );
48                 $delete = $this->getOption( 'delete', false );
49                 $dbw = wfGetDB( DB_MASTER );
50                 $dbw->begin();
51
52                 $tbl_pag = $dbw->tableName( 'page' );
53                 $tbl_rev = $dbw->tableName( 'revision' );
54                 $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns" );
55
56                 $n_deleted = 0;
57
58                 foreach ( $res as $row ) {
59                         // echo "$ns_name:".$row->page_title, "\n";
60                         $title = Title::makeTitle( $ns, $row->page_title );
61                         $id   = $title->getArticleID();
62
63                         // Get corresponding revisions
64                         $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
65                         $revs = array();
66
67                         foreach ( $res2 as $row2 ) {
68                                 $revs[] = $row2->rev_id;
69                         }
70                         $count = count( $revs );
71
72                         // skip anything that looks modified (i.e. multiple revs)
73                         if ( $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                                         $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
81                                         $dbw->commit();
82                                         // Delete revisions as appropriate
83                                         $child = $this->runChild( 'NukePage', 'NukePage.php' );
84                                         $child->deleteRevisions( $revs );
85                                         $this->purgeRedundantText( true );
86                                         $n_deleted ++;
87                                 }
88                         } else {
89                           $this->output( "skip: " . $title->getPrefixedText() . "\n" );
90                         }
91                 }
92                 $dbw->commit();
93
94                 if ( $n_deleted > 0 ) {
95                         # update statistics - better to decrement existing count, or just count
96                         # the page table?
97                         $pages = $dbw->selectField( 'site_stats', 'ss_total_pages' );
98                         $pages -= $n_deleted;
99                         $dbw->update(
100                                 'site_stats',
101                                 array( 'ss_total_pages' => $pages ),
102                                 array( 'ss_row_id' => 1 ),
103                                 __METHOD__
104                         );
105                 }
106
107                 if ( !$delete ) {
108                         $this->output( "To update the database, run the script with the --delete option.\n" );
109                 }
110         }
111 }
112
113 $maintClass = "NukeNS";
114 require_once( RUN_MAINTENANCE_IF_MAIN );