Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / actions / HistoryAction.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup Actions
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use Wikimedia\Rdbms\ResultWrapper;
28 use Wikimedia\Rdbms\FakeResultWrapper;
29
30 /**
31 * This class handles printing the history page for an article. In order to
32 * be efficient, it uses timestamps rather than offsets for paging, to avoid
33 * costly LIMIT,offset queries.
34 *
35 * Construct it by passing in an Article, and call $h->history() to print the
36 * history.
37 *
38 * @ingroup Actions
39 */
40 class HistoryAction extends FormlessAction {
41 const DIR_PREV = 0;
42 const DIR_NEXT = 1;
43
44 /** @var array Array of message keys and strings */
45 public $message;
46
47 public function getName() {
48 return 'history';
49 }
50
51 public function requiresWrite() {
52 return false;
53 }
54
55 public function requiresUnblock() {
56 return false;
57 }
58
59 protected function getPageTitle() {
60 return $this->msg( 'history-title', $this->getTitle()->getPrefixedText() )->text();
61 }
62
63 protected function getDescription() {
64 // Creation of a subtitle link pointing to [[Special:Log]]
65 return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
66 SpecialPage::getTitleFor( 'Log' ),
67 $this->msg( 'viewpagelogs' )->text(),
68 [],
69 [ 'page' => $this->getTitle()->getPrefixedText() ]
70 );
71 }
72
73 /**
74 * @return WikiPage|Article|ImagePage|CategoryPage|Page The Article object we are working on.
75 */
76 public function getArticle() {
77 return $this->page;
78 }
79
80 /**
81 * As we use the same small set of messages in various methods and that
82 * they are called often, we call them once and save them in $this->message
83 */
84 private function preCacheMessages() {
85 // Precache various messages
86 if ( !isset( $this->message ) ) {
87 $msgs = [ 'cur', 'last', 'pipe-separator' ];
88 foreach ( $msgs as $msg ) {
89 $this->message[$msg] = $this->msg( $msg )->escaped();
90 }
91 }
92 }
93
94 /**
95 * Print the history page for an article.
96 */
97 function onView() {
98 $out = $this->getOutput();
99 $request = $this->getRequest();
100
101 /**
102 * Allow client caching.
103 */
104 if ( $out->checkLastModified( $this->page->getTouched() ) ) {
105 return; // Client cache fresh and headers sent, nothing more to do.
106 }
107
108 $this->preCacheMessages();
109 $config = $this->context->getConfig();
110
111 # Fill in the file cache if not set already
112 if ( HTMLFileCache::useFileCache( $this->getContext() ) ) {
113 $cache = new HTMLFileCache( $this->getTitle(), 'history' );
114 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
115 ob_start( [ &$cache, 'saveToFileCache' ] );
116 }
117 }
118
119 // Setup page variables.
120 $out->setFeedAppendQuery( 'action=history' );
121 $out->addModules( 'mediawiki.action.history' );
122 $out->addModuleStyles( [
123 'mediawiki.action.history.styles',
124 'mediawiki.special.changeslist',
125 ] );
126 if ( $config->get( 'UseMediaWikiUIEverywhere' ) ) {
127 $out = $this->getOutput();
128 $out->addModuleStyles( [
129 'mediawiki.ui.input',
130 'mediawiki.ui.checkbox',
131 ] );
132 }
133
134 // Handle atom/RSS feeds.
135 $feedType = $request->getVal( 'feed' );
136 if ( $feedType ) {
137 $this->feed( $feedType );
138
139 return;
140 }
141
142 $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Page_history', true );
143
144 // Fail nicely if article doesn't exist.
145 if ( !$this->page->exists() ) {
146 global $wgSend404Code;
147 if ( $wgSend404Code ) {
148 $out->setStatusCode( 404 );
149 }
150 $out->addWikiMsg( 'nohistory' );
151
152 $dbr = wfGetDB( DB_REPLICA );
153
154 # show deletion/move log if there is an entry
155 LogEventsList::showLogExtract(
156 $out,
157 [ 'delete', 'move' ],
158 $this->getTitle(),
159 '',
160 [ 'lim' => 10,
161 'conds' => [ 'log_action != ' . $dbr->addQuotes( 'revision' ) ],
162 'showIfEmpty' => false,
163 'msgKey' => [ 'moveddeleted-notice' ]
164 ]
165 );
166
167 return;
168 }
169
170 /**
171 * Add date selector to quickly get to a certain time
172 */
173 $year = $request->getInt( 'year' );
174 $month = $request->getInt( 'month' );
175 $tagFilter = $request->getVal( 'tagfilter' );
176 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter, false, $this->getContext() );
177
178 /**
179 * Option to show only revisions that have been (partially) hidden via RevisionDelete
180 */
181 if ( $request->getBool( 'deleted' ) ) {
182 $conds = [ 'rev_deleted != 0' ];
183 } else {
184 $conds = [];
185 }
186 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
187 $checkDeleted = Xml::checkLabel( $this->msg( 'history-show-deleted' )->text(),
188 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
189 } else {
190 $checkDeleted = '';
191 }
192
193 // Add the general form
194 $action = htmlspecialchars( wfScript() );
195 $out->addHTML(
196 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
197 Xml::fieldset(
198 $this->msg( 'history-fieldset-title' )->text(),
199 false,
200 [ 'id' => 'mw-history-search' ]
201 ) .
202 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n" .
203 Html::hidden( 'action', 'history' ) . "\n" .
204 Xml::dateMenu(
205 ( $year == null ? MWTimestamp::getLocalInstance()->format( 'Y' ) : $year ),
206 $month
207 ) . '&#160;' .
208 ( $tagSelector ? ( implode( '&#160;', $tagSelector ) . '&#160;' ) : '' ) .
209 $checkDeleted .
210 Html::submitButton(
211 $this->msg( 'historyaction-submit' )->text(),
212 [],
213 [ 'mw-ui-progressive' ]
214 ) . "\n" .
215 '</fieldset></form>'
216 );
217
218 Hooks::run( 'PageHistoryBeforeList', [ &$this->page, $this->getContext() ] );
219
220 // Create and output the list.
221 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
222 $out->addHTML(
223 $pager->getNavigationBar() .
224 $pager->getBody() .
225 $pager->getNavigationBar()
226 );
227 $out->preventClickjacking( $pager->getPreventClickjacking() );
228 }
229
230 /**
231 * Fetch an array of revisions, specified by a given limit, offset and
232 * direction. This is now only used by the feeds. It was previously
233 * used by the main UI but that's now handled by the pager.
234 *
235 * @param int $limit The limit number of revisions to get
236 * @param int $offset
237 * @param int $direction Either self::DIR_PREV or self::DIR_NEXT
238 * @return ResultWrapper
239 */
240 function fetchRevisions( $limit, $offset, $direction ) {
241 // Fail if article doesn't exist.
242 if ( !$this->getTitle()->exists() ) {
243 return new FakeResultWrapper( [] );
244 }
245
246 $dbr = wfGetDB( DB_REPLICA );
247
248 if ( $direction === self::DIR_PREV ) {
249 list( $dirs, $oper ) = [ "ASC", ">=" ];
250 } else { /* $direction === self::DIR_NEXT */
251 list( $dirs, $oper ) = [ "DESC", "<=" ];
252 }
253
254 if ( $offset ) {
255 $offsets = [ "rev_timestamp $oper " . $dbr->addQuotes( $dbr->timestamp( $offset ) ) ];
256 } else {
257 $offsets = [];
258 }
259
260 $page_id = $this->page->getId();
261
262 return $dbr->select( 'revision',
263 Revision::selectFields(),
264 array_merge( [ 'rev_page' => $page_id ], $offsets ),
265 __METHOD__,
266 [ 'ORDER BY' => "rev_timestamp $dirs",
267 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit ]
268 );
269 }
270
271 /**
272 * Output a subscription feed listing recent edits to this page.
273 *
274 * @param string $type Feed type
275 */
276 function feed( $type ) {
277 if ( !FeedUtils::checkFeedOutput( $type ) ) {
278 return;
279 }
280 $request = $this->getRequest();
281
282 $feedClasses = $this->context->getConfig()->get( 'FeedClasses' );
283 /** @var RSSFeed|AtomFeed $feed */
284 $feed = new $feedClasses[$type](
285 $this->getTitle()->getPrefixedText() . ' - ' .
286 $this->msg( 'history-feed-title' )->inContentLanguage()->text(),
287 $this->msg( 'history-feed-description' )->inContentLanguage()->text(),
288 $this->getTitle()->getFullURL( 'action=history' )
289 );
290
291 // Get a limit on number of feed entries. Provide a sane default
292 // of 10 if none is defined (but limit to $wgFeedLimit max)
293 $limit = $request->getInt( 'limit', 10 );
294 $limit = min(
295 max( $limit, 1 ),
296 $this->context->getConfig()->get( 'FeedLimit' )
297 );
298
299 $items = $this->fetchRevisions( $limit, 0, self::DIR_NEXT );
300
301 // Generate feed elements enclosed between header and footer.
302 $feed->outHeader();
303 if ( $items->numRows() ) {
304 foreach ( $items as $row ) {
305 $feed->outItem( $this->feedItem( $row ) );
306 }
307 } else {
308 $feed->outItem( $this->feedEmpty() );
309 }
310 $feed->outFooter();
311 }
312
313 function feedEmpty() {
314 return new FeedItem(
315 $this->msg( 'nohistory' )->inContentLanguage()->text(),
316 $this->msg( 'history-feed-empty' )->inContentLanguage()->parseAsBlock(),
317 $this->getTitle()->getFullURL(),
318 wfTimestamp( TS_MW ),
319 '',
320 $this->getTitle()->getTalkPage()->getFullURL()
321 );
322 }
323
324 /**
325 * Generate a FeedItem object from a given revision table row
326 * Borrows Recent Changes' feed generation functions for formatting;
327 * includes a diff to the previous revision (if any).
328 *
329 * @param stdClass|array $row Database row
330 * @return FeedItem
331 */
332 function feedItem( $row ) {
333 $rev = new Revision( $row );
334 $rev->setTitle( $this->getTitle() );
335 $text = FeedUtils::formatDiffRow(
336 $this->getTitle(),
337 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
338 $rev->getId(),
339 $rev->getTimestamp(),
340 $rev->getComment()
341 );
342 if ( $rev->getComment() == '' ) {
343 global $wgContLang;
344 $title = $this->msg( 'history-feed-item-nocomment',
345 $rev->getUserText(),
346 $wgContLang->timeanddate( $rev->getTimestamp() ),
347 $wgContLang->date( $rev->getTimestamp() ),
348 $wgContLang->time( $rev->getTimestamp() ) )->inContentLanguage()->text();
349 } else {
350 $title = $rev->getUserText() .
351 $this->msg( 'colon-separator' )->inContentLanguage()->text() .
352 FeedItem::stripComment( $rev->getComment() );
353 }
354
355 return new FeedItem(
356 $title,
357 $text,
358 $this->getTitle()->getFullURL( 'diff=' . $rev->getId() . '&oldid=prev' ),
359 $rev->getTimestamp(),
360 $rev->getUserText(),
361 $this->getTitle()->getTalkPage()->getFullURL()
362 );
363 }
364 }
365
366 /**
367 * @ingroup Pager
368 * @ingroup Actions
369 */
370 class HistoryPager extends ReverseChronologicalPager {
371 /**
372 * @var bool|stdClass
373 */
374 public $lastRow = false;
375
376 public $counter, $historyPage, $buttons, $conds;
377
378 protected $oldIdChecked;
379
380 protected $preventClickjacking = false;
381 /**
382 * @var array
383 */
384 protected $parentLens;
385
386 /** @var bool Whether to show the tag editing UI */
387 protected $showTagEditUI;
388
389 /**
390 * @param HistoryAction $historyPage
391 * @param string $year
392 * @param string $month
393 * @param string $tagFilter
394 * @param array $conds
395 */
396 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = [] ) {
397 parent::__construct( $historyPage->getContext() );
398 $this->historyPage = $historyPage;
399 $this->tagFilter = $tagFilter;
400 $this->getDateCond( $year, $month );
401 $this->conds = $conds;
402 $this->showTagEditUI = ChangeTags::showTagEditingUI( $this->getUser() );
403 }
404
405 // For hook compatibility...
406 function getArticle() {
407 return $this->historyPage->getArticle();
408 }
409
410 function getSqlComment() {
411 if ( $this->conds ) {
412 return 'history page filtered'; // potentially slow, see CR r58153
413 } else {
414 return 'history page unfiltered';
415 }
416 }
417
418 function getQueryInfo() {
419 $queryInfo = [
420 'tables' => [ 'revision', 'user' ],
421 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
422 'conds' => array_merge(
423 [ 'rev_page' => $this->getWikiPage()->getId() ],
424 $this->conds ),
425 'options' => [ 'USE INDEX' => [ 'revision' => 'page_timestamp' ] ],
426 'join_conds' => [ 'user' => Revision::userJoinCond() ],
427 ];
428 ChangeTags::modifyDisplayQuery(
429 $queryInfo['tables'],
430 $queryInfo['fields'],
431 $queryInfo['conds'],
432 $queryInfo['join_conds'],
433 $queryInfo['options'],
434 $this->tagFilter
435 );
436
437 // Avoid PHP 7.1 warning of passing $this by reference
438 $historyPager = $this;
439 Hooks::run( 'PageHistoryPager::getQueryInfo', [ &$historyPager, &$queryInfo ] );
440
441 return $queryInfo;
442 }
443
444 function getIndexField() {
445 return 'rev_timestamp';
446 }
447
448 /**
449 * @param stdClass $row
450 * @return string
451 */
452 function formatRow( $row ) {
453 if ( $this->lastRow ) {
454 $latest = ( $this->counter == 1 && $this->mIsFirst );
455 $firstInList = $this->counter == 1;
456 $this->counter++;
457
458 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
459 ? $this->getTitle()->getNotificationTimestamp( $this->getUser() )
460 : false;
461
462 $s = $this->historyLine(
463 $this->lastRow, $row, $notifTimestamp, $latest, $firstInList );
464 } else {
465 $s = '';
466 }
467 $this->lastRow = $row;
468
469 return $s;
470 }
471
472 function doBatchLookups() {
473 if ( !Hooks::run( 'PageHistoryPager::doBatchLookups', [ $this, $this->mResult ] ) ) {
474 return;
475 }
476
477 # Do a link batch query
478 $this->mResult->seek( 0 );
479 $batch = new LinkBatch();
480 $revIds = [];
481 foreach ( $this->mResult as $row ) {
482 if ( $row->rev_parent_id ) {
483 $revIds[] = $row->rev_parent_id;
484 }
485 if ( !is_null( $row->user_name ) ) {
486 $batch->add( NS_USER, $row->user_name );
487 $batch->add( NS_USER_TALK, $row->user_name );
488 } else { # for anons or usernames of imported revisions
489 $batch->add( NS_USER, $row->rev_user_text );
490 $batch->add( NS_USER_TALK, $row->rev_user_text );
491 }
492 }
493 $this->parentLens = Revision::getParentLengths( $this->mDb, $revIds );
494 $batch->execute();
495 $this->mResult->seek( 0 );
496 }
497
498 /**
499 * Creates begin of history list with a submit button
500 *
501 * @return string HTML output
502 */
503 function getStartBody() {
504 $this->lastRow = false;
505 $this->counter = 1;
506 $this->oldIdChecked = 0;
507
508 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
509 $s = Html::openElement( 'form', [ 'action' => wfScript(),
510 'id' => 'mw-history-compare' ] ) . "\n";
511 $s .= Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n";
512 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
513 $s .= Html::hidden( 'type', 'revision' ) . "\n";
514
515 // Button container stored in $this->buttons for re-use in getEndBody()
516 $this->buttons = '<div>';
517 $className = 'historysubmit mw-history-compareselectedversions-button';
518 $attrs = [ 'class' => $className ]
519 + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' );
520 $this->buttons .= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
521 $attrs
522 ) . "\n";
523
524 $user = $this->getUser();
525 $actionButtons = '';
526 if ( $user->isAllowed( 'deleterevision' ) ) {
527 $actionButtons .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
528 }
529 if ( $this->showTagEditUI ) {
530 $actionButtons .= $this->getRevisionButton( 'editchangetags', 'history-edit-tags' );
531 }
532 if ( $actionButtons ) {
533 $this->buttons .= Xml::tags( 'div', [ 'class' =>
534 'mw-history-revisionactions' ], $actionButtons );
535 }
536
537 if ( $user->isAllowed( 'deleterevision' ) || $this->showTagEditUI ) {
538 $this->buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
539 }
540
541 $this->buttons .= '</div>';
542
543 $s .= $this->buttons;
544 $s .= '<ul id="pagehistory">' . "\n";
545
546 return $s;
547 }
548
549 private function getRevisionButton( $name, $msg ) {
550 $this->preventClickjacking();
551 # Note bug #20966, <button> is non-standard in IE<8
552 $element = Html::element(
553 'button',
554 [
555 'type' => 'submit',
556 'name' => $name,
557 'value' => '1',
558 'class' => "historysubmit mw-history-$name-button",
559 ],
560 $this->msg( $msg )->text()
561 ) . "\n";
562 return $element;
563 }
564
565 function getEndBody() {
566 if ( $this->lastRow ) {
567 $latest = $this->counter == 1 && $this->mIsFirst;
568 $firstInList = $this->counter == 1;
569 if ( $this->mIsBackwards ) {
570 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
571 if ( $this->mOffset == '' ) {
572 $next = null;
573 } else {
574 $next = 'unknown';
575 }
576 } else {
577 # The next row is the past-the-end row
578 $next = $this->mPastTheEndRow;
579 }
580 $this->counter++;
581
582 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
583 ? $this->getTitle()->getNotificationTimestamp( $this->getUser() )
584 : false;
585
586 $s = $this->historyLine(
587 $this->lastRow, $next, $notifTimestamp, $latest, $firstInList );
588 } else {
589 $s = '';
590 }
591 $s .= "</ul>\n";
592 # Add second buttons only if there is more than one rev
593 if ( $this->getNumRows() > 2 ) {
594 $s .= $this->buttons;
595 }
596 $s .= '</form>';
597
598 return $s;
599 }
600
601 /**
602 * Creates a submit button
603 *
604 * @param string $message Text of the submit button, will be escaped
605 * @param array $attributes Attributes
606 * @return string HTML output for the submit button
607 */
608 function submitButton( $message, $attributes = [] ) {
609 # Disable submit button if history has 1 revision only
610 if ( $this->getNumRows() > 1 ) {
611 return Html::submitButton( $message, $attributes );
612 } else {
613 return '';
614 }
615 }
616
617 /**
618 * Returns a row from the history printout.
619 *
620 * @todo document some more, and maybe clean up the code (some params redundant?)
621 *
622 * @param stdClass $row The database row corresponding to the previous line.
623 * @param mixed $next The database row corresponding to the next line
624 * (chronologically previous)
625 * @param bool|string $notificationtimestamp
626 * @param bool $latest Whether this row corresponds to the page's latest revision.
627 * @param bool $firstInList Whether this row corresponds to the first
628 * displayed on this history page.
629 * @return string HTML output for the row
630 */
631 function historyLine( $row, $next, $notificationtimestamp = false,
632 $latest = false, $firstInList = false ) {
633 $rev = new Revision( $row );
634 $rev->setTitle( $this->getTitle() );
635
636 if ( is_object( $next ) ) {
637 $prevRev = new Revision( $next );
638 $prevRev->setTitle( $this->getTitle() );
639 } else {
640 $prevRev = null;
641 }
642
643 $curlink = $this->curLink( $rev, $latest );
644 $lastlink = $this->lastLink( $rev, $next );
645 $curLastlinks = $curlink . $this->historyPage->message['pipe-separator'] . $lastlink;
646 $histLinks = Html::rawElement(
647 'span',
648 [ 'class' => 'mw-history-histlinks' ],
649 $this->msg( 'parentheses' )->rawParams( $curLastlinks )->escaped()
650 );
651
652 $diffButtons = $this->diffButtons( $rev, $firstInList );
653 $s = $histLinks . $diffButtons;
654
655 $link = $this->revLink( $rev );
656 $classes = [];
657
658 $del = '';
659 $user = $this->getUser();
660 $canRevDelete = $user->isAllowed( 'deleterevision' );
661 // Show checkboxes for each revision, to allow for revision deletion and
662 // change tags
663 if ( $canRevDelete || $this->showTagEditUI ) {
664 $this->preventClickjacking();
665 // If revision was hidden from sysops and we don't need the checkbox
666 // for anything else, disable it
667 if ( !$this->showTagEditUI && !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
668 $del = Xml::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
669 // Otherwise, enable the checkbox...
670 } else {
671 $del = Xml::check( 'showhiderevisions', false,
672 [ 'name' => 'ids[' . $rev->getId() . ']' ] );
673 }
674 // User can only view deleted revisions...
675 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
676 // If revision was hidden from sysops, disable the link
677 if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
678 $del = Linker::revDeleteLinkDisabled( false );
679 // Otherwise, show the link...
680 } else {
681 $query = [ 'type' => 'revision',
682 'target' => $this->getTitle()->getPrefixedDBkey(), 'ids' => $rev->getId() ];
683 $del .= Linker::revDeleteLink( $query,
684 $rev->isDeleted( Revision::DELETED_RESTRICTED ), false );
685 }
686 }
687 if ( $del ) {
688 $s .= " $del ";
689 }
690
691 $lang = $this->getLanguage();
692 $dirmark = $lang->getDirMark();
693
694 $s .= " $link";
695 $s .= $dirmark;
696 $s .= " <span class='history-user'>" .
697 Linker::revUserTools( $rev, true ) . "</span>";
698 $s .= $dirmark;
699
700 if ( $rev->isMinor() ) {
701 $s .= ' ' . ChangesList::flag( 'minor', $this->getContext() );
702 }
703
704 # Sometimes rev_len isn't populated
705 if ( $rev->getSize() !== null ) {
706 # Size is always public data
707 $prevSize = isset( $this->parentLens[$row->rev_parent_id] )
708 ? $this->parentLens[$row->rev_parent_id]
709 : 0;
710 $sDiff = ChangesList::showCharacterDifference( $prevSize, $rev->getSize() );
711 $fSize = Linker::formatRevisionSize( $rev->getSize() );
712 $s .= ' <span class="mw-changeslist-separator">. .</span> ' . "$fSize $sDiff";
713 }
714
715 # Text following the character difference is added just before running hooks
716 $s2 = Linker::revComment( $rev, false, true );
717
718 if ( $notificationtimestamp && ( $row->rev_timestamp >= $notificationtimestamp ) ) {
719 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
720 $classes[] = 'mw-history-line-updated';
721 }
722
723 $tools = [];
724
725 # Rollback and undo links
726 if ( $prevRev && $this->getTitle()->quickUserCan( 'edit', $user ) ) {
727 if ( $latest && $this->getTitle()->quickUserCan( 'rollback', $user ) ) {
728 // Get a rollback link without the brackets
729 $rollbackLink = Linker::generateRollback(
730 $rev,
731 $this->getContext(),
732 [ 'verify', 'noBrackets' ]
733 );
734 if ( $rollbackLink ) {
735 $this->preventClickjacking();
736 $tools[] = $rollbackLink;
737 }
738 }
739
740 if ( !$rev->isDeleted( Revision::DELETED_TEXT )
741 && !$prevRev->isDeleted( Revision::DELETED_TEXT )
742 ) {
743 # Create undo tooltip for the first (=latest) line only
744 $undoTooltip = $latest
745 ? [ 'title' => $this->msg( 'tooltip-undo' )->text() ]
746 : [];
747 $undolink = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
748 $this->getTitle(),
749 $this->msg( 'editundo' )->text(),
750 $undoTooltip,
751 [
752 'action' => 'edit',
753 'undoafter' => $prevRev->getId(),
754 'undo' => $rev->getId()
755 ]
756 );
757 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
758 }
759 }
760 // Allow extension to add their own links here
761 Hooks::run( 'HistoryRevisionTools', [ $rev, &$tools, $prevRev, $user ] );
762
763 if ( $tools ) {
764 $s2 .= ' ' . $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped();
765 }
766
767 # Tags
768 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
769 $row->ts_tags,
770 'history',
771 $this->getContext()
772 );
773 $classes = array_merge( $classes, $newClasses );
774 if ( $tagSummary !== '' ) {
775 $s2 .= " $tagSummary";
776 }
777
778 # Include separator between character difference and following text
779 if ( $s2 !== '' ) {
780 $s .= ' <span class="mw-changeslist-separator">. .</span> ' . $s2;
781 }
782
783 $attribs = [ 'data-mw-revid' => $rev->getId() ];
784
785 Hooks::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes, &$attribs ] );
786 $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
787
788 if ( $classes ) {
789 $attribs['class'] = implode( ' ', $classes );
790 }
791
792 return Xml::tags( 'li', $attribs, $s ) . "\n";
793 }
794
795 /**
796 * Create a link to view this revision of the page
797 *
798 * @param Revision $rev
799 * @return string
800 */
801 function revLink( $rev ) {
802 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
803 if ( $rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
804 $link = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
805 $this->getTitle(),
806 $date,
807 [ 'class' => 'mw-changeslist-date' ],
808 [ 'oldid' => $rev->getId() ]
809 );
810 } else {
811 $link = htmlspecialchars( $date );
812 }
813 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
814 $link = "<span class=\"history-deleted\">$link</span>";
815 }
816
817 return $link;
818 }
819
820 /**
821 * Create a diff-to-current link for this revision for this page
822 *
823 * @param Revision $rev
824 * @param bool $latest This is the latest revision of the page?
825 * @return string
826 */
827 function curLink( $rev, $latest ) {
828 $cur = $this->historyPage->message['cur'];
829 if ( $latest || !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
830 return $cur;
831 } else {
832 return MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
833 $this->getTitle(),
834 $cur,
835 [],
836 [
837 'diff' => $this->getWikiPage()->getLatest(),
838 'oldid' => $rev->getId()
839 ]
840 );
841 }
842 }
843
844 /**
845 * Create a diff-to-previous link for this revision for this page.
846 *
847 * @param Revision $prevRev The revision being displayed
848 * @param stdClass|string|null $next The next revision in list (that is
849 * the previous one in chronological order).
850 * May either be a row, "unknown" or null.
851 * @return string
852 */
853 function lastLink( $prevRev, $next ) {
854 $last = $this->historyPage->message['last'];
855
856 if ( $next === null ) {
857 # Probably no next row
858 return $last;
859 }
860
861 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
862 if ( $next === 'unknown' ) {
863 # Next row probably exists but is unknown, use an oldid=prev link
864 return $linkRenderer->makeKnownLink(
865 $this->getTitle(),
866 $last,
867 [],
868 [
869 'diff' => $prevRev->getId(),
870 'oldid' => 'prev'
871 ]
872 );
873 }
874
875 $nextRev = new Revision( $next );
876
877 if ( !$prevRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
878 || !$nextRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
879 ) {
880 return $last;
881 }
882
883 return $linkRenderer->makeKnownLink(
884 $this->getTitle(),
885 $last,
886 [],
887 [
888 'diff' => $prevRev->getId(),
889 'oldid' => $next->rev_id
890 ]
891 );
892 }
893
894 /**
895 * Create radio buttons for page history
896 *
897 * @param Revision $rev
898 * @param bool $firstInList Is this version the first one?
899 *
900 * @return string HTML output for the radio buttons
901 */
902 function diffButtons( $rev, $firstInList ) {
903 if ( $this->getNumRows() > 1 ) {
904 $id = $rev->getId();
905 $radio = [ 'type' => 'radio', 'value' => $id ];
906 /** @todo Move title texts to javascript */
907 if ( $firstInList ) {
908 $first = Xml::element( 'input',
909 array_merge( $radio, [
910 'style' => 'visibility:hidden',
911 'name' => 'oldid',
912 'id' => 'mw-oldid-null' ] )
913 );
914 $checkmark = [ 'checked' => 'checked' ];
915 } else {
916 # Check visibility of old revisions
917 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
918 $radio['disabled'] = 'disabled';
919 $checkmark = []; // We will check the next possible one
920 } elseif ( !$this->oldIdChecked ) {
921 $checkmark = [ 'checked' => 'checked' ];
922 $this->oldIdChecked = $id;
923 } else {
924 $checkmark = [];
925 }
926 $first = Xml::element( 'input',
927 array_merge( $radio, $checkmark, [
928 'name' => 'oldid',
929 'id' => "mw-oldid-$id" ] ) );
930 $checkmark = [];
931 }
932 $second = Xml::element( 'input',
933 array_merge( $radio, $checkmark, [
934 'name' => 'diff',
935 'id' => "mw-diff-$id" ] ) );
936
937 return $first . $second;
938 } else {
939 return '';
940 }
941 }
942
943 /**
944 * This is called if a write operation is possible from the generated HTML
945 * @param bool $enable
946 */
947 function preventClickjacking( $enable = true ) {
948 $this->preventClickjacking = $enable;
949 }
950
951 /**
952 * Get the "prevent clickjacking" flag
953 * @return bool
954 */
955 function getPreventClickjacking() {
956 return $this->preventClickjacking;
957 }
958
959 }