]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/nukeNS.php
MediaWiki 1.14.0
[autoinstallsdev/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  * @file
17  * @ingroup Maintenance
18  * @author Steve Sanbeg
19  * based on nukePage by Rob Church
20  */
21
22 require_once( 'commandLine.inc' );
23 require_once( 'nukePage.inc' );
24
25 $ns = NS_MEDIAWIKI;
26 $delete = false;
27
28 if (isset($options['ns'])) 
29 {
30   $ns = $options['ns'];
31 }
32
33 if (isset( $options['delete'] ) and $options['delete']) 
34 {
35   $delete = true;
36 }
37
38
39 NukeNS( $ns, $delete);
40
41 function NukeNS($ns_no, $delete) {
42
43   $dbw = wfGetDB( DB_MASTER );
44   $dbw->begin();
45   
46   $tbl_pag = $dbw->tableName( 'page' );
47   $tbl_rev = $dbw->tableName( 'revision' );
48   $res = $dbw->query( "SELECT page_title FROM $tbl_pag WHERE page_namespace = $ns_no" );
49
50   $n_deleted = 0;
51   
52   while( $row = $dbw->fetchObject( $res ) ) {
53     //echo "$ns_name:".$row->page_title, "\n";
54     $title = Title::newFromText($row->page_title, $ns_no);
55     $id   = $title->getArticleID();
56
57     // Get corresponding revisions
58     $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
59     $revs = array();
60     
61     while( $row2 = $dbw->fetchObject( $res2 ) ) {
62       $revs[] = $row2->rev_id;
63     }
64     $count = count( $revs );
65
66     //skip anything that looks modified (i.e. multiple revs)
67     if (($count == 1)) {
68       #echo $title->getPrefixedText(), "\t", $count, "\n";
69       echo "delete: ", $title->getPrefixedText(), "\n";
70       
71       //as much as I hate to cut & paste this, it's a little different, and
72       //I already have the id & revs
73       
74       if( $delete ) {
75         $dbw->query( "DELETE FROM $tbl_pag WHERE page_id = $id" );
76         $dbw->commit();
77         // Delete revisions as appropriate
78         DeleteRevisions( $revs );
79         PurgeRedundantText( true );
80         $n_deleted ++;
81       }
82     } else {
83       echo "skip: ", $title->getPrefixedText(), "\n";
84     }
85     
86     
87   }
88   $dbw->commit();
89   
90   if ($n_deleted > 0) {
91     #update statistics - better to decrement existing count, or just count
92     #the page table?
93     $pages = $dbw->selectField('site_stats', 'ss_total_pages');
94     $pages -= $n_deleted;
95     $dbw->update( 'site_stats', 
96                   array('ss_total_pages' => $pages ), 
97                   array( 'ss_row_id' => 1),
98                   __METHOD__ );
99     
100   }
101   
102   if (!$delete) {
103     echo( "To update the database, run the script with the --delete option.\n" );
104   }
105   
106 }
107
108