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' );
117 $action = htmlspecialchars( $wgScript );
119 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
120 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
121 Xml
::hidden( 'title', $this->mTitle
->getPrefixedDBKey() ) . "\n" .
122 Xml
::hidden( 'action', 'history' ) . "\n" .
123 $this->getDateMenu( $year, $month ) . ' ' .
124 implode( ' ', ChangeTags
::buildTagFilterSelector( $tagFilter ) ) . ' ' .
125 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
129 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
134 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
135 $this->linesonpage
= $pager->getNumRows();
137 $pager->getNavigationBar() .
138 $this->beginHistoryList() .
140 $this->endHistoryList() .
141 $pager->getNavigationBar()
144 wfProfileOut( __METHOD__
);
148 * @return string Formatted HTML
152 private function getDateMenu( $year, $month ) {
153 # Offset overrides year/month selection
154 if( $month && $month !== -1 ) {
155 $encMonth = intval( $month );
160 $encYear = intval( $year );
161 } else if( $encMonth ) {
162 $thisMonth = intval( gmdate( 'n' ) );
163 $thisYear = intval( gmdate( 'Y' ) );
164 if( intval($encMonth) > $thisMonth ) {
167 $encYear = $thisYear;
171 return Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
172 Xml
::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) .
174 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
175 Xml
::monthSelector( $encMonth, -1 );
179 * Creates begin of history list with a submit button
181 * @return string HTML output
183 function beginHistoryList() {
184 global $wgTitle, $wgScript, $wgEnableHtmlDiff;
185 $this->lastdate
= '';
186 $s = wfMsgExt( 'histlegend', array( 'parse') );
187 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript, 'id' => 'mw-history-compare' ) );
188 $s .= Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() );
189 if( $wgEnableHtmlDiff ) {
190 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
192 'name' => 'htmldiff',
193 'class' => 'historysubmit',
194 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
195 'title' => wfMsg( 'tooltip-compareselectedversions' ),
198 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
200 'class' => 'historysubmit',
201 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
202 'title' => wfMsg( 'tooltip-compareselectedversions' ),
206 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
208 'class' => 'historysubmit',
209 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
210 'title' => wfMsg( 'tooltip-compareselectedversions' ),
214 $s .= '<ul id="pagehistory">' . "\n";
219 * Creates end of history list with a submit button
221 * @return string HTML output
223 function endHistoryList() {
224 global $wgEnableHtmlDiff;
226 if( $wgEnableHtmlDiff ) {
227 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
229 'name' => 'htmldiff',
230 'class' => 'historysubmit',
231 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
232 'title' => wfMsg( 'tooltip-compareselectedversions' ),
235 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
237 'class' => 'historysubmit',
238 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
239 'title' => wfMsg( 'tooltip-compareselectedversions' ),
243 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
245 'class' => 'historysubmit',
246 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
247 'title' => wfMsg( 'tooltip-compareselectedversions' ),
256 * Creates a submit button
258 * @param array $attributes attributes
259 * @return string HTML output for the submit button
261 function submitButton($message, $attributes = array() ) {
262 # Disable submit button if history has 1 revision only
263 if( $this->linesonpage
> 1 ) {
264 return Xml
::submitButton( $message , $attributes );
271 * Returns a row from the history printout.
273 * @todo document some more, and maybe clean up the code (some params redundant?)
275 * @param Row $row The database row corresponding to the previous line.
276 * @param mixed $next The database row corresponding to the next line.
277 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
278 * @param $notificationtimestamp
279 * @param bool $latest Whether this row corresponds to the page's latest revision.
280 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
281 * @return string HTML output for the row
283 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
284 global $wgUser, $wgLang;
285 $rev = new Revision( $row );
286 $rev->setTitle( $this->mTitle
);
288 $curlink = $this->curLink( $rev, $latest );
289 $lastlink = $this->lastLink( $rev, $next, $counter );
290 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
291 $link = $this->revLink( $rev );
294 $s = "($curlink) ($lastlink) $arbitrary";
296 if( $wgUser->isAllowed( 'deleterevision' ) ) {
298 // We don't currently handle well changing the top revision's settings
299 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message
['rev-delundel'].')' );
300 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
301 // If revision was hidden from sysops
302 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message
['rev-delundel'].')' );
304 $query = array( 'target' => $this->mTitle
->getPrefixedDbkey(),
305 'oldid' => $rev->getId()
307 $del = $this->mSkin
->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
313 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
315 if( $row->rev_minor_edit
) {
316 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
319 if( !is_null( $size = $rev->getSize() ) && $rev->userCan( Revision
::DELETED_TEXT
) ) {
320 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
323 $s .= $this->mSkin
->revComment( $rev, false, true );
325 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
326 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
328 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
329 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
334 if( !is_null( $next ) && is_object( $next ) ) {
335 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
336 $tools[] = '<span class="mw-rollback-link">'.$this->mSkin
->buildRollbackLink( $rev ).'</span>';
339 if( $this->mTitle
->quickUserCan( 'edit' ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
340 !$next->rev_deleted
& Revision
::DELETED_TEXT
)
342 # Create undo tooltip for the first (=latest) line only
343 $undoTooltip = $latest
344 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
346 $undolink = $this->mSkin
->link(
348 wfMsgHtml( 'editundo' ),
350 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
351 array( 'known', 'noclasses' )
353 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
358 $s .= ' (' . implode( ' | ', $tools ) . ')';
362 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
363 $classes = array_merge( $classes, $newClasses );
364 $s .= " $tagSummary";
366 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
368 $classes = implode( ' ', $classes );
370 return "<li class=\"$classes\">$s</li>\n";
374 * Create a link to view this revision of the page
375 * @param Revision $rev
378 function revLink( $rev ) {
380 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
381 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
382 $link = $this->mSkin
->makeKnownLinkObj(
383 $this->mTitle
, $date, "oldid=" . $rev->getId() );
387 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
388 return '<span class="history-deleted">' . $link . '</span>';
394 * Create a diff-to-current link for this revision for this page
395 * @param Revision $rev
396 * @param Bool $latest, this is the latest revision of the page?
399 function curLink( $rev, $latest ) {
400 $cur = $this->message
['cur'];
401 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
404 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
405 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
410 * Create a diff-to-previous link for this revision for this page.
411 * @param Revision $prevRev, the previous revision
412 * @param mixed $next, the newer revision
413 * @param int $counter, what row on the history list this is
416 function lastLink( $prevRev, $next, $counter ) {
417 $last = $this->message
['last'];
418 # $next may either be a Row, null, or "unkown"
419 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
420 if( is_null($next) ) {
421 # Probably no next row
423 } elseif( $next === 'unknown' ) {
424 # Next row probably exists but is unknown, use an oldid=prev link
425 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
426 "diff=" . $prevRev->getId() . "&oldid=prev" );
427 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
430 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
431 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
436 * Create radio buttons for page history
438 * @param object $rev Revision
439 * @param bool $firstInList Is this version the first one?
440 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
441 * @return string HTML output for the radio buttons
443 function diffButtons( $rev, $firstInList, $counter ) {
444 if( $this->linesonpage
> 1) {
447 'value' => $rev->getId(),
450 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
451 $radio['disabled'] = 'disabled';
454 /** @todo: move title texts to javascript */
456 $first = Xml
::element( 'input', array_merge(
459 'style' => 'visibility:hidden',
460 'name' => 'oldid' ) ) );
461 $checkmark = array( 'checked' => 'checked' );
463 if( $counter == 2 ) {
464 $checkmark = array( 'checked' => 'checked' );
466 $checkmark = array();
468 $first = Xml
::element( 'input', array_merge(
471 array( 'name' => 'oldid' ) ) );
472 $checkmark = array();
474 $second = Xml
::element( 'input', array_merge(
477 array( 'name' => 'diff' ) ) );
478 return $first . $second;
485 * Fetch an array of revisions, specified by a given limit, offset and
486 * direction. This is now only used by the feeds. It was previously
487 * used by the main UI but that's now handled by the pager.
489 function fetchRevisions($limit, $offset, $direction) {
490 $dbr = wfGetDB( DB_SLAVE
);
492 if( $direction == PageHistory
::DIR_PREV
)
493 list($dirs, $oper) = array("ASC", ">=");
494 else /* $direction == PageHistory::DIR_NEXT */
495 list($dirs, $oper) = array("DESC", "<=");
498 $offsets = array("rev_timestamp $oper '$offset'");
502 $page_id = $this->mTitle
->getArticleID();
504 return $dbr->select( 'revision',
505 Revision
::selectFields(),
506 array_merge(array("rev_page=$page_id"), $offsets),
508 array( 'ORDER BY' => "rev_timestamp $dirs",
509 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
514 * Output a subscription feed listing recent edits to this page.
515 * @param string $type
517 function feed( $type ) {
518 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
519 if( !FeedUtils
::checkFeedOutput($type) ) {
523 $feed = new $wgFeedClasses[$type](
524 $this->mTitle
->getPrefixedText() . ' - ' .
525 wfMsgForContent( 'history-feed-title' ),
526 wfMsgForContent( 'history-feed-description' ),
527 $this->mTitle
->getFullUrl( 'action=history' ) );
529 // Get a limit on number of feed entries. Provide a sane default
530 // of 10 if none is defined (but limit to $wgFeedLimit max)
531 $limit = $wgRequest->getInt( 'limit', 10 );
532 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
535 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
539 foreach( $items as $row ) {
540 $feed->outItem( $this->feedItem( $row ) );
543 $feed->outItem( $this->feedEmpty() );
548 function feedEmpty() {
551 wfMsgForContent( 'nohistory' ),
552 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
553 $this->mTitle
->getFullUrl(),
554 wfTimestamp( TS_MW
),
556 $this->mTitle
->getTalkPage()->getFullUrl() );
560 * Generate a FeedItem object from a given revision table row
561 * Borrows Recent Changes' feed generation functions for formatting;
562 * includes a diff to the previous revision (if any).
567 function feedItem( $row ) {
568 $rev = new Revision( $row );
569 $rev->setTitle( $this->mTitle
);
570 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
571 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
573 $rev->getTimestamp(),
574 $rev->getComment() );
576 if( $rev->getComment() == '' ) {
578 $title = wfMsgForContent( 'history-feed-item-nocomment',
580 $wgContLang->timeanddate( $rev->getTimestamp() ) );
582 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
588 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
589 $rev->getTimestamp(),
591 $this->mTitle
->getTalkPage()->getFullUrl() );
599 class PageHistoryPager
extends ReverseChronologicalPager
{
600 public $mLastRow = false, $mPageHistory, $mTitle;
602 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
603 parent
::__construct();
604 $this->mPageHistory
= $pageHistory;
605 $this->mTitle
=& $this->mPageHistory
->mTitle
;
606 $this->tagFilter
= $tagFilter;
607 $this->getDateCond( $year, $month );
610 function getQueryInfo() {
612 'tables' => array('revision'),
613 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
614 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
615 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
616 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
618 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
619 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
623 function getIndexField() {
624 return 'rev_timestamp';
627 function formatRow( $row ) {
628 if( $this->mLastRow
) {
629 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
630 $firstInList = $this->mCounter
== 1;
631 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
632 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
636 $this->mLastRow
= $row;
640 function getStartBody() {
641 $this->mLastRow
= false;
646 function getEndBody() {
647 if( $this->mLastRow
) {
648 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
649 $firstInList = $this->mCounter
== 1;
650 if( $this->mIsBackwards
) {
651 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
652 if( $this->mOffset
== '' ) {
658 # The next row is the past-the-end row
659 $next = $this->mPastTheEndRow
;
661 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
662 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );