| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use LogicException; |
| 27 |
use ReflectionClass; |
| 28 |
use Wikimedia\IPUtils; |
| 29 |
use Wikimedia\Rdbms\IDatabase; |
| 30 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ActorMigrationBase { |
| 40 |
|
| 41 |
private $joinCache = []; |
| 42 |
|
| 43 |
|
| 44 |
private $readStage; |
| 45 |
|
| 46 |
|
| 47 |
private $writeStage; |
| 48 |
|
| 49 |
protected ActorStoreFactory $actorStoreFactory; |
| 50 |
|
| 51 |
|
| 52 |
private $fieldInfos; |
| 53 |
|
| 54 |
private bool $allowUnknown; |
| 55 |
|
| 56 |
private bool $forImport = false; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function __construct( |
| 84 |
$fieldInfos, |
| 85 |
$stage, |
| 86 |
ActorStoreFactory $actorStoreFactory, |
| 87 |
$options = [] |
| 88 |
) { |
| 89 |
$this->fieldInfos = $fieldInfos; |
| 90 |
$this->allowUnknown = $options['allowUnknown'] ?? true; |
| 91 |
|
| 92 |
$writeStage = $stage & SCHEMA_COMPAT_WRITE_MASK; |
| 93 |
$readStage = $stage & SCHEMA_COMPAT_READ_MASK; |
| 94 |
if ( $writeStage === 0 ) { |
| 95 |
throw new InvalidArgumentException( '$stage must include a write mode' ); |
| 96 |
} |
| 97 |
if ( $readStage === 0 ) { |
| 98 |
throw new InvalidArgumentException( '$stage must include a read mode' ); |
| 99 |
} |
| 100 |
if ( !in_array( $readStage, [ SCHEMA_COMPAT_READ_OLD, SCHEMA_COMPAT_READ_NEW ] ) ) { |
| 101 |
throw new InvalidArgumentException( 'Cannot read multiple schemas' ); |
| 102 |
} |
| 103 |
if ( $readStage === SCHEMA_COMPAT_READ_OLD && !( $writeStage & SCHEMA_COMPAT_WRITE_OLD ) ) { |
| 104 |
throw new InvalidArgumentException( 'Cannot read the old schema without also writing it' ); |
| 105 |
} |
| 106 |
if ( $readStage === SCHEMA_COMPAT_READ_NEW && !( $writeStage & SCHEMA_COMPAT_WRITE_NEW ) ) { |
| 107 |
throw new InvalidArgumentException( 'Cannot read the new schema without also writing it' ); |
| 108 |
} |
| 109 |
$this->readStage = $readStage; |
| 110 |
$this->writeStage = $writeStage; |
| 111 |
|
| 112 |
$this->actorStoreFactory = $actorStoreFactory; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static function newMigrationForImport() { |
| 120 |
throw new LogicException( __METHOD__ . " must be overridden" ); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
protected function getFieldInfo( $key ) { |
| 132 |
if ( isset( $this->fieldInfos[$key] ) ) { |
| 133 |
return $this->fieldInfos[$key]; |
| 134 |
} elseif ( $this->allowUnknown ) { |
| 135 |
return []; |
| 136 |
} else { |
| 137 |
throw new InvalidArgumentException( $this->getInstanceName() . ": unknown key $key" ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
protected function getInstanceName() { |
| 150 |
if ( ( new ReflectionClass( $this ) )->isAnonymous() ) { |
| 151 |
|
| 152 |
return self::class; |
| 153 |
} else { |
| 154 |
return static::class; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected function checkDeprecation( $key ) { |
| 166 |
$fieldInfo = $this->getFieldInfo( $key ); |
| 167 |
if ( isset( $fieldInfo['removedVersion'] ) ) { |
| 168 |
$removedVersion = $fieldInfo['removedVersion']; |
| 169 |
$component = $fieldInfo['component'] ?? 'MediaWiki'; |
| 170 |
throw new InvalidArgumentException( |
| 171 |
"Use of {$this->getInstanceName()} for '$key' was removed in $component $removedVersion" |
| 172 |
); |
| 173 |
} |
| 174 |
if ( isset( $fieldInfo['deprecatedVersion'] ) ) { |
| 175 |
$deprecatedVersion = $fieldInfo['deprecatedVersion']; |
| 176 |
$component = $fieldInfo['component'] ?? 'MediaWiki'; |
| 177 |
wfDeprecated( "{$this->getInstanceName()} for '$key'", $deprecatedVersion, $component, 3 ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function isAnon( $field ) { |
| 187 |
return ( $this->readStage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NULL" : "$field = 0"; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function isNotAnon( $field ) { |
| 196 |
return ( $this->readStage & SCHEMA_COMPAT_READ_NEW ) ? "$field IS NOT NULL" : "$field != 0"; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private function getFieldNames( $key ) { |
| 205 |
$fieldInfo = $this->getFieldInfo( $key ); |
| 206 |
$textField = $fieldInfo['textField'] ?? $key . '_text'; |
| 207 |
$actorField = $fieldInfo['actorField'] ?? substr( $key, 0, -5 ) . '_actor'; |
| 208 |
return [ $textField, $actorField ]; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function getJoin( $key ) { |
| 224 |
$this->checkDeprecation( $key ); |
| 225 |
|
| 226 |
if ( !isset( $this->joinCache[$key] ) ) { |
| 227 |
$tables = []; |
| 228 |
$fields = []; |
| 229 |
$joins = []; |
| 230 |
|
| 231 |
[ $text, $actor ] = $this->getFieldNames( $key ); |
| 232 |
|
| 233 |
if ( $this->readStage === SCHEMA_COMPAT_READ_OLD ) { |
| 234 |
$fields[$key] = $key; |
| 235 |
$fields[$text] = $text; |
| 236 |
$fields[$actor] = 'NULL'; |
| 237 |
} else { |
| 238 |
$alias = "actor_$key"; |
| 239 |
$tables[$alias] = 'actor'; |
| 240 |
$joins[$alias] = [ 'JOIN', "{$alias}.actor_id = {$actor}" ]; |
| 241 |
|
| 242 |
$fields[$key] = "{$alias}.actor_user"; |
| 243 |
$fields[$text] = "{$alias}.actor_name"; |
| 244 |
$fields[$actor] = $actor; |
| 245 |
} |
| 246 |
|
| 247 |
$this->joinCache[$key] = [ |
| 248 |
'tables' => $tables, |
| 249 |
'fields' => $fields, |
| 250 |
'joins' => $joins, |
| 251 |
]; |
| 252 |
} |
| 253 |
|
| 254 |
return $this->joinCache[$key]; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function getInsertValues( IDatabase $dbw, $key, UserIdentity $user ) { |
| 267 |
$this->checkDeprecation( $key ); |
| 268 |
|
| 269 |
[ $text, $actor ] = $this->getFieldNames( $key ); |
| 270 |
$ret = []; |
| 271 |
if ( $this->writeStage & SCHEMA_COMPAT_WRITE_OLD ) { |
| 272 |
$ret[$key] = $user->getId(); |
| 273 |
$ret[$text] = $user->getName(); |
| 274 |
} |
| 275 |
if ( $this->writeStage & SCHEMA_COMPAT_WRITE_NEW ) { |
| 276 |
$ret[$actor] = $this->getActorNormalization( $dbw->getDomainID() ) |
| 277 |
->acquireActorId( $user, $dbw ); |
| 278 |
} |
| 279 |
return $ret; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public function getWhere( IReadableDatabase $db, $key, $users, $useId = true ) { |
| 307 |
$this->checkDeprecation( $key ); |
| 308 |
|
| 309 |
$tables = []; |
| 310 |
$conds = []; |
| 311 |
$joins = []; |
| 312 |
|
| 313 |
if ( $users instanceof UserIdentity ) { |
| 314 |
$users = [ $users ]; |
| 315 |
} elseif ( $users === null || $users === false ) { |
| 316 |
|
| 317 |
$users = []; |
| 318 |
} elseif ( !is_array( $users ) ) { |
| 319 |
$what = get_debug_type( $users ); |
| 320 |
throw new InvalidArgumentException( |
| 321 |
__METHOD__ . ": Value for \$users must be a UserIdentity or array, got $what" |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
$ids = []; |
| 327 |
$names = []; |
| 328 |
$actors = []; |
| 329 |
foreach ( $users as $user ) { |
| 330 |
if ( $useId && $user->isRegistered() ) { |
| 331 |
$ids[] = $user->getId(); |
| 332 |
} else { |
| 333 |
|
| 334 |
$names[] = IPUtils::sanitizeIP( $user->getName() ); |
| 335 |
} |
| 336 |
$actorId = $this->getActorNormalization( $db->getDomainID() ) |
| 337 |
->findActorId( $user, $db ); |
| 338 |
|
| 339 |
if ( $actorId ) { |
| 340 |
$actors[] = $actorId; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
[ $text, $actor ] = $this->getFieldNames( $key ); |
| 345 |
|
| 346 |
|
| 347 |
if ( $this->readStage === SCHEMA_COMPAT_READ_NEW ) { |
| 348 |
if ( $actors ) { |
| 349 |
$conds['newactor'] = $db->makeList( [ $actor => $actors ], IDatabase::LIST_AND ); |
| 350 |
} |
| 351 |
} else { |
| 352 |
if ( $ids ) { |
| 353 |
$conds['userid'] = $db->makeList( [ $key => $ids ], IDatabase::LIST_AND ); |
| 354 |
} |
| 355 |
if ( $names ) { |
| 356 |
$conds['username'] = $db->makeList( [ $text => $names ], IDatabase::LIST_AND ); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
return [ |
| 361 |
'tables' => $tables, |
| 362 |
'conds' => $conds ? $db->makeList( array_values( $conds ), IDatabase::LIST_OR ) : '1=0', |
| 363 |
'orconds' => $conds, |
| 364 |
'joins' => $joins, |
| 365 |
]; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public function setForImport( bool $forImport ): void { |
| 373 |
$this->forImport = $forImport; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
protected function getActorNormalization( $domainId ): ActorNormalization { |
| 381 |
if ( $this->forImport ) { |
| 382 |
return $this->actorStoreFactory->getActorNormalizationForImport( $domainId ); |
| 383 |
} else { |
| 384 |
return $this->actorStoreFactory->getActorNormalization( $domainId ); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
class_alias( ActorMigrationBase::class, 'ActorMigrationBase' ); |