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
22 var $article, $title, $skin;
25 * Construct a new HistoryPage.
27 * @param $article Article
29 function __construct( $article ) {
31 $this->article
= $article;
32 $this->title
= $article->getTitle();
33 $this->skin
= $wgUser->getSkin();
34 $this->preCacheMessages();
37 function getArticle() {
38 return $this->article
;
46 * As we use the same small set of messages in various methods and that
47 * they are called often, we call them once and save them in $this->message
49 function preCacheMessages() {
50 // Precache various messages
51 if ( !isset( $this->message
) ) {
52 $msgs = array( 'cur', 'last', 'pipe-separator' );
53 foreach ( $msgs as $msg ) {
54 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
60 * Print the history page for an article.
64 global $wgOut, $wgRequest, $wgScript;
67 * Allow client caching.
69 if ( $wgOut->checkLastModified( $this->article
->getTouched() ) )
70 return; // Client cache fresh and headers sent, nothing more to do.
72 wfProfileIn( __METHOD__
);
75 * Setup page variables.
77 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
78 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
79 $wgOut->setArticleFlag( false );
80 $wgOut->setArticleRelated( true );
81 $wgOut->setRobotPolicy( 'noindex,nofollow' );
82 $wgOut->setSyndicated( true );
83 $wgOut->setFeedAppendQuery( 'action=history' );
84 $wgOut->addModules( array( 'mediawiki.legacy.history', 'mediawiki.views.history' ) );
86 $logPage = SpecialPage
::getTitleFor( 'Log' );
87 $logLink = $this->skin
->link(
89 wfMsgHtml( 'viewpagelogs' ),
91 array( 'page' => $this->title
->getPrefixedText() ),
92 array( 'known', 'noclasses' )
94 $wgOut->setSubtitle( $logLink );
96 $feedType = $wgRequest->getVal( 'feed' );
98 wfProfileOut( __METHOD__
);
99 return $this->feed( $feedType );
103 * Fail if article doesn't exist.
105 if ( !$this->title
->exists() ) {
106 $wgOut->addWikiMsg( 'nohistory' );
107 # show deletion/move log if there is an entry
108 LogEventsList
::showLogExtract(
110 array( 'delete', 'move' ),
111 $this->title
->getPrefixedText(),
114 'conds' => array( "log_action != 'revision'" ),
115 'showIfEmpty' => false,
116 'msgKey' => array( 'moveddeleted-notice' )
119 wfProfileOut( __METHOD__
);
124 * Add date selector to quickly get to a certain time
126 $year = $wgRequest->getInt( 'year' );
127 $month = $wgRequest->getInt( 'month' );
128 $tagFilter = $wgRequest->getVal( 'tagfilter' );
129 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
131 * Option to show only revisions that have been (partially) hidden via RevisionDelete
133 if ( $wgRequest->getBool( 'deleted' ) ) {
134 $conds = array( "rev_deleted != '0'" );
138 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
139 'deleted', 'mw-show-deleted-only', $wgRequest->getBool( 'deleted' ) ) . "\n";
141 $action = htmlspecialchars( $wgScript );
143 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
145 wfMsg( 'history-fieldset-title' ),
147 array( 'id' => 'mw-history-search' )
149 Html
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
150 Html
::hidden( 'action', 'history' ) . "\n" .
151 Xml
::dateMenu( $year, $month ) . ' ' .
152 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
154 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
158 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
163 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
165 $pager->getNavigationBar() .
167 $pager->getNavigationBar()
170 wfProfileOut( __METHOD__
);
174 * Fetch an array of revisions, specified by a given limit, offset and
175 * direction. This is now only used by the feeds. It was previously
176 * used by the main UI but that's now handled by the pager.
178 * @param $limit Integer: the limit number of revisions to get
179 * @param $offset Integer
180 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
181 * @return ResultWrapper
183 function fetchRevisions( $limit, $offset, $direction ) {
184 $dbr = wfGetDB( DB_SLAVE
);
186 if ( $direction == HistoryPage
::DIR_PREV
) {
187 list( $dirs, $oper ) = array( "ASC", ">=" );
188 } else { /* $direction == HistoryPage::DIR_NEXT */
189 list( $dirs, $oper ) = array( "DESC", "<=" );
193 $offsets = array( "rev_timestamp $oper '$offset'" );
198 $page_id = $this->title
->getArticleID();
200 return $dbr->select( 'revision',
201 Revision
::selectFields(),
202 array_merge( array( "rev_page=$page_id" ), $offsets ),
204 array( 'ORDER BY' => "rev_timestamp $dirs",
205 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
210 * Output a subscription feed listing recent edits to this page.
212 * @param $type String: feed type
214 function feed( $type ) {
215 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
216 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
220 $feed = new $wgFeedClasses[$type](
221 $this->title
->getPrefixedText() . ' - ' .
222 wfMsgForContent( 'history-feed-title' ),
223 wfMsgForContent( 'history-feed-description' ),
224 $this->title
->getFullUrl( 'action=history' )
227 // Get a limit on number of feed entries. Provide a sane default
228 // of 10 if none is defined (but limit to $wgFeedLimit max)
229 $limit = $wgRequest->getInt( 'limit', 10 );
230 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
233 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
237 foreach ( $items as $row ) {
238 $feed->outItem( $this->feedItem( $row ) );
241 $feed->outItem( $this->feedEmpty() );
246 function feedEmpty() {
249 wfMsgForContent( 'nohistory' ),
250 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
251 $this->title
->getFullUrl(),
252 wfTimestamp( TS_MW
),
254 $this->title
->getTalkPage()->getFullUrl()
259 * Generate a FeedItem object from a given revision table row
260 * Borrows Recent Changes' feed generation functions for formatting;
261 * includes a diff to the previous revision (if any).
263 * @param $row Object: database row
266 function feedItem( $row ) {
267 $rev = new Revision( $row );
268 $rev->setTitle( $this->title
);
269 $text = FeedUtils
::formatDiffRow(
271 $this->title
->getPreviousRevisionID( $rev->getId() ),
273 $rev->getTimestamp(),
276 if ( $rev->getComment() == '' ) {
278 $title = wfMsgForContent( 'history-feed-item-nocomment',
280 $wgContLang->timeanddate( $rev->getTimestamp() ),
281 $wgContLang->date( $rev->getTimestamp() ),
282 $wgContLang->time( $rev->getTimestamp() )
285 $title = $rev->getUserText() .
286 wfMsgForContent( 'colon-separator' ) .
287 FeedItem
::stripComment( $rev->getComment() );
292 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
293 $rev->getTimestamp(),
295 $this->title
->getTalkPage()->getFullUrl()
303 class HistoryPager
extends ReverseChronologicalPager
{
304 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
305 protected $oldIdChecked;
307 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
308 parent
::__construct();
309 $this->historyPage
= $historyPage;
310 $this->title
= $this->historyPage
->title
;
311 $this->tagFilter
= $tagFilter;
312 $this->getDateCond( $year, $month );
313 $this->conds
= $conds;
316 // For hook compatibility...
317 function getArticle() {
318 return $this->historyPage
->getArticle();
321 function getSqlComment() {
322 if ( $this->conds
) {
323 return 'history page filtered'; // potentially slow, see CR r58153
325 return 'history page unfiltered';
329 function getQueryInfo() {
331 'tables' => array( 'revision' ),
332 'fields' => Revision
::selectFields(),
333 'conds' => array_merge(
334 array( 'rev_page' => $this->historyPage
->title
->getArticleID() ),
336 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
337 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
339 ChangeTags
::modifyDisplayQuery(
340 $queryInfo['tables'],
341 $queryInfo['fields'],
343 $queryInfo['join_conds'],
344 $queryInfo['options'],
347 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
351 function getIndexField() {
352 return 'rev_timestamp';
355 function formatRow( $row ) {
356 if ( $this->lastRow
) {
357 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
358 $firstInList = $this->counter
== 1;
360 $s = $this->historyLine( $this->lastRow
, $row,
361 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
365 $this->lastRow
= $row;
370 * Creates begin of history list with a submit button
372 * @return string HTML output
374 function getStartBody() {
375 global $wgScript, $wgUser, $wgOut, $wgContLang;
376 $this->lastRow
= false;
378 $this->oldIdChecked
= 0;
380 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
381 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
382 'id' => 'mw-history-compare' ) ) . "\n";
383 $s .= Html
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
384 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
386 $s .= '<div>' . $this->submitButton( wfMsg( 'compareselectedversions' ),
387 array( 'class' => 'historysubmit' ) ) . "\n";
389 $this->buttons
= '<div>';
390 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions' ),
391 array( 'class' => 'historysubmit' )
392 +
$wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'compareselectedversions' )
395 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
396 $float = $wgContLang->alignEnd();
397 # Note bug #20966, <button> is non-standard in IE<8
398 $element = Html
::element( 'button',
401 'name' => 'revisiondelete',
403 'style' => "float: $float;",
404 'class' => 'mw-history-revisiondelete-button',
406 wfMsg( 'showhideselectedversions' )
409 $this->buttons
.= $element;
411 if ( $wgUser->isAllowed( 'revisionmove' ) ) {
412 $float = $wgContLang->alignEnd();
413 # Note bug #20966, <button> is non-standard in IE<8
414 $element = Html
::element( 'button',
417 'name' => 'revisionmove',
419 'style' => "float: $float;",
420 'class' => 'mw-history-revisionmove-button',
422 wfMsg( 'revisionmoveselectedversions' )
425 $this->buttons
.= $element;
427 $this->buttons
.= '</div>';
428 $s .= '</div><ul id="pagehistory">' . "\n";
432 function getEndBody() {
433 if ( $this->lastRow
) {
434 $latest = $this->counter
== 1 && $this->mIsFirst
;
435 $firstInList = $this->counter
== 1;
436 if ( $this->mIsBackwards
) {
437 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
438 if ( $this->mOffset
== '' ) {
444 # The next row is the past-the-end row
445 $next = $this->mPastTheEndRow
;
448 $s = $this->historyLine( $this->lastRow
, $next,
449 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
454 # Add second buttons only if there is more than one rev
455 if ( $this->getNumRows() > 2 ) {
456 $s .= $this->buttons
;
463 * Creates a submit button
465 * @param $message String: text of the submit button, will be escaped
466 * @param $attributes Array: attributes
467 * @return String: HTML output for the submit button
469 function submitButton( $message, $attributes = array() ) {
470 # Disable submit button if history has 1 revision only
471 if ( $this->getNumRows() > 1 ) {
472 return Xml
::submitButton( $message , $attributes );
479 * Returns a row from the history printout.
481 * @todo document some more, and maybe clean up the code (some params redundant?)
483 * @param $row Object: the database row corresponding to the previous line.
484 * @param $next Mixed: the database row corresponding to the next line.
485 * @param $notificationtimestamp
486 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
487 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
488 * @return String: HTML output for the row
490 function historyLine( $row, $next, $notificationtimestamp = false,
491 $latest = false, $firstInList = false )
493 global $wgUser, $wgLang;
494 $rev = new Revision( $row );
495 $rev->setTitle( $this->title
);
497 $curlink = $this->curLink( $rev, $latest );
498 $lastlink = $this->lastLink( $rev, $next );
499 $diffButtons = $this->diffButtons( $rev, $firstInList );
500 $histLinks = Html
::rawElement(
502 array( 'class' => 'mw-history-histlinks' ),
503 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
505 $s = $histLinks . $diffButtons;
507 $link = $this->revLink( $rev );
511 // Show checkboxes for each revision
512 if ( $wgUser->isAllowed( 'deleterevision' ) ||
$wgUser->isAllowed( 'revisionmove' ) ) {
513 // If revision was hidden from sysops, disable the checkbox
514 // However, if the user has revisionmove rights, we cannot disable the checkbox
515 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) && !$wgUser->isAllowed( 'revisionmove' ) ) {
516 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
517 // Otherwise, enable the checkbox...
519 $del = Xml
::check( 'showhiderevisions', false,
520 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
522 // User can only view deleted revisions...
523 } else if ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
524 // If revision was hidden from sysops, disable the link
525 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
526 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
527 // Otherwise, show the link...
529 $query = array( 'type' => 'revision',
530 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
531 $del .= $this->getSkin()->revDeleteLink( $query,
532 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
540 $s .= " <span class='history-user'>" .
541 $this->getSkin()->revUserTools( $rev, true ) . "</span>";
543 if ( $rev->isMinor() ) {
544 $s .= ' ' . ChangesList
::flag( 'minor' );
547 if ( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
548 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
551 $s .= $this->getSkin()->revComment( $rev, false, true );
553 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
554 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
559 # Rollback and undo links
560 if ( !is_null( $next ) && is_object( $next ) ) {
561 if ( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
562 $tools[] = '<span class="mw-rollback-link">' .
563 $this->getSkin()->buildRollbackLink( $rev ) . '</span>';
566 if ( $this->title
->quickUserCan( 'edit' )
567 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
568 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
570 # Create undo tooltip for the first (=latest) line only
571 $undoTooltip = $latest
572 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
574 $undolink = $this->getSkin()->link(
576 wfMsgHtml( 'editundo' ),
580 'undoafter' => $next->rev_id
,
581 'undo' => $rev->getId()
583 array( 'known', 'noclasses' )
585 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
590 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
594 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
595 $classes = array_merge( $classes, $newClasses );
596 $s .= " $tagSummary";
598 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
602 $attribs['class'] = implode( ' ', $classes );
605 return Xml
::tags( 'li', $attribs, $s ) . "\n";
609 * Create a link to view this revision of the page
611 * @param $rev Revision
614 function revLink( $rev ) {
616 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $rev->getTimestamp() ), true );
617 $date = htmlspecialchars( $date );
618 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
619 $link = $this->getSkin()->link(
623 array( 'oldid' => $rev->getId() ),
624 array( 'known', 'noclasses' )
629 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
630 $link = "<span class=\"history-deleted\">$link</span>";
636 * Create a diff-to-current link for this revision for this page
638 * @param $rev Revision
639 * @param $latest Boolean: this is the latest revision of the page?
642 function curLink( $rev, $latest ) {
643 $cur = $this->historyPage
->message
['cur'];
644 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
647 return $this->getSkin()->link(
652 'diff' => $this->title
->getLatestRevID(),
653 'oldid' => $rev->getId()
655 array( 'known', 'noclasses' )
661 * Create a diff-to-previous link for this revision for this page.
663 * @param $prevRev Revision: the previous revision
664 * @param $next Mixed: the newer revision
667 function lastLink( $prevRev, $next ) {
668 $last = $this->historyPage
->message
['last'];
669 # $next may either be a Row, null, or "unkown"
670 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
671 if ( is_null( $next ) ) {
672 # Probably no next row
674 } elseif ( $next === 'unknown' ) {
675 # Next row probably exists but is unknown, use an oldid=prev link
676 return $this->getSkin()->link(
681 'diff' => $prevRev->getId(),
684 array( 'known', 'noclasses' )
686 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
)
687 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
) )
691 return $this->getSkin()->link(
696 'diff' => $prevRev->getId(),
697 'oldid' => $next->rev_id
699 array( 'known', 'noclasses' )
705 * Create radio buttons for page history
707 * @param $rev Revision object
708 * @param $firstInList Boolean: is this version the first one?
710 * @return String: HTML output for the radio buttons
712 function diffButtons( $rev, $firstInList ) {
713 if ( $this->getNumRows() > 1 ) {
715 $radio = array( 'type' => 'radio', 'value' => $id );
716 /** @todo: move title texts to javascript */
717 if ( $firstInList ) {
718 $first = Xml
::element( 'input',
719 array_merge( $radio, array(
720 'style' => 'visibility:hidden',
722 'id' => 'mw-oldid-null' ) )
724 $checkmark = array( 'checked' => 'checked' );
726 # Check visibility of old revisions
727 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
728 $radio['disabled'] = 'disabled';
729 $checkmark = array(); // We will check the next possible one
730 } else if ( !$this->oldIdChecked
) {
731 $checkmark = array( 'checked' => 'checked' );
732 $this->oldIdChecked
= $id;
734 $checkmark = array();
736 $first = Xml
::element( 'input',
737 array_merge( $radio, $checkmark, array(
739 'id' => "mw-oldid-$id" ) ) );
740 $checkmark = array();
742 $second = Xml
::element( 'input',
743 array_merge( $radio, $checkmark, array(
745 'id' => "mw-diff-$id" ) ) );
746 return $first . $second;
754 * Backwards-compatibility aliases
756 class PageHistory
extends HistoryPage
{}
757 class PageHistoryPager
extends HistoryPager
{}