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.
63 global $wgOut, $wgRequest, $wgScript;
66 * Allow client caching.
68 if( $wgOut->checkLastModified( $this->article
->getTouched() ) )
69 return; // Client cache fresh and headers sent, nothing more to do.
71 wfProfileIn( __METHOD__
);
74 * Setup page variables.
76 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
77 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
78 $wgOut->setArticleFlag( false );
79 $wgOut->setArticleRelated( true );
80 $wgOut->setRobotPolicy( 'noindex,nofollow' );
81 $wgOut->setSyndicated( true );
82 $wgOut->setFeedAppendQuery( 'action=history' );
83 $wgOut->addScriptFile( 'history.js' );
85 $logPage = SpecialPage
::getTitleFor( 'Log' );
86 $logLink = $this->skin
->link(
88 wfMsgHtml( 'viewpagelogs' ),
90 array( 'page' => $this->title
->getPrefixedText() ),
91 array( 'known', 'noclasses' )
93 $wgOut->setSubtitle( $logLink );
95 $feedType = $wgRequest->getVal( 'feed' );
97 wfProfileOut( __METHOD__
);
98 return $this->feed( $feedType );
102 * Fail if article doesn't exist.
104 if( !$this->title
->exists() ) {
105 $wgOut->addWikiMsg( 'nohistory' );
106 # show deletion/move log if there is an entry
107 LogEventsList
::showLogExtract(
109 array( 'delete', 'move' ),
110 $this->title
->getPrefixedText(),
113 'conds' => array( "log_action != 'revision'" ),
114 'showIfEmpty' => false,
115 'msgKey' => array( 'moveddeleted-notice' )
118 wfProfileOut( __METHOD__
);
123 * Add date selector to quickly get to a certain time
125 $year = $wgRequest->getInt( 'year' );
126 $month = $wgRequest->getInt( 'month' );
127 $tagFilter = $wgRequest->getVal( 'tagfilter' );
128 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
130 * Option to show only revisions that have been (partially) hidden via RevisionDelete
131 * Note that this can run a *long* time if there are many revisions to look at.
132 * We use "isBigDeletion" to determine if the history is too big to go through.
133 * Additionally, only users with 'deleterevision' right can filter for deleted edits.
135 if ( $this->title
->userCan( 'deleterevision' ) && ( !$this->article
->isBigDeletion() ||
$this->title
->userCan( 'bigdelete' ) ) ) {
136 $conds = ( $wgRequest->getBool( 'deleted' ) ) ?
array("rev_deleted != '0'") : array();
137 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ), 'deleted', '', $wgRequest->getBool( 'deleted' ) ) . "\n";
139 else { # Don't filter and don't add the checkbox for filtering
144 $action = htmlspecialchars( $wgScript );
146 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
148 wfMsg( 'history-fieldset-title' ),
150 array( 'id' => 'mw-history-search' )
152 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
153 Xml
::hidden( 'action', 'history' ) . "\n" .
154 Xml
::dateMenu( $year, $month ) . ' ' .
155 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
157 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
161 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
166 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
168 $pager->getNavigationBar() .
170 $pager->getNavigationBar()
173 wfProfileOut( __METHOD__
);
177 * Fetch an array of revisions, specified by a given limit, offset and
178 * direction. This is now only used by the feeds. It was previously
179 * used by the main UI but that's now handled by the pager.
181 * @param $limit Integer: the limit number of revisions to get
182 * @param $offset Integer
183 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
184 * @return ResultWrapper
186 function fetchRevisions( $limit, $offset, $direction ) {
187 $dbr = wfGetDB( DB_SLAVE
);
189 if( $direction == HistoryPage
::DIR_PREV
)
190 list($dirs, $oper) = array("ASC", ">=");
191 else /* $direction == HistoryPage::DIR_NEXT */
192 list($dirs, $oper) = array("DESC", "<=");
195 $offsets = array("rev_timestamp $oper '$offset'");
199 $page_id = $this->title
->getArticleID();
201 return $dbr->select( 'revision',
202 Revision
::selectFields(),
203 array_merge(array("rev_page=$page_id"), $offsets),
205 array( 'ORDER BY' => "rev_timestamp $dirs",
206 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
211 * Output a subscription feed listing recent edits to this page.
213 * @param $type String: feed type
215 function feed( $type ) {
216 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
217 if( !FeedUtils
::checkFeedOutput($type) ) {
221 $feed = new $wgFeedClasses[$type](
222 $this->title
->getPrefixedText() . ' - ' .
223 wfMsgForContent( 'history-feed-title' ),
224 wfMsgForContent( 'history-feed-description' ),
225 $this->title
->getFullUrl( 'action=history' )
228 // Get a limit on number of feed entries. Provide a sane default
229 // of 10 if none is defined (but limit to $wgFeedLimit max)
230 $limit = $wgRequest->getInt( 'limit', 10 );
231 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
234 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
238 foreach( $items as $row ) {
239 $feed->outItem( $this->feedItem( $row ) );
242 $feed->outItem( $this->feedEmpty() );
247 function feedEmpty() {
250 wfMsgForContent( 'nohistory' ),
251 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
252 $this->title
->getFullUrl(),
253 wfTimestamp( TS_MW
),
255 $this->title
->getTalkPage()->getFullUrl()
260 * Generate a FeedItem object from a given revision table row
261 * Borrows Recent Changes' feed generation functions for formatting;
262 * includes a diff to the previous revision (if any).
264 * @param $row Object: database row
267 function feedItem( $row ) {
268 $rev = new Revision( $row );
269 $rev->setTitle( $this->title
);
270 $text = FeedUtils
::formatDiffRow(
272 $this->title
->getPreviousRevisionID( $rev->getId() ),
274 $rev->getTimestamp(),
277 if( $rev->getComment() == '' ) {
279 $title = wfMsgForContent( 'history-feed-item-nocomment',
281 $wgContLang->timeanddate( $rev->getTimestamp() ),
282 $wgContLang->date( $rev->getTimestamp() ),
283 $wgContLang->time( $rev->getTimestamp() )
286 $title = $rev->getUserText() .
287 wfMsgForContent( 'colon-separator' ) .
288 FeedItem
::stripComment( $rev->getComment() );
293 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
294 $rev->getTimestamp(),
296 $this->title
->getTalkPage()->getFullUrl()
304 class HistoryPager
extends ReverseChronologicalPager
{
305 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
306 protected $oldIdChecked;
308 function __construct( $historyPage, $year='', $month='', $tagFilter = '', $conds = array() ) {
309 parent
::__construct();
310 $this->historyPage
= $historyPage;
311 $this->title
= $this->historyPage
->title
;
312 $this->tagFilter
= $tagFilter;
313 $this->getDateCond( $year, $month );
314 $this->conds
= $conds;
317 // For hook compatibility...
318 function getArticle() {
319 return $this->historyPage
->getArticle();
322 function getQueryInfo() {
324 'tables' => array('revision'),
325 'fields' => Revision
::selectFields(),
326 'conds' => array_merge( array('rev_page' => $this->historyPage
->title
->getArticleID() ), $this->conds
),
327 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
328 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
330 ChangeTags
::modifyDisplayQuery(
331 $queryInfo['tables'],
332 $queryInfo['fields'],
334 $queryInfo['join_conds'],
335 $queryInfo['options'],
338 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
342 function getIndexField() {
343 return 'rev_timestamp';
346 function formatRow( $row ) {
347 if( $this->lastRow
) {
348 $latest = ($this->counter
== 1 && $this->mIsFirst
);
349 $firstInList = $this->counter
== 1;
350 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
351 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
355 $this->lastRow
= $row;
360 * Creates begin of history list with a submit button
362 * @return string HTML output
364 function getStartBody() {
365 global $wgScript, $wgUser, $wgOut, $wgContLang;
366 $this->lastRow
= false;
368 $this->oldIdChecked
= 0;
370 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
371 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
372 'id' => 'mw-history-compare' ) ) . "\n";
373 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
374 $s .= Xml
::hidden( 'action', 'historysubmit' ) . "\n";
376 $this->buttons
= '<div>';
377 if( $wgUser->isAllowed('deleterevision') ) {
378 $float = $wgContLang->alignEnd();
379 # Note bug #20966, <button> is non-standard in IE<8
380 $this->buttons
.= Xml
::element( 'button',
383 'name' => 'revisiondelete',
385 'style' => "float: $float;",
386 'class' => 'mw-history-revisiondelete-button',
388 wfMsg( 'showhideselectedversions' )
391 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
393 'class' => 'historysubmit',
394 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
395 'title' => wfMsg( 'tooltip-compareselectedversions' ),
398 $this->buttons
.= '</div>';
399 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
403 function getEndBody() {
404 if( $this->lastRow
) {
405 $latest = $this->counter
== 1 && $this->mIsFirst
;
406 $firstInList = $this->counter
== 1;
407 if( $this->mIsBackwards
) {
408 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
409 if( $this->mOffset
== '' ) {
415 # The next row is the past-the-end row
416 $next = $this->mPastTheEndRow
;
418 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
419 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
424 # Add second buttons only if there is more than one rev
425 if( $this->getNumRows() > 2 ) {
426 $s .= $this->buttons
;
433 * Creates a submit button
435 * @param $message String: text of the submit button, will be escaped
436 * @param $attributes Array: attributes
437 * @return String: HTML output for the submit button
439 function submitButton( $message, $attributes = array() ) {
440 # Disable submit button if history has 1 revision only
441 if( $this->getNumRows() > 1 ) {
442 return Xml
::submitButton( $message , $attributes );
449 * Returns a row from the history printout.
451 * @todo document some more, and maybe clean up the code (some params redundant?)
453 * @param $row Object: the database row corresponding to the previous line.
454 * @param $next Mixed: the database row corresponding to the next line.
455 * @param $counter Integer: apparently a counter of what row number we're at, counted from the top row = 1.
456 * @param $notificationtimestamp
457 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
458 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
459 * @return String: HTML output for the row
461 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
462 $latest = false, $firstInList = false )
464 global $wgUser, $wgLang;
465 $rev = new Revision( $row );
466 $rev->setTitle( $this->title
);
468 $curlink = $this->curLink( $rev, $latest );
469 $lastlink = $this->lastLink( $rev, $next, $counter );
470 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
471 $histLinks = Html
::rawElement(
473 array( 'class' => 'mw-history-histlinks' ),
474 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
476 $s = $histLinks . $diffButtons;
478 $link = $this->revLink( $rev );
482 // User can delete revisions...
483 if( $wgUser->isAllowed( 'deleterevision' ) ) {
484 // If revision was hidden from sysops, disable the checkbox
485 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
486 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
487 // Otherwise, enable the checkbox...
489 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => 'ids['.$rev->getId().']' ) );
491 // User can only view deleted revisions...
492 } else if( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
493 // If revision was hidden from sysops, disable the link
494 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
495 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
496 // Otherwise, show the link...
498 $query = array( 'type' => 'revision',
499 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
500 $del .= $this->getSkin()->revDeleteLink( $query,
501 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
504 if( $del ) $s .= " $del ";
507 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
509 if( $rev->isMinor() ) {
510 $s .= ' ' . ChangesList
::flag( 'minor' );
513 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
514 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
517 $s .= $this->getSkin()->revComment( $rev, false, true );
519 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
520 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
525 # Rollback and undo links
526 if( !is_null( $next ) && is_object( $next ) ) {
527 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
528 $tools[] = '<span class="mw-rollback-link">'.
529 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
532 if( $this->title
->quickUserCan( 'edit' )
533 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
534 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
536 # Create undo tooltip for the first (=latest) line only
537 $undoTooltip = $latest
538 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
540 $undolink = $this->getSkin()->link(
542 wfMsgHtml( 'editundo' ),
546 'undoafter' => $next->rev_id
,
547 'undo' => $rev->getId()
549 array( 'known', 'noclasses' )
551 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
556 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
560 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
561 $classes = array_merge( $classes, $newClasses );
562 $s .= " $tagSummary";
564 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
568 $attribs['class'] = implode( ' ', $classes );
571 return Xml
::tags( 'li', $attribs, $s ) . "\n";
575 * Create a link to view this revision of the page
577 * @param $rev Revision
580 function revLink( $rev ) {
582 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
583 $date = htmlspecialchars( $date );
584 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
585 $link = $this->getSkin()->link(
589 array( 'oldid' => $rev->getId() ),
590 array( 'known', 'noclasses' )
593 $link = "<span class=\"history-deleted\">$date</span>";
599 * Create a diff-to-current link for this revision for this page
601 * @param $rev Revision
602 * @param $latest Boolean: this is the latest revision of the page?
605 function curLink( $rev, $latest ) {
606 $cur = $this->historyPage
->message
['cur'];
607 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
610 return $this->getSkin()->link(
615 'diff' => $this->title
->getLatestRevID(),
616 'oldid' => $rev->getId()
618 array( 'known', 'noclasses' )
624 * Create a diff-to-previous link for this revision for this page.
626 * @param $prevRev Revision: the previous revision
627 * @param $next Mixed: the newer revision
628 * @param $counter Integer: what row on the history list this is
631 function lastLink( $prevRev, $next, $counter ) {
632 $last = $this->historyPage
->message
['last'];
633 # $next may either be a Row, null, or "unkown"
634 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
635 if( is_null($next) ) {
636 # Probably no next row
638 } elseif( $next === 'unknown' ) {
639 # Next row probably exists but is unknown, use an oldid=prev link
640 return $this->getSkin()->link(
645 'diff' => $prevRev->getId(),
648 array( 'known', 'noclasses' )
650 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
653 return $this->getSkin()->link(
658 'diff' => $prevRev->getId(),
659 'oldid' => $next->rev_id
661 array( 'known', 'noclasses' )
667 * Create radio buttons for page history
669 * @param $rev Revision object
670 * @param $firstInList Boolean: is this version the first one?
671 * @param $counter Integer: a counter of what row number we're at, counted from the top row = 1.
672 * @return String: HTML output for the radio buttons
674 function diffButtons( $rev, $firstInList, $counter ) {
675 if( $this->getNumRows() > 1 ) {
677 $radio = array( 'type' => 'radio', 'value' => $id );
678 /** @todo: move title texts to javascript */
680 $first = Xml
::element( 'input',
681 array_merge( $radio, array(
682 'style' => 'visibility:hidden',
684 'id' => 'mw-oldid-null' ) )
686 $checkmark = array( 'checked' => 'checked' );
688 # Check visibility of old revisions
689 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
690 $radio['disabled'] = 'disabled';
691 $checkmark = array(); // We will check the next possible one
692 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
693 $checkmark = array( 'checked' => 'checked' );
694 $this->oldIdChecked
= $id;
696 $checkmark = array();
698 $first = Xml
::element( 'input',
699 array_merge( $radio, $checkmark, array(
701 'id' => "mw-oldid-$id" ) ) );
702 $checkmark = array();
704 $second = Xml
::element( 'input',
705 array_merge( $radio, $checkmark, array(
707 'id' => "mw-diff-$id" ) ) );
708 return $first . $second;
716 * Backwards-compatibility aliases
718 class PageHistory
extends HistoryPage
{}
719 class PageHistoryPager
extends HistoryPager
{}