r101992 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101991‎ | r101992 | r101993 >
Date:14:19, 4 November 2011
Author:ariel
Status:ok (Comments)
Tags:
Comment:
allow renames for user with specific uid set in row; minor formatting cleanup; refactor (one large function -> several smaller ones)
Modified paths:
  • /trunk/extensions/Renameuser/renameUserCleanup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Renameuser/renameUserCleanup.php
@@ -35,6 +35,7 @@
3636 $this->mDescription = "Maintenance script to finish incomplete rename user, in particular to reassign edits that were missed";
3737 $this->addOption( 'olduser', 'Old user name', true, true );
3838 $this->addOption( 'newuser', 'New user name', true, true );
 39+ $this->addOption( 'olduid', 'Old user id in revision records (DANGEROUS)', false, true );
3940 $this->mBatchSize = 1000;
4041 }
4142
@@ -42,23 +43,42 @@
4344 $this->output( "Rename User Cleanup starting...\n\n" );
4445 $olduser = User::newFromName( $this->getOption( 'olduser' ) );
4546 $newuser = User::newFromName( $this->getOption( 'newuser' ) );
 47+ $olduid = $this->getOption( 'olduid' );
4648
 49+ $this->checkUserExistence( $olduser, $newuser );
 50+ $this->checkRenameLog( $olduser, $newuser );
 51+
 52+ if ( $olduid ) {
 53+ $this->doUpdates( $olduser, $newuser, $olduid, $dbw );
 54+ }
 55+ $this->doUpdates( $olduser, $newuser, $newuser->getId(), $dbw );
 56+ $this->doUpdates( $olduser, $newuser, 0, $dbw );
 57+
 58+ print "Done!\n";
 59+ exit(0);
 60+ }
 61+
 62+
 63+ public function checkUserExistence( $olduser, $newuser ) {
4764 if ( !$newuser->getId() ) {
4865 $this->error( "No such user: " . $this->getOption( 'newuser' ), true );
4966 exit(1);
5067 }
5168 if ($olduser->getId() ) {
52 - print( "WARNING!!: Old user still exists: " . $this->getOption( 'olduser' ) . "\n");
53 - print("proceed anyways? We'll only re-attribute edits that have the new user uid (or 0) and the old user name. [N/y] ");
 69+ print "WARNING!!: Old user still exists: " . $this->getOption( 'olduser' ) . "\n";
 70+ print "proceed anyways? We'll only re-attribute edits that have the new user uid (or 0)";
 71+ print " or the uid specified by the caller, and the old user name. [N/y] ";
5472 $stdin = fopen ("php://stdin","rt");
5573 $line = fgets($stdin);
5674 fclose($stdin);
5775 if ( $line[0] != "Y" && $line[0] != "y" ) {
58 - print("Exiting at user's request\n");
 76+ print "Exiting at user's request\n";
5977 exit(0);
6078 }
6179 }
 80+ }
6281
 82+ public function checkRenameLog( $olduser, $newuser ) {
6383 $dbr = wfGetDB( DB_SLAVE );
6484 $result = $dbr->select( 'logging', '*',
6585 array( 'log_type' => 'renameuser',
@@ -80,130 +100,111 @@
81101 __METHOD__
82102 );
83103 if (! $result || ! $result->numRows() ) {
84 - print("No log entry found for a rename of ".$olduser->getName()." to ".$newuser->getName().", proceed anyways??? [N/y] ");
 104+ print "No log entry found for a rename of ".$olduser->getName()." to ".$newuser->getName().", proceed anyways??? [N/y] ";
85105 $stdin = fopen ("php://stdin","rt");
86106 $line = fgets($stdin);
87107 fclose($stdin);
88108 if ( $line[0] != "Y" && $line[0] != "y" ) {
89 - print("Exiting at user's request\n");
 109+ print "Exiting at user's request\n";
90110 exit(1);
91111 }
92112 }
93113 else {
94114 foreach ( $result as $row ) {
95 - print("Found possible log entry of the rename, please check: ".$row->log_title." with comment ".$row->log_comment." on $row->log_timestamp\n");
 115+ print "Found possible log entry of the rename, please check: ".$row->log_title." with comment ".$row->log_comment." on $row->log_timestamp\n";
96116 }
97117 }
98118 }
99119 else {
100120 foreach ( $result as $row ) {
101 - print("Found log entry of the rename: ".$olduser->getName()." to ".$newuser->getName()." on $row->log_timestamp\n");
 121+ print "Found log entry of the rename: ".$olduser->getName()." to ".$newuser->getName()." on $row->log_timestamp\n";
102122 }
103123 }
104124 if ($result && $result->numRows() > 1) {
105 - print("More than one rename entry found in the log, not sure what to do. Continue anyways? [N/y] ");
 125+ print "More than one rename entry found in the log, not sure what to do. Continue anyways? [N/y] ";
106126 $stdin = fopen ("php://stdin","rt");
107127 $line = fgets($stdin);
108128 fclose($stdin);
109129 if ( $line[0] != "Y" && $line[0] != "y" ) {
110 - print("Exiting at user's request\n");
 130+ print "Exiting at user's request\n";
111131 exit(1);
112132 }
113133 }
114 - $dbw = wfGetDB( DB_MASTER );
 134+ }
