Remove HWLDFWordAccumulator, deprecated in 1.28
[lhc/web/wiklou.git] / includes / ActorMigration.php
1 <?php
2 /**
3 * Methods to help with the actor table migration
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\User\UserIdentity;
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * This class handles the logic for the actor table migration.
29 *
30 * This is not intended to be a long-term part of MediaWiki; it will be
31 * deprecated and removed along with $wgActorTableSchemaMigrationStage.
32 *
33 * @since 1.31
34 */
35 class ActorMigration {
36
37 /**
38 * Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage
39 * expects MIGRATION_* or SCHEMA_COMPAT_*
40 */
41 const MIGRATION_STAGE_SCHEMA_COMPAT = 1;
42
43 /**
44 * Define fields that use temporary tables for transitional purposes
45 * @var array Keys are '$key', values are arrays with four fields:
46 * - table: Temporary table name
47 * - pk: Temporary table column referring to the main table's primary key
48 * - field: Temporary table column referring actor.actor_id
49 * - joinPK: Main table's primary key
50 */
51 private static $tempTables = [
52 'rev_user' => [
53 'table' => 'revision_actor_temp',
54 'pk' => 'revactor_rev',
55 'field' => 'revactor_actor',
56 'joinPK' => 'rev_id',
57 'extra' => [
58 'revactor_timestamp' => 'rev_timestamp',
59 'revactor_page' => 'rev_page',
60 ],
61 ],
62 ];
63
64 /**
65 * Fields that formerly used $tempTables
66 * @var array Key is '$key', value is the MediaWiki version in which it was
67 * removed from $tempTables.
68 */
69 private static $formerTempTables = [];
70
71 /**
72 * Define fields that use non-standard mapping
73 * @var array Keys are the user id column name, values are arrays with two
74 * elements (the user text column name and the actor id column name)
75 */
76 private static $specialFields = [
77 'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
78 ];
79
80 /** @var array|null Cache for `self::getJoin()` */
81 private $joinCache = null;
82
83 /** @var int Combination of SCHEMA_COMPAT_* constants */
84 private $stage;
85
86 /** @private */
87 public function __construct( $stage ) {
88 if ( ( $stage & SCHEMA_COMPAT_WRITE_BOTH ) === 0 ) {
89 throw new InvalidArgumentException( '$stage must include a write mode' );
90 }
91 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === 0 ) {
92 throw new InvalidArgumentException( '$stage must include a read mode' );
93 }
94 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === SCHEMA_COMPAT_READ_BOTH ) {
95 throw new InvalidArgumentException( 'Cannot read both schemas' );
96 }
97 if ( ( $stage & SCHEMA_COMPAT_READ_OLD ) && !( $stage & SCHEMA_COMPAT_WRITE_OLD ) ) {
98 throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' );
99 }
100 if ( ( $stage & SCHEMA_COMPAT_READ_NEW ) && !( $stage & SCHEMA_COMPAT_WRITE_NEW ) ) {
101 throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' );
102 }
103
104 $this->stage = $stage;
105 }
106
107 /**
108 * Static constructor
109 * @return ActorMigration
110 */
111 public static function newMigration() {
112 return MediaWikiServices::getInstance()->getActorMigration();
113 }
114
115 /**
116 * Return an SQL condition to test if a user field is anonymous
117 * @param string $field Field name or SQL fragment
118 * @return string
119 */
120 public function isAnon( $field ) {
121 return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NULL" : "$field = 0";
122 }
123
124 /**
125 * Return an SQL condition to test if a user field is non-anonymous
126 * @param string $field Field name or SQL fragment
127 * @return string
128 */
129 public function isNotAnon( $field ) {
130 return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NOT NULL" : "$field != 0";
131 }
132
133 /**
134 * @param string $key A key such as "rev_user" identifying the actor
135 * field being fetched.
136 * @return string[] [ $text, $actor ]
137 */
138 private static function getFieldNames( $key ) {
139 return self::$specialFields[$key] ?? [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
140 }
141
142 /**
143 * Get SELECT fields and joins for the actor key
144 *
145 * @param string $key A key such as "rev_user" identifying the actor
146 * field being fetched.
147 * @return array With three keys:
148 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
149 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
150 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
151 * All tables, fields, and joins are aliased, so `+` is safe to use.
152 */
153 public function getJoin( $key ) {
154 if ( !isset( $this->joinCache[$key] ) ) {
155 $tables = [];
156 $fields = [];
157 $joins = [];
158
159 list( $text, $actor ) = self::getFieldNames( $key );
160
161 if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
162 $fields[$key] = $key;
163 $fields[$text] = $text;
164 $fields[$actor] = 'NULL';
165 } else {
166 if ( isset( self::$tempTables[$key] ) ) {
167 $t = self::$tempTables[$key];
168 $alias = "temp_$key";
169 $tables[$alias] = $t['table'];
170 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
171 $joinField = "{$alias}.{$t['field']}";
172 } else {
173 $joinField = $actor;
174 }
175
176 $alias = "actor_$key";
177 $tables[$alias] = 'actor';
178 $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
179
180 $fields[$key] = "{$alias}.actor_user";
181 $fields[$text] = "{$alias}.actor_name";
182 $fields[$actor] = $joinField;
183 }
184
185 $this->joinCache[$key] = [
186 'tables' => $tables,
187 'fields' => $fields,
188 'joins' => $joins,
189 ];
190 }
191
192 return $this->joinCache[$key];
193 }
194
195 /**
196 * Get UPDATE fields for the actor
197 *
198 * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
199 * @param string $key A key such as "rev_user" identifying the actor
200 * field being fetched.
201 * @param UserIdentity $user User to set in the update
202 * @return array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
203 */
204 public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
205 if ( isset( self::$tempTables[$key] ) ) {
206 throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
207 }
208
209 list( $text, $actor ) = self::getFieldNames( $key );
210 $ret = [];
211 if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
212 $ret[$key] = $user->getId();
213 $ret[$text] = $user->getName();
214 }
215 if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
216 // We need to be able to assign an actor ID if none exists
217 if ( !$user instanceof User && !$user->getActorId() ) {
218 $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
219 }
220 $ret[$actor] = $user->getActorId( $dbw );
221 }
222 return $ret;
223 }
224
225 /**
226 * Get UPDATE fields for the actor
227 *
228 * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
229 * @param string $key A key such as "rev_user" identifying the actor
230 * field being fetched.
231 * @param UserIdentity $user User to set in the update
232 * @return array with two values:
233 * - array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
234 * - callback to call with the primary key for the main table insert
235 * and extra fields needed for the temp table.
236 */
237 public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
238 if ( isset( self::$formerTempTables[$key] ) ) {
239 wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
240 } elseif ( !isset( self::$tempTables[$key] ) ) {
241 throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
242 }
243
244 list( $text, $actor ) = self::getFieldNames( $key );
245 $ret = [];
246 $callback = null;
247 if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
248 $ret[$key] = $user->getId();
249 $ret[$text] = $user->getName();
250 }
251 if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
252 // We need to be able to assign an actor ID if none exists
253 if ( !$user instanceof User && !$user->getActorId() ) {
254 $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
255 }
256 $id = $user->getActorId( $dbw );
257
258 if ( isset( self::$tempTables[$key] ) ) {
259 $func = __METHOD__;
260 $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
261 $t = self::$tempTables[$key];
262 $set = [ $t['field'] => $id ];
263 foreach ( $t['extra'] as $to => $from ) {
264 if ( !array_key_exists( $from, $extra ) ) {
265 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
266 }
267 $set[$to] = $extra[$from];
268 }
269 $dbw->upsert(
270 $t['table'],
271 [ $t['pk'] => $pk ] + $set,
272 [ $t['pk'] ],
273 $set,
274 $func
275 );
276 };
277 } else {
278 $ret[$actor] = $id;
279 $callback = function ( $pk, array $extra ) {
280 };
281 }
282 } elseif ( isset( self::$tempTables[$key] ) ) {
283 $func = __METHOD__;
284 $callback = function ( $pk, array $extra ) use ( $key, $func ) {
285 $t = self::$tempTables[$key];
286 foreach ( $t['extra'] as $to => $from ) {
287 if ( !array_key_exists( $from, $extra ) ) {
288 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
289 }
290 }
291 };
292 } else {
293 $callback = function ( $pk, array $extra ) {
294 };
295 }
296 return [ $ret, $callback ];
297 }
298
299 /**
300 * Get WHERE condition for the actor
301 *
302 * @param IDatabase $db Database to use for quoting and list-making
303 * @param string $key A key such as "rev_user" identifying the actor
304 * field being fetched.
305 * @param UserIdentity|UserIdentity[] $users Users to test for
306 * @param bool $useId If false, don't try to query by the user ID.
307 * Intended for use with rc_user since it has an index on
308 * (rc_user_text,rc_timestamp) but not (rc_user,rc_timestamp).
309 * @return array With three keys:
310 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
311 * - conds: (string) to include in the `$cond` to `IDatabase->select()`
312 * - orconds: (array[]) array of alternatives in case a union of multiple
313 * queries would be more efficient than a query with OR. May have keys
314 * 'actor', 'userid', 'username'.
315 * Since 1.32, this is guaranteed to contain just one alternative if
316 * $users contains a single user.
317 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
318 * All tables and joins are aliased, so `+` is safe to use.
319 */
320 public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
321 $tables = [];
322 $conds = [];
323 $joins = [];
324
325 if ( $users instanceof UserIdentity ) {
326 $users = [ $users ];
327 }
328
329 // Get information about all the passed users
330 $ids = [];
331 $names = [];
332 $actors = [];
333 foreach ( $users as $user ) {
334 if ( $useId && $user->getId() ) {
335 $ids[] = $user->getId();
336 } else {
337 $names[] = $user->getName();
338 }
339 $actorId = $user->getActorId();
340 if ( $actorId ) {
341 $actors[] = $actorId;
342 }
343 }
344
345 list( $text, $actor ) = self::getFieldNames( $key );
346
347 // Combine data into conditions to be ORed together
348 if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
349 if ( $actors ) {
350 if ( isset( self::$tempTables[$key] ) ) {
351 $t = self::$tempTables[$key];
352 $alias = "temp_$key";
353 $tables[$alias] = $t['table'];
354 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
355 $joinField = "{$alias}.{$t['field']}";
356 } else {
357 $joinField = $actor;
358 }
359 $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
360 }
361 } else {
362 if ( $ids ) {
363 $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
364 }
365 if ( $names ) {
366 $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
367 }
368 }
369
370 return [
371 'tables' => $tables,
372 'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
373 'orconds' => $conds,
374 'joins' => $joins,
375 ];
376 }
377
378 }