Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / user / UserArray.php
blobbed961951908b4d3574ebadc29a4d211dbd49617
1 <?php
2 /**
3 * Class to walk into a list of User objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\User;
25 use ArrayIterator;
26 use Iterator;
27 use MediaWiki\HookContainer\HookRunner;
28 use MediaWiki\MediaWikiServices;
29 use Wikimedia\Rdbms\IResultWrapper;
31 abstract class UserArray implements Iterator {
32 /**
33 * @note Try to avoid in new code, in case getting UserIdentity batch is enough,
34 * use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}.
35 * In case you need full User objects, you can keep using this method, but it's
36 * moving towards deprecation.
38 * @param IResultWrapper $res
39 * @return UserArrayFromResult|ArrayIterator
41 public static function newFromResult( $res ) {
42 $userArray = null;
43 $hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
44 if ( !$hookRunner->onUserArrayFromResult( $userArray, $res ) ) {
45 return new ArrayIterator( [] );
47 return $userArray ?? new UserArrayFromResult( $res );
50 /**
51 * @note Try to avoid in new code, in case getting UserIdentity batch is enough,
52 * use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}.
53 * In case you need full User objects, you can keep using this method, but it's
54 * moving towards deprecation.
56 * @param array $ids
57 * @return UserArrayFromResult|ArrayIterator
59 public static function newFromIDs( $ids ) {
60 $ids = array_map( 'intval', (array)$ids ); // paranoia
61 if ( !$ids ) {
62 // Database::select() doesn't like empty arrays
63 return new ArrayIterator( [] );
65 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
66 $res = User::newQueryBuilder( $dbr )
67 ->where( [ 'user_id' => array_unique( $ids ) ] )
68 ->caller( __METHOD__ )
69 ->fetchResultSet();
70 return self::newFromResult( $res );
73 /**
74 * @note Try to avoid in new code, in case getting UserIdentity batch is enough,
75 * use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}.
76 * In case you need full User objects, you can keep using this method, but it's
77 * moving towards deprecation.
79 * @since 1.25
80 * @param array $names
81 * @return UserArrayFromResult|ArrayIterator
83 public static function newFromNames( $names ) {
84 $names = array_map( 'strval', (array)$names ); // paranoia
85 if ( !$names ) {
86 // Database::select() doesn't like empty arrays
87 return new ArrayIterator( [] );
89 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
90 $res = User::newQueryBuilder( $dbr )
91 ->where( [ 'user_name' => array_unique( $names ) ] )
92 ->caller( __METHOD__ )
93 ->fetchResultSet();
94 return self::newFromResult( $res );
97 /**
98 * @return User
100 abstract public function current(): User;
103 * @return int
105 abstract public function key(): int;
108 /** @deprecated class alias since 1.41 */
109 class_alias( UserArray::class, 'UserArray' );