89d93744c5bd9f5582b3627e6c0375f5aa67b168
[lhc/web/wiklou.git] / includes / UserArray.php
1 <?php
2
3 abstract class UserArray implements Iterator {
4 /**
5 * @param $res ResultWrapper
6 * @return UserArrayFromResult
7 */
8 static function newFromResult( $res ) {
9 $userArray = null;
10 if ( !wfRunHooks( 'UserArrayFromResult', array( &$userArray, $res ) ) ) {
11 return null;
12 }
13 if ( $userArray === null ) {
14 $userArray = self::newFromResult_internal( $res );
15 }
16 return $userArray;
17 }
18
19 /**
20 * @param $ids array
21 * @return UserArrayFromResult
22 */
23 static function newFromIDs( $ids ) {
24 $ids = array_map( 'intval', (array)$ids ); // paranoia
25 if ( !$ids ) {
26 // Database::select() doesn't like empty arrays
27 return new ArrayIterator(array());
28 }
29 $dbr = wfGetDB( DB_SLAVE );
30 $res = $dbr->select( 'user', '*', array( 'user_id' => $ids ),
31 __METHOD__ );
32 return self::newFromResult( $res );
33 }
34
35 /**
36 * @param $res
37 * @return UserArrayFromResult
38 */
39 protected static function newFromResult_internal( $res ) {
40 return new UserArrayFromResult( $res );
41 }
42 }
43
44 class UserArrayFromResult extends UserArray {
45
46 /**
47 * @var ResultWrapper
48 */
49 var $res;
50 var $key, $current;
51
52 /**
53 * @param $res ResultWrapper
54 */
55 function __construct( $res ) {
56 $this->res = $res;
57 $this->key = 0;
58 $this->setCurrent( $this->res->current() );
59 }
60
61 /**
62 * @param $row
63 */
64 protected function setCurrent( $row ) {
65 if ( $row === false ) {
66 $this->current = false;
67 } else {
68 $this->current = User::newFromRow( $row );
69 }
70 }
71
72 /**
73 * @return int
74 */
75 public function count() {
76 return $this->res->numRows();
77 }
78
79 /**
80 * @return User
81 */
82 function current() {
83 return $this->current;
84 }
85
86 function key() {
87 return $this->key;
88 }
89
90 function next() {
91 $row = $this->res->next();
92 $this->setCurrent( $row );
93 $this->key++;
94 }
95
96 function rewind() {
97 $this->res->rewind();
98 $this->key = 0;
99 $this->setCurrent( $this->res->current() );
100 }
101
102 /**
103 * @return bool
104 */
105 function valid() {
106 return $this->current !== false;
107 }
108 }