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