Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / actions / HistoryAction.php
blobe8aec1cf6eac2c8b70ace991a6b9ad82f34ca24c
1 <?php
2 /**
3 * Page history
5 * Split off from Article.php and Skin.php, 2003-12-22
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @ingroup Actions
26 use MediaWiki\MediaWikiServices;
28 /**
29 * This class handles printing the history page for an article. In order to
30 * be efficient, it uses timestamps rather than offsets for paging, to avoid
31 * costly LIMIT,offset queries.
33 * Construct it by passing in an Article, and call $h->history() to print the
34 * history.
36 * @ingroup Actions
38 class HistoryAction extends FormlessAction {
39 const DIR_PREV = 0;
40 const DIR_NEXT = 1;
42 /** @var array Array of message keys and strings */
43 public $message;
45 public function getName() {
46 return 'history';
49 public function requiresWrite() {
50 return false;
53 public function requiresUnblock() {
54 return false;
57 protected function getPageTitle() {
58 return $this->msg( 'history-title', $this->getTitle()->getPrefixedText() )->text();
61 protected function getDescription() {
62 // Creation of a subtitle link pointing to [[Special:Log]]
63 return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
64 SpecialPage::getTitleFor( 'Log' ),
65 $this->msg( 'viewpagelogs' )->text(),
66 [],
67 [ 'page' => $this->getTitle()->getPrefixedText() ]
71 /**
72 * @return WikiPage|Article|ImagePage|CategoryPage|Page The Article object we are working on.
74 public function getArticle() {
75 return $this->page;
78 /**
79 * As we use the same small set of messages in various methods and that
80 * they are called often, we call them once and save them in $this->message
82 private function preCacheMessages() {
83 // Precache various messages
84 if ( !isset( $this->message ) ) {
85 $msgs = [ 'cur', 'last', 'pipe-separator' ];
86 foreach ( $msgs as $msg ) {
87 $this->message[$msg] = $this->msg( $msg )->escaped();
92 /**
93 * Print the history page for an article.
95 function onView() {
96 $out = $this->getOutput();
97 $request = $this->getRequest();
99 /**
100 * Allow client caching.
102 if ( $out->checkLastModified( $this->page->getTouched() ) ) {
103 return; // Client cache fresh and headers sent, nothing more to do.
106 $this->preCacheMessages();
107 $config = $this->context->getConfig();
109 # Fill in the file cache if not set already
110 if ( HTMLFileCache::useFileCache( $this->getContext() ) ) {
111 $cache = new HTMLFileCache( $this->getTitle(), 'history' );
112 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
113 ob_start( [ &$cache, 'saveToFileCache' ] );
117 // Setup page variables.
118 $out->setFeedAppendQuery( 'action=history' );
119 $out->addModules( 'mediawiki.action.history' );
120 $out->addModuleStyles( [
121 'mediawiki.action.history.styles',
122 'mediawiki.special.changeslist',
123 ] );
124 if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) {
125 $out = $this->getOutput();
126 $out->addModuleStyles( [
127 'mediawiki.ui.input',
128 'mediawiki.ui.checkbox',
129 ] );
132 // Handle atom/RSS feeds.
133 $feedType = $request->getVal( 'feed' );
134 if ( $feedType ) {
135 $this->feed( $feedType );
137 return;
140 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Page_history', true );
142 // Fail nicely if article doesn't exist.
143 if ( !$this->page->exists() ) {
144 global $wgSend404Code;
145 if ( $wgSend404Code ) {
146 $out->setStatusCode( 404 );
148 $out->addWikiMsg( 'nohistory' );
149 # show deletion/move log if there is an entry
150 LogEventsList::showLogExtract(
151 $out,
152 [ 'delete', 'move' ],
153 $this->getTitle(),
155 [ 'lim' => 10,
156 'conds' => [ "log_action != 'revision'" ],
157 'showIfEmpty' => false,
158 'msgKey' => [ 'moveddeleted-notice' ]
162 return;
166 * Add date selector to quickly get to a certain time
168 $year = $request->getInt( 'year' );
169 $month = $request->getInt( 'month' );
170 $tagFilter = $request->getVal( 'tagfilter' );
171 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter, false, $this->getContext() );
174 * Option to show only revisions that have been (partially) hidden via RevisionDelete
176 if ( $request->getBool( 'deleted' ) ) {
177 $conds = [ 'rev_deleted != 0' ];
178 } else {
179 $conds = [];
181 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
182 $checkDeleted = Xml::checkLabel( $this->msg( 'history-show-deleted' )->text(),
183 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
184 } else {
185 $checkDeleted = '';
188 // Add the general form
189 $action = htmlspecialchars( wfScript() );
190 $out->addHTML(
191 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
192 Xml::fieldset(
193 $this->msg( 'history-fieldset-title' )->text(),
194 false,
195 [ 'id' => 'mw-history-search' ]
197 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n" .
198 Html::hidden( 'action', 'history' ) . "\n" .
199 Xml::dateMenu(
200 ( $year == null ? MWTimestamp::getLocalInstance()->format( 'Y' ) : $year ),
201 $month
202 ) . '&#160;' .
203 ( $tagSelector ? ( implode( '&#160;', $tagSelector ) . '&#160;' ) : '' ) .
204 $checkDeleted .
205 Html::submitButton(
206 $this->msg( 'historyaction-submit' )->text(),
208 [ 'mw-ui-progressive' ]
209 ) . "\n" .
210 '</fieldset></form>'
213 Hooks::run( 'PageHistoryBeforeList', [ &$this->page, $this->getContext() ] );
215 // Create and output the list.
216 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
217 $out->addHTML(
218 $pager->getNavigationBar() .
219 $pager->getBody() .
220 $pager->getNavigationBar()
222 $out->preventClickjacking( $pager->getPreventClickjacking() );
226 * Fetch an array of revisions, specified by a given limit, offset and
227 * direction. This is now only used by the feeds. It was previously
228 * used by the main UI but that's now handled by the pager.
230 * @param int $limit The limit number of revisions to get
231 * @param int $offset
232 * @param int $direction Either self::DIR_PREV or self::DIR_NEXT
233 * @return ResultWrapper
235 function fetchRevisions( $limit, $offset, $direction ) {
236 // Fail if article doesn't exist.
237 if ( !$this->getTitle()->exists() ) {
238 return new FakeResultWrapper( [] );
241 $dbr = wfGetDB( DB_REPLICA );
243 if ( $direction === self::DIR_PREV ) {
244 list( $dirs, $oper ) = [ "ASC", ">=" ];
245 } else { /* $direction === self::DIR_NEXT */
246 list( $dirs, $oper ) = [ "DESC", "<=" ];
249 if ( $offset ) {
250 $offsets = [ "rev_timestamp $oper " . $dbr->addQuotes( $dbr->timestamp( $offset ) ) ];
251 } else {
252 $offsets = [];
255 $page_id = $this->page->getId();
257 return $dbr->select( 'revision',
258 Revision::selectFields(),
259 array_merge( [ 'rev_page' => $page_id ], $offsets ),
260 __METHOD__,
261 [ 'ORDER BY' => "rev_timestamp $dirs",
262 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit ]
267 * Output a subscription feed listing recent edits to this page.
269 * @param string $type Feed type
271 function feed( $type ) {
272 if ( !FeedUtils::checkFeedOutput( $type ) ) {
273 return;
275 $request = $this->getRequest();
277 $feedClasses = $this->context->getConfig()->get( 'FeedClasses' );
278 /** @var RSSFeed|AtomFeed $feed */
279 $feed = new $feedClasses[$type](
280 $this->getTitle()->getPrefixedText() . ' - ' .
281 $this->msg( 'history-feed-title' )->inContentLanguage()->text(),
282 $this->msg( 'history-feed-description' )->inContentLanguage()->text(),
283 $this->getTitle()->getFullURL( 'action=history' )
286 // Get a limit on number of feed entries. Provide a sane default
287 // of 10 if none is defined (but limit to $wgFeedLimit max)
288 $limit = $request->getInt( 'limit', 10 );
289 $limit = min(
290 max( $limit, 1 ),
291 $this->context->getConfig()->get( 'FeedLimit' )
294 $items = $this->fetchRevisions( $limit, 0, self::DIR_NEXT );
296 // Generate feed elements enclosed between header and footer.
297 $feed->outHeader();
298 if ( $items->numRows() ) {
299 foreach ( $items as $row ) {
300 $feed->outItem( $this->feedItem( $row ) );
302 } else {
303 $feed->outItem( $this->feedEmpty() );
305 $feed->outFooter();
308 function feedEmpty() {
309 return new FeedItem(
310 $this->msg( 'nohistory' )->inContentLanguage()->text(),
311 $this->msg( 'history-feed-empty' )->inContentLanguage()->parseAsBlock(),
312 $this->getTitle()->getFullURL(),
313 wfTimestamp( TS_MW ),
315 $this->getTitle()->getTalkPage()->getFullURL()
320 * Generate a FeedItem object from a given revision table row
321 * Borrows Recent Changes' feed generation functions for formatting;
322 * includes a diff to the previous revision (if any).
324 * @param stdClass|array $row Database row
325 * @return FeedItem
327 function feedItem( $row ) {
328 $rev = new Revision( $row );
329 $rev->setTitle( $this->getTitle() );
330 $text = FeedUtils::formatDiffRow(
331 $this->getTitle(),
332 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
333 $rev->getId(),
334 $rev->getTimestamp(),
335 $rev->getComment()
337 if ( $rev->getComment() == '' ) {
338 global $wgContLang;
339 $title = $this->msg( 'history-feed-item-nocomment',
340 $rev->getUserText(),
341 $wgContLang->timeanddate( $rev->getTimestamp() ),
342 $wgContLang->date( $rev->getTimestamp() ),
343 $wgContLang->time( $rev->getTimestamp() ) )->inContentLanguage()->text();
344 } else {
345 $title = $rev->getUserText() .
346 $this->msg( 'colon-separator' )->inContentLanguage()->text() .
347 FeedItem::stripComment( $rev->getComment() );
350 return new FeedItem(
351 $title,
352 $text,
353 $this->getTitle()->getFullURL( 'diff=' . $rev->getId() . '&oldid=prev' ),
354 $rev->getTimestamp(),
355 $rev->getUserText(),
356 $this->getTitle()->getTalkPage()->getFullURL()
362 * @ingroup Pager
363 * @ingroup Actions
365 class HistoryPager extends ReverseChronologicalPager {
367 * @var bool|stdClass
369 public $lastRow = false;
371 public $counter, $historyPage, $buttons, $conds;
373 protected $oldIdChecked;
375 protected $preventClickjacking = false;
377 * @var array
379 protected $parentLens;
381 /** @var bool Whether to show the tag editing UI */
382 protected $showTagEditUI;
385 * @param HistoryAction $historyPage
386 * @param string $year
387 * @param string $month
388 * @param string $tagFilter
389 * @param array $conds
391 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = [] ) {
392 parent::__construct( $historyPage->getContext() );
393 $this->historyPage = $historyPage;
394 $this->tagFilter = $tagFilter;
395 $this->getDateCond( $year, $month );
396 $this->conds = $conds;
397 $this->showTagEditUI = ChangeTags::showTagEditingUI( $this->getUser() );
400 // For hook compatibility...
401 function getArticle() {
402 return $this->historyPage->getArticle();
405 function getSqlComment() {
406 if ( $this->conds ) {
407 return 'history page filtered'; // potentially slow, see CR r58153
408 } else {
409 return 'history page unfiltered';
413 function getQueryInfo() {
414 $queryInfo = [
415 'tables' => [ 'revision', 'user' ],
416 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
417 'conds' => array_merge(
418 [ 'rev_page' => $this->getWikiPage()->getId() ],
419 $this->conds ),
420 'options' => [ 'USE INDEX' => [ 'revision' => 'page_timestamp' ] ],
421 'join_conds' => [ 'user' => Revision::userJoinCond() ],
423 ChangeTags::modifyDisplayQuery(
424 $queryInfo['tables'],
425 $queryInfo['fields'],
426 $queryInfo['conds'],
427 $queryInfo['join_conds'],
428 $queryInfo['options'],
429 $this->tagFilter
432 // Avoid PHP 7.1 warning of passing $this by reference
433 $historyPager = $this;
434 Hooks::run( 'PageHistoryPager::getQueryInfo', [ &$historyPager, &$queryInfo ] );
436 return $queryInfo;
439 function getIndexField() {
440 return 'rev_timestamp';
444 * @param stdClass $row
445 * @return string
447 function formatRow( $row ) {
448 if ( $this->lastRow ) {
449 $latest = ( $this->counter == 1 && $this->mIsFirst );
450 $firstInList = $this->counter == 1;
451 $this->counter++;
453 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
454 ? $this->getTitle()->getNotificationTimestamp( $this->getUser() )
455 : false;
457 $s = $this->historyLine(
458 $this->lastRow, $row, $notifTimestamp, $latest, $firstInList );
459 } else {
460 $s = '';
462 $this->lastRow = $row;
464 return $s;
467 function doBatchLookups() {
468 if ( !Hooks::run( 'PageHistoryPager::doBatchLookups', [ $this, $this->mResult ] ) ) {
469 return;
472 # Do a link batch query
473 $this->mResult->seek( 0 );
474 $batch = new LinkBatch();
475 $revIds = [];
476 foreach ( $this->mResult as $row ) {
477 if ( $row->rev_parent_id ) {
478 $revIds[] = $row->rev_parent_id;
480 if ( !is_null( $row->user_name ) ) {
481 $batch->add( NS_USER, $row->user_name );
482 $batch->add( NS_USER_TALK, $row->user_name );
483 } else { # for anons or usernames of imported revisions
484 $batch->add( NS_USER, $row->rev_user_text );
485 $batch->add( NS_USER_TALK, $row->rev_user_text );
488 $this->parentLens = Revision::getParentLengths( $this->mDb, $revIds );
489 $batch->execute();
490 $this->mResult->seek( 0 );
494 * Creates begin of history list with a submit button
496 * @return string HTML output
498 function getStartBody() {
499 $this->lastRow = false;
500 $this->counter = 1;
501 $this->oldIdChecked = 0;
503 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
504 $s = Html::openElement( 'form', [ 'action' => wfScript(),
505 'id' => 'mw-history-compare' ] ) . "\n";
506 $s .= Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n";
507 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
508 $s .= Html::hidden( 'type', 'revision' ) . "\n";
510 // Button container stored in $this->buttons for re-use in getEndBody()
511 $this->buttons = '<div>';
512 $className = 'historysubmit mw-history-compareselectedversions-button';
513 $attrs = [ 'class' => $className ]
514 + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' );
515 $this->buttons .= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
516 $attrs
517 ) . "\n";
519 $user = $this->getUser();
520 $actionButtons = '';
521 if ( $user->isAllowed( 'deleterevision' ) ) {
522 $actionButtons .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
524 if ( $this->showTagEditUI ) {
525 $actionButtons .= $this->getRevisionButton( 'editchangetags', 'history-edit-tags' );
527 if ( $actionButtons ) {
528 $this->buttons .= Xml::tags( 'div', [ 'class' =>
529 'mw-history-revisionactions' ], $actionButtons );
532 if ( $user->isAllowed( 'deleterevision' ) || $this->showTagEditUI ) {
533 $this->buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
536 $this->buttons .= '</div>';
538 $s .= $this->buttons;
539 $s .= '<ul id="pagehistory">' . "\n";
541 return $s;
544 private function getRevisionButton( $name, $msg ) {
545 $this->preventClickjacking();
546 # Note bug #20966, <button> is non-standard in IE<8
547 $element = Html::element(
548 'button',
550 'type' => 'submit',
551 'name' => $name,
552 'value' => '1',
553 'class' => "historysubmit mw-history-$name-button",
555 $this->msg( $msg )->text()
556 ) . "\n";
557 return $element;
560 function getEndBody() {
561 if ( $this->lastRow ) {
562 $latest = $this->counter == 1 && $this->mIsFirst;
563 $firstInList = $this->counter == 1;
564 if ( $this->mIsBackwards ) {
565 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
566 if ( $this->mOffset == '' ) {
567 $next = null;
568 } else {
569 $next = 'unknown';
571 } else {
572 # The next row is the past-the-end row
573 $next = $this->mPastTheEndRow;
575 $this->counter++;
577 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
578 ? $this->getTitle()->getNotificationTimestamp( $this->getUser() )
579 : false;
581 $s = $this->historyLine(
582 $this->lastRow, $next, $notifTimestamp, $latest, $firstInList );
583 } else {
584 $s = '';
586 $s .= "</ul>\n";
587 # Add second buttons only if there is more than one rev
588 if ( $this->getNumRows() > 2 ) {
589 $s .= $this->buttons;
591 $s .= '</form>';
593 return $s;
597 * Creates a submit button
599 * @param string $message Text of the submit button, will be escaped
600 * @param array $attributes Attributes
601 * @return string HTML output for the submit button
603 function submitButton( $message, $attributes = [] ) {
604 # Disable submit button if history has 1 revision only
605 if ( $this->getNumRows() > 1 ) {
606 return Html::submitButton( $message, $attributes );
607 } else {
608 return '';
613 * Returns a row from the history printout.
615 * @todo document some more, and maybe clean up the code (some params redundant?)
617 * @param stdClass $row The database row corresponding to the previous line.
618 * @param mixed $next The database row corresponding to the next line
619 * (chronologically previous)
620 * @param bool|string $notificationtimestamp
621 * @param bool $latest Whether this row corresponds to the page's latest revision.
622 * @param bool $firstInList Whether this row corresponds to the first
623 * displayed on this history page.
624 * @return string HTML output for the row
626 function historyLine( $row, $next, $notificationtimestamp = false,
627 $latest = false, $firstInList = false ) {
628 $rev = new Revision( $row );
629 $rev->setTitle( $this->getTitle() );
631 if ( is_object( $next ) ) {
632 $prevRev = new Revision( $next );
633 $prevRev->setTitle( $this->getTitle() );
634 } else {
635 $prevRev = null;
638 $curlink = $this->curLink( $rev, $latest );
639 $lastlink = $this->lastLink( $rev, $next );
640 $curLastlinks = $curlink . $this->historyPage->message['pipe-separator'] . $lastlink;
641 $histLinks = Html::rawElement(
642 'span',
643 [ 'class' => 'mw-history-histlinks' ],
644 $this->msg( 'parentheses' )->rawParams( $curLastlinks )->escaped()
647 $diffButtons = $this->diffButtons( $rev, $firstInList );
648 $s = $histLinks . $diffButtons;
650 $link = $this->revLink( $rev );
651 $classes = [];
653 $del = '';
654 $user = $this->getUser();
655 $canRevDelete = $user->isAllowed( 'deleterevision' );
656 // Show checkboxes for each revision, to allow for revision deletion and
657 // change tags
658 if ( $canRevDelete || $this->showTagEditUI ) {
659 $this->preventClickjacking();
660 // If revision was hidden from sysops and we don't need the checkbox
661 // for anything else, disable it
662 if ( !$this->showTagEditUI && !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
663 $del = Xml::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
664 // Otherwise, enable the checkbox...
665 } else {
666 $del = Xml::check( 'showhiderevisions', false,
667 [ 'name' => 'ids[' . $rev->getId() . ']' ] );
669 // User can only view deleted revisions...
670 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
671 // If revision was hidden from sysops, disable the link
672 if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
673 $del = Linker::revDeleteLinkDisabled( false );
674 // Otherwise, show the link...
675 } else {
676 $query = [ 'type' => 'revision',
677 'target' => $this->getTitle()->getPrefixedDBkey(), 'ids' => $rev->getId() ];
678 $del .= Linker::revDeleteLink( $query,
679 $rev->isDeleted( Revision::DELETED_RESTRICTED ), false );
682 if ( $del ) {
683 $s .= " $del ";
686 $lang = $this->getLanguage();
687 $dirmark = $lang->getDirMark();
689 $s .= " $link";
690 $s .= $dirmark;
691 $s .= " <span class='history-user'>" .
692 Linker::revUserTools( $rev, true ) . "</span>";
693 $s .= $dirmark;
695 if ( $rev->isMinor() ) {
696 $s .= ' ' . ChangesList::flag( 'minor', $this->getContext() );
699 # Sometimes rev_len isn't populated
700 if ( $rev->getSize() !== null ) {
701 # Size is always public data
702 $prevSize = isset( $this->parentLens[$row->rev_parent_id] )
703 ? $this->parentLens[$row->rev_parent_id]
704 : 0;
705 $sDiff = ChangesList::showCharacterDifference( $prevSize, $rev->getSize() );
706 $fSize = Linker::formatRevisionSize( $rev->getSize() );
707 $s .= ' <span class="mw-changeslist-separator">. .</span> ' . "$fSize $sDiff";
710 # Text following the character difference is added just before running hooks
711 $s2 = Linker::revComment( $rev, false, true );
713 if ( $notificationtimestamp && ( $row->rev_timestamp >= $notificationtimestamp ) ) {
714 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
715 $classes[] = 'mw-history-line-updated';
718 $tools = [];
720 # Rollback and undo links
721 if ( $prevRev && $this->getTitle()->quickUserCan( 'edit', $user ) ) {
722 if ( $latest && $this->getTitle()->quickUserCan( 'rollback', $user ) ) {
723 // Get a rollback link without the brackets
724 $rollbackLink = Linker::generateRollback(
725 $rev,
726 $this->getContext(),
727 [ 'verify', 'noBrackets' ]
729 if ( $rollbackLink ) {
730 $this->preventClickjacking();
731 $tools[] = $rollbackLink;
735 if ( !$rev->isDeleted( Revision::DELETED_TEXT )
736 && !$prevRev->isDeleted( Revision::DELETED_TEXT )
738 # Create undo tooltip for the first (=latest) line only
739 $undoTooltip = $latest
740 ? [ 'title' => $this->msg( 'tooltip-undo' )->text() ]
741 : [];
742 $undolink = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
743 $this->getTitle(),
744 $this->msg( 'editundo' )->text(),
745 $undoTooltip,
747 'action' => 'edit',
748 'undoafter' => $prevRev->getId(),
749 'undo' => $rev->getId()
752 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
755 // Allow extension to add their own links here
756 Hooks::run( 'HistoryRevisionTools', [ $rev, &$tools, $prevRev, $user ] );
758 if ( $tools ) {
759 $s2 .= ' ' . $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped();
762 # Tags
763 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
764 $row->ts_tags,
765 'history',
766 $this->getContext()
768 $classes = array_merge( $classes, $newClasses );
769 if ( $tagSummary !== '' ) {
770 $s2 .= " $tagSummary";
773 # Include separator between character difference and following text
774 if ( $s2 !== '' ) {
775 $s .= ' <span class="mw-changeslist-separator">. .</span> ' . $s2;
778 Hooks::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes ] );
780 $attribs = [];
781 if ( $classes ) {
782 $attribs['class'] = implode( ' ', $classes );
785 return Xml::tags( 'li', $attribs, $s ) . "\n";
789 * Create a link to view this revision of the page
791 * @param Revision $rev
792 * @return string
794 function revLink( $rev ) {
795 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
796 if ( $rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
797 $link = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
798 $this->getTitle(),
799 $date,
800 [ 'class' => 'mw-changeslist-date' ],
801 [ 'oldid' => $rev->getId() ]
803 } else {
804 $link = htmlspecialchars( $date );
806 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
807 $link = "<span class=\"history-deleted\">$link</span>";
810 return $link;
814 * Create a diff-to-current link for this revision for this page
816 * @param Revision $rev
817 * @param bool $latest This is the latest revision of the page?
818 * @return string
820 function curLink( $rev, $latest ) {
821 $cur = $this->historyPage->message['cur'];
822 if ( $latest || !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
823 return $cur;
824 } else {
825 return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
826 $this->getTitle(),
827 $cur,
830 'diff' => $this->getWikiPage()->getLatest(),
831 'oldid' => $rev->getId()
838 * Create a diff-to-previous link for this revision for this page.
840 * @param Revision $prevRev The revision being displayed
841 * @param stdClass|string|null $next The next revision in list (that is
842 * the previous one in chronological order).
843 * May either be a row, "unknown" or null.
844 * @return string
846 function lastLink( $prevRev, $next ) {
847 $last = $this->historyPage->message['last'];
849 if ( $next === null ) {
850 # Probably no next row
851 return $last;
854 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
855 if ( $next === 'unknown' ) {
856 # Next row probably exists but is unknown, use an oldid=prev link
857 return $linkRenderer->makeKnownLink(
858 $this->getTitle(),
859 $last,
862 'diff' => $prevRev->getId(),
863 'oldid' => 'prev'
868 $nextRev = new Revision( $next );
870 if ( !$prevRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
871 || !$nextRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
873 return $last;
876 return $linkRenderer->makeKnownLink(
877 $this->getTitle(),
878 $last,
881 'diff' => $prevRev->getId(),
882 'oldid' => $next->rev_id
888 * Create radio buttons for page history
890 * @param Revision $rev
891 * @param bool $firstInList Is this version the first one?
893 * @return string HTML output for the radio buttons
895 function diffButtons( $rev, $firstInList ) {
896 if ( $this->getNumRows() > 1 ) {
897 $id = $rev->getId();
898 $radio = [ 'type' => 'radio', 'value' => $id ];
899 /** @todo Move title texts to javascript */
900 if ( $firstInList ) {
901 $first = Xml::element( 'input',
902 array_merge( $radio, [
903 'style' => 'visibility:hidden',
904 'name' => 'oldid',
905 'id' => 'mw-oldid-null' ] )
907 $checkmark = [ 'checked' => 'checked' ];
908 } else {
909 # Check visibility of old revisions
910 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
911 $radio['disabled'] = 'disabled';
912 $checkmark = []; // We will check the next possible one
913 } elseif ( !$this->oldIdChecked ) {
914 $checkmark = [ 'checked' => 'checked' ];
915 $this->oldIdChecked = $id;
916 } else {
917 $checkmark = [];
919 $first = Xml::element( 'input',
920 array_merge( $radio, $checkmark, [
921 'name' => 'oldid',
922 'id' => "mw-oldid-$id" ] ) );
923 $checkmark = [];
925 $second = Xml::element( 'input',
926 array_merge( $radio, $checkmark, [
927 'name' => 'diff',
928 'id' => "mw-diff-$id" ] ) );
930 return $first . $second;
931 } else {
932 return '';
937 * This is called if a write operation is possible from the generated HTML
938 * @param bool $enable
940 function preventClickjacking( $enable = true ) {
941 $this->preventClickjacking = $enable;
945 * Get the "prevent clickjacking" flag
946 * @return bool
948 function getPreventClickjacking() {
949 return $this->preventClickjacking;