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 /** Contains the Article object. Passed on construction. */
24 /** The $article title object. Found on construction. */
26 /** Shortcut to the user Skin object. */
30 * Construct a new HistoryPage.
32 * @param $article Article
34 function __construct( $article ) {
36 $this->article
= $article;
37 $this->title
= $article->getTitle();
38 $this->skin
= $wgUser->getSkin();
39 $this->preCacheMessages();
42 /** Get the Article object we are working on. */
43 public function getArticle() {
44 return $this->article
;
47 /** Get the Title object. */
48 public function getTitle() {
53 * As we use the same small set of messages in various methods and that
54 * they are called often, we call them once and save them in $this->message
56 private function preCacheMessages() {
57 // Precache various messages
58 if ( !isset( $this->message
) ) {
59 $msgs = array( 'cur', 'last', 'pipe-separator' );
60 foreach ( $msgs as $msg ) {
61 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
67 * Print the history page for an article.
71 global $wgOut, $wgRequest, $wgScript;
74 * Allow client caching.
76 if ( $wgOut->checkLastModified( $this->article
->getTouched() ) )
77 return; // Client cache fresh and headers sent, nothing more to do.
79 wfProfileIn( __METHOD__
);
81 // Setup page variables.
82 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
83 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
84 $wgOut->setArticleFlag( false );
85 $wgOut->setArticleRelated( true );
86 $wgOut->setRobotPolicy( 'noindex,nofollow' );
87 $wgOut->setSyndicated( true );
88 $wgOut->setFeedAppendQuery( 'action=history' );
89 $wgOut->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
91 // Creation of a subtitle link pointing to [[Special:Log]]
92 $logPage = SpecialPage
::getTitleFor( 'Log' );
93 $logLink = $this->skin
->link(
95 wfMsgHtml( 'viewpagelogs' ),
97 array( 'page' => $this->title
->getPrefixedText() ),
98 array( 'known', 'noclasses' )
100 $wgOut->setSubtitle( $logLink );
102 // Handle atom/RSS feeds.
103 $feedType = $wgRequest->getVal( 'feed' );
105 wfProfileOut( __METHOD__
);
106 return $this->feed( $feedType );
109 // Fail nicely if article doesn't exist.
110 if ( !$this->title
->exists() ) {
111 $wgOut->addWikiMsg( 'nohistory' );
112 # show deletion/move log if there is an entry
113 LogEventsList
::showLogExtract(
115 array( 'delete', 'move' ),
116 $this->title
->getPrefixedText(),
119 'conds' => array( "log_action != 'revision'" ),
120 'showIfEmpty' => false,
121 'msgKey' => array( 'moveddeleted-notice' )
124 wfProfileOut( __METHOD__
);
129 * Add date selector to quickly get to a certain time
131 $year = $wgRequest->getInt( 'year' );
132 $month = $wgRequest->getInt( 'month' );
133 $tagFilter = $wgRequest->getVal( 'tagfilter' );
134 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
137 * Option to show only revisions that have been (partially) hidden via RevisionDelete
139 if ( $wgRequest->getBool( 'deleted' ) ) {
140 $conds = array( "rev_deleted != '0'" );
144 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
145 'deleted', 'mw-show-deleted-only', $wgRequest->getBool( 'deleted' ) ) . "\n";
147 // Add the general form
148 $action = htmlspecialchars( $wgScript );
150 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
152 wfMsg( 'history-fieldset-title' ),
154 array( 'id' => 'mw-history-search' )
156 Html
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
157 Html
::hidden( 'action', 'history' ) . "\n" .
158 Xml
::dateMenu( $year, $month ) . ' ' .
159 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
161 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
165 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
167 // Create and output the list.
168 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
170 $pager->getNavigationBar() .
172 $pager->getNavigationBar()
175 wfProfileOut( __METHOD__
);
179 * Fetch an array of revisions, specified by a given limit, offset and
180 * direction. This is now only used by the feeds. It was previously
181 * used by the main UI but that's now handled by the pager.
183 * @param $limit Integer: the limit number of revisions to get
184 * @param $offset Integer
185 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
186 * @return ResultWrapper
188 function fetchRevisions( $limit, $offset, $direction ) {
189 $dbr = wfGetDB( DB_SLAVE
);
191 if ( $direction == HistoryPage
::DIR_PREV
) {
192 list( $dirs, $oper ) = array( "ASC", ">=" );
193 } else { /* $direction == HistoryPage::DIR_NEXT */
194 list( $dirs, $oper ) = array( "DESC", "<=" );
198 $offsets = array( "rev_timestamp $oper '$offset'" );
203 $page_id = $this->title
->getArticleID();
205 return $dbr->select( 'revision',
206 Revision
::selectFields(),
207 array_merge( array( "rev_page=$page_id" ), $offsets ),
209 array( 'ORDER BY' => "rev_timestamp $dirs",
210 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
215 * Output a subscription feed listing recent edits to this page.
217 * @param $type String: feed type
219 function feed( $type ) {
220 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
221 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
225 $feed = new $wgFeedClasses[$type](
226 $this->title
->getPrefixedText() . ' - ' .
227 wfMsgForContent( 'history-feed-title' ),
228 wfMsgForContent( 'history-feed-description' ),
229 $this->title
->getFullUrl( 'action=history' )
232 // Get a limit on number of feed entries. Provide a sane default
233 // of 10 if none is defined (but limit to $wgFeedLimit max)
234 $limit = $wgRequest->getInt( 'limit', 10 );
235 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
238 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
240 // Generate feed elements enclosed between header and footer.
243 foreach ( $items as $row ) {
244 $feed->outItem( $this->feedItem( $row ) );
247 $feed->outItem( $this->feedEmpty() );
252 function feedEmpty() {
255 wfMsgForContent( 'nohistory' ),
256 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
257 $this->title
->getFullUrl(),
258 wfTimestamp( TS_MW
),
260 $this->title
->getTalkPage()->getFullUrl()
265 * Generate a FeedItem object from a given revision table row
266 * Borrows Recent Changes' feed generation functions for formatting;
267 * includes a diff to the previous revision (if any).
269 * @param $row Object: database row
272 function feedItem( $row ) {
273 $rev = new Revision( $row );
274 $rev->setTitle( $this->title
);
275 $text = FeedUtils
::formatDiffRow(
277 $this->title
->getPreviousRevisionID( $rev->getId() ),
279 $rev->getTimestamp(),
282 if ( $rev->getComment() == '' ) {
284 $title = wfMsgForContent( 'history-feed-item-nocomment',
286 $wgContLang->timeanddate( $rev->getTimestamp() ),
287 $wgContLang->date( $rev->getTimestamp() ),
288 $wgContLang->time( $rev->getTimestamp() )
291 $title = $rev->getUserText() .
292 wfMsgForContent( 'colon-separator' ) .
293 FeedItem
::stripComment( $rev->getComment() );
298 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
299 $rev->getTimestamp(),
301 $this->title
->getTalkPage()->getFullUrl()
309 class HistoryPager
extends ReverseChronologicalPager
{
310 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
311 protected $oldIdChecked;
313 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
314 parent
::__construct();
315 $this->historyPage
= $historyPage;
316 $this->title
= $this->historyPage
->getTitle();
317 $this->tagFilter
= $tagFilter;
318 $this->getDateCond( $year, $month );
319 $this->conds
= $conds;
322 // For hook compatibility...
323 function getArticle() {
324 return $this->historyPage
->getArticle();
327 function getSqlComment() {
328 if ( $this->conds
) {
329 return 'history page filtered'; // potentially slow, see CR r58153
331 return 'history page unfiltered';
335 function getQueryInfo() {
337 'tables' => array( 'revision' ),
338 'fields' => Revision
::selectFields(),
339 'conds' => array_merge(
340 array( 'rev_page' => $this->historyPage
->getTitle()->getArticleID() ),
342 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
343 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
345 ChangeTags
::modifyDisplayQuery(
346 $queryInfo['tables'],
347 $queryInfo['fields'],
349 $queryInfo['join_conds'],
350 $queryInfo['options'],
353 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
357 function getIndexField() {
358 return 'rev_timestamp';
361 function formatRow( $row ) {
362 if ( $this->lastRow
) {
363 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
364 $firstInList = $this->counter
== 1;
366 $s = $this->historyLine( $this->lastRow
, $row,
367 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
371 $this->lastRow
= $row;
376 * Creates begin of history list with a submit button
378 * @return string HTML output
380 function getStartBody() {
381 global $wgScript, $wgUser, $wgOut, $wgContLang;
382 $this->lastRow
= false;
384 $this->oldIdChecked
= 0;
386 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
387 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
388 'id' => 'mw-history-compare' ) ) . "\n";
389 $s .= Html
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
390 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
392 $s .= '<div>' . $this->submitButton( wfMsg( 'compareselectedversions' ),
393 array( 'class' => 'historysubmit' ) ) . "\n";
395 $this->buttons
= '<div>';
396 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions' ),
397 array( 'class' => 'historysubmit' )
398 +
$wgUser->getSkin()->tooltipAndAccessKeyAttribs( 'compareselectedversions' )
401 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
402 $float = $wgContLang->alignEnd();
403 # Note bug #20966, <button> is non-standard in IE<8
404 $element = Html
::element( 'button',
407 'name' => 'revisiondelete',
409 'style' => "float: $float;",
410 'class' => 'mw-history-revisiondelete-button',
412 wfMsg( 'showhideselectedversions' )
415 $this->buttons
.= $element;
417 if ( $wgUser->isAllowed( 'revisionmove' ) ) {
418 $float = $wgContLang->alignEnd();
419 # Note bug #20966, <button> is non-standard in IE<8
420 $element = Html
::element( 'button',
423 'name' => 'revisionmove',
425 'style' => "float: $float;",
426 'class' => 'mw-history-revisionmove-button',
428 wfMsg( 'revisionmoveselectedversions' )
431 $this->buttons
.= $element;
433 $this->buttons
.= '</div>';
434 $s .= '</div><ul id="pagehistory">' . "\n";
438 function getEndBody() {
439 if ( $this->lastRow
) {
440 $latest = $this->counter
== 1 && $this->mIsFirst
;
441 $firstInList = $this->counter
== 1;
442 if ( $this->mIsBackwards
) {
443 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
444 if ( $this->mOffset
== '' ) {
450 # The next row is the past-the-end row
451 $next = $this->mPastTheEndRow
;
454 $s = $this->historyLine( $this->lastRow
, $next,
455 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
460 # Add second buttons only if there is more than one rev
461 if ( $this->getNumRows() > 2 ) {
462 $s .= $this->buttons
;
469 * Creates a submit button
471 * @param $message String: text of the submit button, will be escaped
472 * @param $attributes Array: attributes
473 * @return String: HTML output for the submit button
475 function submitButton( $message, $attributes = array() ) {
476 # Disable submit button if history has 1 revision only
477 if ( $this->getNumRows() > 1 ) {
478 return Xml
::submitButton( $message , $attributes );
485 * Returns a row from the history printout.
487 * @todo document some more, and maybe clean up the code (some params redundant?)
489 * @param $row Object: the database row corresponding to the previous line.
490 * @param $next Mixed: the database row corresponding to the next line.
491 * @param $notificationtimestamp
492 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
493 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
494 * @return String: HTML output for the row
496 function historyLine( $row, $next, $notificationtimestamp = false,
497 $latest = false, $firstInList = false )
499 global $wgUser, $wgLang;
500 $rev = new Revision( $row );
501 $rev->setTitle( $this->title
);
503 $curlink = $this->curLink( $rev, $latest );
504 $lastlink = $this->lastLink( $rev, $next );
505 $diffButtons = $this->diffButtons( $rev, $firstInList );
506 $histLinks = Html
::rawElement(
508 array( 'class' => 'mw-history-histlinks' ),
509 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
511 $s = $histLinks . $diffButtons;
513 $link = $this->revLink( $rev );
517 // Show checkboxes for each revision
518 if ( $wgUser->isAllowed( 'deleterevision' ) ||
$wgUser->isAllowed( 'revisionmove' ) ) {
519 // If revision was hidden from sysops, disable the checkbox
520 // However, if the user has revisionmove rights, we cannot disable the checkbox
521 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) && !$wgUser->isAllowed( 'revisionmove' ) ) {
522 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
523 // Otherwise, enable the checkbox...
525 $del = Xml
::check( 'showhiderevisions', false,
526 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
528 // User can only view deleted revisions...
529 } else if ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
530 // If revision was hidden from sysops, disable the link
531 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
532 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
533 // Otherwise, show the link...
535 $query = array( 'type' => 'revision',
536 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
537 $del .= $this->getSkin()->revDeleteLink( $query,
538 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
546 $s .= " <span class='history-user'>" .
547 $this->getSkin()->revUserTools( $rev, true ) . "</span>";
549 if ( $rev->isMinor() ) {
550 $s .= ' ' . ChangesList
::flag( 'minor' );
553 if ( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
554 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
557 $s .= $this->getSkin()->revComment( $rev, false, true );
559 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
560 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
565 # Rollback and undo links
566 if ( !is_null( $next ) && is_object( $next ) ) {
567 if ( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
568 $tools[] = '<span class="mw-rollback-link">' .
569 $this->getSkin()->buildRollbackLink( $rev ) . '</span>';
572 if ( $this->title
->quickUserCan( 'edit' )
573 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
574 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
576 # Create undo tooltip for the first (=latest) line only
577 $undoTooltip = $latest
578 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
580 $undolink = $this->getSkin()->link(
582 wfMsgHtml( 'editundo' ),
586 'undoafter' => $next->rev_id
,
587 'undo' => $rev->getId()
589 array( 'known', 'noclasses' )
591 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
596 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
600 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
601 $classes = array_merge( $classes, $newClasses );
602 $s .= " $tagSummary";
604 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
608 $attribs['class'] = implode( ' ', $classes );
611 return Xml
::tags( 'li', $attribs, $s ) . "\n";
615 * Create a link to view this revision of the page
617 * @param $rev Revision
620 function revLink( $rev ) {
622 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $rev->getTimestamp() ), true );
623 $date = htmlspecialchars( $date );
624 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
625 $link = $this->getSkin()->link(
629 array( 'oldid' => $rev->getId() ),
630 array( 'known', 'noclasses' )
635 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
636 $link = "<span class=\"history-deleted\">$link</span>";
642 * Create a diff-to-current link for this revision for this page
644 * @param $rev Revision
645 * @param $latest Boolean: this is the latest revision of the page?
648 function curLink( $rev, $latest ) {
649 $cur = $this->historyPage
->message
['cur'];
650 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
653 return $this->getSkin()->link(
658 'diff' => $this->title
->getLatestRevID(),
659 'oldid' => $rev->getId()
661 array( 'known', 'noclasses' )
667 * Create a diff-to-previous link for this revision for this page.
669 * @param $prevRev Revision: the previous revision
670 * @param $next Mixed: the newer revision
673 function lastLink( $prevRev, $next ) {
674 $last = $this->historyPage
->message
['last'];
675 # $next may either be a Row, null, or "unkown"
676 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
677 if ( is_null( $next ) ) {
678 # Probably no next row
680 } elseif ( $next === 'unknown' ) {
681 # Next row probably exists but is unknown, use an oldid=prev link
682 return $this->getSkin()->link(
687 'diff' => $prevRev->getId(),
690 array( 'known', 'noclasses' )
692 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
)
693 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
) )
697 return $this->getSkin()->link(
702 'diff' => $prevRev->getId(),
703 'oldid' => $next->rev_id
705 array( 'known', 'noclasses' )
711 * Create radio buttons for page history
713 * @param $rev Revision object
714 * @param $firstInList Boolean: is this version the first one?
716 * @return String: HTML output for the radio buttons
718 function diffButtons( $rev, $firstInList ) {
719 if ( $this->getNumRows() > 1 ) {
721 $radio = array( 'type' => 'radio', 'value' => $id );
722 /** @todo: move title texts to javascript */
723 if ( $firstInList ) {
724 $first = Xml
::element( 'input',
725 array_merge( $radio, array(
726 'style' => 'visibility:hidden',
728 'id' => 'mw-oldid-null' ) )
730 $checkmark = array( 'checked' => 'checked' );
732 # Check visibility of old revisions
733 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
734 $radio['disabled'] = 'disabled';
735 $checkmark = array(); // We will check the next possible one
736 } else if ( !$this->oldIdChecked
) {
737 $checkmark = array( 'checked' => 'checked' );
738 $this->oldIdChecked
= $id;
740 $checkmark = array();
742 $first = Xml
::element( 'input',
743 array_merge( $radio, $checkmark, array(
745 'id' => "mw-oldid-$id" ) ) );
746 $checkmark = array();
748 $second = Xml
::element( 'input',
749 array_merge( $radio, $checkmark, array(
751 'id' => "mw-diff-$id" ) ) );
752 return $first . $second;
760 * Backwards-compatibility aliases
762 class PageHistory
extends HistoryPage
{}
763 class PageHistoryPager
extends HistoryPager
{}