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