5 * Split off from Article.php and Skin.php, 2003-12-22
10 * This class handles printing the history page for an article. In order to
11 * be efficient, it uses timestamps rather than offsets for paging, to avoid
12 * costly LIMIT,offset queries.
14 * Construct it by passing in an Article, and call $h->history() to print the
18 class HistoryAction
extends FormlessAction
{
22 public function getName() {
26 public function requiresWrite() {
30 public function requiresUnblock() {
34 protected function getPageTitle() {
35 return $this->msg( 'history-title', $this->getTitle()->getPrefixedText() )->text();
38 protected function getDescription() {
39 // Creation of a subtitle link pointing to [[Special:Log]]
40 return Linker
::linkKnown(
41 SpecialPage
::getTitleFor( 'Log' ),
42 $this->msg( 'viewpagelogs' )->escaped(),
44 array( 'page' => $this->getTitle()->getPrefixedText() )
49 * Get the Article object we are working on.
52 public function getArticle() {
57 * As we use the same small set of messages in various methods and that
58 * they are called often, we call them once and save them in $this->message
60 private function preCacheMessages() {
61 // Precache various messages
62 if ( !isset( $this->message
) ) {
63 $msgs = array( 'cur', 'last', 'pipe-separator' );
64 foreach ( $msgs as $msg ) {
65 $this->message
[$msg] = $this->msg( $msg )->escaped();
71 * Print the history page for an article.
74 global $wgScript, $wgUseFileCache, $wgSquidMaxage;
76 $out = $this->getOutput();
77 $request = $this->getRequest();
80 * Allow client caching.
82 if ( $out->checkLastModified( $this->page
->getTouched() ) ) {
83 return; // Client cache fresh and headers sent, nothing more to do.
86 wfProfileIn( __METHOD__
);
88 if ( $request->getFullRequestURL() == $this->getTitle()->getInternalURL( 'action=history' ) ) {
89 $out->setSquidMaxage( $wgSquidMaxage );
92 $this->preCacheMessages();
94 # Fill in the file cache if not set already
95 if ( $wgUseFileCache && HTMLFileCache
::useFileCache( $this->getContext() ) ) {
96 $cache = HTMLFileCache
::newFromTitle( $this->getTitle(), 'history' );
97 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
98 ob_start( array( &$cache, 'saveToFileCache' ) );
102 // Setup page variables.
103 $out->setFeedAppendQuery( 'action=history' );
104 $out->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
106 // Handle atom/RSS feeds.
107 $feedType = $request->getVal( 'feed' );
109 wfProfileOut( __METHOD__
);
110 $this->feed( $feedType );
114 // Fail nicely if article doesn't exist.
115 if ( !$this->page
->exists() ) {
116 $out->addWikiMsg( 'nohistory' );
117 # show deletion/move log if there is an entry
118 LogEventsList
::showLogExtract(
120 array( 'delete', 'move' ),
124 'conds' => array( "log_action != 'revision'" ),
125 'showIfEmpty' => false,
126 'msgKey' => array( 'moveddeleted-notice' )
129 wfProfileOut( __METHOD__
);
134 * Add date selector to quickly get to a certain time
136 $year = $request->getInt( 'year' );
137 $month = $request->getInt( 'month' );
138 $tagFilter = $request->getVal( 'tagfilter' );
139 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
142 * Option to show only revisions that have been (partially) hidden via RevisionDelete
144 if ( $request->getBool( 'deleted' ) ) {
145 $conds = array( "rev_deleted != '0'" );
149 $checkDeleted = Xml
::checkLabel( $this->msg( 'history-show-deleted' )->text(),
150 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
152 // Add the general form
153 $action = htmlspecialchars( $wgScript );
155 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
157 $this->msg( 'history-fieldset-title' )->text(),
159 array( 'id' => 'mw-history-search' )
161 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBKey() ) . "\n" .
162 Html
::hidden( 'action', 'history' ) . "\n" .
163 Xml
::dateMenu( $year, $month ) . ' ' .
164 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
166 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
170 wfRunHooks( 'PageHistoryBeforeList', array( &$this->page
) );
172 // Create and output the list.
173 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
175 $pager->getNavigationBar() .
177 $pager->getNavigationBar()
179 $out->preventClickjacking( $pager->getPreventClickjacking() );
181 wfProfileOut( __METHOD__
);
185 * Fetch an array of revisions, specified by a given limit, offset and
186 * direction. This is now only used by the feeds. It was previously
187 * used by the main UI but that's now handled by the pager.
189 * @param $limit Integer: the limit number of revisions to get
190 * @param $offset Integer
191 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
192 * @return ResultWrapper
194 function fetchRevisions( $limit, $offset, $direction ) {
195 $dbr = wfGetDB( DB_SLAVE
);
197 if ( $direction == HistoryPage
::DIR_PREV
) {
198 list( $dirs, $oper ) = array( "ASC", ">=" );
199 } else { /* $direction == HistoryPage::DIR_NEXT */
200 list( $dirs, $oper ) = array( "DESC", "<=" );
204 $offsets = array( "rev_timestamp $oper '$offset'" );
209 $page_id = $this->page
->getId();
211 return $dbr->select( 'revision',
212 Revision
::selectFields(),
213 array_merge( array( "rev_page=$page_id" ), $offsets ),
215 array( 'ORDER BY' => "rev_timestamp $dirs",
216 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
221 * Output a subscription feed listing recent edits to this page.
223 * @param $type String: feed type
225 function feed( $type ) {
226 global $wgFeedClasses, $wgFeedLimit;
227 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
230 $request = $this->getRequest();
232 $feed = new $wgFeedClasses[$type](
233 $this->getTitle()->getPrefixedText() . ' - ' .
234 wfMsgForContent( 'history-feed-title' ),
235 wfMsgForContent( 'history-feed-description' ),
236 $this->getTitle()->getFullUrl( 'action=history' )
239 // Get a limit on number of feed entries. Provide a sane default
240 // of 10 if none is defined (but limit to $wgFeedLimit max)
241 $limit = $request->getInt( 'limit', 10 );
242 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
245 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
247 // Generate feed elements enclosed between header and footer.
249 if ( $items->numRows() ) {
250 foreach ( $items as $row ) {
251 $feed->outItem( $this->feedItem( $row ) );
254 $feed->outItem( $this->feedEmpty() );
259 function feedEmpty() {
261 wfMsgForContent( 'nohistory' ),
262 $this->getOutput()->parse( wfMsgForContent( 'history-feed-empty' ) ),
263 $this->getTitle()->getFullUrl(),
264 wfTimestamp( TS_MW
),
266 $this->getTitle()->getTalkPage()->getFullUrl()
271 * Generate a FeedItem object from a given revision table row
272 * Borrows Recent Changes' feed generation functions for formatting;
273 * includes a diff to the previous revision (if any).
275 * @param $row Object: database row
278 function feedItem( $row ) {
279 $rev = new Revision( $row );
280 $rev->setTitle( $this->getTitle() );
281 $text = FeedUtils
::formatDiffRow(
283 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
285 $rev->getTimestamp(),
288 if ( $rev->getComment() == '' ) {
290 $title = wfMsgForContent( 'history-feed-item-nocomment',
292 $wgContLang->timeanddate( $rev->getTimestamp() ),
293 $wgContLang->date( $rev->getTimestamp() ),
294 $wgContLang->time( $rev->getTimestamp() )
297 $title = $rev->getUserText() .
298 wfMsgForContent( 'colon-separator' ) .
299 FeedItem
::stripComment( $rev->getComment() );
304 $this->getTitle()->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
305 $rev->getTimestamp(),
307 $this->getTitle()->getTalkPage()->getFullUrl()
315 class HistoryPager
extends ReverseChronologicalPager
{
316 public $lastRow = false, $counter, $historyPage, $buttons, $conds;
317 protected $oldIdChecked;
318 protected $preventClickjacking = false;
320 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
321 parent
::__construct( $historyPage->getContext() );
322 $this->historyPage
= $historyPage;
323 $this->tagFilter
= $tagFilter;
324 $this->getDateCond( $year, $month );
325 $this->conds
= $conds;
328 // For hook compatibility...
329 function getArticle() {
330 return $this->historyPage
->getArticle();
333 function getSqlComment() {
334 if ( $this->conds
) {
335 return 'history page filtered'; // potentially slow, see CR r58153
337 return 'history page unfiltered';
341 function getQueryInfo() {
343 'tables' => array( 'revision', 'user' ),
344 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
345 'conds' => array_merge(
346 array( 'rev_page' => $this->getWikiPage()->getId() ),
348 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
349 'join_conds' => array(
350 'user' => Revision
::userJoinCond(),
351 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
353 ChangeTags
::modifyDisplayQuery(
354 $queryInfo['tables'],
355 $queryInfo['fields'],
357 $queryInfo['join_conds'],
358 $queryInfo['options'],
361 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
365 function getIndexField() {
366 return 'rev_timestamp';
369 function formatRow( $row ) {
370 if ( $this->lastRow
) {
371 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
372 $firstInList = $this->counter
== 1;
374 $s = $this->historyLine( $this->lastRow
, $row,
375 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
379 $this->lastRow
= $row;
383 function doBatchLookups() {
384 # Do a link batch query
385 $this->mResult
->seek( 0 );
386 $batch = new LinkBatch();
387 foreach ( $this->mResult
as $row ) {
388 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
389 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
392 $this->mResult
->seek( 0 );
396 * Creates begin of history list with a submit button
398 * @return string HTML output
400 function getStartBody() {
402 $this->lastRow
= false;
404 $this->oldIdChecked
= 0;
406 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
407 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
408 'id' => 'mw-history-compare' ) ) . "\n";
409 $s .= Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . "\n";
410 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
412 // Button container stored in $this->buttons for re-use in getEndBody()
413 $this->buttons
= '<div>';
414 $this->buttons
.= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
415 array( 'class' => 'historysubmit mw-history-compareselectedversions-button' )
416 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
419 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
420 $this->buttons
.= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
422 $this->buttons
.= '</div>';
424 $s .= $this->buttons
;
425 $s .= '<ul id="pagehistory">' . "\n";
429 private function getRevisionButton( $name, $msg ) {
430 $this->preventClickjacking();
431 # Note bug #20966, <button> is non-standard in IE<8
432 $element = Html
::element( 'button',
437 'class' => "historysubmit mw-history-$name-button",
439 $this->msg( $msg )->text()
444 function getEndBody() {
445 if ( $this->lastRow
) {
446 $latest = $this->counter
== 1 && $this->mIsFirst
;
447 $firstInList = $this->counter
== 1;
448 if ( $this->mIsBackwards
) {
449 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
450 if ( $this->mOffset
== '' ) {
456 # The next row is the past-the-end row
457 $next = $this->mPastTheEndRow
;
460 $s = $this->historyLine( $this->lastRow
, $next,
461 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
466 # Add second buttons only if there is more than one rev
467 if ( $this->getNumRows() > 2 ) {
468 $s .= $this->buttons
;
475 * Creates a submit button
477 * @param $message String: text of the submit button, will be escaped
478 * @param $attributes Array: attributes
479 * @return String: HTML output for the submit button
481 function submitButton( $message, $attributes = array() ) {
482 # Disable submit button if history has 1 revision only
483 if ( $this->getNumRows() > 1 ) {
484 return Xml
::submitButton( $message , $attributes );
491 * Returns a row from the history printout.
493 * @todo document some more, and maybe clean up the code (some params redundant?)
495 * @param $row Object: the database row corresponding to the previous line.
496 * @param $next Mixed: the database row corresponding to the next line. (chronologically previous)
497 * @param $notificationtimestamp
498 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
499 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
500 * @return String: HTML output for the row
502 function historyLine( $row, $next, $notificationtimestamp = false,
503 $latest = false, $firstInList = false )
505 $rev = new Revision( $row );
506 $rev->setTitle( $this->getTitle() );
508 if ( is_object( $next ) ) {
509 $prevRev = new Revision( $next );
510 $prevRev->setTitle( $this->getTitle() );
515 $curlink = $this->curLink( $rev, $latest );
516 $lastlink = $this->lastLink( $rev, $next );
517 $diffButtons = $this->diffButtons( $rev, $firstInList );
518 $histLinks = Html
::rawElement(
520 array( 'class' => 'mw-history-histlinks' ),
521 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
523 $s = $histLinks . $diffButtons;
525 $link = $this->revLink( $rev );
529 $user = $this->getUser();
530 // Show checkboxes for each revision
531 if ( $user->isAllowed( 'deleterevision' ) ) {
532 $this->preventClickjacking();
533 // If revision was hidden from sysops, disable the checkbox
534 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
535 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
536 // Otherwise, enable the checkbox...
538 $del = Xml
::check( 'showhiderevisions', false,
539 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
541 // User can only view deleted revisions...
542 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
543 // If revision was hidden from sysops, disable the link
544 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
545 $cdel = Linker
::revDeleteLinkDisabled( false );
546 // Otherwise, show the link...
548 $query = array( 'type' => 'revision',
549 'target' => $this->getTitle()->getPrefixedDbkey(), 'ids' => $rev->getId() );
550 $del .= Linker
::revDeleteLink( $query,
551 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
558 $lang = $this->getLanguage();
559 $dirmark = $lang->getDirMark();
563 $s .= " <span class='history-user'>" .
564 Linker
::revUserTools( $rev, true ) . "</span>";
567 if ( $rev->isMinor() ) {
568 $s .= ' ' . ChangesList
::flag( 'minor' );
572 && !$prevRev->isDeleted( Revision
::DELETED_TEXT
)
573 && !$rev->isDeleted( Revision
::DELETED_TEXT
) )
575 $sDiff = ChangesList
::showCharacterDifference( $prevRev->getSize(), $rev->getSize() );
576 $s .= ' . . ' . $sDiff . ' . . ';
579 $s .= Linker
::revComment( $rev, false, true );
581 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
582 $s .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
587 # Rollback and undo links
589 !count( $this->getTitle()->getUserPermissionsErrors( 'edit', $this->getUser() ) ) )
591 if ( $latest && !count( $this->getTitle()->getUserPermissionsErrors( 'rollback', $this->getUser() ) ) ) {
592 $this->preventClickjacking();
593 $tools[] = '<span class="mw-rollback-link">' .
594 Linker
::buildRollbackLink( $rev ) . '</span>';
597 if ( !$rev->isDeleted( Revision
::DELETED_TEXT
)
598 && !$prevRev->isDeleted( Revision
::DELETED_TEXT
) )
600 # Create undo tooltip for the first (=latest) line only
601 $undoTooltip = $latest
602 ?
array( 'title' => $this->msg( 'tooltip-undo' )->text() )
604 $undolink = Linker
::linkKnown(
606 $this->msg( 'editundo' )->escaped(),
610 'undoafter' => $prevRev->getId(),
611 'undo' => $rev->getId()
614 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
619 $s .= ' (' . $lang->pipeList( $tools ) . ')';
623 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
624 $classes = array_merge( $classes, $newClasses );
625 $s .= " $tagSummary";
627 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
631 $attribs['class'] = implode( ' ', $classes );
634 return Xml
::tags( 'li', $attribs, $s ) . "\n";
638 * Create a link to view this revision of the page
640 * @param $rev Revision
643 function revLink( $rev ) {
644 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
645 $date = htmlspecialchars( $date );
646 if ( $rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
647 $link = Linker
::linkKnown(
651 array( 'oldid' => $rev->getId() )
656 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
657 $link = "<span class=\"history-deleted\">$link</span>";
663 * Create a diff-to-current link for this revision for this page
665 * @param $rev Revision
666 * @param $latest Boolean: this is the latest revision of the page?
669 function curLink( $rev, $latest ) {
670 $cur = $this->historyPage
->message
['cur'];
671 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
674 return Linker
::linkKnown(
679 'diff' => $this->getWikiPage()->getLatest(),
680 'oldid' => $rev->getId()
687 * Create a diff-to-previous link for this revision for this page.
689 * @param $prevRev Revision: the previous revision
690 * @param $next Mixed: the newer revision
693 function lastLink( $prevRev, $next ) {
694 $last = $this->historyPage
->message
['last'];
695 # $next may either be a Row, null, or "unkown"
696 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
697 if ( is_null( $next ) ) {
698 # Probably no next row
700 } elseif ( $next === 'unknown' ) {
701 # Next row probably exists but is unknown, use an oldid=prev link
702 return Linker
::linkKnown(
707 'diff' => $prevRev->getId(),
711 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() )
712 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) )
716 return Linker
::linkKnown(
721 'diff' => $prevRev->getId(),
722 'oldid' => $next->rev_id
729 * Create radio buttons for page history
731 * @param $rev Revision object
732 * @param $firstInList Boolean: is this version the first one?
734 * @return String: HTML output for the radio buttons
736 function diffButtons( $rev, $firstInList ) {
737 if ( $this->getNumRows() > 1 ) {
739 $radio = array( 'type' => 'radio', 'value' => $id );
740 /** @todo: move title texts to javascript */
741 if ( $firstInList ) {
742 $first = Xml
::element( 'input',
743 array_merge( $radio, array(
744 'style' => 'visibility:hidden',
746 'id' => 'mw-oldid-null' ) )
748 $checkmark = array( 'checked' => 'checked' );
750 # Check visibility of old revisions
751 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
752 $radio['disabled'] = 'disabled';
753 $checkmark = array(); // We will check the next possible one
754 } elseif ( !$this->oldIdChecked
) {
755 $checkmark = array( 'checked' => 'checked' );
756 $this->oldIdChecked
= $id;
758 $checkmark = array();
760 $first = Xml
::element( 'input',
761 array_merge( $radio, $checkmark, array(
763 'id' => "mw-oldid-$id" ) ) );
764 $checkmark = array();
766 $second = Xml
::element( 'input',
767 array_merge( $radio, $checkmark, array(
769 'id' => "mw-diff-$id" ) ) );
770 return $first . $second;
777 * This is called if a write operation is possible from the generated HTML
779 function preventClickjacking( $enable = true ) {
780 $this->preventClickjacking
= $enable;
784 * Get the "prevent clickjacking" flag
787 function getPreventClickjacking() {
788 return $this->preventClickjacking
;
793 * Backwards-compatibility alias
795 class HistoryPage
extends HistoryAction
{
796 public function __construct( Page
$article ) { # Just to make it public
797 parent
::__construct( $article );
800 public function history() {