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 $mArticle, $mTitle, $mSkin;
25 var $mLatestId = null;
27 private $mOldIdChecked = 0;
30 * Construct a new PageHistory.
32 * @param Article $article
35 function __construct( $article ) {
37 $this->mArticle
=& $article;
38 $this->mTitle
=& $article->mTitle
;
39 $this->mSkin
= $wgUser->getSkin();
40 $this->preCacheMessages();
43 function getArticle() {
44 return $this->mArticle
;
52 * As we use the same small set of messages in various methods and that
53 * they are called often, we call them once and save them in $this->message
55 function preCacheMessages() {
56 // Precache various messages
57 if( !isset( $this->message
) ) {
58 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
59 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
65 * Print the history page for an article.
70 global $wgOut, $wgRequest, $wgTitle, $wgScript;
73 * Allow client caching.
75 if( $wgOut->checkLastModified( $this->mArticle
->getTouched() ) )
76 return; // Client cache fresh and headers sent, nothing more to do.
78 wfProfileIn( __METHOD__
);
81 * Setup page variables.
83 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle
->getPrefixedText() ) );
84 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
85 $wgOut->setArticleFlag( false );
86 $wgOut->setArticleRelated( true );
87 $wgOut->setRobotPolicy( 'noindex,nofollow' );
88 $wgOut->setSyndicated( true );
89 $wgOut->setFeedAppendQuery( 'action=history' );
90 $wgOut->addScriptFile( 'history.js' );
92 $logPage = SpecialPage
::getTitleFor( 'Log' );
93 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ),
94 'page=' . $this->mTitle
->getPrefixedUrl() );
95 $wgOut->setSubtitle( $logLink );
97 $feedType = $wgRequest->getVal( 'feed' );
99 wfProfileOut( __METHOD__
);
100 return $this->feed( $feedType );
104 * Fail if article doesn't exist.
106 if( !$this->mTitle
->exists() ) {
107 $wgOut->addWikiMsg( 'nohistory' );
108 wfProfileOut( __METHOD__
);
113 * Add date selector to quickly get to a certain time
115 $year = $wgRequest->getInt( 'year' );
116 $month = $wgRequest->getInt( 'month' );
117 $tagFilter = $wgRequest->getVal( 'tagfilter' );
118 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
120 $action = htmlspecialchars( $wgScript );
122 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
123 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
124 Xml
::hidden( 'title', $this->mTitle
->getPrefixedDBKey() ) . "\n" .
125 Xml
::hidden( 'action', 'history' ) . "\n" .
126 $this->getDateMenu( $year, $month ) . ' ' .
127 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
128 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
132 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
137 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
138 $this->linesonpage
= $pager->getNumRows();
140 $pager->getNavigationBar() .
141 $this->beginHistoryList() .
143 $this->endHistoryList() .
144 $pager->getNavigationBar()
147 wfProfileOut( __METHOD__
);
151 * @return string Formatted HTML
155 private function getDateMenu( $year, $month ) {
156 # Offset overrides year/month selection
157 if( $month && $month !== -1 ) {
158 $encMonth = intval( $month );
163 $encYear = intval( $year );
164 } else if( $encMonth ) {
165 $thisMonth = intval( gmdate( 'n' ) );
166 $thisYear = intval( gmdate( 'Y' ) );
167 if( intval($encMonth) > $thisMonth ) {
170 $encYear = $thisYear;
174 return Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
175 Xml
::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) .
177 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
178 Xml
::monthSelector( $encMonth, -1 );
182 * Creates begin of history list with a submit button
184 * @return string HTML output
186 function beginHistoryList() {
187 global $wgTitle, $wgScript, $wgEnableHtmlDiff;
188 $this->lastdate
= '';
189 $s = wfMsgExt( 'histlegend', array( 'parse') );
190 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript, 'id' => 'mw-history-compare' ) );
191 $s .= Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() );
192 if( $wgEnableHtmlDiff ) {
193 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
195 'name' => 'htmldiff',
196 'class' => 'historysubmit',
197 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
198 'title' => wfMsg( 'tooltip-compareselectedversions' ),
201 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
203 'class' => 'historysubmit',
204 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
205 'title' => wfMsg( 'tooltip-compareselectedversions' ),
209 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
211 'class' => 'historysubmit',
212 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
213 'title' => wfMsg( 'tooltip-compareselectedversions' ),
217 $s .= '<ul id="pagehistory">' . "\n";
222 * Creates end of history list with a submit button
224 * @return string HTML output
226 function endHistoryList() {
227 global $wgEnableHtmlDiff;
229 if( $wgEnableHtmlDiff ) {
230 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
232 'name' => 'htmldiff',
233 'class' => 'historysubmit',
234 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
235 'title' => wfMsg( 'tooltip-compareselectedversions' ),
238 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
240 'class' => 'historysubmit',
241 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
242 'title' => wfMsg( 'tooltip-compareselectedversions' ),
246 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
248 'class' => 'historysubmit',
249 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
250 'title' => wfMsg( 'tooltip-compareselectedversions' ),
259 * Creates a submit button
261 * @param array $attributes attributes
262 * @return string HTML output for the submit button
264 function submitButton($message, $attributes = array() ) {
265 # Disable submit button if history has 1 revision only
266 if( $this->linesonpage
> 1 ) {
267 return Xml
::submitButton( $message , $attributes );
274 * Returns a row from the history printout.
276 * @todo document some more, and maybe clean up the code (some params redundant?)
278 * @param Row $row The database row corresponding to the previous line.
279 * @param mixed $next The database row corresponding to the next line.
280 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
281 * @param $notificationtimestamp
282 * @param bool $latest Whether this row corresponds to the page's latest revision.
283 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
284 * @return string HTML output for the row
286 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
287 global $wgUser, $wgLang;
288 $rev = new Revision( $row );
289 $rev->setTitle( $this->mTitle
);
291 $curlink = $this->curLink( $rev, $latest );
292 $lastlink = $this->lastLink( $rev, $next, $counter );
293 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
294 $link = $this->revLink( $rev );
297 $s = "($curlink) ($lastlink) $arbitrary";
299 if( $wgUser->isAllowed( 'deleterevision' ) ) {
301 // We don't currently handle well changing the top revision's settings
302 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message
['rev-delundel'].')' );
303 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
304 // If revision was hidden from sysops
305 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message
['rev-delundel'].')' );
307 $query = array( 'target' => $this->mTitle
->getPrefixedDbkey(),
308 'oldid' => $rev->getId()
310 $del = $this->mSkin
->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
316 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
318 if( $rev->isMinor() ) {
319 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
322 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
323 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
326 $s .= $this->mSkin
->revComment( $rev, false, true );
328 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
329 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
331 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
332 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
337 if( !is_null( $next ) && is_object( $next ) ) {
338 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
339 $tools[] = '<span class="mw-rollback-link">'.$this->mSkin
->buildRollbackLink( $rev ).'</span>';
342 if( $this->mTitle
->quickUserCan( 'edit' ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
343 !$next->rev_deleted
& Revision
::DELETED_TEXT
)
345 # Create undo tooltip for the first (=latest) line only
346 $undoTooltip = $latest
347 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
349 $undolink = $this->mSkin
->link(
351 wfMsgHtml( 'editundo' ),
353 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
354 array( 'known', 'noclasses' )
356 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
361 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
365 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
366 $classes = array_merge( $classes, $newClasses );
367 $s .= " $tagSummary";
369 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
371 $classes = implode( ' ', $classes );
373 return "<li class=\"$classes\">$s</li>\n";
377 * Create a link to view this revision of the page
378 * @param Revision $rev
381 function revLink( $rev ) {
383 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
384 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
385 $link = $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $date, "oldid=" . $rev->getId() );
387 $link = '<span class="history-deleted">' . $date . '</span>';
393 * Create a diff-to-current link for this revision for this page
394 * @param Revision $rev
395 * @param Bool $latest, this is the latest revision of the page?
398 function curLink( $rev, $latest ) {
399 $cur = $this->message
['cur'];
400 if( $latest ||
$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
403 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
404 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
409 * Create a diff-to-previous link for this revision for this page.
410 * @param Revision $prevRev, the previous revision
411 * @param mixed $next, the newer revision
412 * @param int $counter, what row on the history list this is
415 function lastLink( $prevRev, $next, $counter ) {
416 $last = $this->message
['last'];
417 # $next may either be a Row, null, or "unkown"
418 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
419 if( is_null($next) ) {
420 # Probably no next row
422 } elseif( $next === 'unknown' ) {
423 # Next row probably exists but is unknown, use an oldid=prev link
424 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
425 "diff=" . $prevRev->getId() . "&oldid=prev" );
426 } elseif( $prevRev->isDeleted(Revision
::DELETED_TEXT
) ||
$nextRev->isDeleted(Revision
::DELETED_TEXT
) ) {
429 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
430 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
435 * Create radio buttons for page history
437 * @param object $rev Revision
438 * @param bool $firstInList Is this version the first one?
439 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
440 * @return string HTML output for the radio buttons
442 function diffButtons( $rev, $firstInList, $counter ) {
443 if( $this->linesonpage
> 1 ) {
444 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
445 /** @todo: move title texts to javascript */
447 $first = Xml
::element( 'input',
448 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
450 $checkmark = array( 'checked' => 'checked' );
452 # Check visibility of old revisions
453 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
454 $radio['disabled'] = 'disabled';
455 $checkmark = array(); // We will check the next possible one
456 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
457 $checkmark = array( 'checked' => 'checked' );
458 $this->mOldIdChecked
= $rev->getId();
460 $checkmark = array();
462 $first = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
463 $checkmark = array();
465 $second = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
466 return $first . $second;
473 * Fetch an array of revisions, specified by a given limit, offset and
474 * direction. This is now only used by the feeds. It was previously
475 * used by the main UI but that's now handled by the pager.
477 function fetchRevisions($limit, $offset, $direction) {
478 $dbr = wfGetDB( DB_SLAVE
);
480 if( $direction == PageHistory
::DIR_PREV
)
481 list($dirs, $oper) = array("ASC", ">=");
482 else /* $direction == PageHistory::DIR_NEXT */
483 list($dirs, $oper) = array("DESC", "<=");
486 $offsets = array("rev_timestamp $oper '$offset'");
490 $page_id = $this->mTitle
->getArticleID();
492 return $dbr->select( 'revision',
493 Revision
::selectFields(),
494 array_merge(array("rev_page=$page_id"), $offsets),
496 array( 'ORDER BY' => "rev_timestamp $dirs",
497 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
502 * Output a subscription feed listing recent edits to this page.
503 * @param string $type
505 function feed( $type ) {
506 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
507 if( !FeedUtils
::checkFeedOutput($type) ) {
511 $feed = new $wgFeedClasses[$type](
512 $this->mTitle
->getPrefixedText() . ' - ' .
513 wfMsgForContent( 'history-feed-title' ),
514 wfMsgForContent( 'history-feed-description' ),
515 $this->mTitle
->getFullUrl( 'action=history' ) );
517 // Get a limit on number of feed entries. Provide a sane default
518 // of 10 if none is defined (but limit to $wgFeedLimit max)
519 $limit = $wgRequest->getInt( 'limit', 10 );
520 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
523 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
527 foreach( $items as $row ) {
528 $feed->outItem( $this->feedItem( $row ) );
531 $feed->outItem( $this->feedEmpty() );
536 function feedEmpty() {
539 wfMsgForContent( 'nohistory' ),
540 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
541 $this->mTitle
->getFullUrl(),
542 wfTimestamp( TS_MW
),
544 $this->mTitle
->getTalkPage()->getFullUrl() );
548 * Generate a FeedItem object from a given revision table row
549 * Borrows Recent Changes' feed generation functions for formatting;
550 * includes a diff to the previous revision (if any).
555 function feedItem( $row ) {
556 $rev = new Revision( $row );
557 $rev->setTitle( $this->mTitle
);
558 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
559 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
561 $rev->getTimestamp(),
562 $rev->getComment() );
564 if( $rev->getComment() == '' ) {
566 $title = wfMsgForContent( 'history-feed-item-nocomment',
568 $wgContLang->timeanddate( $rev->getTimestamp() ) );
570 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
576 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
577 $rev->getTimestamp(),
579 $this->mTitle
->getTalkPage()->getFullUrl() );
587 class PageHistoryPager
extends ReverseChronologicalPager
{
588 public $mLastRow = false, $mPageHistory, $mTitle;
590 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
591 parent
::__construct();
592 $this->mPageHistory
= $pageHistory;
593 $this->mTitle
=& $this->mPageHistory
->mTitle
;
594 $this->tagFilter
= $tagFilter;
595 $this->getDateCond( $year, $month );
598 function getQueryInfo() {
600 'tables' => array('revision'),
601 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
602 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
603 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
604 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
606 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
607 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
611 function getIndexField() {
612 return 'rev_timestamp';
615 function formatRow( $row ) {
616 if( $this->mLastRow
) {
617 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
618 $firstInList = $this->mCounter
== 1;
619 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
620 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
624 $this->mLastRow
= $row;
628 function getStartBody() {
629 $this->mLastRow
= false;
634 function getEndBody() {
635 if( $this->mLastRow
) {
636 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
637 $firstInList = $this->mCounter
== 1;
638 if( $this->mIsBackwards
) {
639 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
640 if( $this->mOffset
== '' ) {
646 # The next row is the past-the-end row
647 $next = $this->mPastTheEndRow
;
649 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
650 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );