RevisionListBase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Holders of revision list for a single page
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use Wikimedia\Rdbms\ResultWrapper;
  23. use Wikimedia\Rdbms\IDatabase;
  24. /**
  25. * List for revision table items for a single page
  26. */
  27. abstract class RevisionListBase extends ContextSource implements Iterator {
  28. /** @var Title */
  29. public $title;
  30. /** @var array */
  31. protected $ids;
  32. /** @var ResultWrapper|bool */
  33. protected $res;
  34. /** @var bool|Revision */
  35. protected $current;
  36. /**
  37. * Construct a revision list for a given title
  38. * @param IContextSource $context
  39. * @param Title $title
  40. */
  41. function __construct( IContextSource $context, Title $title ) {
  42. $this->setContext( $context );
  43. $this->title = $title;
  44. }
  45. /**
  46. * Select items only where the ID is any of the specified values
  47. * @param array $ids
  48. */
  49. function filterByIds( array $ids ) {
  50. $this->ids = $ids;
  51. }
  52. /**
  53. * Get the internal type name of this list. Equal to the table name.
  54. * Override this function.
  55. * @return null
  56. */
  57. public function getType() {
  58. return null;
  59. }
  60. /**
  61. * Initialise the current iteration pointer
  62. */
  63. protected function initCurrent() {
  64. $row = $this->res->current();
  65. if ( $row ) {
  66. $this->current = $this->newItem( $row );
  67. } else {
  68. $this->current = false;
  69. }
  70. }
  71. /**
  72. * Start iteration. This must be called before current() or next().
  73. * @return Revision First list item
  74. */
  75. public function reset() {
  76. if ( !$this->res ) {
  77. $this->res = $this->doQuery( wfGetDB( DB_REPLICA ) );
  78. } else {
  79. $this->res->rewind();
  80. }
  81. $this->initCurrent();
  82. return $this->current;
  83. }
  84. public function rewind() {
  85. $this->reset();
  86. }
  87. /**
  88. * Get the current list item, or false if we are at the end
  89. * @return Revision
  90. */
  91. public function current() {
  92. return $this->current;
  93. }
  94. /**
  95. * Move the iteration pointer to the next list item, and return it.
  96. * @return Revision
  97. * @suppress PhanParamSignatureMismatchInternal
  98. */
  99. public function next() {
  100. $this->res->next();
  101. $this->initCurrent();
  102. return $this->current;
  103. }
  104. public function key() {
  105. return $this->res ? $this->res->key() : 0;
  106. }
  107. public function valid() {
  108. return $this->res ? $this->res->valid() : false;
  109. }
  110. /**
  111. * Get the number of items in the list.
  112. * @return int
  113. */
  114. public function length() {
  115. if ( !$this->res ) {
  116. return 0;
  117. } else {
  118. return $this->res->numRows();
  119. }
  120. }
  121. /**
  122. * Do the DB query to iterate through the objects.
  123. * @param IDatabase $db DB object to use for the query
  124. */
  125. abstract public function doQuery( $db );
  126. /**
  127. * Create an item object from a DB result row
  128. * @param object $row
  129. */
  130. abstract public function newItem( $row );
  131. }