summaryrefslogtreecommitdiff
path: root/maintenance/deleteOldRevisions.inc
blob: b681b9d015011d0c4d7669a40f016bfc6988f20e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
 * Support functions for the deleteOldRevisions script
 *
 * @file
 * @ingroup Maintenance
 * @author Rob Church <robchur@gmail.com>
 */
 
require_once( 'purgeOldText.inc' );

function DeleteOldRevisions( $delete = false, $args = array() ) {

	# Data should come off the master, wrapped in a transaction
	$dbw = wfGetDB( DB_MASTER );
	$dbw->begin();
	
	$tbl_pag = $dbw->tableName( 'page' );
	$tbl_rev = $dbw->tableName( 'revision' );
	
	$pageIdClause = '';
	$revPageClause = '';
	
	# If a list of page_ids was provided, limit results to that set of page_ids
	if ( sizeof( $args ) > 0 ) {
		$pageIdList = implode( ',', $args );
		$pageIdClause = " WHERE page_id IN ({$pageIdList})";
		$revPageClause = " AND rev_page IN ({$pageIdList})";
		echo( "Limiting to {$tbl_pag}.page_id IN ({$pageIdList})\n" );
	}
	
	# Get "active" revisions from the page table
	echo( "Searching for active revisions..." );
	$res = $dbw->query( "SELECT page_latest FROM $tbl_pag{$pageIdClause}" );
	while( $row = $dbw->fetchObject( $res ) ) {
		$cur[] = $row->page_latest;
	}
	echo( "done.\n" );
	
	# Get all revisions that aren't in this set
	echo( "Searching for inactive revisions..." );
	$set = implode( ', ', $cur );
	$res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" );
	while( $row = $dbw->fetchObject( $res ) ) {
		$old[] = $row->rev_id;
	}
	echo( "done.\n" );
	
	# Inform the user of what we're going to do
	$count = count( $old );
	echo( "$count old revisions found.\n" );
	
	# Delete as appropriate
	if( $delete && $count ) {
		echo( "Deleting..." );
		$set = implode( ', ', $old );
		$dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
		echo( "done.\n" );
	}
	
	# This bit's done
	# Purge redundant text records
	$dbw->commit();
	if( $delete ) {
		PurgeRedundantText( true );
	}

}