w/s
[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 * @return void
64 */
65 protected function setCurrent( $row ) {
66 if ( $row === false ) {
67 $this->current = false;
68 } else {
69 $this->current = User::newFromRow( $row );
70 }
71 }
72
73 /**
74 * @return int
75 */
76 public function count() {
77 return $this->res->numRows();
78 }
79
80 /**
81 * @return User
82 */
83 function current() {
84 return $this->current;
85 }
86
87 function key() {
88 return $this->key;
89 }
90
91 function next() {
92 $row = $this->res->next();
93 $this->setCurrent( $row );
94 $this->key++;
95 }
96
97 function rewind() {
98 $this->res->rewind();
99 $this->key = 0;
100 $this->setCurrent( $this->res->current() );
101 }
102
103 /**
104 * @return bool
105 */
106 function valid() {
107 return $this->current !== false;
108 }
109 }