Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.88% |
29 / 33 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| UserCondition | |
87.88% |
29 / 33 |
|
50.00% |
3 / 6 |
16.46 | |
0.00% |
0 / 1 |
| validateValue | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| evaluate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| prepareCapture | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| prepareConds | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
7.08 | |||
| makeSet | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| makeNames | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\RecentChanges\ChangesListQuery; |
| 4 | |
| 5 | use MediaWiki\User\UserIdentity; |
| 6 | use MediaWiki\User\UserIdentityValue; |
| 7 | use stdClass; |
| 8 | use Wikimedia\Rdbms\IReadableDatabase; |
| 9 | |
| 10 | class UserCondition extends ChangesListConditionBase { |
| 11 | /** @inheritDoc */ |
| 12 | public function validateValue( $value ) { |
| 13 | if ( $value instanceof UserIdentity ) { |
| 14 | // Convert to UserIdentityValue since that is stringable and so will |
| 15 | // work with array_unique() etc. |
| 16 | return new UserIdentityValue( $value->getId(), $value->getName() ); |
| 17 | } else { |
| 18 | throw new \InvalidArgumentException( 'user filter value must be a UserIdentity' ); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @param stdClass $row |
| 24 | * @param UserIdentityValue $value |
| 25 | * @return bool |
| 26 | */ |
| 27 | public function evaluate( stdClass $row, $value ): bool { |
| 28 | return $row->rc_user_text === $value->getName(); |
| 29 | } |
| 30 | |
| 31 | protected function prepareCapture( IReadableDatabase $dbr, QueryBackend $query ) { |
| 32 | $query->rcUserFields(); |
| 33 | } |
| 34 | |
| 35 | protected function prepareConds( IReadableDatabase $dbr, QueryBackend $query ) { |
| 36 | [ $required, $excluded ] = $this->getUniqueValues(); |
| 37 | if ( $required === [] ) { |
| 38 | $query->forceEmptySet(); |
| 39 | } elseif ( $required ) { |
| 40 | [ $ids, $names ] = $this->makeSet( $required ); |
| 41 | $orConds = []; |
| 42 | if ( $ids ) { |
| 43 | $orConds[] = $dbr->expr( 'actor_user', '=', $ids ); |
| 44 | } |
| 45 | if ( $names ) { |
| 46 | $orConds[] = $dbr->expr( 'actor_name', '=', $names ); |
| 47 | } |
| 48 | $query->where( count( $orConds ) > 1 ? $dbr->orExpr( $orConds ) : $orConds[0] ); |
| 49 | $query->joinForConds( 'actor' )->reorderable(); |
| 50 | $query->adjustDensity( QueryBackend::DENSITY_USER ); |
| 51 | } elseif ( $excluded ) { |
| 52 | $names = $this->makeNames( $excluded ); |
| 53 | $query->where( $dbr->expr( 'actor_name', '!=', $names ) ); |
| 54 | $query->joinForConds( 'actor' )->straight(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param UserIdentityValue[] $users |
| 60 | * @return array{int[],string[]} |
| 61 | */ |
| 62 | private function makeSet( $users ) { |
| 63 | $ids = []; |
| 64 | $names = []; |
| 65 | foreach ( $users as $user ) { |
| 66 | if ( $user->isRegistered() ) { |
| 67 | $ids[] = $user->getId(); |
| 68 | } else { |
| 69 | $names[] = $user->getName(); |
| 70 | } |
| 71 | } |
| 72 | return [ $ids, $names ]; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param UserIdentityValue[] $users |
| 77 | * @return string[] |
| 78 | */ |
| 79 | private function makeNames( $users ) { |
| 80 | $names = []; |
| 81 | foreach ( $users as $user ) { |
| 82 | $names[] = $user->getName(); |
| 83 | } |
| 84 | return $names; |
| 85 | } |
| 86 | } |