MediaWiki REL1_39
ActorMigrationBase.php
Go to the documentation of this file.
1<?php
25use Wikimedia\IPUtils;
27
37 private $joinCache = [];
38
40 private $readStage;
41
43 private $writeStage;
44
46 private $actorStoreFactory;
47
49 private $fieldInfos;
50
52 private $allowUnknown;
53
97 public function __construct(
98 $fieldInfos,
99 $stage,
100 ActorStoreFactory $actorStoreFactory,
101 $options = []
102 ) {
103 $this->fieldInfos = $fieldInfos;
104 $this->allowUnknown = $options['allowUnknown'] ?? true;
105
106 $writeStage = $stage & SCHEMA_COMPAT_WRITE_MASK;
107 $readStage = $stage & SCHEMA_COMPAT_READ_MASK;
108 if ( $writeStage === 0 ) {
109 throw new InvalidArgumentException( '$stage must include a write mode' );
110 }
111 if ( $readStage === 0 ) {
112 throw new InvalidArgumentException( '$stage must include a read mode' );
113 }
114 if ( !in_array( $readStage,
116 ) {
117 throw new InvalidArgumentException( 'Cannot read multiple schemas' );
118 }
119 if ( $readStage === SCHEMA_COMPAT_READ_OLD && !( $writeStage & SCHEMA_COMPAT_WRITE_OLD ) ) {
120 throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' );
121 }
122 if ( $readStage === SCHEMA_COMPAT_READ_TEMP && !( $writeStage & SCHEMA_COMPAT_WRITE_TEMP ) ) {
123 throw new InvalidArgumentException( 'Cannot read the temp schema without also writing it' );
124 }
125 if ( $readStage === SCHEMA_COMPAT_READ_NEW && !( $writeStage & SCHEMA_COMPAT_WRITE_NEW ) ) {
126 throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' );
127 }
128 $this->readStage = $readStage;
129 $this->writeStage = $writeStage;
130
131 $this->actorStoreFactory = $actorStoreFactory;
132 }
133
142 protected function getFieldInfo( $key ) {
143 if ( isset( $this->fieldInfos[$key] ) ) {
144 return $this->fieldInfos[$key];
145 } elseif ( $this->allowUnknown ) {
146 return [];
147 } else {
148 throw new InvalidArgumentException( $this->getInstanceName() . ": unknown key $key" );
149 }
150 }
151
160 protected function getInstanceName() {
161 if ( ( new ReflectionClass( $this ) )->isAnonymous() ) {
162 // Mostly for PHPUnit
163 return self::class;
164 } else {
165 return static::class;
166 }
167 }
168
176 protected function checkDeprecation( $key ) {
177 $fieldInfo = $this->getFieldInfo( $key );
178 if ( isset( $fieldInfo['removedVersion'] ) ) {
179 $removedVersion = $fieldInfo['removedVersion'];
180 $component = $fieldInfo['component'] ?? 'MediaWiki';
181 throw new InvalidArgumentException(
182 "Use of {$this->getInstanceName()} for '$key' was removed in $component $removedVersion"
183 );
184 }
185 if ( isset( $fieldInfo['deprecatedVersion'] ) ) {
186 $deprecatedVersion = $fieldInfo['deprecatedVersion'];
187 $component = $fieldInfo['component'] ?? 'MediaWiki';
188 wfDeprecated( "{$this->getInstanceName()} for '$key'", $deprecatedVersion, $component, 3 );
189 }
190 }
191
197 public function isAnon( $field ) {
198 return ( $this->readStage >= SCHEMA_COMPAT_READ_TEMP ) ? "$field IS NULL" : "$field = 0";
199 }
200
206 public function isNotAnon( $field ) {
207 return ( $this->readStage >= SCHEMA_COMPAT_READ_TEMP ) ? "$field IS NOT NULL" : "$field != 0";
208 }
209
215 private function getFieldNames( $key ) {
216 $fieldInfo = $this->getFieldInfo( $key );
217 $textField = $fieldInfo['textField'] ?? $key . '_text';
218 $actorField = $fieldInfo['actorField'] ?? substr( $key, 0, -5 ) . '_actor';
219 return [ $textField, $actorField ];
220 }
221
228 private function getTempTableInfo( $key ) {
229 $fieldInfo = $this->getFieldInfo( $key );
230 return $fieldInfo['tempTable'] ?? null;
231 }
232
245 public function getJoin( $key ) {
246 $this->checkDeprecation( $key );
247
248 if ( !isset( $this->joinCache[$key] ) ) {
249 $tables = [];
250 $fields = [];
251 $joins = [];
252
253 list( $text, $actor ) = $this->getFieldNames( $key );
254
255 if ( $this->readStage === SCHEMA_COMPAT_READ_OLD ) {
256 $fields[$key] = $key;
257 $fields[$text] = $text;
258 $fields[$actor] = 'NULL';
259 } elseif ( $this->readStage === SCHEMA_COMPAT_READ_TEMP ) {
260 $tempTableInfo = $this->getTempTableInfo( $key );
261 if ( $tempTableInfo ) {
262 $alias = "temp_$key";
263 $tables[$alias] = $tempTableInfo['table'];
264 $joins[$alias] = [ 'JOIN',
265 "{$alias}.{$tempTableInfo['pk']} = {$tempTableInfo['joinPK']}" ];
266 $joinField = "{$alias}.{$tempTableInfo['field']}";
267 } else {
268 $joinField = $actor;
269 }
270
271 $alias = "actor_$key";
272 $tables[$alias] = 'actor';
273 $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$joinField}" ];
274
275 $fields[$key] = "{$alias}.actor_user";
276 $fields[$text] = "{$alias}.actor_name";
277 $fields[$actor] = $joinField;
278 } else /* SCHEMA_COMPAT_READ_NEW */ {
279 $alias = "actor_$key";
280 $tables[$alias] = 'actor';
281 $joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$actor}" ];
282
283 $fields[$key] = "{$alias}.actor_user";
284 $fields[$text] = "{$alias}.actor_name";
285 $fields[$actor] = $actor;
286 }
287
288 $this->joinCache[$key] = [
289 'tables' => $tables,
290 'fields' => $fields,
291 'joins' => $joins,
292 ];
293 }
294
295 return $this->joinCache[$key];
296 }
297
307 public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) {
308 $this->checkDeprecation( $key );
309
310 if ( $this->getTempTableInfo( $key ) ) {
311 throw new InvalidArgumentException( "Must use getInsertValuesWithTempTable() for $key" );
312 }
313
314 list( $text, $actor ) = $this->getFieldNames( $key );
315 $ret = [];
316 if ( $this->writeStage & SCHEMA_COMPAT_WRITE_OLD ) {
317 $ret[$key] = $user->getId();
318 $ret[$text] = $user->getName();
319 }
320 if ( $this->writeStage & SCHEMA_COMPAT_WRITE_TEMP
321 || $this->writeStage & SCHEMA_COMPAT_WRITE_NEW
322 ) {
323 $ret[$actor] = $this->actorStoreFactory
324 ->getActorNormalization( $dbw->getDomainID() )
325 ->acquireActorId( $user, $dbw );
326 }
327 return $ret;
328 }
329
342 public function getInsertValuesWithTempTable( IDatabase $dbw, $key, UserIdentity $user ) {
343 $this->checkDeprecation( $key );
344
345 $fieldInfo = $this->getFieldInfo( $key );
346 $tempTableInfo = $fieldInfo['tempTable'] ?? null;
347 if ( isset( $fieldInfo['formerTempTableVersion'] ) ) {
348 wfDeprecated( __METHOD__ . " for $key",
349 $fieldInfo['formerTempTableVersion'],
350 $fieldInfo['component'] ?? 'MediaWiki' );
351 } elseif ( !$tempTableInfo ) {
352 throw new InvalidArgumentException( "Must use getInsertValues() for $key" );
353 }
354
355 list( $text, $actor ) = $this->getFieldNames( $key );
356 $ret = [];
357 $callback = null;
358
359 if ( $this->writeStage & SCHEMA_COMPAT_WRITE_OLD ) {
360 $ret[$key] = $user->getId();
361 $ret[$text] = $user->getName();
362 }
363 if ( $this->writeStage & ( SCHEMA_COMPAT_WRITE_TEMP | SCHEMA_COMPAT_WRITE_NEW ) ) {
364 $id = $this->actorStoreFactory
365 ->getActorNormalization( $dbw->getDomainID() )
366 ->acquireActorId( $user, $dbw );
367
368 if ( $tempTableInfo ) {
369 if ( $this->writeStage & SCHEMA_COMPAT_WRITE_TEMP ) {
370 $func = __METHOD__;
371 $callback = static function ( $pk, array $extra ) use ( $tempTableInfo, $dbw, $id, $func ) {
372 $set = [ $tempTableInfo['field'] => $id ];
373 foreach ( $tempTableInfo['extra'] as $to => $from ) {
374 if ( !array_key_exists( $from, $extra ) ) {
375 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
376 }
377 $set[$to] = $extra[$from];
378 }
379 $dbw->upsert(
380 $tempTableInfo['table'],
381 [ $tempTableInfo['pk'] => $pk ] + $set,
382 [ [ $tempTableInfo['pk'] ] ],
383 $set,
384 $func
385 );
386 };
387 }
388 if ( $this->writeStage & SCHEMA_COMPAT_WRITE_NEW ) {
389 $ret[$actor] = $id;
390 }
391 } else {
392 $ret[$actor] = $id;
393 }
394 }
395
396 if ( $callback === null ) {
397 // Make a validation-only callback if there was temp table info
398 if ( $tempTableInfo ) {
399 $func = __METHOD__;
400 $callback = static function ( $pk, array $extra ) use ( $tempTableInfo, $func ) {
401 foreach ( $tempTableInfo['extra'] as $to => $from ) {
402 if ( !array_key_exists( $from, $extra ) ) {
403 throw new InvalidArgumentException( "$func callback: \$extra[$from] is not provided" );
404 }
405 }
406 };
407 } else {
408 $callback = static function ( $pk, array $extra ) {
409 };
410 }
411 }
412 return [ $ret, $callback ];
413 }
414
439 public function getWhere( IDatabase $db, $key, $users, $useId = true ) {
440 $this->checkDeprecation( $key );
441
442 $tables = [];
443 $conds = [];
444 $joins = [];
445
446 if ( $users instanceof UserIdentity ) {
447 $users = [ $users ];
448 } elseif ( $users === null || $users === false ) {
449 // DWIM
450 $users = [];
451 } elseif ( !is_array( $users ) ) {
452 $what = is_object( $users ) ? get_class( $users ) : gettype( $users );
453 throw new InvalidArgumentException(
454 __METHOD__ . ": Value for \$users must be a UserIdentity or array, got $what"
455 );
456 }
457
458 // Get information about all the passed users
459 $ids = [];
460 $names = [];
461 $actors = [];
462 foreach ( $users as $user ) {
463 if ( $useId && $user->isRegistered() ) {
464 $ids[] = $user->getId();
465 } else {
466 // make sure to use normalized form of IP for anonymous users
467 $names[] = IPUtils::sanitizeIP( $user->getName() );
468 }
469 $actorId = $this->actorStoreFactory
470 ->getActorNormalization( $db->getDomainID() )
471 ->findActorId( $user, $db );
472
473 if ( $actorId ) {
474 $actors[] = $actorId;
475 }
476 }
477
478 list( $text, $actor ) = $this->getFieldNames( $key );
479
480 // Combine data into conditions to be ORed together
481 if ( $this->readStage === SCHEMA_COMPAT_READ_NEW ) {
482 if ( $actors ) {
483 $conds['newactor'] = $db->makeList( [ $actor => $actors ], IDatabase::LIST_AND );
484 }
485 } elseif ( $this->readStage === SCHEMA_COMPAT_READ_TEMP ) {
486 if ( $actors ) {
487 $tempTableInfo = $this->getTempTableInfo( $key );
488 if ( $tempTableInfo ) {
489 $alias = "temp_$key";
490 $tables[$alias] = $tempTableInfo['table'];
491 $joins[$alias] = [ 'JOIN',
492 "{$alias}.{$tempTableInfo['pk']} = {$tempTableInfo['joinPK']}" ];
493 $joinField = "{$alias}.{$tempTableInfo['field']}";
494 } else {
495 $joinField = $actor;
496 }
497 $conds['actor'] = $db->makeList( [ $joinField => $actors ], IDatabase::LIST_AND );
498 }
499 } else {
500 if ( $ids ) {
501 $conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND );
502 }
503 if ( $names ) {
504 $conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND );
505 }
506 }
507
508 return [
509 'tables' => $tables,
510 'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0',
511 'orconds' => $conds,
512 'joins' => $joins,
513 ];
514 }
515}
const SCHEMA_COMPAT_WRITE_TEMP
Definition Defines.php:267
const SCHEMA_COMPAT_READ_NEW
Definition Defines.php:270
const SCHEMA_COMPAT_WRITE_OLD
Definition Defines.php:265
const SCHEMA_COMPAT_READ_TEMP
Definition Defines.php:268
const SCHEMA_COMPAT_READ_OLD
Definition Defines.php:266
const SCHEMA_COMPAT_WRITE_NEW
Definition Defines.php:269
const SCHEMA_COMPAT_WRITE_MASK
Definition Defines.php:271
const SCHEMA_COMPAT_READ_MASK
Definition Defines.php:272
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
This abstract base class helps migrate core and extension code to use the actor table.
getJoin( $key)
Get SELECT fields and joins for the actor key.
getInstanceName()
Get a name for this instance to use in error messages.
checkDeprecation( $key)
Issue deprecation warning/error as appropriate.
getInsertValues(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
isNotAnon( $field)
Return an SQL condition to test if a user field is non-anonymous.
getWhere(IDatabase $db, $key, $users, $useId=true)
Get WHERE condition for the actor.
__construct( $fieldInfos, $stage, ActorStoreFactory $actorStoreFactory, $options=[])
getInsertValuesWithTempTable(IDatabase $dbw, $key, UserIdentity $user)
Get UPDATE fields for the actor.
getFieldInfo( $key)
Get config information about a field.
isAnon( $field)
Return an SQL condition to test if a user field is anonymous.
Interface for objects representing user identity.
getId( $wikiId=self::LOCAL)
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
getDomainID()
Return the currently selected domain ID.
upsert( $table, array $rows, $uniqueKeys, array $set, $fname=__METHOD__)
Upsert row(s) into a table, in the provided order, while updating conflicting rows.
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.