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