Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UserArray | |
0.00% |
0 / 23 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| newFromResult | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| newFromIDs | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| newFromNames | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| count | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| current | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| key | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\User; |
| 8 | |
| 9 | use Countable; |
| 10 | use Iterator; |
| 11 | use MediaWiki\HookContainer\HookRunner; |
| 12 | use MediaWiki\MediaWikiServices; |
| 13 | use Wikimedia\Rdbms\FakeResultWrapper; |
| 14 | use Wikimedia\Rdbms\IResultWrapper; |
| 15 | |
| 16 | /** |
| 17 | * Class to walk into a list of User objects. |
| 18 | */ |
| 19 | abstract class UserArray implements Iterator, Countable { |
| 20 | /** |
| 21 | * @note Try to avoid in new code, in case getting UserIdentity batch is enough, |
| 22 | * use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}. |
| 23 | * In case you need full User objects, you can keep using this method, but it's |
| 24 | * moving towards deprecation. |
| 25 | * |
| 26 | * @param IResultWrapper $res |
| 27 | * @return self |
| 28 | */ |
| 29 | public static function newFromResult( $res ): self { |
| 30 | $userArray = null; |
| 31 | $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); |
| 32 | if ( !$hookRunner->onUserArrayFromResult( $userArray, $res ) ) { |
| 33 | return new UserArrayFromResult( new FakeResultWrapper( [] ) ); |
| 34 | } |
| 35 | return $userArray ?? new UserArrayFromResult( $res ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @note Try to avoid in new code, in case getting UserIdentity batch is enough, |
| 40 | * use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}. |
| 41 | * In case you need full User objects, you can keep using this method, but it's |
| 42 | * moving towards deprecation. |
| 43 | * |
| 44 | * @param int[] $ids |
| 45 | * @return self |
| 46 | */ |
| 47 | public static function newFromIDs( array $ids ): self { |
| 48 | $ids = array_map( 'intval', $ids ); // paranoia |
| 49 | if ( !$ids ) { |
| 50 | // Database::select() doesn't like empty arrays |
| 51 | return new UserArrayFromResult( new FakeResultWrapper( [] ) ); |
| 52 | } |
| 53 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 54 | $res = User::newQueryBuilder( $dbr ) |
| 55 | ->where( [ 'user_id' => array_unique( $ids ) ] ) |
| 56 | ->caller( __METHOD__ ) |
| 57 | ->fetchResultSet(); |
| 58 | return self::newFromResult( $res ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @note Try to avoid in new code, in case getting UserIdentity batch is enough, |
| 63 | * use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}. |
| 64 | * In case you need full User objects, you can keep using this method, but it's |
| 65 | * moving towards deprecation. |
| 66 | * |
| 67 | * @since 1.25 |
| 68 | * @param string[] $names |
| 69 | * @return self |
| 70 | */ |
| 71 | public static function newFromNames( array $names ): self { |
| 72 | $names = array_map( 'strval', $names ); // paranoia |
| 73 | if ( !$names ) { |
| 74 | // Database::select() doesn't like empty arrays |
| 75 | return new UserArrayFromResult( new FakeResultWrapper( [] ) ); |
| 76 | } |
| 77 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 78 | $res = User::newQueryBuilder( $dbr ) |
| 79 | ->where( [ 'user_name' => array_unique( $names ) ] ) |
| 80 | ->caller( __METHOD__ ) |
| 81 | ->fetchResultSet(); |
| 82 | return self::newFromResult( $res ); |
| 83 | } |
| 84 | |
| 85 | abstract public function count(): int; |
| 86 | |
| 87 | abstract public function current(): User; |
| 88 | |
| 89 | abstract public function key(): int; |
| 90 | } |
| 91 | |
| 92 | /** @deprecated class alias since 1.41 */ |
| 93 | class_alias( UserArray::class, 'UserArray' ); |