Index: trunk/extensions/CheckUser/maintenance/purgeOldData.php |
— | — | @@ -9,28 +9,60 @@ |
10 | 10 | class PurgeOldIPAddressData extends Maintenance { |
11 | 11 | public function __construct() { |
12 | 12 | parent::__construct(); |
13 | | - $this->mDescription = "Purge old IP data in CheckUser and RecentChanges"; |
| 13 | + $this->mDescription = "Purge expired rows in CheckUser and RecentChanges"; |
| 14 | + $this->setBatchSize( 200 ); |
14 | 15 | } |
15 | 16 | |
16 | 17 | public function execute() { |
17 | 18 | global $wgCUDMaxAge, $wgRCMaxAge, $wgPutIPinRC; |
18 | 19 | |
19 | | - $dbw = wfGetDB( DB_MASTER ); |
20 | | - |
21 | 20 | $this->output( "Purging data from cu_changes..." ); |
22 | | - $cutoff = $dbw->timestamp( time() - $wgCUDMaxAge ); |
23 | | - $dbw->delete( 'cu_changes', array( "cuc_timestamp < '{$cutoff}'" ), __METHOD__ ); |
24 | | - $this->output( $dbw->affectedRows() . " rows.\n" ); |
| 21 | + $count = $this->prune( 'cu_changes', 'cuc_timestamp', $wgCUDMaxAge ); |
| 22 | + $this->output( $count . " rows.\n" ); |
25 | 23 | |
26 | 24 | if ( $wgPutIPinRC ) { |
27 | 25 | $this->output( "Purging data from recentchanges..." ); |
28 | | - $cutoff = $dbw->timestamp( time() - $wgRCMaxAge ); |
29 | | - $dbw->delete( 'recentchanges', array( "rc_timestamp < '{$cutoff}'" ), __METHOD__ ); |
30 | | - $this->output( $dbw->affectedRows() . " rows.\n" ); |
| 26 | + $count = $this->prune( 'recentchanges', 'rc_timestamp', $wgRCMaxAge ); |
| 27 | + $this->output( $count . " rows.\n" ); |
31 | 28 | } |
32 | 29 | |
33 | 30 | $this->output( "Done.\n" ); |
34 | 31 | } |
| 32 | + |
| 33 | + protected function prune( $table, $ts_column, $maxAge ) { |
| 34 | + $dbw = wfGetDB( DB_MASTER ); |
| 35 | + |
| 36 | + $expiredCond = "$ts_column < " . $dbw->addQuotes( $dbw->timestamp( time() - $maxAge ) ); |
| 37 | + |
| 38 | + $count = 0; |
| 39 | + while ( true ) { |
| 40 | + // Get the first $this->mBatchSize (or less) items |
| 41 | + $res = $dbw->select( $table, $ts_column, |
| 42 | + $expiredCond, |
| 43 | + __METHOD__, |
| 44 | + array( 'ORDER BY' => "$ts_column ASC", 'LIMIT' => $this->mBatchSize ) |
| 45 | + ); |
| 46 | + if ( !$res->numRows() ) { |
| 47 | + break; // all cleared |
| 48 | + } |
| 49 | + // Record the start and end timestamp for the set |
| 50 | + $blockStart = $res->fetchObject()->$ts_column; |
| 51 | + $res->seek( $res->numRows() - 1 ); |
| 52 | + $blockEnd = $res->fetchObject()->$ts_column; |
| 53 | + $res->free(); |
| 54 | + |
| 55 | + // Do the actual delete... |
| 56 | + $dbw->begin(); |
| 57 | + $dbw->delete( $table, |
| 58 | + array( "$ts_column BETWEEN $blockStart AND $blockEnd" ), __METHOD__ ); |
| 59 | + $count += $dbw->affectedRows(); |
| 60 | + $dbw->commit(); |
| 61 | + |
| 62 | + wfWaitForSlaves(); |
| 63 | + } |
| 64 | + |
| 65 | + return $count; |
| 66 | + } |
35 | 67 | } |
36 | 68 | |
37 | 69 | $maintClass = "PurgeOldIPAddressData"; |
Index: trunk/extensions/CheckUser/CheckUser.hooks.php |
— | — | @@ -58,8 +58,8 @@ |
59 | 59 | |
60 | 60 | # Every 100th edit, prune the checkuser changes table. |
61 | 61 | if ( 0 == mt_rand( 0, 99 ) ) { |
62 | | - $cutoff = $dbw->timestamp( time() - $wgCUDMaxAge ); |
63 | | - $dbw->delete( 'cu_changes', array( "cuc_timestamp < '{$cutoff}'" ), __METHOD__ ); |
| 62 | + $encCutoff = $dbw->addQuotes( $dbw->timestamp( time() - $wgCUDMaxAge ) ); |
| 63 | + $dbw->delete( 'cu_changes', array( "cuc_timestamp < $encCutoff" ), __METHOD__ ); |
64 | 64 | } |
65 | 65 | |
66 | 66 | return true; |