115135
116 - $this->updateTable('revision', 'rev_user_text', 'rev_user', 'rev_timestamp', $olduser, $newuser, $dbw);
117 - $this->updateTable('archive', 'ar_user_text', 'ar_user', 'ar_timestamp', $olduser, $newuser, $dbw);
118 - $this->updateTable('logging', 'log_user_text', 'log_user', 'log_timestamp', $olduser, $newuser, $dbw);
119 - $this->updateTable('image', 'img_user_text', 'img_user', 'img_timestamp', $olduser, $newuser, $dbw);
120 - $this->updateTable('oldimage', 'oi_user_text', 'oi_user', 'oi_timestamp', $olduser, $newuser, $dbw);
121 - $this->updateTable('filearchive', 'fa_user_text','fa_user', 'fa_timestamp', $olduser, $newuser, $dbw);
122 - print "Done!\n";
123 - exit(0);
 136+
 137+ public function doUpdates( $olduser, $newuser, $uid, $dbw ) {
 138+ $this->updateTable( 'revision', 'rev_user_text', 'rev_user', 'rev_timestamp', $olduser, $newuser, $uid );
 139+ $this->updateTable( 'archive', 'ar_user_text', 'ar_user', 'ar_timestamp', $olduser, $newuser, $uid );
 140+ $this->updateTable( 'logging', 'log_user_text', 'log_user', 'log_timestamp', $olduser, $newuser, $uid );
 141+ $this->updateTable( 'image', 'img_user_text', 'img_user', 'img_timestamp', $olduser, $newuser, $uid );
 142+ $this->updateTable( 'oldimage', 'oi_user_text', 'oi_user', 'oi_timestamp', $olduser, $newuser, $uid );
 143+ $this->updateTable( 'filearchive', 'fa_user_text','fa_user', 'fa_timestamp', $olduser, $newuser, $uid );
124144 }
125145
126 - public function updateTable($table,$usernamefield,$useridfield,$timestampfield,$olduser,$newuser,$dbw) {
127 - $doUid = 0;
 146+ public function updateTable( $table,$usernamefield,$useridfield,$timestampfield,$olduser,$newuser,$uid ) {
 147+ $dbw = wfGetDB( DB_MASTER );
128148
129149 $contribs = $dbw->selectField( $table, 'count(*)',
130 - array( $usernamefield => $olduser->getName(), $useridfield => $newuser->getId() ), __METHOD__ );
131 - if ($contribs == 0) {
132 - $contribs = $dbw->selectField( $table, 'count(*)',
133 - array( $usernamefield => $olduser->getName(), $useridfield => 0 ), __METHOD__ );
134 - if ($contribs > 0) {
135 - print("Found $contribs edits to be re-attributed from table $table but the uid present is 0 (should be ".$newuser->getId().")\n");
136 - print("If you proceed, the uid field will be set to that of the new user name (i.e. ".$newuser->getId().") in these rows.\n");
137 - $doUid = 1;
138 - }
139 - else {
140 - print("No edits to be re-attributed from table $table\n");
141 - return(0);
142 - }
 150+ array( $usernamefield => $olduser->getName(), $useridfield => $uid ), __METHOD__ );
 151+
 152+ if ( $contribs == 0 ) {
 153+ print "No edits to be re-attributed from table $table\n" ;
 154+ return(0);
143155 }
144 - else {
145 - print("total number of edits to be re-attributed from table $table: $contribs\n");
 156+
 157+ print "Found $contribs edits to be re-attributed from table $table for uid $uid\n";
 158+ if ( $uid != $newuser->getId() ) {
 159+ print "If you proceed, the uid field will be set to that of the new user name (i.e. ".$newuser->getId().") in these rows.\n";
146160 }
147 - print("proceed? [N/y] ");
 161+
 162+ print "Proceed? [N/y] ";
148163 $stdin = fopen ("php://stdin","rt");
149164 $line = fgets($stdin);
150165 fclose($stdin);
151166 if ( $line[0] != "Y" && $line[0] != "y" ) {
152 - print("skipping at user's request\n");
 167+ print "Skipping at user's request\n";
153168 return(0);
154169 }
155 - $selectConds = array( $usernamefield => $olduser->getName() );
156 - $updateFields = array( $usernamefield => $newuser->getName() );
157 - $updateConds = array( $usernamefield => $olduser->getName() );
158170
159 - $extraConds = array( $useridfield => $newuser->getId() );
160 - $extraCondsNoUid = array( $useridfield => 0 );
161 - # uid in rows is set properly, use as cond to find rows, don't bother to update it
162 - if (! $doUid) {
163 - $selectConds = array_merge( $selectConds, $extraConds );
164 - $updateConds = array_merge( $updateConds, $extraConds );
165 - }
166 - # uid in edit rows is 0, we will set it and we will only update rows with 0 uid and the old user name
167 - else {
168 - $selectConds = array_merge( $selectConds, $extraCondsNoUid );
169 - $updateConds = array_merge( $updateConds, $extraCondsNoUid );
170 - $updateFields = array_merge( $updateFields, $extraConds );
171 - }
 171+ $selectConds = array( $usernamefield => $olduser->getName(), $useridfield => $uid );
 172+ $updateFields = array( $usernamefield => $newuser->getName(), $useridfield => $newuser->getId() );
172173
173 - while ($contribs > 0) {
174 - print("doing batch of up to approximately ".$this->mBatchSize."\n");
175 - print("do this batch? [N/y] ");
 174+ while ( $contribs > 0 ) {
 175+ print "Doing batch of up to approximately ".$this->mBatchSize."\n";
 176+ print "Do this batch? [N/y] ";
176177 $stdin = fopen ("php://stdin","rt");
177178 $line = fgets($stdin);
178179 fclose($stdin);
179180 if ( $line[0] != "Y" && $line[0] != "y" ) {
180 - print("skipping at user's request\n");
 181+ print "Skipping at user's request\n";
181182 return(0);
182183 }
183184 $dbw->begin();
184185 $result = $dbw->select( $table, $timestampfield, $selectConds , __METHOD__,
185186 array( 'ORDER BY' => $timestampfield.' DESC', 'LIMIT' => $this->mBatchSize ) );
186187 if (! $result) {
187 - print("There were rows for updating but now they are gone. Skipping.\n");
 188+ print "There were rows for updating but now they are gone. Skipping.\n";
188189 $dbw->rollback();
189190 return(0);
190191 }
191192 $result->seek($result->numRows() -1 );
192193 $row = $result->fetchObject();
193194 $timestamp = $row->$timestampfield;
194 - $updateCondsWithTime = array_merge( $updateConds, array ("$timestampfield >= $timestamp") );
 195+ $updateCondsWithTime = array_merge( $selectConds, array ("$timestampfield >= $timestamp") );
195196 $success = $dbw->update( $table, $updateFields, $updateCondsWithTime, __METHOD__ );
196 - if ($success) {
 197+ if ( $success ) {
197198 $rowsDone = $dbw->affectedRows();
198199 $dbw->commit();
199200 }
200201 else {
201 - print("problem with the update, rolling back and exiting\n");
 202+ print "Problem with the update, rolling back and exiting\n";
202203 $dbw->rollback();
203204 exit(1);
204205 }
205206 //$contribs = User::edits( $olduser->getId() );
206207 $contribs = $dbw->selectField( $table, 'count(*)', $selectConds, __METHOD__ );
207 - print("updated $rowsDone edits; $contribs edits remaining to be re-attributed\n");
 208+ print "Updated $rowsDone edits; $contribs edits remaining to be re-attributed\n";
208209 }
209210 return(0);
210211 }

Comments

#Comment by Nikerabbit (talk | contribs)   14:53, 4 November 2011

Why is it dangerous?

#Comment by ArielGlenn (talk | contribs)   15:10, 4 November 2011

Get it wrong (i.e. cut and paste from the wrong select line) and you've just reassigned a bunch of edits that shouldn't have been (for example from the user after it was recreated). And good luck finding them, they will now look just like all the other edits that really belong to the new user.

Status & tagging log