Debian Codegraph @

includes/revisionlist/RevisionListBase.php

raw symbols SCIP
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
23 namespace MediaWiki\RevisionList;
24
25 use Iterator;
26 use MediaWiki\Context\ContextSource;
27 use MediaWiki\Context\IContextSource;
28 use MediaWiki\Debug\DeprecationHelper;
29 use MediaWiki\MediaWikiServices;
30 use MediaWiki\Page\PageIdentity;
31 use MediaWiki\Title\Title;
32 use stdClass;
33 use Wikimedia\Rdbms\IReadableDatabase;
34 use Wikimedia\Rdbms\IResultWrapper;
35
36 /**
37 * List for revision table items for a single page
38 */
39 abstract class RevisionListBase extends ContextSource implements Iterator {
40 use DeprecationHelper;
41
42 /** @var PageIdentity */
43 protected $page;
44
45 /** @var int[]|null */
46 protected $ids;
47
48 /** @var IResultWrapper|false */
49 protected $res;
50
51 /** @var RevisionItemBase|false */
52 protected $current;
53
54 /**
55 * Construct a revision list for a given page identity
56 * @param IContextSource $context
57 * @param PageIdentity $page
58 */
59 public function __construct( IContextSource $context, PageIdentity $page ) {
60 $this->setContext( $context );
61 $this->page = $page;
62
63 $this->deprecatePublicPropertyFallback(
64 'title',
65 '1.37',
66 function (): Title {
67 return Title::newFromPageIdentity( $this->page );
68 },
69 function ( PageIdentity $page ) {
70 $this->page = $page;
71 }
72 );
73 }
74
75 /**
76 * @return PageIdentity
77 */
78 public function getPage(): PageIdentity {
79 return $this->page;
80 }
81
82 /**
83 * @internal for use by RevDelItems
84 * @return string
85 */
86 public function getPageName(): string {
87 return Title::newFromPageIdentity( $this->page )->getPrefixedText();
88 }
89
90 /**
91 * Select items only where the ID is any of the specified values
92 * @param int[] $ids
93 */
94 public function filterByIds( array $ids ) {
95 $this->ids = $ids;
96 }
97
98 /**
99 * Get the internal type name of this list. Equal to the table name.
100 * Override this function.
101 * @return string|null
102 */
103 public function getType() {
104 return null;
105 }
106
107 /**
108 * Initialise the current iteration pointer
109 */
110 protected function initCurrent() {
111 $row = $this->res->current();
112 if ( $row ) {
113 $this->current = $this->newItem( $row );
114 } else {
115 $this->current = false;
116 }
117 }
118
119 /**
120 * Start iteration. This must be called before current() or next().
121 * @return RevisionItemBase First list item
122 */
123 public function reset() {
124 if ( !$this->res ) {
125 $this->res = $this->doQuery(
126 MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase()
127 );
128 } else {
129 $this->res->rewind();
130 }
131 $this->initCurrent();
132 return $this->current;
133 }
134
135 public function rewind(): void {
136 $this->reset();
137 }
138
139 /**
140 * Get the current list item, or false if we are at the end
141 * @return RevisionItemBase|false
142 */
143 #[\ReturnTypeWillChange]
144 public function current() {
145 return $this->current;
146 }
147
148 /**
149 * Move the iteration pointer to the next list item, and return it.
150 * @return RevisionItemBase
151 * @suppress PhanParamSignatureMismatchInternal
152 */
153 #[\ReturnTypeWillChange]
154 public function next() {
155 $this->res->next();
156 $this->initCurrent();
157 return $this->current;
158 }
159
160 public function key(): int {
161 return $this->res ? $this->res->key() : 0;
162 }
163
164 public function valid(): bool {
165 return $this->res && $this->res->valid();
166 }
167
168 /**
169 * Get the number of items in the list.
170 * @return int
171 */
172 public function length() {
173 if ( !$this->res ) {
174 return 0;
175 } else {
176 return $this->res->numRows();
177 }
178 }
179
180 /**
181 * Do the DB query to iterate through the objects.
182 * @param IReadableDatabase $db DB object to use for the query
183 * @return IResultWrapper
184 */
185 abstract public function doQuery( $db );
186
187 /**
188 * Create an item object from a DB result row
189 * @param stdClass $row
190 * @return RevisionItemBase
191 */
192 abstract public function newItem( $row );
193 }
194 /** @deprecated class alias since 1.43 */
195 class_alias( RevisionListBase::class, 'RevisionListBase' );