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