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