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