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