UserArray.php 1.6 KB

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