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
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\Rdbms\ResultWrapper
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * List for revision table items for a single page
30 abstract class RevisionListBase
extends ContextSource
implements Iterator
{
37 /** @var ResultWrapper|bool */
40 /** @var bool|Revision */
44 * Construct a revision list for a given title
45 * @param IContextSource $context
48 function __construct( IContextSource
$context, Title
$title ) {
49 $this->setContext( $context );
50 $this->title
= $title;
54 * Select items only where the ID is any of the specified values
57 function filterByIds( array $ids ) {
62 * Get the internal type name of this list. Equal to the table name.
63 * Override this function.
66 public function getType() {
71 * Initialise the current iteration pointer
73 protected function initCurrent() {
74 $row = $this->res
->current();
76 $this->current
= $this->newItem( $row );
78 $this->current
= false;
83 * Start iteration. This must be called before current() or next().
84 * @return Revision First list item
86 public function reset() {
88 $this->res
= $this->doQuery( wfGetDB( DB_REPLICA
) );
93 return $this->current
;
96 public function rewind() {
101 * Get the current list item, or false if we are at the end
104 public function current() {
105 return $this->current
;
109 * Move the iteration pointer to the next list item, and return it.
112 public function next() {
114 $this->initCurrent();
115 return $this->current
;
118 public function key() {
119 return $this->res ?
$this->res
->key() : 0;
122 public function valid() {
123 return $this->res ?
$this->res
->valid() : false;
127 * Get the number of items in the list.
130 public function length() {
134 return $this->res
->numRows();
139 * Do the DB query to iterate through the objects.
140 * @param IDatabase $db DB object to use for the query
142 abstract public function doQuery( $db );
145 * Create an item object from a DB result row
148 abstract public function newItem( $row );
152 * Abstract base class for revision items
154 abstract class RevisionItemBase
{
155 /** @var RevisionListBase The parent */
158 /** The database result row */
162 * @param RevisionListBase $list
163 * @param object $row DB result row
165 public function __construct( $list, $row ) {
171 * Get the DB field name associated with the ID list.
172 * Override this function.
175 public function getIdField() {
180 * Get the DB field name storing timestamps.
181 * Override this function.
184 public function getTimestampField() {
189 * Get the DB field name storing user ids.
190 * Override this function.
193 public function getAuthorIdField() {
198 * Get the DB field name storing user names.
199 * Override this function.
202 public function getAuthorNameField() {
207 * Get the ID, as it would appear in the ids URL parameter
210 public function getId() {
211 $field = $this->getIdField();
212 return $this->row
->$field;
216 * Get the date, formatted in user's language
219 public function formatDate() {
220 return $this->list->getLanguage()->userDate( $this->getTimestamp(),
221 $this->list->getUser() );
225 * Get the time, formatted in user's language
228 public function formatTime() {
229 return $this->list->getLanguage()->userTime( $this->getTimestamp(),
230 $this->list->getUser() );
234 * Get the timestamp in MW 14-char form
237 public function getTimestamp() {
238 $field = $this->getTimestampField();
239 return wfTimestamp( TS_MW
, $this->row
->$field );
243 * Get the author user ID
246 public function getAuthorId() {
247 $field = $this->getAuthorIdField();
248 return intval( $this->row
->$field );
252 * Get the author user name
255 public function getAuthorName() {
256 $field = $this->getAuthorNameField();
257 return strval( $this->row
->$field );
261 * Returns true if the current user can view the item
263 abstract public function canView();
266 * Returns true if the current user can view the item text/file
268 abstract public function canViewContent();
271 * Get the HTML of the list item. Should be include "<li></li>" tags.
272 * This is used to show the list in HTML form, by the special page.
274 abstract public function getHTML();
277 * Returns an instance of LinkRenderer
278 * @return \MediaWiki\Linker\LinkRenderer
280 protected function getLinkRenderer() {
281 return MediaWikiServices
::getInstance()->getLinkRenderer();
285 class RevisionList
extends RevisionListBase
{
286 public function getType() {
291 * @param IDatabase $db
294 public function doQuery( $db ) {
295 $conds = [ 'rev_page' => $this->title
->getArticleID() ];
296 if ( $this->ids
!== null ) {
297 $conds['rev_id'] = array_map( 'intval', $this->ids
);
300 [ 'revision', 'page', 'user' ],
301 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
304 [ 'ORDER BY' => 'rev_id DESC' ],
306 'page' => Revision
::pageJoinCond(),
307 'user' => Revision
::userJoinCond() ]
311 public function newItem( $row ) {
312 return new RevisionItem( $this, $row );
317 * Item class for a live revision table row
319 class RevisionItem
extends RevisionItemBase
{
323 /** @var RequestContext */
326 public function __construct( $list, $row ) {
327 parent
::__construct( $list, $row );
328 $this->revision
= new Revision( $row );
329 $this->context
= $list->getContext();
332 public function getIdField() {
336 public function getTimestampField() {
337 return 'rev_timestamp';
340 public function getAuthorIdField() {
344 public function getAuthorNameField() {
345 return 'rev_user_text';
348 public function canView() {
349 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
, $this->context
->getUser() );
352 public function canViewContent() {
353 return $this->revision
->userCan( Revision
::DELETED_TEXT
, $this->context
->getUser() );
356 public function isDeleted() {
357 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
361 * Get the HTML link to the revision text.
362 * @todo Essentially a copy of RevDelRevisionItem::getRevisionLink. That class
363 * should inherit from this one, and implement an appropriate interface instead
364 * of extending RevDelItem
367 protected function getRevisionLink() {
368 $date = $this->list->getLanguage()->userTimeAndDate(
369 $this->revision
->getTimestamp(), $this->list->getUser() );
371 if ( $this->isDeleted() && !$this->canViewContent() ) {
372 return htmlspecialchars( $date );
374 $linkRenderer = $this->getLinkRenderer();
375 return $linkRenderer->makeKnownLink(
380 'oldid' => $this->revision
->getId(),
387 * Get the HTML link to the diff.
388 * @todo Essentially a copy of RevDelRevisionItem::getDiffLink. That class
389 * should inherit from this one, and implement an appropriate interface instead
390 * of extending RevDelItem
393 protected function getDiffLink() {
394 if ( $this->isDeleted() && !$this->canViewContent() ) {
395 return $this->context
->msg( 'diff' )->escaped();
397 $linkRenderer = $this->getLinkRenderer();
398 return $linkRenderer->makeKnownLink(
400 $this->list->msg( 'diff' )->text(),
403 'diff' => $this->revision
->getId(),
412 * @todo Essentially a copy of RevDelRevisionItem::getHTML. That class
413 * should inherit from this one, and implement an appropriate interface instead
414 * of extending RevDelItem
417 public function getHTML() {
418 $difflink = $this->context
->msg( 'parentheses' )
419 ->rawParams( $this->getDiffLink() )->escaped();
420 $revlink = $this->getRevisionLink();
421 $userlink = Linker
::revUserLink( $this->revision
);
422 $comment = Linker
::revComment( $this->revision
);
423 if ( $this->isDeleted() ) {
424 $revlink = "<span class=\"history-deleted\">$revlink</span>";
426 return "<li>$difflink $revlink $userlink $comment</li>";