TitleArrayFromResult.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Class to walk into a list of Title objects.
  4. *
  5. * Note: this entire file is a byte-for-byte copy of UserArrayFromResult.php
  6. * with s/User/Title/. If anyone can figure out how to do this nicely
  7. * with inheritance or something, please do so.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. use Wikimedia\Rdbms\IResultWrapper;
  27. class TitleArrayFromResult extends TitleArray implements Countable {
  28. /** @var IResultWrapper */
  29. public $res;
  30. public $key;
  31. public $current;
  32. function __construct( $res ) {
  33. $this->res = $res;
  34. $this->key = 0;
  35. $this->setCurrent( $this->res->current() );
  36. }
  37. /**
  38. * @param bool|stdClass $row
  39. * @return void
  40. */
  41. protected function setCurrent( $row ) {
  42. if ( $row === false ) {
  43. $this->current = false;
  44. } else {
  45. $this->current = Title::newFromRow( $row );
  46. }
  47. }
  48. /**
  49. * @return int
  50. */
  51. public function count() {
  52. return $this->res->numRows();
  53. }
  54. function current() {
  55. return $this->current;
  56. }
  57. function key() {
  58. return $this->key;
  59. }
  60. function next() {
  61. $row = $this->res->next();
  62. $this->setCurrent( $row );
  63. $this->key++;
  64. }
  65. function rewind() {
  66. $this->res->rewind();
  67. $this->key = 0;
  68. $this->setCurrent( $this->res->current() );
  69. }
  70. /**
  71. * @return bool
  72. */
  73. function valid() {
  74. return $this->current !== false;
  75. }
  76. }