MediaWiki  master
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  }
94  if ( ( $stage & SCHEMA_COMPAT_READ_BOTH ) === SCHEMA_COMPAT_READ_BOTH ) {
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  return self::$specialFields[$key] ?? [ $key . '_text', substr( $key, 0, -5 ) . '_actor' ];
140  }
141 
154  public function getJoin( $key ) {
155  if ( !isset( $this->joinCache[$key] ) ) {
156  $tables = [];
157  $fields = [];
158  $joins = [];
159 
160  list( $text, $actor ) = self::getFieldNames( $key );
161 
162  if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
163  $fields[$key] = $key;
164  $fields[$text] = $text;
165  $fields[$actor] = 'NULL';
166  } else {
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']}";
173  } else {
174  $joinField = $actor;
175  }
176 
177  $alias = "actor_$key";
178  $tables[$alias] = 'actor';
179  $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
180 
181  $fields[$key] = "{$alias}.actor_user";
182  $fields[$text] = "{$alias}.actor_name";
183  $fields[$actor] = $joinField;
184  }
185 
186  $this->joinCache[$key] = [
187  'tables' => $tables,
188  'fields' => $fields,
189  'joins' => $joins,
190  ];
191  }
192 
193  return $this->joinCache[$key];
194  }
195 
205  public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
206  if ( isset( self::$tempTables[$key] ) ) {
207  throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
208  }
209 
210  list( $text, $actor ) = self::getFieldNames( $key );
211  $ret = [];
212  if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
213  $ret[$key] = $user->getId();
214  $ret[$text] = $user->getName();
215  }
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 );
220  }
221  $ret[$actor] = $user->getActorId( $dbw );
222  }
223  return $ret;
224  }
225 
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" );
243  }
244 
245  list( $text, $actor ) = self::getFieldNames( $key );
246  $ret = [];
247  $callback = null;
248  if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
249  $ret[$key] = $user->getId();
250  $ret[$text] = $user->getName();
251  }
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 );
256  }
257  $id = $user->getActorId( $dbw );
258 
259  if ( isset( self::$tempTables[$key] ) ) {
260  $func = __METHOD__;
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" );
267  }
268  $set[$to] = $extra[$from];
269  }
270  $dbw->upsert(
271  $t['table'],
272  [ $t['pk'] => $pk ] + $set,
273  [ $t['pk'] ],
274  $set,
275  $func
276  );
277  };
278  } else {
279  $ret[$actor] = $id;
280  $callback = function ( $pk, array $extra ) {
281  };
282  }
283  } elseif ( isset( self::$tempTables[$key] ) ) {
284  $func = __METHOD__;
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" );
290  }
291  }
292  };
293  } else {
294  $callback = function ( $pk, array $extra ) {
295  };
296  }
297  return [ $ret, $callback ];
298  }
299 
321  public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
322  $tables = [];
323  $conds = [];
324  $joins = [];
325 
326  if ( $users instanceof UserIdentity ) {
327  $users = [ $users ];
328  }
329 
330  // Get information about all the passed users
331  $ids = [];
332  $names = [];
333  $actors = [];
334  foreach ( $users as $user ) {
335  if ( $useId && $user->getId() ) {
336  $ids[] = $user->getId();
337  } else {
338  $names[] = $user->getName();
339  }
340  $actorId = $user->getActorId();
341  if ( $actorId ) {
342  $actors[] = $actorId;
343  }
344  }
345 
346  list( $text, $actor ) = self::getFieldNames( $key );
347 
348  // Combine data into conditions to be ORed together
349  if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
350  if ( $actors ) {
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']}";
357  } else {
358  $joinField = $actor;
359  }
360  $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
361  }
362  } else {
363  if ( $ids ) {
364  $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
365  }
366  if ( $names ) {
367  $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
368  }
369  }
370 
371  return [
372  'tables' => $tables,
373  'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
374  'orconds' => $conds,
375  'joins' => $joins,
376  ];
377  }
378 
379 }
const SCHEMA_COMPAT_WRITE_OLD
Definition: Defines.php:264
upsert( $table, array $rows, $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
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
const SCHEMA_COMPAT_READ_BOTH
Definition: Defines.php:269
isNotAnon( $field)
Return an SQL condition to test if a user field is non-anonymous.
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:1972
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static newFromAnyId( $userId, $userName, $actorId, $dbDomain=false)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:627
static array array null $joinCache
Cache for self::getJoin()
const SCHEMA_COMPAT_READ_NEW
Definition: Defines.php:267
getWhere(IDatabase $db, $key, $users, $useId=true)
Get WHERE condition for the actor.
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
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:960
static array $tempTables
Define fields that use temporary tables for transitional purposes Keys are &#39;$key&#39;, values are arrays with four fields:
static array $specialFields
Define fields that use non-standard mapping Keys are the user id column name, values are arrays with ...
static getFieldNames( $key)
Interface for objects representing user identity.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
static newMigration()
Static constructor.
const LIST_AND
Definition: Defines.php:39
const SCHEMA_COMPAT_WRITE_NEW
Definition: Defines.php:266
__construct( $stage)
const MIGRATION_STAGE_SCHEMA_COMPAT
Constant for extensions to feature-test whether $wgActorTableSchemaMigrationStage expects MIGRATION_*...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
getInsertValues(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
int $stage
Combination of SCHEMA_COMPAT_* constants.
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
const SCHEMA_COMPAT_WRITE_BOTH
Definition: Defines.php:268
const LIST_OR
Definition: Defines.php:42
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
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
getJoin( $key)
Get SELECT fields and joins for the actor key.
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static array static array $formerTempTables
Fields that formerly used $tempTables Key is &#39;$key&#39;, value is the MediaWiki version in which it was r...
const SCHEMA_COMPAT_READ_OLD
Definition: Defines.php:265
isAnon( $field)
Return an SQL condition to test if a user field is anonymous.
getInsertValuesWithTempTable(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1454