MediaWiki  1.31.0
ActorMigration.php
Go to the documentation of this file.
1 <?php
26 
36 
45  private static $tempTables = [
46  'rev_user' => [
47  'table' => 'revision_actor_temp',
48  'pk' => 'revactor_rev',
49  'field' => 'revactor_actor',
50  'joinPK' => 'rev_id',
51  'extra' => [
52  'revactor_timestamp' => 'rev_timestamp',
53  'revactor_page' => 'rev_page',
54  ],
55  ],
56  ];
57 
63  private static $formerTempTables = [];
64 
70  private static $specialFields = [
71  'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
72  ];
73 
75  private $joinCache = null;
76 
78  private $stage;
79 
81  public function __construct( $stage ) {
82  $this->stage = $stage;
83  }
84 
89  public static function newMigration() {
90  return MediaWikiServices::getInstance()->getActorMigration();
91  }
92 
98  public function isAnon( $field ) {
99  return $this->stage === MIGRATION_NEW ? "$field IS NULL" : "$field = 0";
100  }
101 
107  public function isNotAnon( $field ) {
108  return $this->stage === MIGRATION_NEW ? "$field IS NOT NULL" : "$field != 0";
109  }
110 
116  private static function getFieldNames( $key ) {
117  if ( isset( self::$specialFields[$key] ) ) {
118  return self::$specialFields[$key];
119  }
120 
121  return [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
122  }
123 
135  public function getJoin( $key ) {
136  if ( !isset( $this->joinCache[$key] ) ) {
137  $tables = [];
138  $fields = [];
139  $joins = [];
140 
141  list( $text, $actor ) = self::getFieldNames( $key );
142 
143  if ( $this->stage === MIGRATION_OLD ) {
144  $fields[$key] = $key;
145  $fields[$text] = $text;
146  $fields[$actor] = 'NULL';
147  } else {
148  $join = $this->stage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN';
149 
150  if ( isset( self::$tempTables[$key] ) ) {
151  $t = self::$tempTables[$key];
152  $alias = "temp_$key";
153  $tables[$alias] = $t['table'];
154  $joins[$alias] = [ $join, "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
155  $joinField = "{$alias}.{$t['field']}";
156  } else {
157  $joinField = $actor;
158  }
159 
160  $alias = "actor_$key";
161  $tables[$alias] = 'actor';
162  $joins[$alias] = [ $join, "{$alias}.actor_id = {$joinField}" ];
163 
164  if ( $this->stage === MIGRATION_NEW ) {
165  $fields[$key] = "{$alias}.actor_user";
166  $fields[$text] = "{$alias}.actor_name";
167  } else {
168  $fields[$key] = "COALESCE( {$alias}.actor_user, $key )";
169  $fields[$text] = "COALESCE( {$alias}.actor_name, $text )";
170  }
171  $fields[$actor] = $joinField;
172  }
173 
174  $this->joinCache[$key] = [
175  'tables' => $tables,
176  'fields' => $fields,
177  'joins' => $joins,
178  ];
179  }
180 
181  return $this->joinCache[$key];
182  }
183 
193  public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
194  if ( isset( self::$tempTables[$key] ) ) {
195  throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
196  }
197 
198  list( $text, $actor ) = self::getFieldNames( $key );
199  $ret = [];
200  if ( $this->stage <= MIGRATION_WRITE_BOTH ) {
201  $ret[$key] = $user->getId();
202  $ret[$text] = $user->getName();
203  }
204  if ( $this->stage >= MIGRATION_WRITE_BOTH ) {
205  // We need to be able to assign an actor ID if none exists
206  if ( !$user instanceof User && !$user->getActorId() ) {
207  $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
208  }
209  $ret[$actor] = $user->getActorId( $dbw );
210  }
211  return $ret;
212  }
213 
226  public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
227  if ( isset( self::$formerTempTables[$key] ) ) {
228  wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
229  } elseif ( !isset( self::$tempTables[$key] ) ) {
230  throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
231  }
232 
233  list( $text, $actor ) = self::getFieldNames( $key );
234  $ret = [];
235  $callback = null;
236  if ( $this->stage <= MIGRATION_WRITE_BOTH ) {
237  $ret[$key] = $user->getId();
238  $ret[$text] = $user->getName();
239  }
240  if ( $this->stage >= MIGRATION_WRITE_BOTH ) {
241  // We need to be able to assign an actor ID if none exists
242  if ( !$user instanceof User && !$user->getActorId() ) {
243  $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
244  }
245  $id = $user->getActorId( $dbw );
246 
247  if ( isset( self::$tempTables[$key] ) ) {
248  $func = __METHOD__;
249  $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
250  $t = self::$tempTables[$key];
251  $set = [ $t['field'] => $id ];
252  foreach ( $t['extra'] as $to => $from ) {
253  if ( !array_key_exists( $from, $extra ) ) {
254  throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
255  }
256  $set[$to] = $extra[$from];
257  }
258  $dbw->upsert(
259  $t['table'],
260  [ $t['pk'] => $pk ] + $set,
261  [ $t['pk'] ],
262  $set,
263  $func
264  );
265  };
266  } else {
267  $ret[$actor] = $id;
268  $callback = function ( $pk, array $extra ) {
269  };
270  }
271  } elseif ( isset( self::$tempTables[$key] ) ) {
272  $func = __METHOD__;
273  $callback = function ( $pk, array $extra ) use ( $key, $func ) {
274  $t = self::$tempTables[$key];
275  foreach ( $t['extra'] as $to => $from ) {
276  if ( !array_key_exists( $from, $extra ) ) {
277  throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
278  }
279  }
280  };
281  } else {
282  $callback = function ( $pk, array $extra ) {
283  };
284  }
285  return [ $ret, $callback ];
286  }
287 
307  public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
308  $tables = [];
309  $conds = [];
310  $joins = [];
311 
312  if ( $users instanceof UserIdentity ) {
313  $users = [ $users ];
314  }
315 
316  // Get information about all the passed users
317  $ids = [];
318  $names = [];
319  $actors = [];
320  foreach ( $users as $user ) {
321  if ( $useId && $user->getId() ) {
322  $ids[] = $user->getId();
323  } else {
324  $names[] = $user->getName();
325  }
326  $actorId = $user->getActorId();
327  if ( $actorId ) {
328  $actors[] = $actorId;
329  }
330  }
331 
332  list( $text, $actor ) = self::getFieldNames( $key );
333 
334  // Combine data into conditions to be ORed together
335  $actorNotEmpty = [];
336  if ( $this->stage === MIGRATION_OLD ) {
337  $actors = [];
338  $actorEmpty = [];
339  } elseif ( isset( self::$tempTables[$key] ) ) {
340  $t = self::$tempTables[$key];
341  $alias = "temp_$key";
342  $tables[$alias] = $t['table'];
343  $joins[$alias] = [
344  $this->stage === MIGRATION_NEW ? 'JOIN' : 'LEFT JOIN',
345  "{$alias}.{$t['pk']} = {$t['joinPK']}"
346  ];
347  $joinField = "{$alias}.{$t['field']}";
348  $actorEmpty = [ $joinField => null ];
349  if ( $this->stage !== MIGRATION_NEW ) {
350  // Otherwise the resulting test can evaluate to NULL, and
351  // NOT(NULL) is NULL rather than true.
352  $actorNotEmpty = [ "$joinField IS NOT NULL" ];
353  }
354  } else {
355  $joinField = $actor;
356  $actorEmpty = [ $joinField => 0 ];
357  }
358 
359  if ( $actors ) {
360  $conds['actor'] = $db->makeList(
361  $actorNotEmpty + [ $joinField => $actors ], IDatabase::LIST_AND
362  );
363  }
364  if ( $this->stage < MIGRATION_NEW && $ids ) {
365  $conds['userid'] = $db->makeList(
366  $actorEmpty + [ $key => $ids ], IDatabase::LIST_AND
367  );
368  }
369  if ( $this->stage < MIGRATION_NEW && $names ) {
370  $conds['username'] = $db->makeList(
371  $actorEmpty + [ $text => $names ], IDatabase::LIST_AND
372  );
373  }
374 
375  return [
376  'tables' => $tables,
377  'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
378  'orconds' => $conds,
379  'joins' => $joins,
380  ];
381  }
382 
383 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
$tables
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition: hooks.txt:990
Wikimedia\Rdbms\IDatabase\makeList
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
ActorMigration\__construct
__construct( $stage)
Definition: ActorMigration.php:81
MIGRATION_NEW
const MIGRATION_NEW
Definition: Defines.php:296
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MIGRATION_WRITE_BOTH
const MIGRATION_WRITE_BOTH
Definition: Defines.php:294
User\newFromAnyId
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:657
ActorMigration
This class handles the logic for the actor table migration.
Definition: ActorMigration.php:35
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:89
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
LIST_AND
const LIST_AND
Definition: Defines.php:44
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
ActorMigration\isAnon
isAnon( $field)
Return an SQL condition to test if a user field is anonymous.
Definition: ActorMigration.php:98
ActorMigration\$joinCache
array null $joinCache
Cache for self::getJoin()
Definition: ActorMigration.php:75
ActorMigration\getFieldNames
static getFieldNames( $key)
Definition: ActorMigration.php:116
LIST_OR
const LIST_OR
Definition: Defines.php:47
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1111
ActorMigration\$stage
int $stage
One of the MIGRATION_* constants.
Definition: ActorMigration.php:78
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
ActorMigration\isNotAnon
isNotAnon( $field)
Return an SQL condition to test if a user field is non-anonymous.
Definition: ActorMigration.php:107
MIGRATION_OLD
const MIGRATION_OLD
Definition: Defines.php:293
ActorMigration\$specialFields
static array $specialFields
Define fields that use non-standard mapping Keys are the user id column name, values are arrays with ...
Definition: ActorMigration.php:70
ActorMigration\getWhere
getWhere(IDatabase $db, $key, $users, $useId=true)
Get WHERE condition for the actor.
Definition: ActorMigration.php:307
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1987
ActorMigration\getJoin
getJoin( $key)
Get SELECT fields and joins for the actor key.
Definition: ActorMigration.php:135
Wikimedia\Rdbms\IDatabase\upsert
upsert( $table, array $rows, array $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
ActorMigration\getInsertValuesWithTempTable
getInsertValuesWithTempTable(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
Definition: ActorMigration.php:226
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ActorMigration\$tempTables
static array $tempTables
Define fields that use temporary tables for transitional purposes Keys are '$key',...
Definition: ActorMigration.php:45
$t
$t
Definition: testCompression.php:69
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
ActorMigration\getInsertValues
getInsertValues(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
Definition: ActorMigration.php:193
array
the array() calling protocol came about after MediaWiki 1.4rc1.
ActorMigration\$formerTempTables
static array $formerTempTables
Fields that formerly used $tempTables Key is '$key', value is the MediaWiki version in which it was r...
Definition: ActorMigration.php:63