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