123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- /**
- * Methods to help with the actor table migration
- *
- * 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
- */
- use MediaWiki\MediaWikiServices;
- use MediaWiki\User\UserIdentity;
- use Wikimedia\Rdbms\IDatabase;
- /**
- * This class handles the logic for the actor table migration.
- *
- * This is not intended to be a long-term part of MediaWiki; it will be
- * deprecated and removed once actor migration is complete.
- *
- * @since 1.31
- * @since 1.34 Use with 'ar_user', 'img_user', 'oi_user', 'fa_user',
- * 'rc_user', 'log_user', and 'ipb_by' is deprecated. Callers should
- * reference the corresponding actor fields directly.
- */
- class ActorMigration {
- /**
- * Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage
- * (in MW <1.34) expects MIGRATION_* or SCHEMA_COMPAT_*
- */
- const MIGRATION_STAGE_SCHEMA_COMPAT = 1;
- /**
- * Define fields that use temporary tables for transitional purposes
- * @var array Keys are '$key', values are arrays with four fields:
- * - table: Temporary table name
- * - pk: Temporary table column referring to the main table's primary key
- * - field: Temporary table column referring actor.actor_id
- * - joinPK: Main table's primary key
- */
- private static $tempTables = [
- 'rev_user' => [
- 'table' => 'revision_actor_temp',
- 'pk' => 'revactor_rev',
- 'field' => 'revactor_actor',
- 'joinPK' => 'rev_id',
- 'extra' => [
- 'revactor_timestamp' => 'rev_timestamp',
- 'revactor_page' => 'rev_page',
- ],
- ],
- ];
- /**
- * Fields that formerly used $tempTables
- * @var array Key is '$key', value is the MediaWiki version in which it was
- * removed from $tempTables.
- */
- private static $formerTempTables = [];
- /**
- * Define fields that are deprecated for use with this class.
- * @var (string|null)[] Keys are '$key', value is null for soft deprecation
- * or a string naming the deprecated version for hard deprecation.
- */
- private static $deprecated = [
- 'ar_user' => null, // 1.34
- 'img_user' => null, // 1.34
- 'oi_user' => null, // 1.34
- 'fa_user' => null, // 1.34
- 'rc_user' => null, // 1.34
- 'log_user' => null, // 1.34
- 'ipb_by' => null, // 1.34
- ];
- /**
- * Define fields that are removed for use with this class.
- * @var string[] Keys are '$key', value is the MediaWiki version in which
- * use was removed.
- */
- private static $removed = [];
- /**
- * Define fields that use non-standard mapping
- * @var array Keys are the user id column name, values are arrays with two
- * elements (the user text column name and the actor id column name)
- */
- private static $specialFields = [
- 'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
- ];
- /** @var array|null Cache for `self::getJoin()` */
- private $joinCache = null;
- /** @var int Combination of SCHEMA_COMPAT_* constants */
- private $stage;
- /** @private */
- public function __construct( $stage ) {
- if ( ( $stage & SCHEMA_COMPAT_WRITE_BOTH ) === 0 ) {
- throw new InvalidArgumentException( '$stage must include a write mode' );
- }
- if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === 0 ) {
- throw new InvalidArgumentException( '$stage must include a read mode' );
- }
- if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === SCHEMA_COMPAT_READ_BOTH ) {
- throw new InvalidArgumentException( 'Cannot read both schemas' );
- }
- if ( ( $stage & SCHEMA_COMPAT_READ_OLD ) && !( $stage & SCHEMA_COMPAT_WRITE_OLD ) ) {
- throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' );
- }
- if ( ( $stage & SCHEMA_COMPAT_READ_NEW ) && !( $stage & SCHEMA_COMPAT_WRITE_NEW ) ) {
- throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' );
- }
- $this->stage = $stage;
- }
- /**
- * Static constructor
- * @return ActorMigration
- */
- public static function newMigration() {
- return MediaWikiServices::getInstance()->getActorMigration();
- }
- /**
- * Issue deprecation warning/error as appropriate.
- * @param string $key
- */
- private static function checkDeprecation( $key ) {
- if ( isset( self::$removed[$key] ) ) {
- throw new InvalidArgumentException(
- "Use of " . static::class . " for '$key' was removed in MediaWiki " . self::$removed[$key]
- );
- }
- if ( !empty( self::$deprecated[$key] ) ) {
- wfDeprecated( static::class . " for '$key'", self::$deprecated[$key], false, 3 );
- }
- }
- /**
- * Return an SQL condition to test if a user field is anonymous
- * @param string $field Field name or SQL fragment
- * @return string
- */
- public function isAnon( $field ) {
- return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NULL" : "$field = 0";
- }
- /**
- * Return an SQL condition to test if a user field is non-anonymous
- * @param string $field Field name or SQL fragment
- * @return string
- */
- public function isNotAnon( $field ) {
- return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NOT NULL" : "$field != 0";
- }
- /**
- * @param string $key A key such as "rev_user" identifying the actor
- * field being fetched.
- * @return string[] [ $text, $actor ]
- */
- private static function getFieldNames( $key ) {
- return self::$specialFields[$key] ?? [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
- }
- /**
- * Get SELECT fields and joins for the actor key
- *
- * @param string $key A key such as "rev_user" identifying the actor
- * field being fetched.
- * @return array[] With three keys:
- * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
- * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
- * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
- * All tables, fields, and joins are aliased, so `+` is safe to use.
- * @phan-return array{tables:string[],fields:string[],joins:array}
- */
- public function getJoin( $key ) {
- self::checkDeprecation( $key );
- if ( !isset( $this->joinCache[$key] ) ) {
- $tables = [];
- $fields = [];
- $joins = [];
- list( $text, $actor ) = self::getFieldNames( $key );
- if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
- $fields[$key] = $key;
- $fields[$text] = $text;
- $fields[$actor] = 'NULL';
- } else {
- if ( isset( self::$tempTables[$key] ) ) {
- $t = self::$tempTables[$key];
- $alias = "temp_$key";
- $tables[$alias] = $t['table'];
- $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
- $joinField = "{$alias}.{$t['field']}";
- } else {
- $joinField = $actor;
- }
- $alias = "actor_$key";
- $tables[$alias] = 'actor';
- $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
- $fields[$key] = "{$alias}.actor_user";
- $fields[$text] = "{$alias}.actor_name";
- $fields[$actor] = $joinField;
- }
- $this->joinCache[$key] = [
- 'tables' => $tables,
- 'fields' => $fields,
- 'joins' => $joins,
- ];
- }
- return $this->joinCache[$key];
- }
- /**
- * Get UPDATE fields for the actor
- *
- * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
- * @param string $key A key such as "rev_user" identifying the actor
- * field being fetched.
- * @param UserIdentity $user User to set in the update
- * @return array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
- */
- public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
- self::checkDeprecation( $key );
- if ( isset( self::$tempTables[$key] ) ) {
- throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
- }
- list( $text, $actor ) = self::getFieldNames( $key );
- $ret = [];
- if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
- $ret[$key] = $user->getId();
- $ret[$text] = $user->getName();
- }
- if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
- // We need to be able to assign an actor ID if none exists
- if ( !$user instanceof User && !$user->getActorId() ) {
- $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
- }
- $ret[$actor] = $user->getActorId( $dbw );
- }
- return $ret;
- }
- /**
- * Get UPDATE fields for the actor
- *
- * @param IDatabase $dbw Database to use for creating an actor ID, if necessary
- * @param string $key A key such as "rev_user" identifying the actor
- * field being fetched.
- * @param UserIdentity $user User to set in the update
- * @return array with two values:
- * - array to merge into `$values` to `IDatabase->update()` or `$a` to `IDatabase->insert()`
- * - callback to call with the primary key for the main table insert
- * and extra fields needed for the temp table.
- */
- public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
- self::checkDeprecation( $key );
- if ( isset( self::$formerTempTables[$key] ) ) {
- wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
- } elseif ( !isset( self::$tempTables[$key] ) ) {
- throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
- }
- list( $text, $actor ) = self::getFieldNames( $key );
- $ret = [];
- $callback = null;
- if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
- $ret[$key] = $user->getId();
- $ret[$text] = $user->getName();
- }
- if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
- // We need to be able to assign an actor ID if none exists
- if ( !$user instanceof User && !$user->getActorId() ) {
- $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
- }
- $id = $user->getActorId( $dbw );
- if ( isset( self::$tempTables[$key] ) ) {
- $func = __METHOD__;
- $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
- $t = self::$tempTables[$key];
- $set = [ $t['field'] => $id ];
- foreach ( $t['extra'] as $to => $from ) {
- if ( !array_key_exists( $from, $extra ) ) {
- throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
- }
- $set[$to] = $extra[$from];
- }
- $dbw->upsert(
- $t['table'],
- [ $t['pk'] => $pk ] + $set,
- [ $t['pk'] ],
- $set,
- $func
- );
- };
- } else {
- $ret[$actor] = $id;
- $callback = function ( $pk, array $extra ) {
- };
- }
- } elseif ( isset( self::$tempTables[$key] ) ) {
- $func = __METHOD__;
- $callback = function ( $pk, array $extra ) use ( $key, $func ) {
- $t = self::$tempTables[$key];
- foreach ( $t['extra'] as $to => $from ) {
- if ( !array_key_exists( $from, $extra ) ) {
- throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
- }
- }
- };
- } else {
- $callback = function ( $pk, array $extra ) {
- };
- }
- return [ $ret, $callback ];
- }
- /**
- * Get WHERE condition for the actor
- *
- * @param IDatabase $db Database to use for quoting and list-making
- * @param string $key A key such as "rev_user" identifying the actor
- * field being fetched.
- * @param UserIdentity|UserIdentity[] $users Users to test for
- * @param bool $useId If false, don't try to query by the user ID.
- * Intended for use with rc_user since it has an index on
- * (rc_user_text,rc_timestamp) but not (rc_user,rc_timestamp).
- * @return array With three keys:
- * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
- * - conds: (string) to include in the `$cond` to `IDatabase->select()`
- * - orconds: (array[]) array of alternatives in case a union of multiple
- * queries would be more efficient than a query with OR. May have keys
- * 'actor', 'userid', 'username'.
- * Since 1.32, this is guaranteed to contain just one alternative if
- * $users contains a single user.
- * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
- * All tables and joins are aliased, so `+` is safe to use.
- */
- public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
- self::checkDeprecation( $key );
- $tables = [];
- $conds = [];
- $joins = [];
- if ( $users instanceof UserIdentity ) {
- $users = [ $users ];
- }
- // Get information about all the passed users
- $ids = [];
- $names = [];
- $actors = [];
- foreach ( $users as $user ) {
- if ( $useId && $user->getId() ) {
- $ids[] = $user->getId();
- } else {
- $names[] = $user->getName();
- }
- $actorId = $user->getActorId();
- if ( $actorId ) {
- $actors[] = $actorId;
- }
- }
- list( $text, $actor ) = self::getFieldNames( $key );
- // Combine data into conditions to be ORed together
- if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
- if ( $actors ) {
- if ( isset( self::$tempTables[$key] ) ) {
- $t = self::$tempTables[$key];
- $alias = "temp_$key";
- $tables[$alias] = $t['table'];
- $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
- $joinField = "{$alias}.{$t['field']}";
- } else {
- $joinField = $actor;
- }
- $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
- }
- } else {
- if ( $ids ) {
- $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
- }
- if ( $names ) {
- $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
- }
- }
- return [
- 'tables' => $tables,
- 'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
- 'orconds' => $conds,
- 'joins' => $joins,
- ];
- }
- }
|