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
60 public function __construct(
61 HistoryAction
$historyPage,
68 parent
::__construct( $historyPage->getContext() );
69 $this->historyPage
= $historyPage;
70 $this->tagFilter
= $tagFilter;
71 $this->getDateCond( $year, $month, $day );
72 $this->conds
= $conds;
73 $this->showTagEditUI
= ChangeTags
::showTagEditingUI( $this->getUser() );
76 // For hook compatibility...
77 function getArticle() {
78 return $this->historyPage
->getArticle();
81 function getSqlComment() {
83 return 'history page filtered'; // potentially slow, see CR r58153
85 return 'history page unfiltered';
89 function getQueryInfo() {
90 $revQuery = Revision
::getQueryInfo( [ 'user' ] );
92 'tables' => $revQuery['tables'],
93 'fields' => $revQuery['fields'],
94 'conds' => array_merge(
95 [ 'rev_page' => $this->getWikiPage()->getId() ],
97 'options' => [ 'USE INDEX' => [ 'revision' => 'page_timestamp' ] ],
98 'join_conds' => $revQuery['joins'],
100 ChangeTags
::modifyDisplayQuery(
101 $queryInfo['tables'],
102 $queryInfo['fields'],
104 $queryInfo['join_conds'],
105 $queryInfo['options'],
109 // Avoid PHP 7.1 warning of passing $this by reference
110 $historyPager = $this;
111 Hooks
::run( 'PageHistoryPager::getQueryInfo', [ &$historyPager, &$queryInfo ] );
116 function getIndexField() {
117 return 'rev_timestamp';
121 * @param stdClass $row
124 function formatRow( $row ) {
125 if ( $this->lastRow
) {
126 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
127 $firstInList = $this->counter
== 1;
130 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
131 ?
$this->getTitle()->getNotificationTimestamp( $this->getUser() )
134 $s = $this->historyLine(
135 $this->lastRow
, $row, $notifTimestamp, $latest, $firstInList );
139 $this->lastRow
= $row;
144 protected function doBatchLookups() {
145 if ( !Hooks
::run( 'PageHistoryPager::doBatchLookups', [ $this, $this->mResult
] ) ) {
149 # Do a link batch query
150 $this->mResult
->seek( 0 );
151 $batch = new LinkBatch();
153 foreach ( $this->mResult
as $row ) {
154 if ( $row->rev_parent_id
) {
155 $revIds[] = $row->rev_parent_id
;
157 if ( $row->user_name
!== null ) {
158 $batch->add( NS_USER
, $row->user_name
);
159 $batch->add( NS_USER_TALK
, $row->user_name
);
160 } else { # for anons or usernames of imported revisions
161 $batch->add( NS_USER
, $row->rev_user_text
);
162 $batch->add( NS_USER_TALK
, $row->rev_user_text
);
165 $this->parentLens
= Revision
::getParentLengths( $this->mDb
, $revIds );
167 $this->mResult
->seek( 0 );
171 * Creates begin of history list with a submit button
173 * @return string HTML output
175 protected function getStartBody() {
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()
188 $this->buttons
= Html
::openElement( 'div', [ 'class' => 'mw-history-compareselectedversions' ] );
189 $className = 'historysubmit mw-history-compareselectedversions-button';
190 $attrs = [ 'class' => $className ]
191 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' );
192 $this->buttons
.= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
196 $user = $this->getUser();
198 if ( $user->isAllowed( 'deleterevision' ) ) {
199 $actionButtons .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
201 if ( $this->showTagEditUI
) {
202 $actionButtons .= $this->getRevisionButton( 'editchangetags', 'history-edit-tags' );
204 if ( $actionButtons ) {
205 $this->buttons
.= Xml
::tags( 'div', [ 'class' =>
206 'mw-history-revisionactions' ], $actionButtons );
209 if ( $user->isAllowed( 'deleterevision' ) ||
$this->showTagEditUI
) {
210 $this->buttons
.= ( new ListToggle( $this->getOutput() ) )->getHTML();
213 $this->buttons
.= '</div>';
215 $s .= $this->buttons
;
216 $s .= '<ul id="pagehistory">' . "\n";
221 private function getRevisionButton( $name, $msg ) {
222 $this->preventClickjacking();
223 # Note T22966, <button> is non-standard in IE<8
224 $element = Html
::element(
230 'class' => "historysubmit mw-history-$name-button",
232 $this->msg( $msg )->text()
237 protected function getEndBody() {
238 if ( $this->lastRow
) {
239 $latest = $this->counter
== 1 && $this->mIsFirst
;
240 $firstInList = $this->counter
== 1;
241 if ( $this->mIsBackwards
) {
242 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
243 if ( $this->mOffset
== '' ) {
249 # The next row is the past-the-end row
250 $next = $this->mPastTheEndRow
;
254 $notifTimestamp = $this->getConfig()->get( 'ShowUpdatedMarker' )
255 ?
$this->getTitle()->getNotificationTimestamp( $this->getUser() )
258 $s = $this->historyLine(
259 $this->lastRow
, $next, $notifTimestamp, $latest, $firstInList );
264 # Add second buttons only if there is more than one rev
265 if ( $this->getNumRows() > 2 ) {
266 $s .= $this->buttons
;
274 * Creates a submit button
276 * @param string $message Text of the submit button, will be escaped
277 * @param array $attributes
278 * @return string HTML output for the submit button
280 function submitButton( $message, $attributes = [] ) {
281 # Disable submit button if history has 1 revision only
282 if ( $this->getNumRows() > 1 ) {
283 return Html
::submitButton( $message, $attributes );
290 * Returns a row from the history printout.
292 * @todo document some more, and maybe clean up the code (some params redundant?)
294 * @param stdClass $row The database row corresponding to the previous line.
295 * @param mixed $next The database row corresponding to the next line
296 * (chronologically previous)
297 * @param bool|string $notificationtimestamp
298 * @param bool $latest Whether this row corresponds to the page's latest revision.
299 * @param bool $firstInList Whether this row corresponds to the first
300 * displayed on this history page.
301 * @return string HTML output for the row
303 function historyLine( $row, $next, $notificationtimestamp = false,
304 $latest = false, $firstInList = false ) {
305 $rev = new Revision( $row, 0, $this->getTitle() );
307 if ( is_object( $next ) ) {
308 $prevRev = new Revision( $next, 0, $this->getTitle() );
313 $curlink = $this->curLink( $rev, $latest );
314 $lastlink = $this->lastLink( $rev, $next );
315 $curLastlinks = Html
::rawElement( 'span', [], $curlink ) .
316 Html
::rawElement( 'span', [], $lastlink );
317 $histLinks = Html
::rawElement(
319 [ 'class' => 'mw-history-histlinks mw-changeslist-links' ],
323 $diffButtons = $this->diffButtons( $rev, $firstInList );
324 $s = $histLinks . $diffButtons;
326 $link = $this->revLink( $rev );
330 $user = $this->getUser();
331 $canRevDelete = $user->isAllowed( 'deleterevision' );
332 // Show checkboxes for each revision, to allow for revision deletion and
334 if ( $canRevDelete ||
$this->showTagEditUI
) {
335 $this->preventClickjacking();
336 // If revision was hidden from sysops and we don't need the checkbox
337 // for anything else, disable it
338 if ( !$this->showTagEditUI
&& !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
339 $del = Xml
::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
340 // Otherwise, enable the checkbox...
342 $del = Xml
::check( 'showhiderevisions', false,
343 [ 'name' => 'ids[' . $rev->getId() . ']' ] );
345 // User can only view deleted revisions...
346 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
347 // If revision was hidden from sysops, disable the link
348 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
349 $del = Linker
::revDeleteLinkDisabled( false );
350 // Otherwise, show the link...
352 $query = [ 'type' => 'revision',
353 'target' => $this->getTitle()->getPrefixedDBkey(), 'ids' => $rev->getId() ];
354 $del .= Linker
::revDeleteLink( $query,
355 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
362 $lang = $this->getLanguage();
363 $dirmark = $lang->getDirMark();
367 $s .= " <span class='history-user'>" .
368 Linker
::revUserTools( $rev, true, false ) . "</span>";
371 if ( $rev->isMinor() ) {
372 $s .= ' ' . ChangesList
::flag( 'minor', $this->getContext() );
375 # Sometimes rev_len isn't populated
376 if ( $rev->getSize() !== null ) {
377 # Size is always public data
378 $prevSize = $this->parentLens
[$row->rev_parent_id
] ??
0;
379 $sDiff = ChangesList
::showCharacterDifference( $prevSize, $rev->getSize() );
380 $fSize = Linker
::formatRevisionSize( $rev->getSize(), false );
381 $s .= ' <span class="mw-changeslist-separator"></span> ' . "$fSize $sDiff";
384 # Text following the character difference is added just before running hooks
385 $s2 = Linker
::revComment( $rev, false, true, false );
387 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
388 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
389 $classes[] = 'mw-history-line-updated';
394 # Rollback and undo links
395 if ( $prevRev && $this->getTitle()->quickUserCan( 'edit', $user ) ) {
396 if ( $latest && $this->getTitle()->quickUserCan( 'rollback', $user ) ) {
397 // Get a rollback link without the brackets
398 $rollbackLink = Linker
::generateRollback(
401 [ 'verify', 'noBrackets' ]
403 if ( $rollbackLink ) {
404 $this->preventClickjacking();
405 $tools[] = $rollbackLink;
409 if ( !$rev->isDeleted( Revision
::DELETED_TEXT
)
410 && !$prevRev->isDeleted( Revision
::DELETED_TEXT
)
412 # Create undo tooltip for the first (=latest) line only
413 $undoTooltip = $latest
414 ?
[ 'title' => $this->msg( 'tooltip-undo' )->text() ]
416 $undolink = MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
418 $this->msg( 'editundo' )->text(),
422 'undoafter' => $prevRev->getId(),
423 'undo' => $rev->getId()
426 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
429 // Allow extension to add their own links here
430 Hooks
::run( 'HistoryRevisionTools', [ $rev, &$tools, $prevRev, $user ] );
433 $s2 .= ' ' . Html
::openElement( 'span', [ 'class' => 'mw-changeslist-links' ] );
434 foreach ( $tools as $tool ) {
435 $s2 .= Html
::rawElement( 'span', [], $tool );
437 $s2 .= Html
::closeElement( 'span' );
441 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
446 $classes = array_merge( $classes, $newClasses );
447 if ( $tagSummary !== '' ) {
448 $s2 .= " $tagSummary";
451 # Include separator between character difference and following text
453 $s .= ' <span class="mw-changeslist-separator"></span> ' . $s2;
456 $attribs = [ 'data-mw-revid' => $rev->getId() ];
458 Hooks
::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes, &$attribs ] );
459 $attribs = array_filter( $attribs,
460 [ Sanitizer
::class, 'isReservedDataAttribute' ],
465 $attribs['class'] = implode( ' ', $classes );
468 return Xml
::tags( 'li', $attribs, $s ) . "\n";
472 * Create a link to view this revision of the page
474 * @param Revision $rev
477 function revLink( $rev ) {
478 return ChangesList
::revDateLink( $rev, $this->getUser(), $this->getLanguage(),
483 * Create a diff-to-current link for this revision for this page
485 * @param Revision $rev
486 * @param bool $latest This is the latest revision of the page?
489 function curLink( $rev, $latest ) {
490 $cur = $this->historyPage
->message
['cur'];
491 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
494 return MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
496 new HtmlArmor( $cur ),
499 'diff' => $this->getWikiPage()->getLatest(),
500 'oldid' => $rev->getId()
507 * Create a diff-to-previous link for this revision for this page.
509 * @param Revision $prevRev The revision being displayed
510 * @param stdClass|string|null $next The next revision in list (that is
511 * the previous one in chronological order).
512 * May either be a row, "unknown" or null.
515 function lastLink( $prevRev, $next ) {
516 $last = $this->historyPage
->message
['last'];
518 if ( $next === null ) {
519 # Probably no next row
523 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
524 if ( $next === 'unknown' ) {
525 # Next row probably exists but is unknown, use an oldid=prev link
526 return $linkRenderer->makeKnownLink(
528 new HtmlArmor( $last ),
531 'diff' => $prevRev->getId(),
537 $nextRev = new Revision( $next, 0, $this->getTitle() );
539 if ( !$prevRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() )
540 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() )
545 return $linkRenderer->makeKnownLink(
547 new HtmlArmor( $last ),
550 'diff' => $prevRev->getId(),
551 'oldid' => $next->rev_id
557 * Create radio buttons for page history
559 * @param Revision $rev
560 * @param bool $firstInList Is this version the first one?
562 * @return string HTML output for the radio buttons
564 function diffButtons( $rev, $firstInList ) {
565 if ( $this->getNumRows() > 1 ) {
567 $radio = [ 'type' => 'radio', 'value' => $id ];
568 /** @todo Move title texts to javascript */
569 if ( $firstInList ) {
570 $first = Xml
::element( 'input',
571 array_merge( $radio, [
572 'style' => 'visibility:hidden',
574 'id' => 'mw-oldid-null' ] )
576 $checkmark = [ 'checked' => 'checked' ];
578 # Check visibility of old revisions
579 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
580 $radio['disabled'] = 'disabled';
581 $checkmark = []; // We will check the next possible one
582 } elseif ( !$this->oldIdChecked
) {
583 $checkmark = [ 'checked' => 'checked' ];
584 $this->oldIdChecked
= $id;
588 $first = Xml
::element( 'input',
589 array_merge( $radio, $checkmark, [
591 'id' => "mw-oldid-$id" ] ) );
594 $second = Xml
::element( 'input',
595 array_merge( $radio, $checkmark, [
597 'id' => "mw-diff-$id" ] ) );
599 return $first . $second;
608 function getDefaultQuery() {
609 parent
::getDefaultQuery();
610 unset( $this->mDefaultQuery
['date-range-to'] );
611 return $this->mDefaultQuery
;
615 * This is called if a write operation is possible from the generated HTML
616 * @param bool $enable
618 function preventClickjacking( $enable = true ) {
619 $this->preventClickjacking
= $enable;
623 * Get the "prevent clickjacking" flag
626 function getPreventClickjacking() {
627 return $this->preventClickjacking
;