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->addScriptFile( 'history.js' );
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 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
150 Xml
::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", "<=");
192 $offsets = array("rev_timestamp $oper '$offset'");
196 $page_id = $this->title
->getArticleID();
198 return $dbr->select( 'revision',
199 Revision
::selectFields(),
200 array_merge(array("rev_page=$page_id"), $offsets),
202 array( 'ORDER BY' => "rev_timestamp $dirs",
203 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
208 * Output a subscription feed listing recent edits to this page.
210 * @param $type String: feed type
212 function feed( $type ) {
213 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
214 if( !FeedUtils
::checkFeedOutput($type) ) {
218 $feed = new $wgFeedClasses[$type](
219 $this->title
->getPrefixedText() . ' - ' .
220 wfMsgForContent( 'history-feed-title' ),
221 wfMsgForContent( 'history-feed-description' ),
222 $this->title
->getFullUrl( 'action=history' )
225 // Get a limit on number of feed entries. Provide a sane default
226 // of 10 if none is defined (but limit to $wgFeedLimit max)
227 $limit = $wgRequest->getInt( 'limit', 10 );
228 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
231 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
235 foreach( $items as $row ) {
236 $feed->outItem( $this->feedItem( $row ) );
239 $feed->outItem( $this->feedEmpty() );
244 function feedEmpty() {
247 wfMsgForContent( 'nohistory' ),
248 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
249 $this->title
->getFullUrl(),
250 wfTimestamp( TS_MW
),
252 $this->title
->getTalkPage()->getFullUrl()
257 * Generate a FeedItem object from a given revision table row
258 * Borrows Recent Changes' feed generation functions for formatting;
259 * includes a diff to the previous revision (if any).
261 * @param $row Object: database row
264 function feedItem( $row ) {
265 $rev = new Revision( $row );
266 $rev->setTitle( $this->title
);
267 $text = FeedUtils
::formatDiffRow(
269 $this->title
->getPreviousRevisionID( $rev->getId() ),
271 $rev->getTimestamp(),
274 if( $rev->getComment() == '' ) {
276 $title = wfMsgForContent( 'history-feed-item-nocomment',
278 $wgContLang->timeanddate( $rev->getTimestamp() ),
279 $wgContLang->date( $rev->getTimestamp() ),
280 $wgContLang->time( $rev->getTimestamp() )
283 $title = $rev->getUserText() .
284 wfMsgForContent( 'colon-separator' ) .
285 FeedItem
::stripComment( $rev->getComment() );
290 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
291 $rev->getTimestamp(),
293 $this->title
->getTalkPage()->getFullUrl()
301 class HistoryPager
extends ReverseChronologicalPager
{
302 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
303 protected $oldIdChecked;
305 function __construct( $historyPage, $year='', $month='', $tagFilter = '', $conds = array() ) {
306 parent
::__construct();
307 $this->historyPage
= $historyPage;
308 $this->title
= $this->historyPage
->title
;
309 $this->tagFilter
= $tagFilter;
310 $this->getDateCond( $year, $month );
311 $this->conds
= $conds;
314 // For hook compatibility...
315 function getArticle() {
316 return $this->historyPage
->getArticle();
319 function getSqlComment() {
320 if ( $this->conds
) {
321 return 'history page filtered'; // potentially slow, see CR r58153
323 return 'history page unfiltered';
327 function getQueryInfo() {
329 'tables' => array('revision'),
330 'fields' => Revision
::selectFields(),
331 'conds' => array_merge(
332 array( 'rev_page' => $this->historyPage
->title
->getArticleID() ),
334 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
335 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
337 ChangeTags
::modifyDisplayQuery(
338 $queryInfo['tables'],
339 $queryInfo['fields'],
341 $queryInfo['join_conds'],
342 $queryInfo['options'],
345 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
349 function getIndexField() {
350 return 'rev_timestamp';
353 function formatRow( $row ) {
354 if( $this->lastRow
) {
355 $latest = ($this->counter
== 1 && $this->mIsFirst
);
356 $firstInList = $this->counter
== 1;
357 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
358 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
362 $this->lastRow
= $row;
367 * Creates begin of history list with a submit button
369 * @return string HTML output
371 function getStartBody() {
372 global $wgScript, $wgUser, $wgOut, $wgContLang;
373 $this->lastRow
= false;
375 $this->oldIdChecked
= 0;
377 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
378 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
379 'id' => 'mw-history-compare' ) ) . "\n";
380 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
381 $s .= Xml
::hidden( 'action', 'historysubmit' ) . "\n";
383 $this->buttons
= '<div>';
384 if( $wgUser->isAllowed('deleterevision') ) {
385 $float = $wgContLang->alignEnd();
386 # Note bug #20966, <button> is non-standard in IE<8
387 $this->buttons
.= Xml
::element( 'button',
390 'name' => 'revisiondelete',
392 'style' => "float: $float;",
393 'class' => 'mw-history-revisiondelete-button',
395 wfMsg( 'showhideselectedversions' )
398 if( $wgUser->isAllowed( 'revisionmove' ) ) {
399 $float = $wgContLang->alignEnd();
400 # Note bug #20966, <button> is non-standard in IE<8
401 $this->buttons
.= Xml
::element( 'button',
404 'name' => 'revisionmove',
406 'style' => "float: $float;",
407 'class' => 'mw-history-revisionmove-button',
409 wfMsg( 'revisionmoveselectedversions' )
412 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
414 'class' => 'historysubmit',
415 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
416 'title' => wfMsg( 'tooltip-compareselectedversions' ),
419 $this->buttons
.= '</div>';
420 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
424 function getEndBody() {
425 if( $this->lastRow
) {
426 $latest = $this->counter
== 1 && $this->mIsFirst
;
427 $firstInList = $this->counter
== 1;
428 if( $this->mIsBackwards
) {
429 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
430 if( $this->mOffset
== '' ) {
436 # The next row is the past-the-end row
437 $next = $this->mPastTheEndRow
;
439 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
440 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
445 # Add second buttons only if there is more than one rev
446 if( $this->getNumRows() > 2 ) {
447 $s .= $this->buttons
;
454 * Creates a submit button
456 * @param $message String: text of the submit button, will be escaped
457 * @param $attributes Array: attributes
458 * @return String: HTML output for the submit button
460 function submitButton( $message, $attributes = array() ) {
461 # Disable submit button if history has 1 revision only
462 if( $this->getNumRows() > 1 ) {
463 return Xml
::submitButton( $message , $attributes );
470 * Returns a row from the history printout.
472 * @todo document some more, and maybe clean up the code (some params redundant?)
474 * @param $row Object: the database row corresponding to the previous line.
475 * @param $next Mixed: the database row corresponding to the next line.
476 * @param $counter Integer: apparently a counter of what row number we're at, counted from the top row = 1.
477 * @param $notificationtimestamp
478 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
479 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
480 * @return String: HTML output for the row
482 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
483 $latest = false, $firstInList = false )
485 global $wgUser, $wgLang;
486 $rev = new Revision( $row );
487 $rev->setTitle( $this->title
);
489 $curlink = $this->curLink( $rev, $latest );
490 $lastlink = $this->lastLink( $rev, $next, $counter );
491 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
492 $histLinks = Html
::rawElement(
494 array( 'class' => 'mw-history-histlinks' ),
495 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
497 $s = $histLinks . $diffButtons;
499 $link = $this->revLink( $rev );
503 // Show checkboxes for each revision
504 if( $wgUser->isAllowed( 'deleterevision' ) ||
$wgUser->isAllowed( 'revisionmove' ) ) {
505 // If revision was hidden from sysops, disable the checkbox
506 // However, if the user has revisionmove rights, we cannot disable the checkbox
507 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) && !$wgUser->isAllowed( 'revisionmove' ) ) {
508 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
509 // Otherwise, enable the checkbox...
511 $del = Xml
::check( 'showhiderevisions', false,
512 array( 'name' => 'ids['.$rev->getId().']' ) );
514 // User can only view deleted revisions...
515 } else if( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
516 // If revision was hidden from sysops, disable the link
517 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
518 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
519 // Otherwise, show the link...
521 $query = array( 'type' => 'revision',
522 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
523 $del .= $this->getSkin()->revDeleteLink( $query,
524 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
527 if( $del ) $s .= " $del ";
530 $s .= " <span class='history-user'>" .
531 $this->getSkin()->revUserTools( $rev, true ) . "</span>";
533 if( $rev->isMinor() ) {
534 $s .= ' ' . ChangesList
::flag( 'minor' );
537 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
538 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
541 $s .= $this->getSkin()->revComment( $rev, false, true );
543 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
544 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
549 # Rollback and undo links
550 if( !is_null( $next ) && is_object( $next ) ) {
551 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
552 $tools[] = '<span class="mw-rollback-link">'.
553 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
556 if( $this->title
->quickUserCan( 'edit' )
557 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
558 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
560 # Create undo tooltip for the first (=latest) line only
561 $undoTooltip = $latest
562 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
564 $undolink = $this->getSkin()->link(
566 wfMsgHtml( 'editundo' ),
570 'undoafter' => $next->rev_id
,
571 'undo' => $rev->getId()
573 array( 'known', 'noclasses' )
575 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
580 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
584 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
585 $classes = array_merge( $classes, $newClasses );
586 $s .= " $tagSummary";
588 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
592 $attribs['class'] = implode( ' ', $classes );
595 return Xml
::tags( 'li', $attribs, $s ) . "\n";
599 * Create a link to view this revision of the page
601 * @param $rev Revision
604 function revLink( $rev ) {
606 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
607 $date = htmlspecialchars( $date );
608 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
609 $link = $this->getSkin()->link(
613 array( 'oldid' => $rev->getId() ),
614 array( 'known', 'noclasses' )
619 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
620 $link = "<span class=\"history-deleted\">$link</span>";
626 * Create a diff-to-current link for this revision for this page
628 * @param $rev Revision
629 * @param $latest Boolean: this is the latest revision of the page?
632 function curLink( $rev, $latest ) {
633 $cur = $this->historyPage
->message
['cur'];
634 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
637 return $this->getSkin()->link(
642 'diff' => $this->title
->getLatestRevID(),
643 'oldid' => $rev->getId()
645 array( 'known', 'noclasses' )
651 * Create a diff-to-previous link for this revision for this page.
653 * @param $prevRev Revision: the previous revision
654 * @param $next Mixed: the newer revision
655 * @param $counter Integer: what row on the history list this is
658 function lastLink( $prevRev, $next, $counter ) {
659 $last = $this->historyPage
->message
['last'];
660 # $next may either be a Row, null, or "unkown"
661 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
662 if( is_null($next) ) {
663 # Probably no next row
665 } elseif( $next === 'unknown' ) {
666 # Next row probably exists but is unknown, use an oldid=prev link
667 return $this->getSkin()->link(
672 'diff' => $prevRev->getId(),
675 array( 'known', 'noclasses' )
677 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
)
678 ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) )
682 return $this->getSkin()->link(
687 'diff' => $prevRev->getId(),
688 'oldid' => $next->rev_id
690 array( 'known', 'noclasses' )
696 * Create radio buttons for page history
698 * @param $rev Revision object
699 * @param $firstInList Boolean: is this version the first one?
700 * @param $counter Integer: a counter of what row number we're at, counted from the top row = 1.
701 * @return String: HTML output for the radio buttons
703 function diffButtons( $rev, $firstInList, $counter ) {
704 if( $this->getNumRows() > 1 ) {
706 $radio = array( 'type' => 'radio', 'value' => $id );
707 /** @todo: move title texts to javascript */
709 $first = Xml
::element( 'input',
710 array_merge( $radio, array(
711 'style' => 'visibility:hidden',
713 'id' => 'mw-oldid-null' ) )
715 $checkmark = array( 'checked' => 'checked' );
717 # Check visibility of old revisions
718 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
719 $radio['disabled'] = 'disabled';
720 $checkmark = array(); // We will check the next possible one
721 } else if( !$this->oldIdChecked
) {
722 $checkmark = array( 'checked' => 'checked' );
723 $this->oldIdChecked
= $id;
725 $checkmark = array();
727 $first = Xml
::element( 'input',
728 array_merge( $radio, $checkmark, array(
730 'id' => "mw-oldid-$id" ) ) );
731 $checkmark = array();
733 $second = Xml
::element( 'input',
734 array_merge( $radio, $checkmark, array(
736 'id' => "mw-diff-$id" ) ) );
737 return $first . $second;
745 * Backwards-compatibility aliases
747 class PageHistory
extends HistoryPage
{}
748 class PageHistoryPager
extends HistoryPager
{}