Update git submodules
[mediawiki.git] / includes / revisionlist / RevisionListBase.php
blob37791d6dd61a67d983cb9fc1a9eb7da344acda4a
1 <?php
2 /**
3 * Holders of revision list for a single page
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.
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.
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
20 * @file
23 use MediaWiki\Page\PageIdentity;
24 use MediaWiki\Title\Title;
25 use Wikimedia\Rdbms\IDatabase;
26 use Wikimedia\Rdbms\IResultWrapper;
28 /**
29 * List for revision table items for a single page
31 abstract class RevisionListBase extends ContextSource implements Iterator {
32 use DeprecationHelper;
34 /** @var PageIdentity */
35 protected $page;
37 /** @var int[]|null */
38 protected $ids;
40 /** @var IResultWrapper|false */
41 protected $res;
43 /** @var RevisionItemBase|false */
44 protected $current;
46 /**
47 * Construct a revision list for a given page identity
48 * @param IContextSource $context
49 * @param PageIdentity $page
51 public function __construct( IContextSource $context, PageIdentity $page ) {
52 $this->setContext( $context );
53 $this->page = $page;
55 $this->deprecatePublicPropertyFallback(
56 'title',
57 '1.37',
58 function (): Title {
59 return Title::newFromPageIdentity( $this->page );
61 function ( PageIdentity $page ) {
62 $this->page = $page;
67 /**
68 * @return PageIdentity
70 public function getPage(): PageIdentity {
71 return $this->page;
74 /**
75 * @internal for use by RevDelItems
76 * @return string
78 public function getPageName(): string {
79 return Title::newFromPageIdentity( $this->page )->getPrefixedText();
82 /**
83 * Select items only where the ID is any of the specified values
84 * @param int[] $ids
86 public function filterByIds( array $ids ) {
87 $this->ids = $ids;
90 /**
91 * Get the internal type name of this list. Equal to the table name.
92 * Override this function.
93 * @return string|null
95 public function getType() {
96 return null;
99 /**
100 * Initialise the current iteration pointer
102 protected function initCurrent() {
103 $row = $this->res->current();
104 if ( $row ) {
105 $this->current = $this->newItem( $row );
106 } else {
107 $this->current = false;
112 * Start iteration. This must be called before current() or next().
113 * @return RevisionItemBase First list item
115 public function reset() {
116 if ( !$this->res ) {
117 $this->res = $this->doQuery( wfGetDB( DB_REPLICA ) );
118 } else {
119 $this->res->rewind();
121 $this->initCurrent();
122 return $this->current;
125 public function rewind(): void {
126 $this->reset();
130 * Get the current list item, or false if we are at the end
131 * @return RevisionItemBase|false
133 #[\ReturnTypeWillChange]
134 public function current() {
135 return $this->current;
139 * Move the iteration pointer to the next list item, and return it.
140 * @return RevisionItemBase
141 * @suppress PhanParamSignatureMismatchInternal
143 #[\ReturnTypeWillChange]
144 public function next() {
145 $this->res->next();
146 $this->initCurrent();
147 return $this->current;
150 public function key(): int {
151 return $this->res ? $this->res->key() : 0;
154 public function valid(): bool {
155 return $this->res && $this->res->valid();
159 * Get the number of items in the list.
160 * @return int
162 public function length() {
163 if ( !$this->res ) {
164 return 0;
165 } else {
166 return $this->res->numRows();
171 * Do the DB query to iterate through the objects.
172 * @param IDatabase $db DB object to use for the query
173 * @return IResultWrapper
175 abstract public function doQuery( $db );
178 * Create an item object from a DB result row
179 * @param stdClass $row
180 * @return RevisionItemBase
182 abstract public function newItem( $row );