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 xml
::dateMenu( $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 * Creates begin of history list with a submit button
153 * @return string HTML output
155 function beginHistoryList() {
156 global $wgTitle, $wgScript, $wgEnableHtmlDiff;
157 $this->lastdate
= '';
158 $s = wfMsgExt( 'histlegend', array( 'parse') );
159 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript, 'id' => 'mw-history-compare' ) );
160 $s .= Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() );
161 if( $wgEnableHtmlDiff ) {
162 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
164 'name' => 'htmldiff',
165 'class' => 'historysubmit',
166 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
167 'title' => wfMsg( 'tooltip-compareselectedversions' ),
170 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
172 'class' => 'historysubmit',
173 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
174 'title' => wfMsg( 'tooltip-compareselectedversions' ),
178 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
180 'class' => 'historysubmit',
181 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
182 'title' => wfMsg( 'tooltip-compareselectedversions' ),
186 $s .= '<ul id="pagehistory">' . "\n";
191 * Creates end of history list with a submit button
193 * @return string HTML output
195 function endHistoryList() {
196 global $wgEnableHtmlDiff;
198 if( $wgEnableHtmlDiff ) {
199 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
201 'name' => 'htmldiff',
202 'class' => 'historysubmit',
203 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
204 'title' => wfMsg( 'tooltip-compareselectedversions' ),
207 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
209 'class' => 'historysubmit',
210 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
211 'title' => wfMsg( 'tooltip-compareselectedversions' ),
215 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
217 'class' => 'historysubmit',
218 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
219 'title' => wfMsg( 'tooltip-compareselectedversions' ),
228 * Creates a submit button
230 * @param array $attributes attributes
231 * @return string HTML output for the submit button
233 function submitButton($message, $attributes = array() ) {
234 # Disable submit button if history has 1 revision only
235 if( $this->linesonpage
> 1 ) {
236 return Xml
::submitButton( $message , $attributes );
243 * Returns a row from the history printout.
245 * @todo document some more, and maybe clean up the code (some params redundant?)
247 * @param Row $row The database row corresponding to the previous line.
248 * @param mixed $next The database row corresponding to the next line.
249 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
250 * @param $notificationtimestamp
251 * @param bool $latest Whether this row corresponds to the page's latest revision.
252 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
253 * @return string HTML output for the row
255 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
256 $latest = false, $firstInList = false )
258 global $wgUser, $wgLang;
259 $rev = new Revision( $row );
260 $rev->setTitle( $this->mTitle
);
262 $curlink = $this->curLink( $rev, $latest );
263 $lastlink = $this->lastLink( $rev, $next, $counter );
264 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
265 $link = $this->revLink( $rev );
268 $s = "($curlink) ($lastlink) $arbitrary";
270 if( $wgUser->isAllowed( 'deleterevision' ) ) {
272 // We don't currently handle well changing the top revision's settings
273 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.
274 $this->message
['rev-delundel'].')' );
275 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
276 // If revision was hidden from sysops
277 $del = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.
278 $this->message
['rev-delundel'].')' );
280 $query = array( 'target' => $this->mTitle
->getPrefixedDbkey(),
281 'oldid' => $rev->getId()
283 $del = $this->mSkin
->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
289 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
291 if( $rev->isMinor() ) {
292 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
295 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
296 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
299 $s .= $this->mSkin
->revComment( $rev, false, true );
301 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
302 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
304 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
305 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
310 if( !is_null( $next ) && is_object( $next ) ) {
311 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
312 $tools[] = '<span class="mw-rollback-link">'.$this->mSkin
->buildRollbackLink( $rev ).'</span>';
315 if( $this->mTitle
->quickUserCan( 'edit' ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
316 !$next->rev_deleted
& Revision
::DELETED_TEXT
)
318 # Create undo tooltip for the first (=latest) line only
319 $undoTooltip = $latest
320 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
322 $undolink = $this->mSkin
->link(
324 wfMsgHtml( 'editundo' ),
326 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
327 array( 'known', 'noclasses' )
329 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
334 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
338 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
339 $classes = array_merge( $classes, $newClasses );
340 $s .= " $tagSummary";
342 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
344 $classes = implode( ' ', $classes );
346 return "<li class=\"$classes\">$s</li>\n";
350 * Create a link to view this revision of the page
351 * @param Revision $rev
354 function revLink( $rev ) {
356 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
357 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
358 $link = $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $date, "oldid=" . $rev->getId() );
362 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
363 $link = "<span class=\"history-deleted\">$link</span>";
369 * Create a diff-to-current link for this revision for this page
370 * @param Revision $rev
371 * @param Bool $latest, this is the latest revision of the page?
374 function curLink( $rev, $latest ) {
375 $cur = $this->message
['cur'];
376 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
379 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
380 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
385 * Create a diff-to-previous link for this revision for this page.
386 * @param Revision $prevRev, the previous revision
387 * @param mixed $next, the newer revision
388 * @param int $counter, what row on the history list this is
391 function lastLink( $prevRev, $next, $counter ) {
392 $last = $this->message
['last'];
393 # $next may either be a Row, null, or "unkown"
394 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
395 if( is_null($next) ) {
396 # Probably no next row
398 } elseif( $next === 'unknown' ) {
399 # Next row probably exists but is unknown, use an oldid=prev link
400 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
401 "diff=" . $prevRev->getId() . "&oldid=prev" );
402 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
405 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
406 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
411 * Create radio buttons for page history
413 * @param object $rev Revision
414 * @param bool $firstInList Is this version the first one?
415 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
416 * @return string HTML output for the radio buttons
418 function diffButtons( $rev, $firstInList, $counter ) {
419 if( $this->linesonpage
> 1 ) {
420 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
421 /** @todo: move title texts to javascript */
423 $first = Xml
::element( 'input',
424 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
426 $checkmark = array( 'checked' => 'checked' );
428 # Check visibility of old revisions
429 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
430 $radio['disabled'] = 'disabled';
431 $checkmark = array(); // We will check the next possible one
432 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
433 $checkmark = array( 'checked' => 'checked' );
434 $this->mOldIdChecked
= $rev->getId();
436 $checkmark = array();
438 $first = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
439 $checkmark = array();
441 $second = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
442 return $first . $second;
449 * Fetch an array of revisions, specified by a given limit, offset and
450 * direction. This is now only used by the feeds. It was previously
451 * used by the main UI but that's now handled by the pager.
453 function fetchRevisions($limit, $offset, $direction) {
454 $dbr = wfGetDB( DB_SLAVE
);
456 if( $direction == PageHistory
::DIR_PREV
)
457 list($dirs, $oper) = array("ASC", ">=");
458 else /* $direction == PageHistory::DIR_NEXT */
459 list($dirs, $oper) = array("DESC", "<=");
462 $offsets = array("rev_timestamp $oper '$offset'");
466 $page_id = $this->mTitle
->getArticleID();
468 return $dbr->select( 'revision',
469 Revision
::selectFields(),
470 array_merge(array("rev_page=$page_id"), $offsets),
472 array( 'ORDER BY' => "rev_timestamp $dirs",
473 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
478 * Output a subscription feed listing recent edits to this page.
479 * @param string $type
481 function feed( $type ) {
482 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
483 if( !FeedUtils
::checkFeedOutput($type) ) {
487 $feed = new $wgFeedClasses[$type](
488 $this->mTitle
->getPrefixedText() . ' - ' .
489 wfMsgForContent( 'history-feed-title' ),
490 wfMsgForContent( 'history-feed-description' ),
491 $this->mTitle
->getFullUrl( 'action=history' ) );
493 // Get a limit on number of feed entries. Provide a sane default
494 // of 10 if none is defined (but limit to $wgFeedLimit max)
495 $limit = $wgRequest->getInt( 'limit', 10 );
496 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
499 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
503 foreach( $items as $row ) {
504 $feed->outItem( $this->feedItem( $row ) );
507 $feed->outItem( $this->feedEmpty() );
512 function feedEmpty() {
515 wfMsgForContent( 'nohistory' ),
516 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
517 $this->mTitle
->getFullUrl(),
518 wfTimestamp( TS_MW
),
520 $this->mTitle
->getTalkPage()->getFullUrl() );
524 * Generate a FeedItem object from a given revision table row
525 * Borrows Recent Changes' feed generation functions for formatting;
526 * includes a diff to the previous revision (if any).
531 function feedItem( $row ) {
532 $rev = new Revision( $row );
533 $rev->setTitle( $this->mTitle
);
534 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
535 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
537 $rev->getTimestamp(),
538 $rev->getComment() );
540 if( $rev->getComment() == '' ) {
542 $title = wfMsgForContent( 'history-feed-item-nocomment',
544 $wgContLang->timeanddate( $rev->getTimestamp() ) );
546 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
552 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
553 $rev->getTimestamp(),
555 $this->mTitle
->getTalkPage()->getFullUrl() );
563 class PageHistoryPager
extends ReverseChronologicalPager
{
564 public $mLastRow = false, $mPageHistory, $mTitle;
566 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
567 parent
::__construct();
568 $this->mPageHistory
= $pageHistory;
569 $this->mTitle
=& $this->mPageHistory
->mTitle
;
570 $this->tagFilter
= $tagFilter;
571 $this->getDateCond( $year, $month );
574 function getQueryInfo() {
576 'tables' => array('revision'),
577 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
578 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
579 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
580 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
582 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter
);
583 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
587 function getIndexField() {
588 return 'rev_timestamp';
591 function formatRow( $row ) {
592 if( $this->mLastRow
) {
593 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
594 $firstInList = $this->mCounter
== 1;
595 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
596 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
600 $this->mLastRow
= $row;
604 function getStartBody() {
605 $this->mLastRow
= false;
610 function getEndBody() {
611 if( $this->mLastRow
) {
612 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
613 $firstInList = $this->mCounter
== 1;
614 if( $this->mIsBackwards
) {
615 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
616 if( $this->mOffset
== '' ) {
622 # The next row is the past-the-end row
623 $next = $this->mPastTheEndRow
;
625 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
626 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );