2008-07-29 14:53:10 +00:00
|
|
|
<?php
|
2012-05-09 17:55:56 +00:00
|
|
|
/**
|
2014-01-26 17:49:18 +00:00
|
|
|
* Class to walk into a list of User objects.
|
2012-05-09 17:55:56 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2008-07-29 14:53:10 +00:00
|
|
|
|
2023-05-06 20:01:10 +00:00
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-04-05 22:06:35 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
2017-02-19 05:03:13 +00:00
|
|
|
|
2008-07-29 14:53:10 +00:00
|
|
|
abstract class UserArray implements Iterator {
|
2011-05-21 19:36:03 +00:00
|
|
|
/**
|
2021-02-10 21:56:38 +00:00
|
|
|
* @note Try to avoid in new code, in case getting UserIdentity batch is enough,
|
|
|
|
|
* use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}.
|
|
|
|
|
* In case you need full User objects, you can keep using this method, but it's
|
|
|
|
|
* moving towards deprecation.
|
|
|
|
|
*
|
2018-04-05 22:06:35 +00:00
|
|
|
* @param IResultWrapper $res
|
2021-09-20 21:29:30 +00:00
|
|
|
* @return UserArrayFromResult|ArrayIterator
|
2011-05-21 19:36:03 +00:00
|
|
|
*/
|
2020-05-17 22:39:57 +00:00
|
|
|
public static function newFromResult( $res ) {
|
2008-07-29 14:53:10 +00:00
|
|
|
$userArray = null;
|
2023-05-06 20:01:10 +00:00
|
|
|
$hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() );
|
|
|
|
|
if ( !$hookRunner->onUserArrayFromResult( $userArray, $res ) ) {
|
2021-09-20 21:29:30 +00:00
|
|
|
return new ArrayIterator( [] );
|
2008-07-29 14:53:10 +00:00
|
|
|
}
|
2019-05-28 14:07:50 +00:00
|
|
|
return $userArray ?? new UserArrayFromResult( $res );
|
2008-07-29 14:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-21 19:36:03 +00:00
|
|
|
/**
|
2021-02-10 21:56:38 +00:00
|
|
|
* @note Try to avoid in new code, in case getting UserIdentity batch is enough,
|
|
|
|
|
* use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}.
|
|
|
|
|
* In case you need full User objects, you can keep using this method, but it's
|
|
|
|
|
* moving towards deprecation.
|
|
|
|
|
*
|
2014-04-10 18:50:10 +00:00
|
|
|
* @param array $ids
|
2017-02-19 05:03:13 +00:00
|
|
|
* @return UserArrayFromResult|ArrayIterator
|
2011-05-21 19:36:03 +00:00
|
|
|
*/
|
2020-05-17 22:39:57 +00:00
|
|
|
public static function newFromIDs( $ids ) {
|
2009-03-02 12:15:28 +00:00
|
|
|
$ids = array_map( 'intval', (array)$ids ); // paranoia
|
2011-05-21 19:36:03 +00:00
|
|
|
if ( !$ids ) {
|
2009-03-02 12:15:28 +00:00
|
|
|
// Database::select() doesn't like empty arrays
|
2016-02-17 09:09:32 +00:00
|
|
|
return new ArrayIterator( [] );
|
2011-05-21 19:36:03 +00:00
|
|
|
}
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2023-06-26 06:43:41 +00:00
|
|
|
$res = User::newQueryBuilder( $dbr )
|
|
|
|
|
->where( [ 'user_id' => array_unique( $ids ) ] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchResultSet();
|
2009-03-02 12:15:28 +00:00
|
|
|
return self::newFromResult( $res );
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 20:46:13 +00:00
|
|
|
/**
|
2021-02-10 21:56:38 +00:00
|
|
|
* @note Try to avoid in new code, in case getting UserIdentity batch is enough,
|
|
|
|
|
* use {@link \MediaWiki\User\UserIdentityLookup::newSelectQueryBuilder()}.
|
|
|
|
|
* In case you need full User objects, you can keep using this method, but it's
|
|
|
|
|
* moving towards deprecation.
|
|
|
|
|
*
|
2015-02-24 20:46:13 +00:00
|
|
|
* @since 1.25
|
|
|
|
|
* @param array $names
|
2017-02-19 05:03:13 +00:00
|
|
|
* @return UserArrayFromResult|ArrayIterator
|
2015-02-24 20:46:13 +00:00
|
|
|
*/
|
2020-05-17 22:39:57 +00:00
|
|
|
public static function newFromNames( $names ) {
|
2015-02-24 20:46:13 +00:00
|
|
|
$names = array_map( 'strval', (array)$names ); // paranoia
|
|
|
|
|
if ( !$names ) {
|
|
|
|
|
// Database::select() doesn't like empty arrays
|
2016-02-17 09:09:32 +00:00
|
|
|
return new ArrayIterator( [] );
|
2015-02-24 20:46:13 +00:00
|
|
|
}
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2023-06-26 06:43:41 +00:00
|
|
|
$res = User::newQueryBuilder( $dbr )
|
|
|
|
|
->where( [ 'user_name' => array_unique( $names ) ] )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->fetchResultSet();
|
2015-02-24 20:46:13 +00:00
|
|
|
return self::newFromResult( $res );
|
|
|
|
|
}
|
2021-08-30 20:08:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return User
|
|
|
|
|
*/
|
|
|
|
|
abstract public function current(): User;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
abstract public function key(): int;
|
2014-03-20 18:59:20 +00:00
|
|
|
}
|