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