http://www.mediawiki.org/wiki/Special:Code/MediaWiki/100488

Revision: 100488
Author:   aaron
Date:     2011-10-22 02:15:47 +0000 (Sat, 22 Oct 2011)
Log Message:
-----------
MFT r99187,r99193 - 'Added script to manually clear out old data (per 
$wgCUDMaxAge)'

Modified Paths:
--------------
    branches/wmf/1.18wmf1/extensions/CheckUser/CheckUser.hooks.php
    branches/wmf/1.18wmf1/extensions/CheckUser/maintenance/purgeOldData.php

Added Paths:
-----------
    branches/wmf/1.18wmf1/extensions/CheckUser/maintenance/

Modified: branches/wmf/1.18wmf1/extensions/CheckUser/CheckUser.hooks.php
===================================================================
--- branches/wmf/1.18wmf1/extensions/CheckUser/CheckUser.hooks.php      
2011-10-22 02:14:13 UTC (rev 100487)
+++ branches/wmf/1.18wmf1/extensions/CheckUser/CheckUser.hooks.php      
2011-10-22 02:15:47 UTC (rev 100488)
@@ -60,11 +60,8 @@
 
                # Every 100th edit, prune the checkuser changes table.
                if ( 0 == mt_rand( 0, 99 ) ) {
-                       # Periodically flush old entries from the recentchanges 
table.
-                       $cutoff = $dbw->timestamp( time() - $wgCUDMaxAge );
-                       $recentchanges = $dbw->tableName( 'cu_changes' );
-                       $sql = "DELETE FROM $recentchanges WHERE cuc_timestamp 
< '{$cutoff}'";
-                       $dbw->query( $sql, __METHOD__ );
+                       $encCutoff = $dbw->addQuotes( $dbw->timestamp( time() - 
$wgCUDMaxAge ) );
+                       $dbw->delete( 'cu_changes', array( "cuc_timestamp < 
$encCutoff" ), __METHOD__ );
                }
        
                return true;


Property changes on: branches/wmf/1.18wmf1/extensions/CheckUser/maintenance
___________________________________________________________________
Added: bugtraq:number
   + true

Modified: 
branches/wmf/1.18wmf1/extensions/CheckUser/maintenance/purgeOldData.php
===================================================================
--- trunk/extensions/CheckUser/maintenance/purgeOldData.php     2011-10-07 
03:28:57 UTC (rev 99187)
+++ branches/wmf/1.18wmf1/extensions/CheckUser/maintenance/purgeOldData.php     
2011-10-22 02:15:47 UTC (rev 100488)
@@ -9,28 +9,60 @@
 class PurgeOldIPAddressData extends Maintenance {
        public function __construct() {
                parent::__construct();
-               $this->mDescription = "Purge old IP data in CheckUser and 
RecentChanges";
+               $this->mDescription = "Purge expired rows in CheckUser and 
RecentChanges";
+               $this->setBatchSize( 200 );
        }
 
        public function execute() {
                global $wgCUDMaxAge, $wgRCMaxAge, $wgPutIPinRC;
 
-               $dbw = wfGetDB( DB_MASTER );
-
                $this->output( "Purging data from cu_changes..." );
-               $cutoff = $dbw->timestamp( time() - $wgCUDMaxAge );
-               $dbw->delete( 'cu_changes', array( "cuc_timestamp < 
'{$cutoff}'" ), __METHOD__ );
-               $this->output( $dbw->affectedRows() . " rows.\n" );
+               $count = $this->prune( 'cu_changes', 'cuc_timestamp', 
$wgCUDMaxAge );
+               $this->output( $count . " rows.\n" );
 
                if ( $wgPutIPinRC ) {
                        $this->output( "Purging data from recentchanges..." );
-                       $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
-                       $dbw->delete( 'recentchanges', array( "rc_timestamp < 
'{$cutoff}'" ), __METHOD__ );
-                       $this->output( $dbw->affectedRows() . " rows.\n" );
+                       $count = $this->prune( 'recentchanges', 'rc_timestamp', 
$wgRCMaxAge );
+                       $this->output( $count . " rows.\n" );
                }
 
                $this->output( "Done.\n" );
        }
+
+       protected function prune( $table, $ts_column, $maxAge ) {
+               $dbw = wfGetDB( DB_MASTER );
+
+               $expiredCond = "$ts_column < " . $dbw->addQuotes( 
$dbw->timestamp( time() - $maxAge ) );
+
+               $count = 0;
+               while ( true ) {
+                       // Get the first $this->mBatchSize (or less) items
+                       $res = $dbw->select( $table, $ts_column,
+                               $expiredCond,
+                               __METHOD__,
+                               array( 'ORDER BY' => "$ts_column ASC", 'LIMIT' 
=> $this->mBatchSize )
+                       );
+                       if ( !$res->numRows() ) {
+                               break; // all cleared
+                       }
+                       // Record the start and end timestamp for the set
+                       $blockStart = $res->fetchObject()->$ts_column;
+                       $res->seek( $res->numRows() - 1 );
+                       $blockEnd = $res->fetchObject()->$ts_column;
+                       $res->free();
+
+                       // Do the actual delete...
+                       $dbw->begin();
+                       $dbw->delete( $table,
+                               array( "$ts_column BETWEEN $blockStart AND 
$blockEnd" ), __METHOD__ );
+                       $count += $dbw->affectedRows();
+                       $dbw->commit();
+
+                       wfWaitForSlaves();
+               }
+
+               return $count;
+       }
 }
 
 $maintClass = "PurgeOldIPAddressData";


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to