MediaWiki  1.32.5
ActorMigration.php
Go to the documentation of this file.
1 <?php
26 
36 
42 
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 
69  private static $formerTempTables = [];
70 
76  private static $specialFields = [
77  'ipb_by' => [ 'ipb_by_text', 'ipb_by_actor' ],
78  ];
79 
81  private $joinCache = null;
82 
84  private $stage;
85 
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  }
95  throw new InvalidArgumentException( 'Cannot read both schemas' );
96  }
98  throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' );
99  }
101  throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' );
102  }
103 
104  $this->stage = $stage;
105  }
106 
111  public static function newMigration() {
112  return MediaWikiServices::getInstance()->getActorMigration();
113  }
114 
120  public function isAnon( $field ) {
121  return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NULL" : "$field = 0";
122  }
123 
129  public function isNotAnon( $field ) {
130  return ( $this->stage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NOT NULL" : "$field != 0";
131  }
132 
138  private static function getFieldNames( $key ) {
139  if ( isset( self::$specialFields[$key] ) ) {
140  return self::$specialFields[$key];
141  }
142 
143  return [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
144  }
145 
157  public function getJoin( $key ) {
158  if ( !isset( $this->joinCache[$key] ) ) {
159  $tables = [];
160  $fields = [];
161  $joins = [];
162 
163  list( $text, $actor ) = self::getFieldNames( $key );
164 
165  if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
166  $fields[$key] = $key;
167  $fields[$text] = $text;
168  $fields[$actor] = 'NULL';
169  } else {
170  if ( isset( self::$tempTables[$key] ) ) {
171  $t = self::$tempTables[$key];
172  $alias = "temp_$key";
173  $tables[$alias] = $t['table'];
174  $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
175  $joinField = "{$alias}.{$t['field']}";
176  } else {
177  $joinField = $actor;
178  }
179 
180  $alias = "actor_$key";
181  $tables[$alias] = 'actor';
182  $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
183 
184  $fields[$key] = "{$alias}.actor_user";
185  $fields[$text] = "{$alias}.actor_name";
186  $fields[$actor] = $joinField;
187  }
188 
189  $this->joinCache[$key] = [
190  'tables' => $tables,
191  'fields' => $fields,
192  'joins' => $joins,
193  ];
194  }
195 
196  return $this->joinCache[$key];
197  }
198 
208  public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
209  if ( isset( self::$tempTables[$key] ) ) {
210  throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
211  }
212 
213  list( $text, $actor ) = self::getFieldNames( $key );
214  $ret = [];
215  if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
216  $ret[$key] = $user->getId();
217  $ret[$text] = $user->getName();
218  }
219  if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
220  // We need to be able to assign an actor ID if none exists
221  if ( !$user instanceof User && !$user->getActorId() ) {
222  $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
223  }
224  $ret[$actor] = $user->getActorId( $dbw );
225  }
226  return $ret;
227  }
228 
241  public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
242  if ( isset( self::$formerTempTables[$key] ) ) {
243  wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
244  } elseif ( !isset( self::$tempTables[$key] ) ) {
245  throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
246  }
247 
248  list( $text, $actor ) = self::getFieldNames( $key );
249  $ret = [];
250  $callback = null;
251  if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
252  $ret[$key] = $user->getId();
253  $ret[$text] = $user->getName();
254  }
255  if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
256  // We need to be able to assign an actor ID if none exists
257  if ( !$user instanceof User && !$user->getActorId() ) {
258  $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
259  }
260  $id = $user->getActorId( $dbw );
261 
262  if ( isset( self::$tempTables[$key] ) ) {
263  $func = __METHOD__;
264  $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
265  $t = self::$tempTables[$key];
266  $set = [ $t['field'] => $id ];
267  foreach ( $t['extra'] as $to => $from ) {
268  if ( !array_key_exists( $from, $extra ) ) {
269  throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
270  }
271  $set[$to] = $extra[$from];
272  }
273  $dbw->upsert(
274  $t['table'],
275  [ $t['pk'] => $pk ] + $set,
276  [ $t['pk'] ],
277  $set,
278  $func
279  );
280  };
281  } else {
282  $ret[$actor] = $id;
283  $callback = function ( $pk, array $extra ) {
284  };
285  }
286  } elseif ( isset( self::$tempTables[$key] ) ) {
287  $func = __METHOD__;
288  $callback = function ( $pk, array $extra ) use ( $key, $func ) {
289  $t = self::$tempTables[$key];
290  foreach ( $t['extra'] as $to => $from ) {
291  if ( !array_key_exists( $from, $extra ) ) {
292  throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
293  }
294  }
295  };
296  } else {
297  $callback = function ( $pk, array $extra ) {
298  };
299  }
300  return [ $ret, $callback ];
301  }
302 
324  public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
325  $tables = [];
326  $conds = [];
327  $joins = [];
328 
329  if ( $users instanceof UserIdentity ) {
330  $users = [ $users ];
331  }
332 
333  // Get information about all the passed users
334  $ids = [];
335  $names = [];
336  $actors = [];
337  foreach ( $users as $user ) {
338  if ( $useId && $user->getId() ) {
339  $ids[] = $user->getId();
340  } else {
341  $names[] = $user->getName();
342  }
343  $actorId = $user->getActorId();
344  if ( $actorId ) {
345  $actors[] = $actorId;
346  }
347  }
348 
349  list( $text, $actor ) = self::getFieldNames( $key );
350 
351  // Combine data into conditions to be ORed together
352  if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
353  if ( $actors ) {
354  if ( isset( self::$tempTables[$key] ) ) {
355  $t = self::$tempTables[$key];
356  $alias = "temp_$key";
357  $tables[$alias] = $t['table'];
358  $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
359  $joinField = "{$alias}.{$t['field']}";
360  } else {
361  $joinField = $actor;
362  }
363  $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
364  }
365  } else {
366  if ( $ids ) {
367  $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
368  }
369  if ( $names ) {
370  $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
371  }
372  }
373 
374  return [
375  'tables' => $tables,
376  'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
377  'orconds' => $conds,
378  'joins' => $joins,
379  ];
380  }
381 
382 }
ActorMigration\MIGRATION_STAGE_SCHEMA_COMPAT
const MIGRATION_STAGE_SCHEMA_COMPAT
Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage expects MIGRATION_*...
Definition: ActorMigration.php:41
$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
SCHEMA_COMPAT_READ_NEW
const SCHEMA_COMPAT_READ_NEW
Definition: Defines.php:287
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:87
$tables
this hook is for auditing only 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:1018
User\newFromAnyId
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:682
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:111
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:43
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:120
ActorMigration\$joinCache
array null $joinCache
Cache for self::getJoin()
Definition: ActorMigration.php:81
ActorMigration\getFieldNames
static getFieldNames( $key)
Definition: ActorMigration.php:138
LIST_OR
const LIST_OR
Definition: Defines.php:46
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1118
ActorMigration\$stage
int $stage
Combination of SCHEMA_COMPAT_* constants.
Definition: ActorMigration.php:84
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
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
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:129
SCHEMA_COMPAT_WRITE_OLD
const SCHEMA_COMPAT_WRITE_OLD
Definition: Defines.php:284
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:76
SCHEMA_COMPAT_WRITE_NEW
const SCHEMA_COMPAT_WRITE_NEW
Definition: Defines.php:286
ActorMigration\getWhere
getWhere(IDatabase $db, $key, $users, $useId=true)
Get WHERE condition for the actor.
Definition: ActorMigration.php:324
$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:2044
ActorMigration\getJoin
getJoin( $key)
Get SELECT fields and joins for the actor key.
Definition: ActorMigration.php:157
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:241
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:51
SCHEMA_COMPAT_WRITE_BOTH
const SCHEMA_COMPAT_WRITE_BOTH
Definition: Defines.php:288
$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:47
ActorMigration\getInsertValues
getInsertValues(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
Definition: ActorMigration.php:208
SCHEMA_COMPAT_READ_BOTH
const SCHEMA_COMPAT_READ_BOTH
Definition: Defines.php:289
SCHEMA_COMPAT_READ_OLD
const SCHEMA_COMPAT_READ_OLD
Definition: Defines.php:285
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:69