a1ae3f02a3e68f05132743f6bcd4e340202a9cd0
[lhc/web/wiklou.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 * @addtogroup Maintenance
17 * @author Steve Sanbeg
18 * based on nukePage by Rob Church
19 */
20
21 require_once( 'commandLine.inc' );
22 require_once( 'nukePage.inc' );
23
24 $ns = NS_MEDIAWIKI;
25 $delete = false;
26
27 if (isset($options['ns']))
28 {
29 $ns = $options['ns'];
30 }
31
32 if (isset( $options['delete'] ) and $options['delete'])
33 {
34 $delete = true;
35 }
36
37
38 NukeNS( $ns, $delete);
39
40 function NukeNS($ns_no, $delete) {
41
42 $dbw =& wfGetDB( DB_MASTER );
43 $dbw->begin();
44
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" );
48
49 $n_deleted = 0;
50
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();
55
56 // Get corresponding revisions
57 $res2 = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_page = $id" );
58 $revs = array();
59
60 while( $row2 = $dbw->fetchObject( $res2 ) ) {
61 $revs[] = $row2->rev_id;
62 }
63 $count = count( $revs );
64
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";
69
70 //as much as I hate to cut & paste this, it's a little different, and
71 //I already have the id & revs
72
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 ++;
80 }
81 } else {
82 echo "skip: ", $title->getPrefixedText(), "\n";
83 }
84
85
86 }
87 $dbw->commit();
88
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__ );
98
99 }
100
101 if (!$delete) {
102 echo( "To update the database, run the script with the --delete option.\n" );
103 }
104
105 }
106
107
108 ?>