Index: maintenance/archives/patch-rc_user_text-index.sql
===================================================================
--- maintenance/archives/patch-rc_user_text-index.sql	(revision 0)
+++ maintenance/archives/patch-rc_user_text-index.sql	(revision 0)
@@ -0,0 +1,7 @@
+-- Add an index to recentchanges on rc_user_text
+--
+-- Added 2006-11-08
+--
+
+     ALTER TABLE /*$wgDBprefix*/recentchanges
+ADD INDEX rc_user_text(rc_user_text, rc_timestamp);
\ No newline at end of file

Property changes on: maintenance/archives/patch-rc_user_text-index.sql
___________________________________________________________________
Name: svn:eol-style
   + native

Index: maintenance/updaters.inc
===================================================================
--- maintenance/updaters.inc	(revision 17485)
+++ maintenance/updaters.inc	(working copy)
@@ -779,8 +779,20 @@
 		dbsource( archive( 'patch-recentchanges-utindex.sql' ) );
 	} else {
 		# Index seems to exist
-		echo( "...seems to be ok\n" );
+		echo( "...index on ( rc_namespace, rc_user_text ) seems to be ok\n" );
 	}
+
+	#Add (rc_user_text, rc_timestamp) index [A. Garrett], November 2006
+	# See if we can find the index we want
+	$info = $wgDatabase->indexInfo( 'recentchanges', 'rc_user_text', __METHOD__ );
+	if( !$info ) {
+		# None, so create
+		echo( "...index on ( rc_user_text, rc_timestamp ) not found; creating\n" );
+		dbsource( archive( 'patch-rc_user_text-index.sql' ) );
+	} else {
+		# Index seems to exist
+		echo( "...index on ( rc_user_text, rc_timestamp ) seems to be ok\n" );
+	}
 }
 
 function do_all_updates( $doShared = false ) {
Index: maintenance/tables.sql
===================================================================
--- maintenance/tables.sql	(revision 17485)
+++ maintenance/tables.sql	(working copy)
@@ -804,7 +804,8 @@
   INDEX rc_cur_id (rc_cur_id),
   INDEX new_name_timestamp (rc_new,rc_namespace,rc_timestamp),
   INDEX rc_ip (rc_ip),
-  INDEX rc_ns_usertext (rc_namespace, rc_user_text)
+  INDEX rc_ns_usertext (rc_namespace, rc_user_text),
+  INDEX rc_user_text (rc_user_text, rc_timestamp)
 
 ) TYPE=InnoDB;
 
Index: includes/User.php
===================================================================
--- includes/User.php	(revision 17485)
+++ includes/User.php	(working copy)
@@ -1924,46 +1924,8 @@
 			return;
 		}
 
-		if ( !$userblock->mEnableAutoblock ) {
-			return;
-		}
+		$userblock->doAutoblock( wfGetIp() );
 
