3 * Handles action=history.
4 * 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 class HistoryAction
extends FormlessAction
{
18 public function getName() {
22 public function getRestriction() {
27 * Get the Page object we are working on.
31 public function getArticle() {
36 * As we use the same small set of messages in various methods and that
37 * they are called often, we call them once and save them in $this->message
39 private function preCacheMessages() {
40 // Precache various messages
41 if ( !isset( $this->message
) ) {
42 $msgs = array( 'cur', 'last', 'pipe-separator' );
43 foreach ( $msgs as $msg ) {
44 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
50 * Print the history page for an article.
54 global $wgSquidMaxage, $wgScript;
56 $out = $this->getOutput();
57 $request = $this->getRequest();
59 // Allow client caching.
60 if ( $out->checkLastModified( $this->page
->getTouched() ) ) {
61 return; // Client cache fresh and headers sent, nothing more to do.
64 if ( $request->getFullRequestURL() == $this->getTitle()->getInternalURL( 'action=history' ) ) {
65 $out->setSquidMaxage( $wgSquidMaxage );
68 wfProfileIn( __METHOD__
);
70 $this->preCacheMessages();
72 // Setup page variables.
73 $out->setPageTitle( wfMsg( 'history-title', $this->getTitle()->getPrefixedText() ) );
74 $out->setRobotPolicy( 'noindex,nofollow' );
75 $out->setFeedAppendQuery( 'action=history' );
76 $out->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
78 // Creation of a subtitle link pointing to [[Special:Log]]
79 $logPage = SpecialPage
::getTitleFor( 'Log' );
80 $logLink = Linker
::linkKnown(
82 wfMsgHtml( 'viewpagelogs' ),
84 array( 'page' => $this->getTitle()->getPrefixedText() )
86 $out->setSubtitle( $logLink );
88 // Handle atom/RSS feeds.
89 $feedType = $request->getVal( 'feed' );
91 wfProfileOut( __METHOD__
);
92 return $this->feed( $feedType );
95 // Fail nicely if article doesn't exist.
96 if ( !$this->getTitle()->exists() ) {
97 $out->addWikiMsg( 'nohistory' );
98 # show deletion/move log if there is an entry
99 LogEventsList
::showLogExtract(
101 array( 'delete', 'move' ),
102 $this->getTitle()->getPrefixedText(),
105 'conds' => array( "log_action != 'revision'" ),
106 'showIfEmpty' => false,
107 'msgKey' => array( 'moveddeleted-notice' )
110 wfProfileOut( __METHOD__
);
115 * Add date selector to quickly get to a certain time
117 $year = $request->getInt( 'year' );
118 $month = $request->getInt( 'month' );
119 $tagFilter = $request->getVal( 'tagfilter' );
120 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
123 * Option to show only revisions that have been (partially) hidden via RevisionDelete
125 if ( $request->getBool( 'deleted' ) ) {
126 $conds = array( "rev_deleted != '0'" );
130 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
131 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
133 // Add the general form
134 $action = htmlspecialchars( $wgScript );
136 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
138 wfMsg( 'history-fieldset-title' ),
140 array( 'id' => 'mw-history-search' )
142 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBKey() ) . "\n" .
143 Html
::hidden( 'action', 'history' ) . "\n" .
144 Xml
::dateMenu( $year, $month ) . ' ' .
145 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
147 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
151 wfRunHooks( 'PageHistoryBeforeList', array( &$this->page
) );
153 // Create and output the list.
154 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
156 $pager->getNavigationBar() .
158 $pager->getNavigationBar()
160 $out->preventClickjacking( $pager->getPreventClickjacking() );
162 wfProfileOut( __METHOD__
);
166 * Fetch an array of revisions, specified by a given limit, offset and
167 * direction. This is now only used by the feeds. It was previously
168 * used by the main UI but that's now handled by the pager.
170 * @param $limit Integer: the limit number of revisions to get
171 * @param $offset Integer
172 * @param $direction Integer: either HistoryAction::DIR_PREV or HistoryAction::DIR_NEXT
173 * @return ResultWrapper
175 function fetchRevisions( $limit, $offset, $direction ) {
176 $dbr = wfGetDB( DB_SLAVE
);
178 if ( $direction == self
::DIR_PREV
) {
179 list( $dirs, $oper ) = array( "ASC", ">=" );
180 } else { /* $direction == self::DIR_NEXT */
181 list( $dirs, $oper ) = array( "DESC", "<=" );
185 $offsets = array( "rev_timestamp $oper '$offset'" );
190 $page_id = $this->getTitle()->getArticleID();
192 return $dbr->select( 'revision',
193 Revision
::selectFields(),
194 array_merge( array( "rev_page=$page_id" ), $offsets ),
196 array( 'ORDER BY' => "rev_timestamp $dirs",
197 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
202 * Output a subscription feed listing recent edits to this page.
204 * @param $type String: feed type
206 function feed( $type ) {
207 global $wgFeedClasses, $wgFeedLimit;
208 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
212 $feed = new $wgFeedClasses[$type](
213 $this->getTitle()->getPrefixedText() . ' - ' .
214 wfMsgForContent( 'history-feed-title' ),
215 wfMsgForContent( 'history-feed-description' ),
216 $this->getTitle()->getFullUrl( 'action=history' )
219 // Get a limit on number of feed entries. Provide a sane default
220 // of 10 if none is defined (but limit to $wgFeedLimit max)
221 $limit = $this->getRequest()->getInt( 'limit', 10 );
222 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
225 $items = $this->fetchRevisions( $limit, 0, self
::DIR_NEXT
);
227 // Generate feed elements enclosed between header and footer.
229 if ( $items->numRows() ) {
230 foreach ( $items as $row ) {
231 $feed->outItem( $this->feedItem( $row ) );
234 $feed->outItem( $this->feedEmpty() );
239 function feedEmpty() {
241 wfMsgForContent( 'nohistory' ),
242 $this->getOutput()->parse( wfMsgForContent( 'history-feed-empty' ) ),
243 $this->getTitle()->getFullUrl(),
244 wfTimestamp( TS_MW
),
246 $this->getTitle()->getTalkPage()->getFullUrl()
251 * Generate a FeedItem object from a given revision table row
252 * Borrows Recent Changes' feed generation functions for formatting;
253 * includes a diff to the previous revision (if any).
255 * @param $row Object: database row
258 function feedItem( $row ) {
259 $rev = new Revision( $row );
260 $titleObj = $this->getTitle();
261 $rev->setTitle( $titleObj );
262 $text = FeedUtils
::formatDiffRow(
264 $titleObj->getPreviousRevisionID( $rev->getId() ),
266 $rev->getTimestamp(),
269 if ( $rev->getComment() == '' ) {
271 $title = wfMsgForContent( 'history-feed-item-nocomment',
273 $wgContLang->timeanddate( $rev->getTimestamp() ),
274 $wgContLang->date( $rev->getTimestamp() ),
275 $wgContLang->time( $rev->getTimestamp() )
278 $title = $rev->getUserText() .
279 wfMsgForContent( 'colon-separator' ) .
280 FeedItem
::stripComment( $rev->getComment() );
285 $titleObj->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
286 $rev->getTimestamp(),
288 $titleObj->getTalkPage()->getFullUrl()
296 class HistoryPager
extends ReverseChronologicalPager
{
297 public $lastRow = false, $counter, $history, $buttons, $conds;
298 protected $oldIdChecked;
299 protected $preventClickjacking = false;
301 function __construct( $history, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
302 parent
::__construct();
303 $this->history
= $history;
304 $this->tagFilter
= $tagFilter;
305 $this->getDateCond( $year, $month );
306 $this->conds
= $conds;
309 // For hook compatibility...
310 function getArticle() {
311 return $this->history
->getArticle();
314 function getSqlComment() {
315 if ( $this->conds
) {
316 return 'history page filtered'; // potentially slow, see CR r58153
318 return 'history page unfiltered';
322 function getQueryInfo() {
324 'tables' => array( 'revision' ),
325 'fields' => Revision
::selectFields(),
326 'conds' => array_merge(
327 array( 'rev_page' => $this->getTitle()->getArticleID() ),
329 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
330 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
332 ChangeTags
::modifyDisplayQuery(
333 $queryInfo['tables'],
334 $queryInfo['fields'],
336 $queryInfo['join_conds'],
337 $queryInfo['options'],
340 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
344 function getIndexField() {
345 return 'rev_timestamp';
348 function formatRow( $row ) {
349 if ( $this->lastRow
) {
350 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
351 $firstInList = $this->counter
== 1;
353 $s = $this->historyLine( $this->lastRow
, $row,
354 $this->getTitle()->getNotificationTimestamp(), $latest, $firstInList );
358 $this->lastRow
= $row;
363 * Creates begin of history list with a submit button
365 * @return string HTML output
367 function getStartBody() {
369 $this->lastRow
= false;
371 $this->oldIdChecked
= 0;
373 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
374 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
375 'id' => 'mw-history-compare' ) ) . "\n";
376 $s .= Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . "\n";
377 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
379 $s .= '<div>' . $this->submitButton( wfMsg( 'compareselectedversions' ),
380 array( 'class' => 'historysubmit' ) ) . "\n";
382 $this->buttons
= '<div>';
383 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions' ),
384 array( 'class' => 'historysubmit' )
385 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
388 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
389 $s .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
391 $this->buttons
.= '</div>';
392 $s .= '</div><ul id="pagehistory">' . "\n";
396 private function getRevisionButton( $name, $msg ) {
397 $this->preventClickjacking();
398 # Note bug #20966, <button> is non-standard in IE<8
399 $element = Html
::element( 'button',
404 'class' => "mw-history-$name-button mw-float-end",
408 $this->buttons
.= $element;
412 function getEndBody() {
413 if ( $this->lastRow
) {
414 $latest = $this->counter
== 1 && $this->mIsFirst
;
415 $firstInList = $this->counter
== 1;
416 if ( $this->mIsBackwards
) {
417 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
418 if ( $this->mOffset
== '' ) {
424 # The next row is the past-the-end row
425 $next = $this->mPastTheEndRow
;
428 $s = $this->historyLine( $this->lastRow
, $next,
429 $this->getTitle()->getNotificationTimestamp(), $latest, $firstInList );
434 # Add second buttons only if there is more than one rev
435 if ( $this->getNumRows() > 2 ) {
436 $s .= $this->buttons
;
443 * Creates a submit button
445 * @param $message String: text of the submit button, will be escaped
446 * @param $attributes Array: attributes
447 * @return String: HTML output for the submit button
449 function submitButton( $message, $attributes = array() ) {
450 # Disable submit button if history has 1 revision only
451 if ( $this->getNumRows() > 1 ) {
452 return Xml
::submitButton( $message , $attributes );
459 * Returns a row from the history printout.
461 * @todo document some more, and maybe clean up the code (some params redundant?)
463 * @param $row Object: the database row corresponding to the previous line.
464 * @param $next Mixed: the database row corresponding to the next line.
465 * @param $notificationtimestamp
466 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
467 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
468 * @return String: HTML output for the row
470 function historyLine( $row, $next, $notificationtimestamp = false,
471 $latest = false, $firstInList = false )
473 $rev = new Revision( $row );
474 $rev->setTitle( $this->getTitle() );
476 $curlink = $this->curLink( $rev, $latest );
477 $lastlink = $this->lastLink( $rev, $next );
478 $diffButtons = $this->diffButtons( $rev, $firstInList );
479 $histLinks = Html
::rawElement(
481 array( 'class' => 'mw-history-histlinks' ),
482 '(' . $curlink . $this->history
->message
['pipe-separator'] . $lastlink . ') '
484 $s = $histLinks . $diffButtons;
486 $link = $this->revLink( $rev );
490 // Show checkboxes for each revision
491 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
492 $this->preventClickjacking();
493 // If revision was hidden from sysops, disable the checkbox
494 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
495 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
496 // Otherwise, enable the checkbox...
498 $del = Xml
::check( 'showhiderevisions', false,
499 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
501 // User can only view deleted revisions...
502 } elseif ( $rev->getVisibility() && $this->getUser()->isAllowed( 'deletedhistory' ) ) {
503 // If revision was hidden from sysops, disable the link
504 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
505 $cdel = Linker
::revDeleteLinkDisabled( false );
506 // Otherwise, show the link...
508 $query = array( 'type' => 'revision',
509 'target' => $this->getTitle()->getPrefixedDbkey(), 'ids' => $rev->getId() );
510 $del .= Linker
::revDeleteLink( $query,
511 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
518 $dirmark = $this->getLang()->getDirMark();
522 $s .= " <span class='history-user'>" .
523 Linker
::revUserTools( $rev, true ) . "</span>";
526 if ( $rev->isMinor() ) {
527 $s .= ' ' . ChangesList
::flag( 'minor' );
530 if ( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
531 $s .= ' ' . Linker
::formatRevisionSize( $size );
534 $s .= Linker
::revComment( $rev, false, true );
536 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
537 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
542 # Rollback and undo links
543 if ( !is_null( $next ) && is_object( $next ) ) {
544 if ( $latest && $this->getTitle()->userCan( 'rollback' ) && $this->getTitle()->userCan( 'edit' ) ) {
545 $this->preventClickjacking();
546 $tools[] = '<span class="mw-rollback-link">' .
547 Linker
::buildRollbackLink( $rev ) . '</span>';
550 if ( $this->getTitle()->quickUserCan( 'edit' )
551 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
552 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
554 # Create undo tooltip for the first (=latest) line only
555 $undoTooltip = $latest
556 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
558 $undolink = Linker
::linkKnown(
560 wfMsgHtml( 'editundo' ),
564 'undoafter' => $next->rev_id
,
565 'undo' => $rev->getId()
568 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
573 $s .= ' (' . $this->getLang()->pipeList( $tools ) . ')';
577 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
578 $classes = array_merge( $classes, $newClasses );
579 $s .= " $tagSummary";
581 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
585 $attribs['class'] = implode( ' ', $classes );
588 return Xml
::tags( 'li', $attribs, $s ) . "\n";
592 * Create a link to view this revision of the page
594 * @param $rev Revision
597 function revLink( $rev ) {
598 $date = $this->getLang()->timeanddate( wfTimestamp( TS_MW
, $rev->getTimestamp() ), true );
599 $date = htmlspecialchars( $date );
600 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
601 $link = Linker
::linkKnown(
605 array( 'oldid' => $rev->getId() )
610 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
611 $link = "<span class=\"history-deleted\">$link</span>";
617 * Create a diff-to-current link for this revision for this page
619 * @param $rev Revision
620 * @param $latest Boolean: this is the latest revision of the page?
623 function curLink( $rev, $latest ) {
624 $cur = $this->history
->message
['cur'];
625 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
628 return Linker
::linkKnown(
633 'diff' => $this->getTitle()->getLatestRevID(),
634 'oldid' => $rev->getId()
641 * Create a diff-to-previous link for this revision for this page.
643 * @param $prevRev Revision: the previous revision
644 * @param $next Mixed: the newer revision
647 function lastLink( $prevRev, $next ) {
648 $last = $this->history
->message
['last'];
649 # $next may either be a Row, null, or "unkown"
650 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
651 if ( is_null( $next ) ) {
652 # Probably no next row
654 } elseif ( $next === 'unknown' ) {
655 # Next row probably exists but is unknown, use an oldid=prev link
661 'diff' => $prevRev->getId(),
665 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
)
666 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
) )
670 return Linker
::linkKnown(
675 'diff' => $prevRev->getId(),
676 'oldid' => $next->rev_id
683 * Create radio buttons for page history
685 * @param $rev Revision object
686 * @param $firstInList Boolean: is this version the first one?
688 * @return String: HTML output for the radio buttons
690 function diffButtons( $rev, $firstInList ) {
691 if ( $this->getNumRows() > 1 ) {
693 $radio = array( 'type' => 'radio', 'value' => $id );
694 /** @todo: move title texts to javascript */
695 if ( $firstInList ) {
696 $first = Xml
::element( 'input',
697 array_merge( $radio, array(
698 'style' => 'visibility:hidden',
700 'id' => 'mw-oldid-null' ) )
702 $checkmark = array( 'checked' => 'checked' );
704 # Check visibility of old revisions
705 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
706 $radio['disabled'] = 'disabled';
707 $checkmark = array(); // We will check the next possible one
708 } elseif ( !$this->oldIdChecked
) {
709 $checkmark = array( 'checked' => 'checked' );
710 $this->oldIdChecked
= $id;
712 $checkmark = array();
714 $first = Xml
::element( 'input',
715 array_merge( $radio, $checkmark, array(
717 'id' => "mw-oldid-$id" ) ) );
718 $checkmark = array();
720 $second = Xml
::element( 'input',
721 array_merge( $radio, $checkmark, array(
723 'id' => "mw-diff-$id" ) ) );
724 return $first . $second;
731 * This is called if a write operation is possible from the generated HTML
733 function preventClickjacking( $enable = true ) {
734 $this->preventClickjacking
= $enable;
738 * Get the "prevent clickjacking" flag
740 function getPreventClickjacking() {
741 return $this->preventClickjacking
;
746 * Backwards-compatibility aliases
748 class HistoryPage
extends HistoryAction
{
749 public function __construct( Page
$article ) { # Just to make it public
750 parent
::__construct( $article );
753 public function history() {