Index: trunk/extensions/Renameuser/renameUserCleanup.php |
— | — | @@ -35,6 +35,7 @@ |
36 | 36 | $this->mDescription = "Maintenance script to finish incomplete rename user, in particular to reassign edits that were missed"; |
37 | 37 | $this->addOption( 'olduser', 'Old user name', true, true ); |
38 | 38 | $this->addOption( 'newuser', 'New user name', true, true ); |
| 39 | + $this->addOption( 'olduid', 'Old user id in revision records (DANGEROUS)', false, true ); |
39 | 40 | $this->mBatchSize = 1000; |
40 | 41 | } |
41 | 42 | |
— | — | @@ -42,23 +43,42 @@ |
43 | 44 | $this->output( "Rename User Cleanup starting...\n\n" ); |
44 | 45 | $olduser = User::newFromName( $this->getOption( 'olduser' ) ); |
45 | 46 | $newuser = User::newFromName( $this->getOption( 'newuser' ) ); |
| 47 | + $olduid = $this->getOption( 'olduid' ); |
46 | 48 | |
| 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 ) { |
47 | 64 | if ( !$newuser->getId() ) { |
48 | 65 | $this->error( "No such user: " . $this->getOption( 'newuser' ), true ); |
49 | 66 | exit(1); |
50 | 67 | } |
51 | 68 | 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] "; |
54 | 72 | $stdin = fopen ("php://stdin","rt"); |
55 | 73 | $line = fgets($stdin); |
56 | 74 | fclose($stdin); |
57 | 75 | if ( $line[0] != "Y" && $line[0] != "y" ) { |
58 | | - print("Exiting at user's request\n"); |
| 76 | + print "Exiting at user's request\n"; |
59 | 77 | exit(0); |
60 | 78 | } |
61 | 79 | } |
| 80 | + } |
62 | 81 | |
| 82 | + public function checkRenameLog( $olduser, $newuser ) { |
63 | 83 | $dbr = wfGetDB( DB_SLAVE ); |
64 | 84 | $result = $dbr->select( 'logging', '*', |
65 | 85 | array( 'log_type' => 'renameuser', |
— | — | @@ -80,130 +100,111 @@ |
81 | 101 | __METHOD__ |
82 | 102 | ); |
83 | 103 | 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] "; |
85 | 105 | $stdin = fopen ("php://stdin","rt"); |
86 | 106 | $line = fgets($stdin); |
87 | 107 | fclose($stdin); |
88 | 108 | if ( $line[0] != "Y" && $line[0] != "y" ) { |
89 | | - print("Exiting at user's request\n"); |
| 109 | + print "Exiting at user's request\n"; |
90 | 110 | exit(1); |
91 | 111 | } |
92 | 112 | } |
93 | 113 | else { |
94 | 114 | 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"; |
96 | 116 | } |
97 | 117 | } |
98 | 118 | } |
99 | 119 | else { |
100 | 120 | 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"; |
102 | 122 | } |
103 | 123 | } |
104 | 124 | 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] "; |
106 | 126 | $stdin = fopen ("php://stdin","rt"); |
107 | 127 | $line = fgets($stdin); |
108 | 128 | fclose($stdin); |
109 | 129 | if ( $line[0] != "Y" && $line[0] != "y" ) { |
110 | | - print("Exiting at user's request\n"); |
| 130 | + print "Exiting at user's request\n"; |
111 | 131 | exit(1); |
112 | 132 | } |
113 | 133 | } |
114 | | - $dbw = wfGetDB( DB_MASTER ); |
| 134 | + } |
115 | 135 | |
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 ); |
124 | 144 | } |
125 | 145 | |
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 ); |
128 | 148 | |
129 | 149 | $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); |
143 | 155 | } |
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"; |
146 | 160 | } |
147 | | - print("proceed? [N/y] "); |
| 161 | + |
| 162 | + print "Proceed? [N/y] "; |
148 | 163 | $stdin = fopen ("php://stdin","rt"); |
149 | 164 | $line = fgets($stdin); |
150 | 165 | fclose($stdin); |
151 | 166 | if ( $line[0] != "Y" && $line[0] != "y" ) { |
152 | | - print("skipping at user's request\n"); |
| 167 | + print "Skipping at user's request\n"; |
153 | 168 | return(0); |
154 | 169 | } |
155 | | - $selectConds = array( $usernamefield => $olduser->getName() ); |
156 | | - $updateFields = array( $usernamefield => $newuser->getName() ); |
157 | | - $updateConds = array( $usernamefield => $olduser->getName() ); |
158 | 170 | |
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() ); |
172 | 173 | |
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] "; |
176 | 177 | $stdin = fopen ("php://stdin","rt"); |
177 | 178 | $line = fgets($stdin); |
178 | 179 | fclose($stdin); |
179 | 180 | if ( $line[0] != "Y" && $line[0] != "y" ) { |
180 | | - print("skipping at user's request\n"); |
| 181 | + print "Skipping at user's request\n"; |
181 | 182 | return(0); |
182 | 183 | } |
183 | 184 | $dbw->begin(); |
184 | 185 | $result = $dbw->select( $table, $timestampfield, $selectConds , __METHOD__, |
185 | 186 | array( 'ORDER BY' => $timestampfield.' DESC', 'LIMIT' => $this->mBatchSize ) ); |
186 | 187 | 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"; |
188 | 189 | $dbw->rollback(); |
189 | 190 | return(0); |
190 | 191 | } |
191 | 192 | $result->seek($result->numRows() -1 ); |
192 | 193 | $row = $result->fetchObject(); |
193 | 194 | $timestamp = $row->$timestampfield; |
194 | | - $updateCondsWithTime = array_merge( $updateConds, array ("$timestampfield >= $timestamp") ); |
| 195 | + $updateCondsWithTime = array_merge( $selectConds, array ("$timestampfield >= $timestamp") ); |
195 | 196 | $success = $dbw->update( $table, $updateFields, $updateCondsWithTime, __METHOD__ ); |
196 | | - if ($success) { |
| 197 | + if ( $success ) { |
197 | 198 | $rowsDone = $dbw->affectedRows(); |
198 | 199 | $dbw->commit(); |
199 | 200 | } |
200 | 201 | else { |
201 | | - print("problem with the update, rolling back and exiting\n"); |
| 202 | + print "Problem with the update, rolling back and exiting\n"; |
202 | 203 | $dbw->rollback(); |
203 | 204 | exit(1); |
204 | 205 | } |
205 | 206 | //$contribs = User::edits( $olduser->getId() ); |
206 | 207 | $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"; |
208 | 209 | } |
209 | 210 | return(0); |
210 | 211 | } |