| 1 | <?php |
| 2 | /** |
| 3 | * Class to walk into a list of User objects. |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 19 | * |
| 20 | * @file |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\User; |
| 24 | |
| 25 | use ArrayIterator; |
| 26 | use Iterator; |
| 27 | use MediaWiki\HookContainer\HookRunner; |
| 28 | use MediaWiki\MediaWikiServices; |
| 29 | use Wikimedia\Rdbms\IResultWrapper; |
| 30 | |
| 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. |
| 37 | * |
| 38 | * @param IResultWrapper $res |
| 39 | * @return UserArrayFromResult|ArrayIterator |
| 40 | */ |
| 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( [] ); |
| 46 | } |
| 47 | return $userArray ?? new UserArrayFromResult( $res ); |
| 48 | } |
| 49 | |
| 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. |
| 55 | * |
| 56 | * @param array $ids |
| 57 | * @return UserArrayFromResult|ArrayIterator |
| 58 | */ |
| 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( [] ); |
| 64 | } |
| 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 ); |
| 71 | } |
| 72 | |
| 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. |
| 78 | * |
| 79 | * @since 1.25 |
| 80 | * @param array $names |
| 81 | * @return UserArrayFromResult|ArrayIterator |
| 82 | */ |
| 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( [] ); |
| 88 | } |
| 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 ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @return User |
| 99 | */ |
| 100 | abstract public function current(): User; |
| 101 | |
| 102 | /** |
| 103 | * @return int |
| 104 | */ |
| 105 | abstract public function key(): int; |
| 106 | } |
| 107 | |
| 108 | /** @deprecated class alias since 1.41 */ |
| 109 | class_alias( UserArray::class, 'UserArray' ); |