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
30 function __construct( $article ) {
32 $this->article
= $article;
33 $this->title
= $article->getTitle();
34 $this->skin
= $wgUser->getSkin();
35 $this->preCacheMessages();
38 function getArticle() {
39 return $this->article
;
47 * As we use the same small set of messages in various methods and that
48 * they are called often, we call them once and save them in $this->message
50 function preCacheMessages() {
51 // Precache various messages
52 if( !isset( $this->message
) ) {
53 $msgs = array( 'cur', 'last', 'rev-delundel' );
54 foreach( $msgs as $msg ) {
55 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
61 * Print the history page for an article.
66 global $wgOut, $wgRequest, $wgScript;
69 * Allow client caching.
71 if( $wgOut->checkLastModified( $this->article
->getTouched() ) )
72 return; // Client cache fresh and headers sent, nothing more to do.
74 wfProfileIn( __METHOD__
);
77 * Setup page variables.
79 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
80 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
81 $wgOut->setArticleFlag( false );
82 $wgOut->setArticleRelated( true );
83 $wgOut->setRobotPolicy( 'noindex,nofollow' );
84 $wgOut->setSyndicated( true );
85 $wgOut->setFeedAppendQuery( 'action=history' );
86 $wgOut->addScriptFile( 'history.js' );
88 $logPage = SpecialPage
::getTitleFor( 'Log' );
89 $logLink = $this->skin
->link(
91 wfMsgHtml( 'viewpagelogs' ),
93 array( 'page' => $this->title
->getPrefixedText() ),
94 array( 'known', 'noclasses' )
96 $wgOut->setSubtitle( $logLink );
98 $feedType = $wgRequest->getVal( 'feed' );
100 wfProfileOut( __METHOD__
);
101 return $this->feed( $feedType );
105 * Fail if article doesn't exist.
107 if( !$this->title
->exists() ) {
108 $wgOut->addWikiMsg( 'nohistory' );
109 # show deletion/move log if there is an entry
110 LogEventsList
::showLogExtract(
112 array( 'delete', 'move' ),
113 $this->title
->getPrefixedText(),
116 'conds' => array( "log_action != 'revision'" ),
117 'showIfEmpty' => false,
118 'msgKey' => array( 'moveddeleted-notice' )
121 wfProfileOut( __METHOD__
);
126 * Add date selector to quickly get to a certain time
128 $year = $wgRequest->getInt( 'year' );
129 $month = $wgRequest->getInt( 'month' );
130 $tagFilter = $wgRequest->getVal( 'tagfilter' );
131 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
133 $action = htmlspecialchars( $wgScript );
135 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
137 wfMsg( 'history-fieldset-title' ),
139 array( 'id' => 'mw-history-search' )
141 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
142 Xml
::hidden( 'action', 'history' ) . "\n" .
143 xml
::dateMenu( $year, $month ) . ' ' .
144 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
145 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
149 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
154 $pager = new HistoryPager( $this, $year, $month, $tagFilter );
156 $pager->getNavigationBar() .
158 $pager->getNavigationBar()
161 wfProfileOut( __METHOD__
);
165 * Fetch an array of revisions, specified by a given limit, offset and
166 * direction. This is now only used by the feeds. It was previously
167 * used by the main UI but that's now handled by the pager.
169 function fetchRevisions($limit, $offset, $direction) {
170 $dbr = wfGetDB( DB_SLAVE
);
172 if( $direction == HistoryPage
::DIR_PREV
)
173 list($dirs, $oper) = array("ASC", ">=");
174 else /* $direction == HistoryPage::DIR_NEXT */
175 list($dirs, $oper) = array("DESC", "<=");
178 $offsets = array("rev_timestamp $oper '$offset'");
182 $page_id = $this->title
->getArticleID();
184 return $dbr->select( 'revision',
185 Revision
::selectFields(),
186 array_merge(array("rev_page=$page_id"), $offsets),
188 array( 'ORDER BY' => "rev_timestamp $dirs",
189 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
194 * Output a subscription feed listing recent edits to this page.
195 * @param string $type
197 function feed( $type ) {
198 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
199 if( !FeedUtils
::checkFeedOutput($type) ) {
203 $feed = new $wgFeedClasses[$type](
204 $this->title
->getPrefixedText() . ' - ' .
205 wfMsgForContent( 'history-feed-title' ),
206 wfMsgForContent( 'history-feed-description' ),
207 $this->title
->getFullUrl( 'action=history' )
210 // Get a limit on number of feed entries. Provide a sane default
211 // of 10 if none is defined (but limit to $wgFeedLimit max)
212 $limit = $wgRequest->getInt( 'limit', 10 );
213 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
216 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
220 foreach( $items as $row ) {
221 $feed->outItem( $this->feedItem( $row ) );
224 $feed->outItem( $this->feedEmpty() );
229 function feedEmpty() {
232 wfMsgForContent( 'nohistory' ),
233 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
234 $this->title
->getFullUrl(),
235 wfTimestamp( TS_MW
),
237 $this->title
->getTalkPage()->getFullUrl()
242 * Generate a FeedItem object from a given revision table row
243 * Borrows Recent Changes' feed generation functions for formatting;
244 * includes a diff to the previous revision (if any).
249 function feedItem( $row ) {
250 $rev = new Revision( $row );
251 $rev->setTitle( $this->title
);
252 $text = FeedUtils
::formatDiffRow(
254 $this->title
->getPreviousRevisionID( $rev->getId() ),
256 $rev->getTimestamp(),
259 if( $rev->getComment() == '' ) {
261 $title = wfMsgForContent( 'history-feed-item-nocomment',
263 $wgContLang->timeanddate( $rev->getTimestamp() ) );
265 $title = $rev->getUserText() .
266 wfMsgForContent( 'colon-separator' ) .
267 FeedItem
::stripComment( $rev->getComment() );
272 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
273 $rev->getTimestamp(),
275 $this->title
->getTalkPage()->getFullUrl()
283 class HistoryPager
extends ReverseChronologicalPager
{
284 public $lastRow = false, $counter, $historyPage, $title, $buttons;
285 protected $oldIdChecked;
287 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
288 parent
::__construct();
289 $this->historyPage
= $historyPage;
290 $this->title
= $this->historyPage
->title
;
291 $this->tagFilter
= $tagFilter;
292 $this->getDateCond( $year, $month );
295 // For hook compatibility...
296 function getArticle() {
297 return $this->historyPage
->getArticle();
300 function getQueryInfo() {
302 'tables' => array('revision'),
303 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
304 'conds' => array('rev_page' => $this->historyPage
->title
->getArticleID() ),
305 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
306 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
308 ChangeTags
::modifyDisplayQuery(
309 $queryInfo['tables'],
310 $queryInfo['fields'],
312 $queryInfo['join_conds'],
313 $queryInfo['options'],
316 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
320 function getIndexField() {
321 return 'rev_timestamp';
324 function formatRow( $row ) {
325 if( $this->lastRow
) {
326 $latest = ($this->counter
== 1 && $this->mIsFirst
);
327 $firstInList = $this->counter
== 1;
328 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
329 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
333 $this->lastRow
= $row;
338 * Creates begin of history list with a submit button
340 * @return string HTML output
342 function getStartBody() {
343 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut, $wgContLang;
344 $this->lastRow
= false;
346 $this->oldIdChecked
= 0;
348 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
349 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
350 'id' => 'mw-history-compare' ) ) . "\n";
351 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
353 $this->buttons
= '<div>';
354 if( $wgUser->isAllowed('deletedhistory') ) {
355 $float = $wgContLang->isRTL() ?
'left' : 'right';
356 $this->buttons
.= Xml
::element( 'button',
360 'value' => 'revisiondelete',
361 'style' => "float: $float;",
362 'class' => 'mw-history-revisiondelete-button',
364 wfMsg( 'showhideselectedversions' )
367 if( $wgEnableHtmlDiff ) {
368 $this->buttons
.= Xml
::element( 'button',
371 'name' => 'htmldiff',
373 'class' => 'historysubmit',
374 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
375 'title' => wfMsg( 'tooltip-compareselectedversions' ),
377 wfMsg( 'visualcomparison')
379 $this->buttons
.= $this->submitButton( wfMsg( 'wikicodecomparison'),
381 'class' => 'historysubmit',
382 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
383 'title' => wfMsg( 'tooltip-compareselectedversions' ),
387 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
389 'class' => 'historysubmit',
390 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
391 'title' => wfMsg( 'tooltip-compareselectedversions' ),
395 $this->buttons
.= '</div>';
396 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
400 function getEndBody() {
401 if( $this->lastRow
) {
402 $latest = $this->counter
== 1 && $this->mIsFirst
;
403 $firstInList = $this->counter
== 1;
404 if( $this->mIsBackwards
) {
405 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
406 if( $this->mOffset
== '' ) {
412 # The next row is the past-the-end row
413 $next = $this->mPastTheEndRow
;
415 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
416 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
421 $s .= $this->buttons
;
427 * Creates a submit button
429 * @param array $attributes attributes
430 * @return string HTML output for the submit button
432 function submitButton($message, $attributes = array() ) {
433 # Disable submit button if history has 1 revision only
434 if( $this->getNumRows() > 1 ) {
435 return Xml
::submitButton( $message , $attributes );
442 * Returns a row from the history printout.
444 * @todo document some more, and maybe clean up the code (some params redundant?)
446 * @param Row $row The database row corresponding to the previous line.
447 * @param mixed $next The database row corresponding to the next line.
448 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
449 * @param $notificationtimestamp
450 * @param bool $latest Whether this row corresponds to the page's latest revision.
451 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
452 * @return string HTML output for the row
454 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
455 $latest = false, $firstInList = false )
457 global $wgUser, $wgLang;
458 $rev = new Revision( $row );
459 $rev->setTitle( $this->title
);
461 $curlink = $this->curLink( $rev, $latest );
462 $lastlink = $this->lastLink( $rev, $next, $counter );
463 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
464 $link = $this->revLink( $rev );
467 $s = "($curlink) ($lastlink) $diffButtons";
469 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
470 // Don't show useless link to people who cannot hide revisions
471 if( !$rev->getVisibility() && !$wgUser->isAllowed( 'deleterevision' ) ) {
472 $del = Xml
::check( 'deleterevisions', false, array('class' => 'mw-revdelundel-hidden') );
473 // If revision was hidden from sysops
474 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
475 $del = Xml
::check( 'deleterevisions', false, array('disabled' => 'disabled') );
476 // Otherwise, show the link...
479 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
485 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
487 if( $rev->isMinor() ) {
488 $s .= ' ' . ChangesList
::flag( 'minor' );
491 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
492 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
495 $s .= $this->getSkin()->revComment( $rev, false, true );
497 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
498 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
503 # Rollback and undo links
504 if( !is_null( $next ) && is_object( $next ) ) {
505 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
506 $tools[] = '<span class="mw-rollback-link">'.
507 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
510 if( $this->title
->quickUserCan( 'edit' )
511 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
512 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
514 # Create undo tooltip for the first (=latest) line only
515 $undoTooltip = $latest
516 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
518 $undolink = $this->getSkin()->link(
520 wfMsgHtml( 'editundo' ),
524 'undoafter' => $next->rev_id
,
525 'undo' => $rev->getId()
527 array( 'known', 'noclasses' )
529 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
534 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
538 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
539 $classes = array_merge( $classes, $newClasses );
540 $s .= " $tagSummary";
542 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
546 $attribs['class'] = implode( ' ', $classes );
549 return Xml
::tags( 'li', $attribs, $s ) . "\n";
553 * Create a link to view this revision of the page
554 * @param Revision $rev
557 function revLink( $rev ) {
559 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
560 $date = htmlspecialchars( $date );
561 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
562 $link = $this->getSkin()->link(
566 array( 'oldid' => $rev->getId() ),
567 array( 'known', 'noclasses' )
570 $link = "<span class=\"history-deleted\">$date</span>";
576 * Create a diff-to-current link for this revision for this page
577 * @param Revision $rev
578 * @param Bool $latest, this is the latest revision of the page?
581 function curLink( $rev, $latest ) {
582 $cur = $this->historyPage
->message
['cur'];
583 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
586 return $this->getSkin()->link(
591 'diff' => $this->title
->getLatestRevID(),
592 'oldid' => $rev->getId()
594 array( 'known', 'noclasses' )
600 * Create a diff-to-previous link for this revision for this page.
601 * @param Revision $prevRev, the previous revision
602 * @param mixed $next, the newer revision
603 * @param int $counter, what row on the history list this is
606 function lastLink( $prevRev, $next, $counter ) {
607 $last = $this->historyPage
->message
['last'];
608 # $next may either be a Row, null, or "unkown"
609 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
610 if( is_null($next) ) {
611 # Probably no next row
613 } elseif( $next === 'unknown' ) {
614 # Next row probably exists but is unknown, use an oldid=prev link
615 return $this->getSkin()->link(
620 'diff' => $prevRev->getId(),
623 array( 'known', 'noclasses' )
625 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
628 return $this->getSkin()->link(
633 'diff' => $prevRev->getId(),
634 'oldid' => $next->rev_id
636 array( 'known', 'noclasses' )
642 * Create radio buttons for page history
644 * @param object $rev Revision
645 * @param bool $firstInList Is this version the first one?
646 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
647 * @return string HTML output for the radio buttons
649 function diffButtons( $rev, $firstInList, $counter ) {
650 if( $this->getNumRows() > 1 ) {
652 $radio = array( 'type' => 'radio', 'value' => $id );
653 /** @todo: move title texts to javascript */
655 $first = Xml
::element( 'input',
656 array_merge( $radio, array(
657 'style' => 'visibility:hidden',
659 'id' => 'mw-oldid-null' ) )
661 $checkmark = array( 'checked' => 'checked' );
663 # Check visibility of old revisions
664 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
665 $radio['disabled'] = 'disabled';
666 $checkmark = array(); // We will check the next possible one
667 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
668 $checkmark = array( 'checked' => 'checked' );
669 $this->oldIdChecked
= $id;
671 $checkmark = array();
673 $first = Xml
::element( 'input',
674 array_merge( $radio, $checkmark, array(
676 'id' => "mw-oldid-$id" ) ) );
677 $checkmark = array();
679 $second = Xml
::element( 'input',
680 array_merge( $radio, $checkmark, array(
682 'id' => "mw-diff-$id" ) ) );
683 return $first . $second;
691 * Backwards-compatibility aliases
693 class PageHistory
extends HistoryPage
{}
694 class PageHistoryPager
extends HistoryPager
{}