CentralIdLookup: Add @since to factoryNonLocal()
[mediawiki.git] / includes / ActorMigration.php
blobc2ad1cdb8c97cc71ffcd76967be6e832d81f2b77
1 <?php
2 /**
3 * Methods to help with the actor table migration
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.
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.
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
20 * @file
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\User\UserIdentity;
25 use Wikimedia\Rdbms\IDatabase;
27 /**
28 * This class handles the logic for the actor table migration and should
29 * always be used in lieu of directly accessing database tables.
31 * This is not intended to be a long-term part of MediaWiki; it will be
32 * deprecated and removed once actor migration is complete.
34 * @since 1.31
35 * @since 1.34 Use with 'ar_user', 'img_user', 'oi_user', 'fa_user',
36 * 'rc_user', 'log_user', and 'ipb_by' is deprecated. Callers should
37 * reference the corresponding actor fields directly.
39 class ActorMigration {
41 /**
42 * Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage
43 * (in MW <1.34) expects MIGRATION_* or SCHEMA_COMPAT_*
45 public const MIGRATION_STAGE_SCHEMA_COMPAT = 1;
47 /**
48 * Define fields that use temporary tables for transitional purposes
49 * @var array Keys are '$key', values are arrays with four fields:
50 * - table: Temporary table name
51 * - pk: Temporary table column referring to the main table's primary key
52 * - field: Temporary table column referring actor.actor_id
53 * - joinPK: Main table's primary key
55 private static $tempTables = [
56 'rev_user' => [
57 'table' => 'revision_actor_temp',
58 'pk' => 'revactor_rev',
59 'field' => 'revactor_actor',
60 'joinPK' => 'rev_id',
61 'extra' => [
62 'revactor_timestamp' => 'rev_timestamp',
63 'revactor_page' => 'rev_page',
68 /**
69 * Fields that formerly used $tempTables
70 * @var array Key is '$key', value is the MediaWiki version in which it was
71 * removed from $tempTables.
73 private static $formerTempTables = [];
75 /**
76 * Define fields that are deprecated for use with this class.
77 * @var (string|null)[] Keys are '$key', value is null for soft deprecation
78 * or a string naming the deprecated version for hard deprecation.
80 private static $deprecated = [
81 'ar_user' => null, // 1.34
82 'img_user' => null, // 1.34
83 'oi_user' => null, // 1.34
84 'fa_user' => null, // 1.34
85 'rc_user' => null, // 1.34
86 'log_user' => null, // 1.34
87 'ipb_by' => null, // 1.34
90 /**
91 * Define fields that are removed for use with this class.
92 * @var string[] Keys are '$key', value is the MediaWiki version in which
93 * use was removed.
95 private static $removed = [];
97 /**
98 * Define fields that use non-standard mapping
99 * @var array Keys are the user id column name, values are arrays with two
100 * elements (the user text column name and the actor id column name)
102 private static $specialFields = [
103 'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
106 /** @var array Cache for `self::getJoin()` */
107 private $joinCache = [];
109 /** @var int Combination of SCHEMA_COMPAT_* constants */
110 private $stage;
113 * @internal
114 * @param int $stage
116 public function __construct( $stage ) {
117 if ( ( $stage & SCHEMA_COMPAT_WRITE_BOTH ) === 0 ) {
118 throw new InvalidArgumentException( '$stage must include a write mode' );
120 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === 0 ) {
121 throw new InvalidArgumentException( '$stage must include a read mode' );
123 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === SCHEMA_COMPAT_READ_BOTH ) {
124 throw new InvalidArgumentException( 'Cannot read both schemas' );
126 if ( ( $stage & SCHEMA_COMPAT_READ_OLD ) && !( $stage & SCHEMA_COMPAT_WRITE_OLD ) ) {
127 throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' );
129 if ( ( $stage & SCHEMA_COMPAT_READ_NEW ) && !( $stage & SCHEMA_COMPAT_WRITE_NEW ) ) {
130 throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' );
133 $this->stage = $stage;
137 * Static constructor
138 * @return ActorMigration
140 public static function newMigration() {
141 return MediaWikiServices::getInstance()->getActorMigration();
145 * Issue deprecation warning/error as appropriate.
146 * @param string $key
148 private static function checkDeprecation( $key ) {
149 if ( isset( self::$removed[$key] ) ) {
150 throw new InvalidArgumentException(
151 "Use of " . static::class . " for '$key' was removed in MediaWiki " . self::$removed[$key]
154 if ( !empty( self::$deprecated[$key] ) ) {
155 wfDeprecated( static::class . " for '$key'", self::$deprecated[$key], false, 3 );
160 * Return an SQL condition to test if a user field is anonymous
161 * @param string $field Field name or SQL fragment
162 * @return string
164 public function isAnon( $field ) {
165 return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NULL" : "$field = 0";
169 * Return an SQL condition to test if a user field is non-anonymous
170 * @param string $field Field name or SQL fragment
171 * @return string
173 public function isNotAnon( $field ) {
174 return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NOT NULL" : "$field != 0";
178 * @param string $key A key such as "rev_user" identifying the actor
179 * field being fetched.
180 * @return string[] [ $text, $actor ]
182 private static function getFieldNames( $key ) {
183 return self::$specialFields[$key] ?? [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
187 * Get SELECT fields and joins for the actor key
189 * @param string $key A key such as "rev_user" identifying the actor
190 * field being fetched.
191 * @return array[] With three keys:
192 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
193 * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
194 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
195 * All tables, fields, and joins are aliased, so `+` is safe to use.
196 * @phan-return array{tables:string[],fields:string[],joins:array}
198 public function getJoin( $key ) {
199 self::checkDeprecation( $key );
201 if ( !isset( $this->joinCache[$key] ) ) {
202 $tables = [];
203 $fields = [];
204 $joins = [];
206 list( $text, $actor ) = self::getFieldNames( $key );
208 if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
209 $fields[$key] = $key;
210 $fields[$text] = $text;
211 $fields[$actor] = 'NULL';
212 } else {
213 if ( isset( self::$tempTables[$key] ) ) {
214 $t = self::$tempTables[$key];
215 $alias = "temp_$key";
216 $tables[$alias] = $t['table'];
217 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
218 $joinField = "{$alias}.{$t['field']}";
219 } else {
220 $joinField = $actor;
223 $alias = "actor_$key";
224 $tables[$alias] = 'actor';
225 $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
227 $fields[$key] = "{$alias}.actor_user";
228 $fields[$text] = "{$alias}.actor_name";
229 $fields[$actor] = $joinField;
232 $this->joinCache[$key] = [
233 'tables' => $tables,
234 'fields' => $fields,
235 'joins' => $joins,
239 return $this->joinCache[$key];
243 * Get UPDATE fields for the actor
245 * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
246 * @param string $key A key such as "rev_user" identifying the actor
247 * field being fetched.
248 * @param UserIdentity $user User to set in the update
249 * @return array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
251 public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
252 self::checkDeprecation( $key );
254 if ( isset( self::$tempTables[$key] ) ) {
255 throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
258 list( $text, $actor ) = self::getFieldNames( $key );
259 $ret = [];
260 if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
261 $ret[$key] = $user->getId();
262 $ret[$text] = $user->getName();
264 if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
265 // We need to be able to assign an actor ID if none exists
266 if ( !$user instanceof User && !$user->getActorId() ) {
267 $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
269 $ret[$actor] = $user->getActorId( $dbw );
271 return $ret;
275 * Get UPDATE fields for the actor
277 * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
278 * @param string $key A key such as "rev_user" identifying the actor
279 * field being fetched.
280 * @param UserIdentity $user User to set in the update
281 * @return array with two values:
282 * - array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
283 * - callback to call with the primary key for the main table insert
284 * and extra fields needed for the temp table.
286 public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
287 self::checkDeprecation( $key );
289 if ( isset( self::$formerTempTables[$key] ) ) {
290 wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
291 } elseif ( !isset( self::$tempTables[$key] ) ) {
292 throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
295 list( $text, $actor ) = self::getFieldNames( $key );
296 $ret = [];
297 $callback = null;
298 if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
299 $ret[$key] = $user->getId();
300 $ret[$text] = $user->getName();
302 if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
303 // We need to be able to assign an actor ID if none exists
304 if ( !$user instanceof User && !$user->getActorId() ) {
305 $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
307 $id = $user->getActorId( $dbw );
309 if ( isset( self::$tempTables[$key] ) ) {
310 $func = __METHOD__;
311 $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
312 $t = self::$tempTables[$key];
313 $set = [ $t['field'] => $id ];
314 foreach ( $t['extra'] as $to => $from ) {
315 if ( !array_key_exists( $from, $extra ) ) {
316 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
318 $set[$to] = $extra[$from];
320 $dbw->upsert(
321 $t['table'],
322 [ $t['pk'] => $pk ] + $set,
323 [ [ $t['pk'] ] ],
324 $set,
325 $func
328 } else {
329 $ret[$actor] = $id;
330 $callback = function ( $pk, array $extra ) {
333 } elseif ( isset( self::$tempTables[$key] ) ) {
334 $func = __METHOD__;
335 $callback = function ( $pk, array $extra ) use ( $key, $func ) {
336 $t = self::$tempTables[$key];
337 foreach ( $t['extra'] as $to => $from ) {
338 if ( !array_key_exists( $from, $extra ) ) {
339 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
343 } else {
344 $callback = function ( $pk, array $extra ) {
347 return [ $ret, $callback ];
351 * Get WHERE condition for the actor
353 * @param IDatabase $db Database to use for quoting and list-making
354 * @param string $key A key such as "rev_user" identifying the actor
355 * field being fetched.
356 * @param UserIdentity|UserIdentity[]|null|false $users Users to test for.
357 * Passing null, false, or the empty array will return 'conds' that never match,
358 * and an empty array for 'orconds'.
359 * @param bool $useId If false, don't try to query by the user ID.
360 * Intended for use with rc_user since it has an index on
361 * (rc_user_text,rc_timestamp) but not (rc_user,rc_timestamp).
362 * @return array With three keys:
363 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
364 * - conds: (string) to include in the `$cond` to `IDatabase->select()`
365 * - orconds: (array[]) array of alternatives in case a union of multiple
366 * queries would be more efficient than a query with OR. May have keys
367 * 'actor', 'userid', 'username'.
368 * Since 1.32, this is guaranteed to contain just one alternative if
369 * $users contains a single user.
370 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
371 * All tables and joins are aliased, so `+` is safe to use.
373 public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
374 self::checkDeprecation( $key );
376 $tables = [];
377 $conds = [];
378 $joins = [];
380 if ( $users instanceof UserIdentity ) {
381 $users = [ $users ];
382 } elseif ( $users === null || $users === false ) {
383 // DWIM
384 $users = [];
385 } elseif ( !is_array( $users ) ) {
386 $what = is_object( $users ) ? get_class( $users ) : gettype( $users );
387 throw new InvalidArgumentException(
388 __METHOD__ . ": Value for \$users must be a UserIdentity or array, got $what"
392 // Get information about all the passed users
393 $ids = [];
394 $names = [];
395 $actors = [];
396 foreach ( $users as $user ) {
397 if ( $useId && $user->getId() ) {
398 $ids[] = $user->getId();
399 } else {
400 $names[] = $user->getName();
402 $actorId = $user->getActorId();
403 if ( $actorId ) {
404 $actors[] = $actorId;
408 list( $text, $actor ) = self::getFieldNames( $key );
410 // Combine data into conditions to be ORed together
411 if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
412 if ( $actors ) {
413 if ( isset( self::$tempTables[$key] ) ) {
414 $t = self::$tempTables[$key];
415 $alias = "temp_$key";
416 $tables[$alias] = $t['table'];
417 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
418 $joinField = "{$alias}.{$t['field']}";
419 } else {
420 $joinField = $actor;
422 $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
424 } else {
425 if ( $ids ) {
426 $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
428 if ( $names ) {
429 $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
433 return [
434 'tables' => $tables,
435 'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
436 'orconds' => $conds,
437 'joins' => $joins,