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 
153  public function getJoin( $key ) {
154  if ( !isset( $this->joinCache[$key] ) ) {
155  $tables = [];
156  $fields = [];
157  $joins = [];
158 
159  list( $text, $actor ) = self::getFieldNames( $key );
160 
161  if ( $this->stage & SCHEMA_COMPAT_READ_OLD ) {
162  $fields[$key] = $key;
163  $fields[$text] = $text;
164  $fields[$actor] = 'NULL';
165  } else {
166  if ( isset( self::$tempTables[$key] ) ) {
167  $t = self::$tempTables[$key];
168  $alias = "temp_$key";
169  $tables[$alias] = $t['table'];
170  $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
171  $joinField = "{$alias}.{$t['field']}";
172  } else {
173  $joinField = $actor;
174  }
175 
176  $alias = "actor_$key";
177  $tables[$alias] = 'actor';
178  $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
179 
180  $fields[$key] = "{$alias}.actor_user";
181  $fields[$text] = "{$alias}.actor_name";
182  $fields[$actor] = $joinField;
183  }
184 
185  $this->joinCache[$key] = [
186  'tables' => $tables,
187  'fields' => $fields,
188  'joins' => $joins,
189  ];
190  }
191 
192  return $this->joinCache[$key];
193  }
194 
204  public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
205  if ( isset( self::$tempTables[$key] ) ) {
206  throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
207  }
208 
209  list( $text, $actor ) = self::getFieldNames( $key );
210  $ret = [];
211  if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
212  $ret[$key] = $user->getId();
213  $ret[$text] = $user->getName();
214  }
215  if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
216  // We need to be able to assign an actor ID if none exists
217  if ( !$user instanceof User && !$user->getActorId() ) {
218  $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
219  }
220  $ret[$actor] = $user->getActorId( $dbw );
221  }
222  return $ret;
223  }
224 
237  public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
238  if ( isset( self::$formerTempTables[$key] ) ) {
239  wfDeprecated( __METHOD__ . " for $key", self::$formerTempTables[$key] );
240  } elseif ( !isset( self::$tempTables[$key] ) ) {
241  throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
242  }
243 
244  list( $text, $actor ) = self::getFieldNames( $key );
245  $ret = [];
246  $callback = null;
247  if ( $this->stage & SCHEMA_COMPAT_WRITE_OLD ) {
248  $ret[$key] = $user->getId();
249  $ret[$text] = $user->getName();
250  }
251  if ( $this->stage & SCHEMA_COMPAT_WRITE_NEW ) {
252  // We need to be able to assign an actor ID if none exists
253  if ( !$user instanceof User && !$user->getActorId() ) {
254  $user = User::newFromAnyId( $user->getId(), $user->getName(), null );
255  }
256  $id = $user->getActorId( $dbw );
257 
258  if ( isset( self::$tempTables[$key] ) ) {
259  $func = __METHOD__;
260  $callback = function ( $pk, array $extra ) use ( $dbw, $key, $id, $func ) {
261  $t = self::$tempTables[$key];
262  $set = [ $t['field'] => $id ];
263  foreach ( $t['extra'] as $to => $from ) {
264  if ( !array_key_exists( $from, $extra ) ) {
265  throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
266  }
267  $set[$to] = $extra[$from];
268  }
269  $dbw->upsert(
270  $t['table'],
271  [ $t['pk'] => $pk ] + $set,
272  [ $t['pk'] ],
273  $set,
274  $func
275  );
276  };
277  } else {
278  $ret[$actor] = $id;
279  $callback = function ( $pk, array $extra ) {
280  };
281  }
282  } elseif ( isset( self::$tempTables[$key] ) ) {
283  $func = __METHOD__;
284  $callback = function ( $pk, array $extra ) use ( $key, $func ) {
285  $t = self::$tempTables[$key];
286  foreach ( $t['extra'] as $to => $from ) {
287  if ( !array_key_exists( $from, $extra ) ) {
288  throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
289  }
290  }
291  };
292  } else {
293  $callback = function ( $pk, array $extra ) {
294  };
295  }
296  return [ $ret, $callback ];
297  }
298 
320  public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
321  $tables = [];
322  $conds = [];
323  $joins = [];
324 
325  if ( $users instanceof UserIdentity ) {
326  $users = [ $users ];
327  }
328 
329  // Get information about all the passed users
330  $ids = [];
331  $names = [];
332  $actors = [];
333  foreach ( $users as $user ) {
334  if ( $useId && $user->getId() ) {
335  $ids[] = $user->getId();
336  } else {
337  $names[] = $user->getName();
338  }
339  $actorId = $user->getActorId();
340  if ( $actorId ) {
341  $actors[] = $actorId;
342  }
343  }
344 
345  list( $text, $actor ) = self::getFieldNames( $key );
346 
347  // Combine data into conditions to be ORed together
348  if ( $this->stage & SCHEMA_COMPAT_READ_NEW ) {
349  if ( $actors ) {
350  if ( isset( self::$tempTables[$key] ) ) {
351  $t = self::$tempTables[$key];
352  $alias = "temp_$key";
353  $tables[$alias] = $t['table'];
354  $joins[$alias] = [ 'JOIN', "{$alias}.{$t['pk']} = {$t['joinPK']}" ];
355  $joinField = "{$alias}.{$t['field']}";
356  } else {
357  $joinField = $actor;
358  }
359  $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
360  }
361  } else {
362  if ( $ids ) {
363  $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
364  }
365  if ( $names ) {
366  $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
367  }
368  }
369 
370  return [
371  'tables' => $tables,
372  'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
373  'orconds' => $conds,
374  'joins' => $joins,
375  ];
376  }
377 
378 }
const SCHEMA_COMPAT_WRITE_OLD
Definition: Defines.php:284
upsert( $table, array $rows, $uniqueIndexes, array $set, $fname=__METHOD__)
INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
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))
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:289
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:1982
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static array array null $joinCache
Cache for self::getJoin()
const SCHEMA_COMPAT_READ_NEW
Definition: Defines.php:287
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:979
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:48
static newMigration()
Static constructor.
const LIST_AND
Definition: Defines.php:43
const SCHEMA_COMPAT_WRITE_NEW
Definition: Defines.php:286
__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:780
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:288
const LIST_OR
Definition: Defines.php:46
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
static newFromAnyId( $userId, $userName, $actorId)
Static factory method for creation from an ID, name, and/or actor ID.
Definition: User.php:678
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:285
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:1473