-		# Check if this IP address is already blocked
-		$ipblock = Block::newFromDB( wfGetIP() );
-		if ( $ipblock ) {
-			# If the user is already blocked. Then check if the autoblock would
-			# exceed the user block. If it would exceed, then do nothing, else
-			# prolong block time
-			if ($userblock->mExpiry &&
-				($userblock->mExpiry < Block::getAutoblockExpiry($ipblock->mTimestamp))) {
-				return;
-			}
-			# Just update the timestamp
-			$ipblock->updateTimestamp();
-			return;
-		} else {
-			$ipblock = new Block;
-		}
-
-		# Make a new block object with the desired properties
-		wfDebug( "Autoblocking {$this->mName}@" . wfGetIP() . "\n" );
-		$ipblock->mAddress = wfGetIP();
-		$ipblock->mUser = 0;
-		$ipblock->mBy = $userblock->mBy;
-		$ipblock->mReason = wfMsg( 'autoblocker', $this->getName(), $userblock->mReason );
-		$ipblock->mTimestamp = wfTimestampNow();
-		$ipblock->mAuto = 1;
-		# If the user is already blocked with an expiry date, we don't
-		# want to pile on top of that!
-		if($userblock->mExpiry) {
-			$ipblock->mExpiry = min ( $userblock->mExpiry, Block::getAutoblockExpiry( $ipblock->mTimestamp ));
-		} else {
-			$ipblock->mExpiry = Block::getAutoblockExpiry( $ipblock->mTimestamp );
-		}
-
-		# Insert it
-		$ipblock->insert();
-
 	}
 
 	/**
Index: includes/Block.php
===================================================================
--- includes/Block.php	(revision 17485)
+++ includes/Block.php	(working copy)
@@ -395,9 +395,79 @@
 		);
 		$affected = $dbw->affectedRows();
 		$dbw->commit();
+
+		$this->doRetroactiveAutoblock();
+
 		return $affected;
 	}
 
+	function doRetroactiveAutoblock() {
+		$dbr = wfGetDb( DB_SLAVE );
+		#If autoblock is enabled, autoblock the LAST IP used
+		# - stolen shamelessly from CheckUser_body.php
+
+		if ($this->mEnableAutoblock && $this->mUser) {
+			wfDebug("Doing retroactive autoblocks for " . $this->mAddress . "\n");
+
+			$row = $dbr->selectRow( 'recentchanges', array( 'rc_ip' ), array( 'rc_user_text' => $this->mAddress ),
+				$fname, array( 'ORDER BY' => 'rc_timestamp DESC' ) );
+
+	                if ( !$row ) {
+				#No results, don't autoblock anything
+				wfDebug("No IP found to retroactively autoblock\n");
+			} else {
+				#Limit is 1, so no loop needed.
+				$retroblockip = $row->rc_ip;
+				$this->doAutoblock($retroblockip);
+			}
+		}
+	}
+
+	function doAutoblock( $autoblockip ) {
+		# Check if this IP address is already blocked
+		$dbw =& wfGetDb( DB_MASTER );
+		$dbw->begin();
+
+		if ( !$this->mEnableAutoblock ) {
+			return;
+		}
+
+		$ipblock = Block::newFromDB( $autoblockip );
+		if ( $ipblock ) {
+			# If the user is already blocked. Then check if the autoblock would
+			# exceed the user block. If it would exceed, then do nothing, else
+			# prolong block time
+			if ($this->mExpiry &&
+			($this->mExpiry < Block::getAutoblockExpiry($ipblock->mTimestamp))) {
+				return;
+			}
+			# Just update the timestamp
+			$ipblock->updateTimestamp();
+			return;
+		} else {
+			$ipblock = new Block;
+		}
+
+		# Make a new block object with the desired properties
+		wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockip . "\n" );
+		$ipblock->mAddress = $autoblockip;
+		$ipblock->mUser = 0;
+		$ipblock->mBy = $this->mBy;
+		$ipblock->mReason = wfMsg( 'autoblocker', $this->mAddress, $this->mReason );
+		$ipblock->mTimestamp = wfTimestampNow();
+		$ipblock->mAuto = 1;
+
+		# If the user is already blocked with an expiry date, we don't
+		# want to pile on top of that!
+		if($this->mExpiry) {
+			$ipblock->mExpiry = min ( $this->mExpiry, Block::getAutoblockExpiry( $this->mTimestamp ));
+		} else {
+			$ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
+		}
+		# Insert it
+		$ipblock->insert();
+	}
+
 	function deleteIfExpired()
 	{
 		$fname = 'Block::deleteIfExpired';
Index: languages/messages/MessagesEn.php
===================================================================
--- languages/messages/MessagesEn.php	(revision 17485)
+++ languages/messages/MessagesEn.php	(working copy)
@@ -1770,7 +1770,7 @@
 'ipbreason'		=> 'Reason',
 'ipbanononly'   => 'Block anonymous users only',
 'ipbcreateaccount' => 'Prevent account creation',
-'ipbenableautoblock' => 'Automatically block IP addresses used by this user',
+'ipbenableautoblock' => 'Automatically block the last IP address used by this user, and any subsequent addresses they try to edit from',
 'ipbsubmit'		=> 'Block this user',
 'ipbother'		=> 'Other time',
 'ipboptions'		=> '2 hours:2 hours,1 day:1 day,3 days:3 days,1 week:1 week,2 weeks:2 weeks,1 month:1 month,3 months:3 months,6 months:6 months,1 year:1 year,infinite:infinite',
