summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/cleanupUsersWithNoId.php
blob: b2fdf2f9c733cfba6993e17d83b09c15235cfcae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
 * Cleanup tables that have valid usernames with no user ID
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

use Wikimedia\Rdbms\IDatabase;

require_once __DIR__ . '/Maintenance.php';

/**
 * Maintenance script that cleans up tables that have valid usernames with no
 * user ID.
 *
 * @ingroup Maintenance
 * @since 1.31
 */
class CleanupUsersWithNoId extends LoggedUpdateMaintenance {
	private $prefix, $table, $assign;
	private $triedCreations = [];

	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Cleans up tables that have valid usernames with no user ID' );
		$this->addOption( 'prefix', 'Interwiki prefix to apply to the usernames', true, true, 'p' );
		$this->addOption( 'table', 'Only clean up this table', false, true );
		$this->addOption( 'assign', 'Assign edits to existing local users if they exist', false, false );
		$this->setBatchSize( 100 );
	}

	protected function getUpdateKey() {
		return __CLASS__;
	}

	protected function doDBUpdates() {
		$this->prefix = $this->getOption( 'prefix' );
		$this->table = $this->getOption( 'table', null );
		$this->assign = $this->getOption( 'assign' );

		$this->cleanup(
			'revision', 'rev_id', 'rev_user', 'rev_user_text',
			[ 'rev_user' => 0 ], [ 'rev_timestamp', 'rev_id' ]
		);
		$this->cleanup(
			'archive', 'ar_id', 'ar_user', 'ar_user_text',
			[], [ 'ar_id' ]
		);
		$this->cleanup(
			'logging', 'log_id', 'log_user', 'log_user_text',
			[ 'log_user' => 0 ], [ 'log_timestamp', 'log_id' ]
		);
		$this->cleanup(
			'image', 'img_name', 'img_user', 'img_user_text',
			[ 'img_user' => 0 ], [ 'img_timestamp', 'img_name' ]
		);
		$this->cleanup(
			'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_user', 'oi_user_text',
			[], [ 'oi_name', 'oi_timestamp' ]
		);
		$this->cleanup(
			'filearchive', 'fa_id', 'fa_user', 'fa_user_text',
			[], [ 'fa_id' ]
		);
		$this->cleanup(
			'ipblocks', 'ipb_id', 'ipb_by', 'ipb_by_text',
			[], [ 'ipb_id' ]
		);
		$this->cleanup(
			'recentchanges', 'rc_id', 'rc_user', 'rc_user_text',
			[], [ 'rc_id' ]
		);

		return true;
	}

	/**
	 * Calculate a "next" condition and progress display string
	 * @param IDatabase $dbw
	 * @param string[] $indexFields Fields in the index being ordered by
	 * @param object $row Database row
	 * @return array [ string $next, string $display ]
	 */
	private function makeNextCond( $dbw, $indexFields, $row ) {
		$next = '';
		$display = [];
		for ( $i = count( $indexFields ) - 1; $i >= 0; $i-- ) {
			$field = $indexFields[$i];
			$display[] = $field . '=' . $row->$field;
			$value = $dbw->addQuotes( $row->$field );
			if ( $next === '' ) {
				$next = "$field > $value";
			} else {
				$next = "$field > $value OR $field = $value AND ($next)";
			}
		}
		$display = implode( ' ', array_reverse( $display ) );
		return [ $next, $display ];
	}

	/**
	 * Cleanup a table
	 *
	 * @param string $table Table to migrate
	 * @param string|string[] $primaryKey Primary key of the table.
	 * @param string $idField User ID field name
	 * @param string $nameField User name field name
	 * @param array $conds Query conditions
	 * @param string[] $orderby Fields to order by
	 */
	protected function cleanup(
		$table, $primaryKey, $idField, $nameField, array $conds, array $orderby
	) {
		if ( $this->table !== null && $this->table !== $table ) {
			return;
		}

		$primaryKey = (array)$primaryKey;
		$pkFilter = array_flip( $primaryKey );
		$this->output(
			"Beginning cleanup of $table\n"
		);

		$dbw = $this->getDB( DB_MASTER );
		$next = '1=1';
		$countAssigned = 0;
		$countPrefixed = 0;
		while ( true ) {
			// Fetch the rows needing update
			$res = $dbw->select(
				$table,
				array_merge( $primaryKey, [ $idField, $nameField ], $orderby ),
				array_merge( $conds, [ $next ] ),
				__METHOD__,
				[
					'ORDER BY' => $orderby,
					'LIMIT' => $this->mBatchSize,
				]
			);
			if ( !$res->numRows() ) {
				break;
			}

			// Update the existing rows
			foreach ( $res as $row ) {
				$name = $row->$nameField;
				if ( $row->$idField || !User::isUsableName( $name ) ) {
					continue;
				}

				$id = 0;
				if ( $this->assign ) {
					$id = (int)User::idFromName( $name );
					if ( !$id ) {
						// See if any extension wants to create it.
						if ( !isset( $this->triedCreations[$name] ) ) {
							$this->triedCreations[$name] = true;
							if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) ) {
								$id = (int)User::idFromName( $name, User::READ_LATEST );
							}
						}
					}
				}
				if ( $id ) {
					$set = [ $idField => $id ];
					$counter = &$countAssigned;
				} else {
					$set = [ $nameField => substr( $this->prefix . '>' . $name, 0, 255 ) ];
					$counter = &$countPrefixed;
				}

				$dbw->update(
					$table,
					$set,
					array_intersect_key( (array)$row, $pkFilter ) + [
						$idField => 0,
						$nameField => $name,
					],
					__METHOD__
				);
				$counter += $dbw->affectedRows();
			}

			list( $next, $display ) = $this->makeNextCond( $dbw, $orderby, $row );
			$this->output( "... $display\n" );
			wfWaitForSlaves();
		}

		$this->output(
			"Completed cleanup, assigned $countAssigned and prefixed $countPrefixed row(s)\n"
		);
	}
}

$maintClass = CleanupUsersWithNoId::class;
require_once RUN_MAINTENANCE_IF_MAIN;