No more undefined usage of rev{Start,End}Id warnings
[mediawiki.git] / includes / UserArray.php
blobc5ba0b2b350fa8c90643d5ca924c062509a0fead
1 <?php
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;
13 if ( $userArray === null ) {
14 $userArray = self::newFromResult_internal( $res );
16 return $userArray;
19 /**
20 * @param $ids array
21 * @return UserArrayFromResult
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());
29 $dbr = wfGetDB( DB_SLAVE );
30 $res = $dbr->select( 'user', '*', array( 'user_id' => $ids ),
31 __METHOD__ );
32 return self::newFromResult( $res );
35 /**
36 * @param $res
37 * @return UserArrayFromResult
39 protected static function newFromResult_internal( $res ) {
40 return new UserArrayFromResult( $res );
44 class UserArrayFromResult extends UserArray {
46 /**
47 * @var ResultWrapper
49 var $res;
50 var $key, $current;
52 /**
53 * @param $res ResultWrapper
55 function __construct( $res ) {
56 $this->res = $res;
57 $this->key = 0;
58 $this->setCurrent( $this->res->current() );
61 /**
62 * @param $row
63 * @return void
65 protected function setCurrent( $row ) {
66 if ( $row === false ) {
67 $this->current = false;
68 } else {
69 $this->current = User::newFromRow( $row );
73 /**
74 * @return int
76 public function count() {
77 return $this->res->numRows();
80 /**
81 * @return User
83 function current() {
84 return $this->current;
87 function key() {
88 return $this->key;
91 function next() {
92 $row = $this->res->next();
93 $this->setCurrent( $row );
94 $this->key++;
97 function rewind() {
98 $this->res->rewind();
99 $this->key = 0;
100 $this->setCurrent( $this->res->current() );
104 * @return bool
106 function valid() {
107 return $this->current !== false;