5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
30 class HistoryPager
extends ReverseChronologicalPager
{
34 public $lastRow = false;
36 public $counter, $historyPage, $buttons, $conds;
38 protected $oldIdChecked;
40 protected $preventClickjacking = false;
44 protected $parentLens;
46 /** @var bool Whether to show the tag editing UI */
47 protected $showTagEditUI;
53 * @param HistoryAction $historyPage
55 * @param string $month
56 * @param string $tagFilter
59 public function __construct(
60 HistoryAction
$historyPage,
66 parent
::__construct( $historyPage->getContext() );
67 $this->historyPage
= $historyPage;
68 $this->tagFilter
= $tagFilter;
69 $this->getDateCond( $year, $month );
70 $this->conds
= $conds;
71 $this->showTagEditUI
= ChangeTags
::showTagEditingUI( $this->getUser() );
74 // For hook compatibility...
75 function getArticle() {
76 return $this->historyPage
->getArticle();
79 function getSqlComment() {
81 return 'history page filtered'; // potentially slow, see CR r58153
83 return 'history page unfiltered';
87 function getQueryInfo() {
88 $revQuery = Revision
::getQueryInfo( [ 'user' ] );
90 'tables' => $revQuery['tables'],
91 'fields' => $revQuery['fields'],
92 'conds' => array_merge(
93 [ 'rev_page' => $this->getWikiPage()->getId() ],
95 'options' => [ 'USE INDEX' => [ 'revision' => 'page_timestamp' ] ],
96 'join_conds' => $revQuery['joins'],
98 ChangeTags
::modifyDisplayQuery(
100 $queryInfo['fields'],
102 $queryInfo['join_conds'],
103 $queryInfo['options'],
107 // Avoid PHP 7.1 warning of passing $this by reference
108 $historyPager = $this;
109 Hooks
::run( 'PageHistoryPager::getQueryInfo', [ &$historyPager, &$queryInfo ] );
114 function getIndexField() {
115 return 'rev_timestamp';
119 * @param stdClass $row
122 function formatRow( $row ) {
123 if ( $this->lastRow
) {
124 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
125 $firstInList = $this->counter
== 1;
128 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
129 ?
$this->getTitle()->getNotificationTimestamp( $this->getUser() )
132 $s = $this->historyLine(
133 $this->lastRow
, $row, $notifTimestamp, $latest, $firstInList );
137 $this->lastRow
= $row;
142 protected function doBatchLookups() {
143 if ( !Hooks
::run( 'PageHistoryPager::doBatchLookups', [ $this, $this->mResult
] ) ) {
147 # Do a link batch query
148 $this->mResult
->seek( 0 );
149 $batch = new LinkBatch();
151 foreach ( $this->mResult
as $row ) {
152 if ( $row->rev_parent_id
) {
153 $revIds[] = $row->rev_parent_id
;
155 if ( $row->user_name
!== null ) {
156 $batch->add( NS_USER
, $row->user_name
);
157 $batch->add( NS_USER_TALK
, $row->user_name
);
158 } else { # for anons or usernames of imported revisions
159 $batch->add( NS_USER
, $row->rev_user_text
);
160 $batch->add( NS_USER_TALK
, $row->rev_user_text
);
163 $this->parentLens
= Revision
::getParentLengths( $this->mDb
, $revIds );
165 $this->mResult
->seek( 0 );
169 * Creates begin of history list with a submit button
171 * @return string HTML output
173 protected function getStartBody() {
174 $this->lastRow
= false;
176 $this->oldIdChecked
= 0;
178 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
179 $s = Html
::openElement( 'form', [ 'action' => wfScript(),
180 'id' => 'mw-history-compare' ] ) . "\n";
181 $s .= Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n";
182 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
183 $s .= Html
::hidden( 'type', 'revision' ) . "\n";
185 // Button container stored in $this->buttons for re-use in getEndBody()
186 $this->buttons
= '<div>';
187 $className = 'historysubmit mw-history-compareselectedversions-button';
188 $attrs = [ 'class' => $className ]
189 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' );
190 $this->buttons
.= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
194 $user = $this->getUser();
196 if ( $user->isAllowed( 'deleterevision' ) ) {
197 $actionButtons .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
199 if ( $this->showTagEditUI
) {
200 $actionButtons .= $this->getRevisionButton( 'editchangetags', 'history-edit-tags' );
202 if ( $actionButtons ) {
203 $this->buttons
.= Xml
::tags( 'div', [ 'class' =>
204 'mw-history-revisionactions' ], $actionButtons );
207 if ( $user->isAllowed( 'deleterevision' ) ||
$this->showTagEditUI
) {
208 $this->buttons
.= ( new ListToggle( $this->getOutput() ) )->getHTML();
211 $this->buttons
.= '</div>';
213 $s .= $this->buttons
;
214 $s .= '<ul id="pagehistory">' . "\n";
219 private function getRevisionButton( $name, $msg ) {
220 $this->preventClickjacking();
221 # Note T22966, <button> is non-standard in IE<8
222 $element = Html
::element(
228 'class' => "historysubmit mw-history-$name-button",
230 $this->msg( $msg )->text()
235 protected function getEndBody() {
236 if ( $this->lastRow
) {
237 $latest = $this->counter
== 1 && $this->mIsFirst
;
238 $firstInList = $this->counter
== 1;
239 if ( $this->mIsBackwards
) {
240 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
241 if ( $this->mOffset
== '' ) {
247 # The next row is the past-the-end row
248 $next = $this->mPastTheEndRow
;
252 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
253 ?
$this->getTitle()->getNotificationTimestamp( $this->getUser() )
256 $s = $this->historyLine(
257 $this->lastRow
, $next, $notifTimestamp, $latest, $firstInList );
262 # Add second buttons only if there is more than one rev
263 if ( $this->getNumRows() > 2 ) {
264 $s .= $this->buttons
;
272 * Creates a submit button
274 * @param string $message Text of the submit button, will be escaped
275 * @param array $attributes
276 * @return string HTML output for the submit button
278 function submitButton( $message, $attributes = [] ) {
279 # Disable submit button if history has 1 revision only
280 if ( $this->getNumRows() > 1 ) {
281 return Html
::submitButton( $message, $attributes );
288 * Returns a row from the history printout.
290 * @todo document some more, and maybe clean up the code (some params redundant?)
292 * @param stdClass $row The database row corresponding to the previous line.
293 * @param mixed $next The database row corresponding to the next line
294 * (chronologically previous)
295 * @param bool|string $notificationtimestamp
296 * @param bool $latest Whether this row corresponds to the page's latest revision.
297 * @param bool $firstInList Whether this row corresponds to the first
298 * displayed on this history page.
299 * @return string HTML output for the row
301 function historyLine( $row, $next, $notificationtimestamp = false,
302 $latest = false, $firstInList = false ) {
303 $rev = new Revision( $row, 0, $this->getTitle() );
305 if ( is_object( $next ) ) {
306 $prevRev = new Revision( $next, 0, $this->getTitle() );
311 $curlink = $this->curLink( $rev, $latest );
312 $lastlink = $this->lastLink( $rev, $next );
313 $curLastlinks = $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink;
314 $histLinks = Html
::rawElement(
316 [ 'class' => 'mw-history-histlinks' ],
317 $this->msg( 'parentheses' )->rawParams( $curLastlinks )->escaped()
320 $diffButtons = $this->diffButtons( $rev, $firstInList );
321 $s = $histLinks . $diffButtons;
323 $link = $this->revLink( $rev );
327 $user = $this->getUser();
328 $canRevDelete = $user->isAllowed( 'deleterevision' );
329 // Show checkboxes for each revision, to allow for revision deletion and
331 if ( $canRevDelete ||
$this->showTagEditUI
) {
332 $this->preventClickjacking();
333 // If revision was hidden from sysops and we don't need the checkbox
334 // for anything else, disable it
335 if ( !$this->showTagEditUI
&& !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
336 $del = Xml
::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
337 // Otherwise, enable the checkbox...
339 $del = Xml
::check( 'showhiderevisions', false,
340 [ 'name' => 'ids[' . $rev->getId() . ']' ] );
342 // User can only view deleted revisions...
343 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
344 // If revision was hidden from sysops, disable the link
345 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
346 $del = Linker
::revDeleteLinkDisabled( false );
347 // Otherwise, show the link...
349 $query = [ 'type' => 'revision',
350 'target' => $this->getTitle()->getPrefixedDBkey(), 'ids' => $rev->getId() ];
351 $del .= Linker
::revDeleteLink( $query,
352 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
359 $lang = $this->getLanguage();
360 $dirmark = $lang->getDirMark();
364 $s .= " <span class='history-user'>" .
365 Linker
::revUserTools( $rev, true ) . "</span>";
368 if ( $rev->isMinor() ) {
369 $s .= ' ' . ChangesList
::flag( 'minor', $this->getContext() );
372 # Sometimes rev_len isn't populated
373 if ( $rev->getSize() !== null ) {
374 # Size is always public data
375 $prevSize = $this->parentLens
[$row->rev_parent_id
] ??
0;
376 $sDiff = ChangesList
::showCharacterDifference( $prevSize, $rev->getSize() );
377 $fSize = Linker
::formatRevisionSize( $rev->getSize() );
378 $s .= ' <span class="mw-changeslist-separator">. .</span> ' . "$fSize $sDiff";
381 # Text following the character difference is added just before running hooks
382 $s2 = Linker
::revComment( $rev, false, true );
384 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
385 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
386 $classes[] = 'mw-history-line-updated';
391 # Rollback and undo links
392 if ( $prevRev && $this->getTitle()->quickUserCan( 'edit', $user ) ) {
393 if ( $latest && $this->getTitle()->quickUserCan( 'rollback', $user ) ) {
394 // Get a rollback link without the brackets
395 $rollbackLink = Linker
::generateRollback(
398 [ 'verify', 'noBrackets' ]
400 if ( $rollbackLink ) {
401 $this->preventClickjacking();
402 $tools[] = $rollbackLink;
406 if ( !$rev->isDeleted( Revision
::DELETED_TEXT
)
407 && !$prevRev->isDeleted( Revision
::DELETED_TEXT
)
409 # Create undo tooltip for the first (=latest) line only
410 $undoTooltip = $latest
411 ?
[ 'title' => $this->msg( 'tooltip-undo' )->text() ]
413 $undolink = MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
415 $this->msg( 'editundo' )->text(),
419 'undoafter' => $prevRev->getId(),
420 'undo' => $rev->getId()
423 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
426 // Allow extension to add their own links here
427 Hooks
::run( 'HistoryRevisionTools', [ $rev, &$tools, $prevRev, $user ] );
430 $s2 .= ' ' . $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped();
434 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
439 $classes = array_merge( $classes, $newClasses );
440 if ( $tagSummary !== '' ) {
441 $s2 .= " $tagSummary";
444 # Include separator between character difference and following text
446 $s .= ' <span class="mw-changeslist-separator">. .</span> ' . $s2;
449 $attribs = [ 'data-mw-revid' => $rev->getId() ];
451 Hooks
::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes, &$attribs ] );
452 $attribs = array_filter( $attribs,
453 [ Sanitizer
::class, 'isReservedDataAttribute' ],
458 $attribs['class'] = implode( ' ', $classes );
461 return Xml
::tags( 'li', $attribs, $s ) . "\n";
465 * Create a link to view this revision of the page
467 * @param Revision $rev
470 function revLink( $rev ) {
471 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
472 if ( $rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
473 $link = MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
476 [ 'class' => 'mw-changeslist-date' ],
477 [ 'oldid' => $rev->getId() ]
480 $link = htmlspecialchars( $date );
482 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
483 $link = "<span class=\"history-deleted\">$link</span>";
490 * Create a diff-to-current link for this revision for this page
492 * @param Revision $rev
493 * @param bool $latest This is the latest revision of the page?
496 function curLink( $rev, $latest ) {
497 $cur = $this->historyPage
->message
['cur'];
498 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
501 return MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
503 new HtmlArmor( $cur ),
506 'diff' => $this->getWikiPage()->getLatest(),
507 'oldid' => $rev->getId()
514 * Create a diff-to-previous link for this revision for this page.
516 * @param Revision $prevRev The revision being displayed
517 * @param stdClass|string|null $next The next revision in list (that is
518 * the previous one in chronological order).
519 * May either be a row, "unknown" or null.
522 function lastLink( $prevRev, $next ) {
523 $last = $this->historyPage
->message
['last'];
525 if ( $next === null ) {
526 # Probably no next row
530 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
531 if ( $next === 'unknown' ) {
532 # Next row probably exists but is unknown, use an oldid=prev link
533 return $linkRenderer->makeKnownLink(
535 new HtmlArmor( $last ),
538 'diff' => $prevRev->getId(),
544 $nextRev = new Revision( $next );
546 if ( !$prevRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() )
547 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() )
552 return $linkRenderer->makeKnownLink(
554 new HtmlArmor( $last ),
557 'diff' => $prevRev->getId(),
558 'oldid' => $next->rev_id
564 * Create radio buttons for page history
566 * @param Revision $rev
567 * @param bool $firstInList Is this version the first one?
569 * @return string HTML output for the radio buttons
571 function diffButtons( $rev, $firstInList ) {
572 if ( $this->getNumRows() > 1 ) {
574 $radio = [ 'type' => 'radio', 'value' => $id ];
575 /** @todo Move title texts to javascript */
576 if ( $firstInList ) {
577 $first = Xml
::element( 'input',
578 array_merge( $radio, [
579 'style' => 'visibility:hidden',
581 'id' => 'mw-oldid-null' ] )
583 $checkmark = [ 'checked' => 'checked' ];
585 # Check visibility of old revisions
586 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
587 $radio['disabled'] = 'disabled';
588 $checkmark = []; // We will check the next possible one
589 } elseif ( !$this->oldIdChecked
) {
590 $checkmark = [ 'checked' => 'checked' ];
591 $this->oldIdChecked
= $id;
595 $first = Xml
::element( 'input',
596 array_merge( $radio, $checkmark, [
598 'id' => "mw-oldid-$id" ] ) );
601 $second = Xml
::element( 'input',
602 array_merge( $radio, $checkmark, [
604 'id' => "mw-diff-$id" ] ) );
606 return $first . $second;
613 * This is called if a write operation is possible from the generated HTML
614 * @param bool $enable
616 function preventClickjacking( $enable = true ) {
617 $this->preventClickjacking
= $enable;
621 * Get the "prevent clickjacking" flag
624 function getPreventClickjacking() {
625 return $this->preventClickjacking
;