r99193 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99192‎ | r99193 | r99194 >
Date:04:52, 7 October 2011
Author:aaron
Status:ok
Tags:
Comment:
FU r99187:
* Added begin()/commit() as needed
* Do the deletions in batches
* Use addQuotes() as needed
Modified paths:
  • /trunk/extensions/CheckUser/CheckUser.hooks.php (modified) (history)
  • /trunk/extensions/CheckUser/maintenance/purgeOldData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CheckUser/maintenance/purgeOldData.php
@@ -9,28 +9,60 @@
1010 class PurgeOldIPAddressData extends Maintenance {
1111 public function __construct() {
1212 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 );
1415 }
1516
1617 public function execute() {
1718 global $wgCUDMaxAge, $wgRCMaxAge, $wgPutIPinRC;
1819
19 - $dbw = wfGetDB( DB_MASTER );
20 -
2120 $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" );
2523
2624 if ( $wgPutIPinRC ) {
2725 $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" );
3128 }
3229
3330 $this->output( "Done.\n" );
3431 }
 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+ }
3567 }
3668
3769 $maintClass = "PurgeOldIPAddressData";
Index: trunk/extensions/CheckUser/CheckUser.hooks.php
@@ -58,8 +58,8 @@
5959
6060 # Every 100th edit, prune the checkuser changes table.
6161 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__ );
6464 }
6565
6666 return true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r100488MFT r99187,r99193 - 'Added script to manually clear out old data (per $wgCUDM...aaron02:15, 22 October 2011
r100491MFT r99187, r99193reedy02:35, 22 October 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r99187* Added script to manually clear out old data (per $wgCUDMaxAge)...aaron03:28, 7 October 2011

Status & tagging log