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
;
25 use MediaWiki\Revision\RevisionRecord
;
31 class HistoryPager
extends ReverseChronologicalPager
{
35 public $lastRow = false;
37 public $counter, $historyPage, $buttons, $conds;
39 protected $oldIdChecked;
41 protected $preventClickjacking = false;
45 protected $parentLens;
47 /** @var bool Whether to show the tag editing UI */
48 protected $showTagEditUI;
54 * @param HistoryAction $historyPage
56 * @param string $month
57 * @param string $tagFilter
61 public function __construct(
62 HistoryAction
$historyPage,
69 parent
::__construct( $historyPage->getContext() );
70 $this->historyPage
= $historyPage;
71 $this->tagFilter
= $tagFilter;
72 $this->getDateCond( $year, $month, $day );
73 $this->conds
= $conds;
74 $this->showTagEditUI
= ChangeTags
::showTagEditingUI( $this->getUser() );
77 // For hook compatibility...
78 function getArticle() {
79 return $this->historyPage
->getArticle();
82 function getSqlComment() {
84 return 'history page filtered'; // potentially slow, see CR r58153
86 return 'history page unfiltered';
90 function getQueryInfo() {
91 $revQuery = Revision
::getQueryInfo( [ 'user' ] );
93 'tables' => $revQuery['tables'],
94 'fields' => $revQuery['fields'],
95 'conds' => array_merge(
96 [ 'rev_page' => $this->getWikiPage()->getId() ],
98 'options' => [ 'USE INDEX' => [ 'revision' => 'page_timestamp' ] ],
99 'join_conds' => $revQuery['joins'],
101 ChangeTags
::modifyDisplayQuery(
102 $queryInfo['tables'],
103 $queryInfo['fields'],
105 $queryInfo['join_conds'],
106 $queryInfo['options'],
110 // Avoid PHP 7.1 warning of passing $this by reference
111 $historyPager = $this;
112 Hooks
::run( 'PageHistoryPager::getQueryInfo', [ &$historyPager, &$queryInfo ] );
117 function getIndexField() {
118 return 'rev_timestamp';
122 * @param stdClass $row
125 function formatRow( $row ) {
126 if ( $this->lastRow
) {
127 $firstInList = $this->counter
== 1;
130 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
131 ?
$this->getTitle()->getNotificationTimestamp( $this->getUser() )
134 $s = $this->historyLine( $this->lastRow
, $row, $notifTimestamp, false, $firstInList );
138 $this->lastRow
= $row;
143 protected function doBatchLookups() {
144 if ( !Hooks
::run( 'PageHistoryPager::doBatchLookups', [ $this, $this->mResult
] ) ) {
148 # Do a link batch query
149 $this->mResult
->seek( 0 );
150 $batch = new LinkBatch();
152 foreach ( $this->mResult
as $row ) {
153 if ( $row->rev_parent_id
) {
154 $revIds[] = $row->rev_parent_id
;
156 if ( $row->user_name
!== null ) {
157 $batch->add( NS_USER
, $row->user_name
);
158 $batch->add( NS_USER_TALK
, $row->user_name
);
159 } else { # for anons or usernames of imported revisions
160 $batch->add( NS_USER
, $row->rev_user_text
);
161 $batch->add( NS_USER_TALK
, $row->rev_user_text
);
164 $this->parentLens
= Revision
::getParentLengths( $this->mDb
, $revIds );
166 $this->mResult
->seek( 0 );
170 * Creates begin of history list with a submit button
172 * @return string HTML output
174 protected function getStartBody() {
175 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
176 $this->lastRow
= false;
178 $this->oldIdChecked
= 0;
180 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
181 $s = Html
::openElement( 'form', [ 'action' => wfScript(),
182 'id' => 'mw-history-compare' ] ) . "\n";
183 $s .= Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n";
184 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
185 $s .= Html
::hidden( 'type', 'revision' ) . "\n";
187 // Button container stored in $this->buttons for re-use in getEndBody()
189 if ( $this->getNumRows() > 0 ) {
190 $this->buttons
.= Html
::openElement(
191 'div', [ 'class' => 'mw-history-compareselectedversions' ] );
192 $className = 'historysubmit mw-history-compareselectedversions-button';
193 $attrs = [ 'class' => $className ]
194 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' );
195 $this->buttons
.= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
199 $user = $this->getUser();
201 if ( $permissionManager->userHasRight( $user, 'deleterevision' ) ) {
202 $actionButtons .= $this->getRevisionButton(
203 'revisiondelete', 'showhideselectedversions' );
205 if ( $this->showTagEditUI
) {
206 $actionButtons .= $this->getRevisionButton(
207 'editchangetags', 'history-edit-tags' );
209 if ( $actionButtons ) {
210 $this->buttons
.= Xml
::tags( 'div', [ 'class' =>
211 'mw-history-revisionactions' ], $actionButtons );
214 if ( $permissionManager->userHasRight( $user, 'deleterevision' ) ||
$this->showTagEditUI
) {
215 $this->buttons
.= ( new ListToggle( $this->getOutput() ) )->getHTML();
218 $this->buttons
.= '</div>';
220 $s .= $this->buttons
;
222 $s .= '<ul id="pagehistory">' . "\n";
227 private function getRevisionButton( $name, $msg ) {
228 $this->preventClickjacking();
229 # Note T22966, <button> is non-standard in IE<8
230 $element = Html
::element(
236 'class' => "historysubmit mw-history-$name-button",
238 $this->msg( $msg )->text()
243 protected function getEndBody() {
244 if ( $this->lastRow
) {
245 $firstInList = $this->counter
== 1;
246 if ( $this->mIsBackwards
) {
247 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
248 if ( $this->mOffset
== '' ) {
254 # The next row is the past-the-end row
255 $next = $this->mPastTheEndRow
;
259 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
260 ?
$this->getTitle()->getNotificationTimestamp( $this->getUser() )
263 $s = $this->historyLine( $this->lastRow
, $next, $notifTimestamp, false, $firstInList );
268 # Add second buttons only if there is more than one rev
269 if ( $this->getNumRows() > 2 ) {
270 $s .= $this->buttons
;
278 * Creates a submit button
280 * @param string $message Text of the submit button, will be escaped
281 * @param array $attributes
282 * @return string HTML output for the submit button
284 function submitButton( $message, $attributes = [] ) {
285 # Disable submit button if history has 1 revision only
286 if ( $this->getNumRows() > 1 ) {
287 return Html
::submitButton( $message, $attributes );
294 * Returns a row from the history printout.
296 * @todo document some more, and maybe clean up the code (some params redundant?)
298 * @param stdClass $row The database row corresponding to the previous line.
299 * @param mixed $next The database row corresponding to the next line
300 * (chronologically previous)
301 * @param bool|string $notificationtimestamp
302 * @param bool $dummy Unused.
303 * @param bool $firstInList Whether this row corresponds to the first
304 * displayed on this history page.
305 * @return string HTML output for the row
307 function historyLine( $row, $next, $notificationtimestamp = false,
308 $dummy = false, $firstInList = false ) {
309 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
310 $rev = new Revision( $row, 0, $this->getTitle() );
312 if ( is_object( $next ) ) {
313 $prevRev = new Revision( $next, 0, $this->getTitle() );
318 $latest = $rev->getId() === $this->getWikiPage()->getLatest();
319 $curlink = $this->curLink( $rev );
320 $lastlink = $this->lastLink( $rev, $next );
321 $curLastlinks = Html
::rawElement( 'span', [], $curlink ) .
322 Html
::rawElement( 'span', [], $lastlink );
323 $histLinks = Html
::rawElement(
325 [ 'class' => 'mw-history-histlinks mw-changeslist-links' ],
329 $diffButtons = $this->diffButtons( $rev, $firstInList );
330 $s = $histLinks . $diffButtons;
332 $link = $this->revLink( $rev );
336 $user = $this->getUser();
337 $canRevDelete = $permissionManager->userHasRight( $user, 'deleterevision' );
338 // Show checkboxes for each revision, to allow for revision deletion and
340 if ( $canRevDelete ||
$this->showTagEditUI
) {
341 $this->preventClickjacking();
342 // If revision was hidden from sysops and we don't need the checkbox
343 // for anything else, disable it
344 if ( !$this->showTagEditUI
345 && !$rev->userCan( RevisionRecord
::DELETED_RESTRICTED
, $user )
347 $del = Xml
::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
348 // Otherwise, enable the checkbox...
350 $del = Xml
::check( 'showhiderevisions', false,
351 [ 'name' => 'ids[' . $rev->getId() . ']' ] );
353 // User can only view deleted revisions...
354 } elseif ( $rev->getVisibility() &&
355 $permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
356 // If revision was hidden from sysops, disable the link
357 if ( !$rev->userCan( RevisionRecord
::DELETED_RESTRICTED
, $user ) ) {
358 $del = Linker
::revDeleteLinkDisabled( false );
359 // Otherwise, show the link...
361 $query = [ 'type' => 'revision',
362 'target' => $this->getTitle()->getPrefixedDBkey(), 'ids' => $rev->getId() ];
363 $del .= Linker
::revDeleteLink( $query,
364 $rev->isDeleted( RevisionRecord
::DELETED_RESTRICTED
), false );
371 $lang = $this->getLanguage();
372 $dirmark = $lang->getDirMark();
376 $s .= " <span class='history-user'>" .
377 Linker
::revUserTools( $rev, true, false ) . "</span>";
380 if ( $rev->isMinor() ) {
381 $s .= ' ' . ChangesList
::flag( 'minor', $this->getContext() );
384 # Sometimes rev_len isn't populated
385 if ( $rev->getSize() !== null ) {
386 # Size is always public data
387 $prevSize = $this->parentLens
[$row->rev_parent_id
] ??
0;
388 $sDiff = ChangesList
::showCharacterDifference( $prevSize, $rev->getSize() );
389 $fSize = Linker
::formatRevisionSize( $rev->getSize(), false );
390 $s .= ' <span class="mw-changeslist-separator"></span> ' . "$fSize $sDiff";
393 # Text following the character difference is added just before running hooks
394 $s2 = Linker
::revComment( $rev, false, true, false );
396 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
397 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
398 $classes[] = 'mw-history-line-updated';
403 # Rollback and undo links
405 if ( $prevRev && $permissionManager->quickUserCan( 'edit', $user, $this->getTitle() ) ) {
406 if ( $latest && $permissionManager->quickUserCan( 'rollback',
407 $user, $this->getTitle() )
409 // Get a rollback link without the brackets
410 $rollbackLink = Linker
::generateRollback(
413 [ 'verify', 'noBrackets' ]
415 if ( $rollbackLink ) {
416 $this->preventClickjacking();
417 $tools[] = $rollbackLink;
421 if ( !$rev->isDeleted( RevisionRecord
::DELETED_TEXT
)
422 && !$prevRev->isDeleted( RevisionRecord
::DELETED_TEXT
)
424 # Create undo tooltip for the first (=latest) line only
425 $undoTooltip = $latest
426 ?
[ 'title' => $this->msg( 'tooltip-undo' )->text() ]
428 $undolink = $this->getLinkRenderer()->makeKnownLink(
430 $this->msg( 'editundo' )->text(),
434 'undoafter' => $prevRev->getId(),
435 'undo' => $rev->getId()
438 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
441 // Allow extension to add their own links here
442 Hooks
::run( 'HistoryRevisionTools', [ $rev, &$tools, $prevRev, $user ] );
445 $s2 .= ' ' . Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
446 foreach ( $tools as $tool ) {
447 $s2 .= Html
::rawElement( 'span', [], $tool );
449 $s2 .= Html
::closeElement( 'span' );
453 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
458 $classes = array_merge( $classes, $newClasses );
459 if ( $tagSummary !== '' ) {
460 $s2 .= " $tagSummary";
463 # Include separator between character difference and following text
465 $s .= ' <span class="mw-changeslist-separator"></span> ' . $s2;
468 $attribs = [ 'data-mw-revid' => $rev->getId() ];
470 Hooks
::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes, &$attribs ] );
471 $attribs = array_filter( $attribs,
472 [ Sanitizer
::class, 'isReservedDataAttribute' ],
477 $attribs['class'] = implode( ' ', $classes );
480 return Xml
::tags( 'li', $attribs, $s ) . "\n";
484 * Create a link to view this revision of the page
486 * @param Revision $rev
489 function revLink( $rev ) {
490 return ChangesList
::revDateLink( $rev, $this->getUser(), $this->getLanguage(),
495 * Create a diff-to-current link for this revision for this page
497 * @param Revision $rev
500 function curLink( $rev ) {
501 $cur = $this->historyPage
->message
['cur'];
502 $latest = $this->getWikiPage()->getLatest();
503 if ( $latest === $rev->getId()
504 ||
!$rev->userCan( RevisionRecord
::DELETED_TEXT
, $this->getUser() )
508 return $this->getLinkRenderer()->makeKnownLink(
510 new HtmlArmor( $cur ),
514 'oldid' => $rev->getId()
521 * Create a diff-to-previous link for this revision for this page.
523 * @param Revision $prevRev The revision being displayed
524 * @param stdClass|string|null $next The next revision in list (that is
525 * the previous one in chronological order).
526 * May either be a row, "unknown" or null.
529 function lastLink( $prevRev, $next ) {
530 $last = $this->historyPage
->message
['last'];
532 if ( $next === null ) {
533 # Probably no next row
537 $linkRenderer = $this->getLinkRenderer();
538 if ( $next === 'unknown' ) {
539 # Next row probably exists but is unknown, use an oldid=prev link
540 return $linkRenderer->makeKnownLink(
542 new HtmlArmor( $last ),
545 'diff' => $prevRev->getId(),
551 $nextRev = new Revision( $next, 0, $this->getTitle() );
553 if ( !$prevRev->userCan( RevisionRecord
::DELETED_TEXT
, $this->getUser() )
554 ||
!$nextRev->userCan( RevisionRecord
::DELETED_TEXT
, $this->getUser() )
559 return $linkRenderer->makeKnownLink(
561 new HtmlArmor( $last ),
564 'diff' => $prevRev->getId(),
565 'oldid' => $next->rev_id
571 * Create radio buttons for page history
573 * @param Revision $rev
574 * @param bool $firstInList Is this version the first one?
576 * @return string HTML output for the radio buttons
578 function diffButtons( $rev, $firstInList ) {
579 if ( $this->getNumRows() > 1 ) {
581 $radio = [ 'type' => 'radio', 'value' => $id ];
582 /** @todo Move title texts to javascript */
583 if ( $firstInList ) {
584 $first = Xml
::element( 'input',
585 array_merge( $radio, [
586 'style' => 'visibility:hidden',
588 'id' => 'mw-oldid-null' ] )
590 $checkmark = [ 'checked' => 'checked' ];
592 # Check visibility of old revisions
593 if ( !$rev->userCan( RevisionRecord
::DELETED_TEXT
, $this->getUser() ) ) {
594 $radio['disabled'] = 'disabled';
595 $checkmark = []; // We will check the next possible one
596 } elseif ( !$this->oldIdChecked
) {
597 $checkmark = [ 'checked' => 'checked' ];
598 $this->oldIdChecked
= $id;
602 $first = Xml
::element( 'input',
603 array_merge( $radio, $checkmark, [
605 'id' => "mw-oldid-$id" ] ) );
608 $second = Xml
::element( 'input',
609 array_merge( $radio, $checkmark, [
611 'id' => "mw-diff-$id" ] ) );
613 return $first . $second;
622 function getDefaultQuery() {
623 parent
::getDefaultQuery();
624 unset( $this->mDefaultQuery
['date-range-to'] );
625 return $this->mDefaultQuery
;
629 * This is called if a write operation is possible from the generated HTML
630 * @param bool $enable
632 function preventClickjacking( $enable = true ) {
633 $this->preventClickjacking
= $enable;
637 * Get the "prevent clickjacking" flag
640 function getPreventClickjacking() {
641 return $this->preventClickjacking
;