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
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\User\UserIdentity
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * This class handles the logic for the actor table migration.
30 * This is not intended to be a long-term part of MediaWiki; it will be
31 * deprecated and removed along with $wgActorTableSchemaMigrationStage.
35 class ActorMigration
{
38 * Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage
39 * expects MIGRATION_* or SCHEMA_COMPAT_*
41 const MIGRATION_STAGE_SCHEMA_COMPAT
= 1;
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
51 private static $tempTables = [
53 'table' => 'revision_actor_temp',
54 'pk' => 'revactor_rev',
55 'field' => 'revactor_actor',
58 'revactor_timestamp' => 'rev_timestamp',
59 'revactor_page' => 'rev_page',
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.
69 private static $formerTempTables = [];
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)
76 private static $specialFields = [
77 'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
80 /** @var array|null Cache for `self::getJoin()` */
81 private $joinCache = null;
83 /** @var int Combination of SCHEMA_COMPAT_* constants */
87 public function __construct( $stage ) {
88 if ( ( $stage & SCHEMA_COMPAT_WRITE_BOTH
) === 0 ) {
89 throw new InvalidArgumentException( '$stage must include a write mode' );
91 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH
) === 0 ) {
92 throw new InvalidArgumentException( '$stage must include a read mode' );
94 if ( ( $stage & SCHEMA_COMPAT_READ_BOTH
) === SCHEMA_COMPAT_READ_BOTH
) {
95 throw new InvalidArgumentException( 'Cannot read both schemas' );
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' );
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' );
104 $this->stage
= $stage;
109 * @return ActorMigration
111 public static function newMigration() {
112 return MediaWikiServices
::getInstance()->getActorMigration();
116 * Return an SQL condition to test if a user field is anonymous
117 * @param string $field Field name or SQL fragment
120 public function isAnon( $field ) {
121 return ( $this->stage
& SCHEMA_COMPAT_READ_NEW
) ?
"$field IS NULL" : "$field = 0";
125 * Return an SQL condition to test if a user field is non-anonymous
126 * @param string $field Field name or SQL fragment
129 public function isNotAnon( $field ) {
130 return ( $this->stage
& SCHEMA_COMPAT_READ_NEW
) ?
"$field IS NOT NULL" : "$field != 0";
134 * @param string $key A key such as "rev_user" identifying the actor
135 * field being fetched.
136 * @return string[] [ $text, $actor ]
138 private static function getFieldNames( $key ) {
139 return self
::$specialFields[$key] ??
[ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
143 * Get SELECT fields and joins for the actor key
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 * @phan-return array{tables:string[],fields:string[],joins:array}
154 public function getJoin( $key ) {
155 if ( !isset( $this->joinCache
[$key] ) ) {
160 list( $text, $actor ) = self
::getFieldNames( $key );
162 if ( $this->stage
& SCHEMA_COMPAT_READ_OLD
) {
163 $fields[$key] = $key;
164 $fields[$text] = $text;
165 $fields[$actor] = 'NULL';
167 if ( isset( self
::$tempTables[$key] ) ) {
168 $t = self
::$tempTables[$key];
169 $alias = "temp_$key";
170 $tables[$alias] = $t['table'];
171 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
172 $joinField = "{$alias}.{$t['field']}";
177 $alias = "actor_$key";
178 $tables[$alias] = 'actor';
179 $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
181 $fields[$key] = "{$alias}.actor_user";
182 $fields[$text] = "{$alias}.actor_name";
183 $fields[$actor] = $joinField;
186 $this->joinCache
[$key] = [
193 return $this->joinCache
[$key];
197 * Get UPDATE fields for the actor
199 * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
200 * @param string $key A key such as "rev_user" identifying the actor
201 * field being fetched.
202 * @param UserIdentity $user User to set in the update
203 * @return array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
205 public function getInsertValues( IDatabase
$dbw, $key, UserIdentity
$user ) {
206 if ( isset( self
::$tempTables[$key] ) ) {
207 throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
210 list( $text, $actor ) = self
::getFieldNames( $key );
212 if ( $this->stage
& SCHEMA_COMPAT_WRITE_OLD
) {
213 $ret[$key] = $user->getId();
214 $ret[$text] = $user->getName();
216 if ( $this->stage
& SCHEMA_COMPAT_WRITE_NEW
) {
217 // We need to be able to assign an actor ID if none exists
218 if ( !$user instanceof User
&& !$user->getActorId() ) {
219 $user = User
::newFromAnyId( $user->getId(), $user->getName(), null );
221 $ret[$actor] = $user->getActorId( $dbw );
227 * Get UPDATE fields for the actor
229 * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
230 * @param string $key A key such as "rev_user" identifying the actor
231 * field being fetched.
232 * @param UserIdentity $user User to set in the update
233 * @return array with two values:
234 * - array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
235 * - callback to call with the primary key for the main table insert
236 * and extra fields needed for the temp table.
238 public function getInsertValuesWithTempTable( IDatabase
$dbw, $key, UserIdentity
$user ) {
239 if ( isset( self
::$formerTempTables[$key] ) ) {
240 wfDeprecated( __METHOD__
. " for $key", self
::$formerTempTables[$key] );
241 } elseif ( !isset( self
::$tempTables[$key] ) ) {
242 throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
245 list( $text, $actor ) = self
::getFieldNames( $key );
248 if ( $this->stage
& SCHEMA_COMPAT_WRITE_OLD
) {
249 $ret[$key] = $user->getId();
250 $ret[$text] = $user->getName();
252 if ( $this->stage
& SCHEMA_COMPAT_WRITE_NEW
) {
253 // We need to be able to assign an actor ID if none exists
254 if ( !$user instanceof User
&& !$user->getActorId() ) {
255 $user = User
::newFromAnyId( $user->getId(), $user->getName(), null );
257 $id = $user->getActorId( $dbw );
259 if ( isset( self
::$tempTables[$key] ) ) {
261 $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
262 $t = self
::$tempTables[$key];
263 $set = [ $t['field'] => $id ];
264 foreach ( $t['extra'] as $to => $from ) {
265 if ( !array_key_exists( $from, $extra ) ) {
266 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
268 $set[$to] = $extra[$from];
272 [ $t['pk'] => $pk ] +
$set,
280 $callback = function ( $pk, array $extra ) {
283 } elseif ( isset( self
::$tempTables[$key] ) ) {
285 $callback = function ( $pk, array $extra ) use ( $key, $func ) {
286 $t = self
::$tempTables[$key];
287 foreach ( $t['extra'] as $to => $from ) {
288 if ( !array_key_exists( $from, $extra ) ) {
289 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
294 $callback = function ( $pk, array $extra ) {
297 return [ $ret, $callback ];
301 * Get WHERE condition for the actor
303 * @param IDatabase $db Database to use for quoting and list-making
304 * @param string $key A key such as "rev_user" identifying the actor
305 * field being fetched.
306 * @param UserIdentity|UserIdentity[] $users Users to test for
307 * @param bool $useId If false, don't try to query by the user ID.
308 * Intended for use with rc_user since it has an index on
309 * (rc_user_text,rc_timestamp) but not (rc_user,rc_timestamp).
310 * @return array With three keys:
311 * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
312 * - conds: (string) to include in the `$cond` to `IDatabase->select()`
313 * - orconds: (array[]) array of alternatives in case a union of multiple
314 * queries would be more efficient than a query with OR. May have keys
315 * 'actor', 'userid', 'username'.
316 * Since 1.32, this is guaranteed to contain just one alternative if
317 * $users contains a single user.
318 * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
319 * All tables and joins are aliased, so `+` is safe to use.
321 public function getWhere( IDatabase
$db, $key, $users, $useId = true ) {
326 if ( $users instanceof UserIdentity
) {
330 // Get information about all the passed users
334 foreach ( $users as $user ) {
335 if ( $useId && $user->getId() ) {
336 $ids[] = $user->getId();
338 $names[] = $user->getName();
340 $actorId = $user->getActorId();
342 $actors[] = $actorId;
346 list( $text, $actor ) = self
::getFieldNames( $key );
348 // Combine data into conditions to be ORed together
349 if ( $this->stage
& SCHEMA_COMPAT_READ_NEW
) {
351 if ( isset( self
::$tempTables[$key] ) ) {
352 $t = self
::$tempTables[$key];
353 $alias = "temp_$key";
354 $tables[$alias] = $t['table'];
355 $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
356 $joinField = "{$alias}.{$t['field']}";
360 $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase
::LIST_AND
);
364 $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase
::LIST_AND
);
367 $conds['username'] = $db->makeList( [ $text => $names ], IDatabase
::LIST_AND
);
373 'conds' => $conds ?
$db->makeList( array_values( $conds ), IDatabase
::LIST_OR
) : '1=0',