[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Class which performs the actual renaming of users 4 */ 5 6 class RenameuserSQL { 7 /** 8 * The old username 9 * 10 * @var string 11 * @access private 12 */ 13 public $old; 14 15 /** 16 * The new username 17 * 18 * @var string 19 * @access private 20 */ 21 public $new; 22 23 /** 24 * The user ID 25 * 26 * @var integer 27 * @access private 28 */ 29 public $uid; 30 31 /** 32 * The the tables => fields to be updated 33 * 34 * @var array 35 * @access private 36 */ 37 public $tables; 38 39 /** 40 * Flag that can be set to false, in case another process has already started 41 * the updates and the old username may have already been renamed in the user table. 42 * 43 * @var bool 44 * @access private 45 */ 46 public $checkIfUserExists; 47 48 /** 49 * Constructor 50 * 51 * @param $old string The old username 52 * @param $new string The new username 53 * @param $uid 54 * @param $options Array of options 55 * 'checkIfUserExists' - bool, whether to update the user table 56 */ 57 function __construct( $old, $new, $uid, $options = array() ) { 58 $this->old = $old; 59 $this->new = $new; 60 $this->uid = $uid; 61 $this->checkIfUserExists = true; 62 63 if ( isset ( $options['checkIfUserExists'] ) ) { 64 $this->checkIfUserExists = $options['checkIfUserExists']; 65 } 66 67 $this->tables = array(); // Immediate updates 68 $this->tables['image'] = array( 'img_user_text', 'img_user' ); 69 $this->tables['oldimage'] = array( 'oi_user_text', 'oi_user' ); 70 $this->tables['filearchive'] = array('fa_user_text','fa_user'); 71 $this->tablesJob = array(); // Slow updates 72 // If this user has a large number of edits, use the jobqueue 73 if ( User::newFromId( $uid )->getEditCount() > RENAMEUSER_CONTRIBJOB ) { 74 $this->tablesJob['revision'] = array( 'rev_user_text', 'rev_user', 'rev_timestamp' ); 75 $this->tablesJob['archive'] = array( 'ar_user_text', 'ar_user', 'ar_timestamp' ); 76 $this->tablesJob['logging'] = array( 'log_user_text', 'log_user', 'log_timestamp' ); 77 } else { 78 $this->tables['revision'] = array( 'rev_user_text', 'rev_user' ); 79 $this->tables['archive'] = array( 'ar_user_text', 'ar_user' ); 80 $this->tables['logging'] = array( 'log_user_text', 'log_user' ); 81 } 82 // Recent changes is pretty hot, deadlocks occur if done all at once 83 if ( wfQueriesMustScale() ) { 84 $this->tablesJob['recentchanges'] = array( 'rc_user_text', 'rc_user', 'rc_timestamp' ); 85 } else { 86 $this->tables['recentchanges'] = array( 'rc_user_text', 'rc_user' ); 87 } 88 89 wfRunHooks( 'RenameUserSQL', array( $this ) ); 90 } 91 92 /** 93 * Do the rename operation 94 */ 95 function rename() { 96 global $wgMemc, $wgAuth, $wgUpdateRowsPerJob; 97 98 wfProfileIn( __METHOD__ ); 99 100 $dbw = wfGetDB( DB_MASTER ); 101 $dbw->begin(); 102 wfRunHooks( 'RenameUserPreRename', array( $this->uid, $this->old, $this->new ) ); 103 104 // Rename and touch the user before re-attributing edits, 105 // this avoids users still being logged in and making new edits while 106 // being renamed, which leaves edits at the old name. 107 $dbw->update( 'user', 108 array( 'user_name' => $this->new, 'user_touched' => $dbw->timestamp() ), 109 array( 'user_name' => $this->old, 'user_id' => $this->uid ), 110 __METHOD__ 111 ); 112 113 if ( !$dbw->affectedRows() && $this->checkIfUserExists ) { 114 $dbw->rollback(); 115 wfProfileOut( __METHOD__ ); 116 return false; 117 } 118 119 // Reset token to break login with central auth systems. 120 // Again, avoids user being logged in with old name. 121 $user = User::newFromId( $this->uid ); 122 $authUser = $wgAuth->getUserInstance( $user ); 123 $authUser->resetAuthToken(); 124 125 // Delete from memcached. 126 $wgMemc->delete( wfMemcKey( 'user', 'id', $this->uid ) ); 127 128 // Update ipblock list if this user has a block in there. 129 $dbw->update( 'ipblocks', 130 array( 'ipb_address' => $this->new ), 131 array( 'ipb_user' => $this->uid, 'ipb_address' => $this->old ), 132 __METHOD__ ); 133 // Update this users block/rights log. Ideally, the logs would be historical, 134 // but it is really annoying when users have "clean" block logs by virtue of 135 // being renamed, which makes admin tasks more of a pain... 136 $oldTitle = Title::makeTitle( NS_USER, $this->old ); 137 $newTitle = Title::makeTitle( NS_USER, $this->new ); 138 $dbw->update( 'logging', 139 array( 'log_title' => $newTitle->getDBkey() ), 140 array( 'log_type' => array( 'block', 'rights' ), 141 'log_namespace' => NS_USER, 142 'log_title' => $oldTitle->getDBkey() ), 143 __METHOD__ ); 144 // Do immediate updates! 145 foreach ( $this->tables as $table => $fieldSet ) { 146 list( $nameCol, $userCol ) = $fieldSet; 147 $dbw->update( $table, 148 array( $nameCol => $this->new ), 149 array( $nameCol => $this->old, $userCol => $this->uid ), 150 __METHOD__ 151 ); 152 } 153 154 // Increase time limit (like CheckUser); this can take a while... 155 if ( $this->tablesJob ) { 156 wfSuppressWarnings(); 157 set_time_limit( 120 ); 158 wfRestoreWarnings(); 159 } 160 161 $jobs = array(); // jobs for all tables 162 // Construct jobqueue updates... 163 // FIXME: if a bureaucrat renames a user in error, he/she 164 // must be careful to wait until the rename finishes before 165 // renaming back. This is due to the fact the the job "queue" 166 // is not really FIFO, so we might end up with a bunch of edits 167 // randomly mixed between the two new names. Some sort of rename 168 // lock might be in order... 169 foreach ( $this->tablesJob as $table => $params ) { 170 $userTextC = $params[0]; // some *_user_text column 171 $userIDC = $params[1]; // some *_user column 172 $timestampC = $params[2]; // some *_timestamp column 173 174 $res = $dbw->select( $table, 175 array( $timestampC ), 176 array( $userTextC => $this->old, $userIDC => $this->uid ), 177 __METHOD__, 178 array( 'ORDER BY' => "$timestampC ASC" ) 179 ); 180 181 $jobParams = array(); 182 $jobParams['table'] = $table; 183 $jobParams['column'] = $userTextC; 184 $jobParams['uidColumn'] = $userIDC; 185 $jobParams['timestampColumn'] = $timestampC; 186 $jobParams['oldname'] = $this->old; 187 $jobParams['newname'] = $this->new; 188 $jobParams['userID'] = $this->uid; 189 // Timestamp column data for index optimizations 190 $jobParams['minTimestamp'] = '0'; 191 $jobParams['maxTimestamp'] = '0'; 192 $jobParams['count'] = 0; 193 194 // Insert jobs into queue! 195 while ( true ) { 196 $row = $dbw->fetchObject( $res ); 197 if ( !$row ) { 198 # If there are any job rows left, add it to the queue as one job 199 if ( $jobParams['count'] > 0 ) { 200 $jobs[] = Job::factory( 'renameUser', $oldTitle, $jobParams ); 201 } 202 break; 203 } 204 # Since the ORDER BY is ASC, set the min timestamp with first row 205 if ( $jobParams['count'] == 0 ) { 206 $jobParams['minTimestamp'] = $row->$timestampC; 207 } 208 # Keep updating the last timestamp, so it should be correct 209 # when the last item is added. 210 $jobParams['maxTimestamp'] = $row->$timestampC; 211 # Update row counter 212 $jobParams['count']++; 213 # Once a job has $wgUpdateRowsPerJob rows, add it to the queue 214 if ( $jobParams['count'] >= $wgUpdateRowsPerJob ) { 215 $jobs[] = Job::factory( 'renameUser', $oldTitle, $jobParams ); 216 $jobParams['minTimestamp'] = '0'; 217 $jobParams['maxTimestamp'] = '0'; 218 $jobParams['count'] = 0; 219 } 220 } 221 $dbw->freeResult( $res ); 222 } 223 224 if ( count( $jobs ) > 0 ) { 225 JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC ); // don't commit yet 226 } 227 228 // Commit the transaction 229 $dbw->commit(); 230 231 // Delete from memcached again to make sure 232 $wgMemc->delete( wfMemcKey( 'user', 'id', $this->uid ) ); 233 234 // Clear caches and inform authentication plugins 235 $user = User::newFromId( $this->uid ); 236 $wgAuth->updateExternalDB( $user ); 237 wfRunHooks( 'RenameUserComplete', array( $this->uid, $this->old, $this->new ) ); 238 239 wfProfileOut( __METHOD__ ); 240 return true; 241 } 242 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |