Fixed warning. QueryPage subclasses are required to allow construction with no constr...
[mediawiki.git] / maintenance / nukeNS.php
blob4f9fe926d2c41649805317f3c61ebc616f4d7414
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 * @file
17 * @ingroup Maintenance
18 * @author Steve Sanbeg
19 * based on nukePage by Rob Church
22 require_once( 'commandLine.inc' );
23 require_once( 'nukePage.inc' );
25 $ns = NS_MEDIAWIKI;
26 $delete = false;
28 if (isset($options['ns']))
30 $ns = $options['ns'];
33 if (isset( $options['delete'] ) and $options['delete'])
35 $delete = true;
39 NukeNS( $ns, $delete);
41 function NukeNS($ns_no, $delete) {
43 $dbw = wfGetDB( DB_MASTER );
44 $dbw->begin();
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" );
50 $n_deleted = 0;
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();
57 // Get corresponding revisions
58 $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
59 $revs = array();
61 while( $row2 = $dbw->fetchObject( $res2 ) ) {
62 $revs[] = $row2->rev_id;
64 $count = count( $revs );
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";
71 //as much as I hate to cut & paste this, it's a little different, and
72 //I already have the id & revs
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 ++;
82 } else {
83 echo "skip: ", $title->getPrefixedText(), "\n";
88 $dbw->commit();
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__ );
102 if (!$delete) {
103 echo( "To update the database, run the script with the --delete option.\n" );