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;
28 * Construct a new PageHistory.
30 * @param Article $article
33 function __construct( $article ) {
35 $this->mArticle
=& $article;
36 $this->mTitle
=& $article->mTitle
;
37 $this->mSkin
= $wgUser->getSkin();
38 $this->preCacheMessages();
41 function getArticle() {
42 return $this->mArticle
;
50 * As we use the same small set of messages in various methods and that
51 * they are called often, we call them once and save them in $this->message
53 function preCacheMessages() {
54 // Precache various messages
55 if( !isset( $this->message
) ) {
56 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
57 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
63 * Print the history page for an article.
68 global $wgOut, $wgRequest, $wgTitle, $wgScript;
71 * Allow client caching.
73 if( $wgOut->checkLastModified( $this->mArticle
->getTouched() ) )
74 return; // Client cache fresh and headers sent, nothing more to do.
76 wfProfileIn( __METHOD__
);
79 * Setup page variables.
81 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle
->getPrefixedText() ) );
82 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
83 $wgOut->setArticleFlag( false );
84 $wgOut->setArticleRelated( true );
85 $wgOut->setRobotPolicy( 'noindex,nofollow' );
86 $wgOut->setSyndicated( true );
87 $wgOut->setFeedAppendQuery( 'action=history' );
88 $wgOut->addScriptFile( 'history.js' );
90 $logPage = SpecialPage
::getTitleFor( 'Log' );
91 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ),
92 'page=' . $this->mTitle
->getPrefixedUrl() );
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->mTitle
->exists() ) {
105 $wgOut->addWikiMsg( 'nohistory' );
106 wfProfileOut( __METHOD__
);
111 * Add date selector to quickly get to a certain time
113 $year = $wgRequest->getInt( 'year' );
114 $month = $wgRequest->getInt( 'month' );
115 $tagFilter = $wgRequest->getVal( 'tagfilter' );
116 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
118 $action = htmlspecialchars( $wgScript );
120 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
121 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
122 Xml
::hidden( 'title', $this->mTitle
->getPrefixedDBKey() ) . "\n" .
123 Xml
::hidden( 'action', 'history' ) . "\n" .
124 $this->getDateMenu( $year, $month ) . ' ' .
125 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
126 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
130 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
135 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
136 $this->linesonpage
= $pager->getNumRows();
138 $pager->getNavigationBar() .
139 $this->beginHistoryList() .
141 $this->endHistoryList() .
142 $pager->getNavigationBar()
145 wfProfileOut( __METHOD__
);
149 * @return string Formatted HTML
153 private function getDateMenu( $year, $month ) {
154 # Offset overrides year/month selection
155 if( $month && $month !== -1 ) {
156 $encMonth = intval( $month );
161 $encYear = intval( $year );
162 } else if( $encMonth ) {
163 $thisMonth = intval( gmdate( 'n' ) );
164 $thisYear = intval( gmdate( 'Y' ) );
165 if( intval($encMonth) > $thisMonth ) {
168 $encYear = $thisYear;
172 return Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
173 Xml
::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) .
175 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
176 Xml
::monthSelector( $encMonth, -1 );
180 * Creates begin of history list with a submit button
182 * @return string HTML output
184 function beginHistoryList() {
185 global $wgTitle, $wgScript, $wgEnableHtmlDiff;
186 $this->lastdate
= '';
187 $s = wfMsgExt( 'histlegend', array( 'parse') );
188 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript, 'id' => 'mw-history-compare' ) );
189 $s .= Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() );
190 if( $wgEnableHtmlDiff ) {
191 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
193 'name' => 'htmldiff',
194 'class' => 'historysubmit',
195 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
196 'title' => wfMsg( 'tooltip-compareselectedversions' ),
199 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
201 'class' => 'historysubmit',
202 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
203 'title' => wfMsg( 'tooltip-compareselectedversions' ),
207 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
209 'class' => 'historysubmit',
210 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
211 'title' => wfMsg( 'tooltip-compareselectedversions' ),
215 $s .= '<ul id="pagehistory">' . "\n";
220 * Creates end of history list with a submit button
222 * @return string HTML output
224 function endHistoryList() {
225 global $wgEnableHtmlDiff;
227 if( $wgEnableHtmlDiff ) {
228 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
230 'name' => 'htmldiff',
231 'class' => 'historysubmit',
232 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
233 'title' => wfMsg( 'tooltip-compareselectedversions' ),
236 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
238 'class' => 'historysubmit',
239 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
240 'title' => wfMsg( 'tooltip-compareselectedversions' ),
244 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
246 'class' => 'historysubmit',
247 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
248 'title' => wfMsg( 'tooltip-compareselectedversions' ),
257 * Creates a submit button
259 * @param array $attributes attributes
260 * @return string HTML output for the submit button
262 function submitButton($message, $attributes = array() ) {
263 # Disable submit button if history has 1 revision only
264 if( $this->linesonpage
> 1 ) {
265 return Xml
::submitButton( $message , $attributes );
272 * Returns a row from the history printout.
274 * @todo document some more, and maybe clean up the code (some params redundant?)
276 * @param Row $row The database row corresponding to the previous line.
277 * @param mixed $next The database row corresponding to the next line.
278 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
279 * @param $notificationtimestamp
280 * @param bool $latest Whether this row corresponds to the page's latest revision.
281 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
282 * @return string HTML output for the row
284 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
285 global $wgUser, $wgLang;
286 $rev = new Revision( $row );
287 $rev->setTitle( $this->mTitle
);
289 $curlink = $this->curLink( $rev, $latest );
290 $lastlink = $this->lastLink( $rev, $next, $counter );
291 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
292 $link = $this->revLink( $rev );
295 $s = "($curlink) ($lastlink) $arbitrary";
297 if( $wgUser->isAllowed( 'deleterevision' ) ) {
299 // We don't currently handle well changing the top revision's settings
300 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message
['rev-delundel'].')' );
301 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
302 // If revision was hidden from sysops
303 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message
['rev-delundel'].')' );
305 $query = array( 'target' => $this->mTitle
->getPrefixedDbkey(),
306 'oldid' => $rev->getId()
308 $del = $this->mSkin
->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
314 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
316 if( $row->rev_minor_edit
) {
317 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
320 if( !is_null( $size = $rev->getSize() ) && $rev->userCan( Revision
::DELETED_TEXT
) ) {
321 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
324 $s .= $this->mSkin
->revComment( $rev, false, true );
326 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
327 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
329 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
330 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
335 if( !is_null( $next ) && is_object( $next ) ) {
336 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
337 $tools[] = '<span class="mw-rollback-link">'.$this->mSkin
->buildRollbackLink( $rev ).'</span>';
340 if( $this->mTitle
->quickUserCan( 'edit' ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
341 !$next->rev_deleted
& Revision
::DELETED_TEXT
)
343 # Create undo tooltip for the first (=latest) line only
344 $undoTooltip = $latest
345 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
347 $undolink = $this->mSkin
->link(
349 wfMsgHtml( 'editundo' ),
351 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
352 array( 'known', 'noclasses' )
354 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
359 $s .= ' (' . implode( ' | ', $tools ) . ')';
363 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
364 $classes = array_merge( $classes, $newClasses );
365 $s .= " $tagSummary";
367 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
369 $classes = implode( ' ', $classes );
371 return "<li class=\"$classes\">$s</li>\n";
375 * Create a link to view this revision of the page
376 * @param Revision $rev
379 function revLink( $rev ) {
381 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
382 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
383 $link = $this->mSkin
->makeKnownLinkObj(
384 $this->mTitle
, $date, "oldid=" . $rev->getId() );
388 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
389 return '<span class="history-deleted">' . $link . '</span>';
395 * Create a diff-to-current link for this revision for this page
396 * @param Revision $rev
397 * @param Bool $latest, this is the latest revision of the page?
400 function curLink( $rev, $latest ) {
401 $cur = $this->message
['cur'];
402 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
405 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
406 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
411 * Create a diff-to-previous link for this revision for this page.
412 * @param Revision $prevRev, the previous revision
413 * @param mixed $next, the newer revision
414 * @param int $counter, what row on the history list this is
417 function lastLink( $prevRev, $next, $counter ) {
418 $last = $this->message
['last'];
419 # $next may either be a Row, null, or "unkown"
420 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
421 if( is_null($next) ) {
422 # Probably no next row
424 } elseif( $next === 'unknown' ) {
425 # Next row probably exists but is unknown, use an oldid=prev link
426 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
427 "diff=" . $prevRev->getId() . "&oldid=prev" );
428 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
431 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
432 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
437 * Create radio buttons for page history
439 * @param object $rev Revision
440 * @param bool $firstInList Is this version the first one?
441 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
442 * @return string HTML output for the radio buttons
444 function diffButtons( $rev, $firstInList, $counter ) {
445 if( $this->linesonpage
> 1) {
448 'value' => $rev->getId(),
451 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
452 $radio['disabled'] = 'disabled';
455 /** @todo: move title texts to javascript */
457 $first = Xml
::element( 'input', array_merge(
460 'style' => 'visibility:hidden',
461 'name' => 'oldid' ) ) );
462 $checkmark = array( 'checked' => 'checked' );
464 if( $counter == 2 ) {
465 $checkmark = array( 'checked' => 'checked' );
467 $checkmark = array();
469 $first = Xml
::element( 'input', array_merge(
472 array( 'name' => 'oldid' ) ) );
473 $checkmark = array();
475 $second = Xml
::element( 'input', array_merge(
478 array( 'name' => 'diff' ) ) );
479 return $first . $second;
486 * Fetch an array of revisions, specified by a given limit, offset and
487 * direction. This is now only used by the feeds. It was previously
488 * used by the main UI but that's now handled by the pager.
490 function fetchRevisions($limit, $offset, $direction) {
491 $dbr = wfGetDB( DB_SLAVE
);
493 if( $direction == PageHistory
::DIR_PREV
)
494 list($dirs, $oper) = array("ASC", ">=");
495 else /* $direction == PageHistory::DIR_NEXT */
496 list($dirs, $oper) = array("DESC", "<=");
499 $offsets = array("rev_timestamp $oper '$offset'");
503 $page_id = $this->mTitle
->getArticleID();
505 return $dbr->select( 'revision',
506 Revision
::selectFields(),
507 array_merge(array("rev_page=$page_id"), $offsets),
509 array( 'ORDER BY' => "rev_timestamp $dirs",
510 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
515 * Output a subscription feed listing recent edits to this page.
516 * @param string $type
518 function feed( $type ) {
519 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
520 if( !FeedUtils
::checkFeedOutput($type) ) {
524 $feed = new $wgFeedClasses[$type](
525 $this->mTitle
->getPrefixedText() . ' - ' .
526 wfMsgForContent( 'history-feed-title' ),
527 wfMsgForContent( 'history-feed-description' ),
528 $this->mTitle
->getFullUrl( 'action=history' ) );
530 // Get a limit on number of feed entries. Provide a sane default
531 // of 10 if none is defined (but limit to $wgFeedLimit max)
532 $limit = $wgRequest->getInt( 'limit', 10 );
533 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
536 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
540 foreach( $items as $row ) {
541 $feed->outItem( $this->feedItem( $row ) );
544 $feed->outItem( $this->feedEmpty() );
549 function feedEmpty() {
552 wfMsgForContent( 'nohistory' ),
553 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
554 $this->mTitle
->getFullUrl(),
555 wfTimestamp( TS_MW
),
557 $this->mTitle
->getTalkPage()->getFullUrl() );
561 * Generate a FeedItem object from a given revision table row
562 * Borrows Recent Changes' feed generation functions for formatting;
563 * includes a diff to the previous revision (if any).
568 function feedItem( $row ) {
569 $rev = new Revision( $row );
570 $rev->setTitle( $this->mTitle
);
571 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
572 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
574 $rev->getTimestamp(),
575 $rev->getComment() );
577 if( $rev->getComment() == '' ) {
579 $title = wfMsgForContent( 'history-feed-item-nocomment',
581 $wgContLang->timeanddate( $rev->getTimestamp() ) );
583 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
589 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
590 $rev->getTimestamp(),
592 $this->mTitle
->getTalkPage()->getFullUrl() );
600 class PageHistoryPager
extends ReverseChronologicalPager
{
601 public $mLastRow = false, $mPageHistory, $mTitle;
603 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
604 parent
::__construct();
605 $this->mPageHistory
= $pageHistory;
606 $this->mTitle
=& $this->mPageHistory
->mTitle
;
607 $this->tagFilter
= $tagFilter;
608 $this->getDateCond( $year, $month );
611 function getQueryInfo() {
613 'tables' => array('revision'),
614 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
615 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
616 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
617 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
619 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
620 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
624 function getIndexField() {
625 return 'rev_timestamp';
628 function formatRow( $row ) {
629 if( $this->mLastRow
) {
630 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
631 $firstInList = $this->mCounter
== 1;
632 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
633 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
637 $this->mLastRow
= $row;
641 function getStartBody() {
642 $this->mLastRow
= false;
647 function getEndBody() {
648 if( $this->mLastRow
) {
649 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
650 $firstInList = $this->mCounter
== 1;
651 if( $this->mIsBackwards
) {
652 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
653 if( $this->mOffset
== '' ) {
659 # The next row is the past-the-end row
660 $next = $this->mPastTheEndRow
;
662 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
663 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );