Handle empty sets correctly
[mediawiki.git] / maintenance / nukeNS.php
blob9bf7004b1f647cd7ed4528fcb27f7227fcd5c661
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 * @addtogroup Maintenance
17 * @author Steve Sanbeg
18 * based on nukePage by Rob Church
21 require_once( 'commandLine.inc' );
22 require_once( 'nukePage.inc' );
24 $ns = NS_MEDIAWIKI;
25 $delete = false;
27 if (isset($options['ns']))
29 $ns = $options['ns'];
32 if (isset( $options['delete'] ) and $options['delete'])
34 $delete = true;
38 NukeNS( $ns, $delete);
40 function NukeNS($ns_no, $delete) {
42 $dbw = wfGetDB( DB_MASTER );
43 $dbw->begin();
45 $tbl_pag = $dbw->tableName( 'page' );
46 $tbl_rev = $dbw->tableName( 'revision' );
47 $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns_no" );
49 $n_deleted = 0;
51 while( $row = $dbw->fetchObject( $res ) ) {
52 //echo "$ns_name:".$row->page_title, "\n";
53 $title = Title::newFromText($row->page_title, $ns_no);
54 $id = $title->getArticleID();
56 // Get corresponding revisions
57 $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
58 $revs = array();
60 while( $row2 = $dbw->fetchObject( $res2 ) ) {
61 $revs[] = $row2->rev_id;
63 $count = count( $revs );
65 //skip anything that looks modified (i.e. multiple revs)
66 if (($count == 1)) {
67 #echo $title->getPrefixedText(), "\t", $count, "\n";
68 echo "delete: ", $title->getPrefixedText(), "\n";
70 //as much as I hate to cut & paste this, it's a little different, and
71 //I already have the id & revs
73 if( $delete ) {
74 $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
75 $dbw->commit();
76 // Delete revisions as appropriate
77 DeleteRevisions( $revs );
78 PurgeRedundantText( true );
79 $n_deleted ++;
81 } else {
82 echo "skip: ", $title->getPrefixedText(), "\n";
87 $dbw->commit();
89 if ($n_deleted > 0) {
90 #update statistics - better to decrement existing count, or just count
91 #the page table?
92 $pages = $dbw->selectField('site_stats', 'ss_total_pages');
93 $pages -= $n_deleted;
94 $dbw->update( 'site_stats',
95 array('ss_total_pages' => $pages ),
96 array( 'ss_row_id' => 1),
97 __METHOD__ );
101 if (!$delete) {
102 echo( "To update the database, run the script with the --delete option.\n" );