Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / RevisionList.php
blob53cf6998a869d423c443ede30c65d5935eacdca0
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\MediaWikiServices;
25 /**
26 * List for revision table items for a single page
28 abstract class RevisionListBase extends ContextSource implements Iterator {
29 /** @var Title */
30 public $title;
32 /** @var array */
33 protected $ids;
35 /** @var ResultWrapper|bool */
36 protected $res;
38 /** @var bool|object */
39 protected $current;
41 /**
42 * Construct a revision list for a given title
43 * @param IContextSource $context
44 * @param Title $title
46 function __construct( IContextSource $context, Title $title ) {
47 $this->setContext( $context );
48 $this->title = $title;
51 /**
52 * Select items only where the ID is any of the specified values
53 * @param array $ids
55 function filterByIds( array $ids ) {
56 $this->ids = $ids;
59 /**
60 * Get the internal type name of this list. Equal to the table name.
61 * Override this function.
62 * @return null
64 public function getType() {
65 return null;
68 /**
69 * Initialise the current iteration pointer
71 protected function initCurrent() {
72 $row = $this->res->current();
73 if ( $row ) {
74 $this->current = $this->newItem( $row );
75 } else {
76 $this->current = false;
80 /**
81 * Start iteration. This must be called before current() or next().
82 * @return Revision First list item
84 public function reset() {
85 if ( !$this->res ) {
86 $this->res = $this->doQuery( wfGetDB( DB_REPLICA ) );
87 } else {
88 $this->res->rewind();
90 $this->initCurrent();
91 return $this->current;
94 public function rewind() {
95 $this->reset();
98 /**
99 * Get the current list item, or false if we are at the end
100 * @return Revision
102 public function current() {
103 return $this->current;
107 * Move the iteration pointer to the next list item, and return it.
108 * @return Revision
110 public function next() {
111 $this->res->next();
112 $this->initCurrent();
113 return $this->current;
116 public function key() {
117 return $this->res ? $this->res->key(): 0;
120 public function valid() {
121 return $this->res ? $this->res->valid() : false;
125 * Get the number of items in the list.
126 * @return int
128 public function length() {
129 if ( !$this->res ) {
130 return 0;
131 } else {
132 return $this->res->numRows();
137 * Do the DB query to iterate through the objects.
138 * @param IDatabase $db DB object to use for the query
140 abstract public function doQuery( $db );
143 * Create an item object from a DB result row
144 * @param object $row
146 abstract public function newItem( $row );
150 * Abstract base class for revision items
152 abstract class RevisionItemBase {
153 /** @var RevisionListBase The parent */
154 protected $list;
156 /** The database result row */
157 protected $row;
160 * @param RevisionListBase $list
161 * @param object $row DB result row
163 public function __construct( $list, $row ) {
164 $this->list = $list;
165 $this->row = $row;
169 * Get the DB field name associated with the ID list.
170 * Override this function.
171 * @return null
173 public function getIdField() {
174 return null;
178 * Get the DB field name storing timestamps.
179 * Override this function.
180 * @return bool
182 public function getTimestampField() {
183 return false;
187 * Get the DB field name storing user ids.
188 * Override this function.
189 * @return bool
191 public function getAuthorIdField() {
192 return false;
196 * Get the DB field name storing user names.
197 * Override this function.
198 * @return bool
200 public function getAuthorNameField() {
201 return false;
205 * Get the ID, as it would appear in the ids URL parameter
206 * @return int
208 public function getId() {
209 $field = $this->getIdField();
210 return $this->row->$field;
214 * Get the date, formatted in user's language
215 * @return string
217 public function formatDate() {
218 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
219 $this->list->getUser() );
223 * Get the time, formatted in user's language
224 * @return string
226 public function formatTime() {
227 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
228 $this->list->getUser() );
232 * Get the timestamp in MW 14-char form
233 * @return mixed
235 public function getTimestamp() {
236 $field = $this->getTimestampField();
237 return wfTimestamp( TS_MW, $this->row->$field );
241 * Get the author user ID
242 * @return int
244 public function getAuthorId() {
245 $field = $this->getAuthorIdField();
246 return intval( $this->row->$field );
250 * Get the author user name
251 * @return string
253 public function getAuthorName() {
254 $field = $this->getAuthorNameField();
255 return strval( $this->row->$field );
259 * Returns true if the current user can view the item
261 abstract public function canView();
264 * Returns true if the current user can view the item text/file
266 abstract public function canViewContent();
269 * Get the HTML of the list item. Should be include "<li></li>" tags.
270 * This is used to show the list in HTML form, by the special page.
272 abstract public function getHTML();
275 * Returns an instance of LinkRenderer
276 * @return \MediaWiki\Linker\LinkRenderer
278 protected function getLinkRenderer() {
279 return MediaWikiServices::getInstance()->getLinkRenderer();
283 class RevisionList extends RevisionListBase {
284 public function getType() {
285 return 'revision';
289 * @param IDatabase $db
290 * @return mixed
292 public function doQuery( $db ) {
293 $conds = [ 'rev_page' => $this->title->getArticleID() ];
294 if ( $this->ids !== null ) {
295 $conds['rev_id'] = array_map( 'intval', $this->ids );
297 return $db->select(
298 [ 'revision', 'page', 'user' ],
299 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
300 $conds,
301 __METHOD__,
302 [ 'ORDER BY' => 'rev_id DESC' ],
304 'page' => Revision::pageJoinCond(),
305 'user' => Revision::userJoinCond() ]
309 public function newItem( $row ) {
310 return new RevisionItem( $this, $row );
315 * Item class for a live revision table row
317 class RevisionItem extends RevisionItemBase {
318 /** @var Revision */
319 protected $revision;
321 /** @var RequestContext */
322 protected $context;
324 public function __construct( $list, $row ) {
325 parent::__construct( $list, $row );
326 $this->revision = new Revision( $row );
327 $this->context = $list->getContext();
330 public function getIdField() {
331 return 'rev_id';
334 public function getTimestampField() {
335 return 'rev_timestamp';
338 public function getAuthorIdField() {
339 return 'rev_user';
342 public function getAuthorNameField() {
343 return 'rev_user_text';
346 public function canView() {
347 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->context->getUser() );
350 public function canViewContent() {
351 return $this->revision->userCan( Revision::DELETED_TEXT, $this->context->getUser() );
354 public function isDeleted() {
355 return $this->revision->isDeleted( Revision::DELETED_TEXT );
359 * Get the HTML link to the revision text.
360 * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
361 * should inherit from this one, and implement an appropriate interface instead
362 * of extending RevDelItem
363 * @return string
365 protected function getRevisionLink() {
366 $date = $this->list->getLanguage()->userTimeAndDate(
367 $this->revision->getTimestamp(), $this->list->getUser() );
369 if ( $this->isDeleted() && !$this->canViewContent() ) {
370 return htmlspecialchars( $date );
372 $linkRenderer = $this->getLinkRenderer();
373 return $linkRenderer->makeKnownLink(
374 $this->list->title,
375 $date,
378 'oldid' => $this->revision->getId(),
379 'unhide' => 1
385 * Get the HTML link to the diff.
386 * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
387 * should inherit from this one, and implement an appropriate interface instead
388 * of extending RevDelItem
389 * @return string
391 protected function getDiffLink() {
392 if ( $this->isDeleted() && !$this->canViewContent() ) {
393 return $this->context->msg( 'diff' )->escaped();
394 } else {
395 $linkRenderer = $this->getLinkRenderer();
396 return $linkRenderer->makeKnownLink(
397 $this->list->title,
398 $this->list->msg( 'diff' )->text(),
401 'diff' => $this->revision->getId(),
402 'oldid' => 'prev',
403 'unhide' => 1
410 * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
411 * should inherit from this one, and implement an appropriate interface instead
412 * of extending RevDelItem
413 * @return string
415 public function getHTML() {
416 $difflink = $this->context->msg( 'parentheses' )
417 ->rawParams( $this->getDiffLink() )->escaped();
418 $revlink = $this->getRevisionLink();
419 $userlink = Linker::revUserLink( $this->revision );
420 $comment = Linker::revComment( $this->revision );
421 if ( $this->isDeleted() ) {
422 $revlink = "<span class=\"history-deleted\">$revlink</span>";
424 return "<li>$difflink $revlink $userlink $comment</li>";