Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / user / UserArray.php
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 use Wikimedia\Rdbms\ResultWrapper;
24
25 abstract class UserArray implements Iterator {
26 /**
27 * @param ResultWrapper $res
28 * @return UserArrayFromResult
29 */
30 static function newFromResult( $res ) {
31 $userArray = null;
32 if ( !Hooks::run( 'UserArrayFromResult', [ &$userArray, $res ] ) ) {
33 return null;
34 }
35 if ( $userArray === null ) {
36 $userArray = self::newFromResult_internal( $res );
37 }
38 return $userArray;
39 }
40
41 /**
42 * @param array $ids
43 * @return UserArrayFromResult|ArrayIterator
44 */
45 static function newFromIDs( $ids ) {
46 $ids = array_map( 'intval', (array)$ids ); // paranoia
47 if ( !$ids ) {
48 // Database::select() doesn't like empty arrays
49 return new ArrayIterator( [] );
50 }
51 $dbr = wfGetDB( DB_REPLICA );
52 $userQuery = User::getQueryInfo();
53 $res = $dbr->select(
54 $userQuery['tables'],
55 $userQuery['fields'],
56 [ 'user_id' => array_unique( $ids ) ],
57 __METHOD__,
58 [],
59 $userQuery['joins']
60 );
61 return self::newFromResult( $res );
62 }
63
64 /**
65 * @since 1.25
66 * @param array $names
67 * @return UserArrayFromResult|ArrayIterator
68 */
69 static function newFromNames( $names ) {
70 $names = array_map( 'strval', (array)$names ); // paranoia
71 if ( !$names ) {
72 // Database::select() doesn't like empty arrays
73 return new ArrayIterator( [] );
74 }
75 $dbr = wfGetDB( DB_REPLICA );
76 $userQuery = User::getQueryInfo();
77 $res = $dbr->select(
78 $userQuery['tables'],
79 $userQuery['fields'],
80 [ 'user_name' => array_unique( $names ) ],
81 __METHOD__,
82 [],
83 $userQuery['joins']
84 );
85 return self::newFromResult( $res );
86 }
87
88 /**
89 * @param ResultWrapper $res
90 * @return UserArrayFromResult
91 */
92 protected static function newFromResult_internal( $res ) {
93 return new UserArrayFromResult( $res );
94 }
95 }