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, $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
->link(
95 wfMsgHtml( 'viewpagelogs' ),
97 array( 'page' => $this->mTitle
->getPrefixedText() ),
98 array( 'known', 'noclasses' )
100 $wgOut->setSubtitle( $logLink );
102 $feedType = $wgRequest->getVal( 'feed' );
104 wfProfileOut( __METHOD__
);
105 return $this->feed( $feedType );
109 * Fail if article doesn't exist.
111 if( !$this->mTitle
->exists() ) {
112 $wgOut->addWikiMsg( 'nohistory' );
113 wfProfileOut( __METHOD__
);
118 * Add date selector to quickly get to a certain time
120 $year = $wgRequest->getInt( 'year' );
121 $month = $wgRequest->getInt( 'month' );
122 $tagFilter = $wgRequest->getVal( 'tagfilter' );
123 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
125 $action = htmlspecialchars( $wgScript );
127 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
128 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
129 Xml
::hidden( 'title', $this->mTitle
->getPrefixedDBKey() ) . "\n" .
130 Xml
::hidden( 'action', 'history' ) . "\n" .
131 xml
::dateMenu( $year, $month ) . ' ' .
132 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
133 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
137 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
142 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
143 $this->linesonpage
= $pager->getNumRows();
145 $pager->getNavigationBar() .
146 $this->beginHistoryList() .
148 $this->endHistoryList() .
149 $pager->getNavigationBar()
152 wfProfileOut( __METHOD__
);
156 * Creates begin of history list with a submit button
158 * @return string HTML output
160 function beginHistoryList() {
161 global $wgUser, $wgScript, $wgEnableHtmlDiff;
162 $this->lastdate
= '';
163 $s = wfMsgExt( 'histlegend', array( 'parse') );
164 if( $this->linesonpage
> 1 && $wgUser->isAllowed('deleterevision') ) {
165 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
166 $s .= Xml
::openElement( 'form',
168 'action' => $revdel->getLocalURL( array(
169 'type' => 'revision',
170 'target' => $this->mTitle
->getPrefixedDbKey()
173 'id' => 'mw-history-revdeleteform',
174 'style' => 'visibility:hidden;float:right;'
177 $s .= Xml
::hidden( 'ids', '', array('id'=>'revdel-oldid') );
178 $s .= Xml
::submitButton( wfMsg( 'showhideselectedversions' ) );
179 $s .= Xml
::closeElement( 'form' );
181 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript,
182 'id' => 'mw-history-compare' ) );
183 $s .= Xml
::hidden( 'title', $this->mTitle
->getPrefixedDbKey() );
184 if( $wgEnableHtmlDiff ) {
185 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
187 'name' => 'htmldiff',
188 'class' => 'historysubmit',
189 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
190 'title' => wfMsg( 'tooltip-compareselectedversions' ),
193 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
195 'class' => 'historysubmit',
196 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
197 'title' => wfMsg( 'tooltip-compareselectedversions' ),
201 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
203 'class' => 'historysubmit',
204 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
205 'title' => wfMsg( 'tooltip-compareselectedversions' ),
209 $s .= '<ul id="pagehistory">' . "\n";
214 * Creates end of history list with a submit button
216 * @return string HTML output
218 function endHistoryList() {
219 global $wgEnableHtmlDiff;
221 if( $wgEnableHtmlDiff ) {
222 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
224 'name' => 'htmldiff',
225 'class' => 'historysubmit',
226 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
227 'title' => wfMsg( 'tooltip-compareselectedversions' ),
230 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
232 'class' => 'historysubmit',
233 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
234 'title' => wfMsg( 'tooltip-compareselectedversions' ),
238 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
240 'class' => 'historysubmit',
241 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
242 'title' => wfMsg( 'tooltip-compareselectedversions' ),
251 * Creates a submit button
253 * @param array $attributes attributes
254 * @return string HTML output for the submit button
256 function submitButton($message, $attributes = array() ) {
257 # Disable submit button if history has 1 revision only
258 if( $this->linesonpage
> 1 ) {
259 return Xml
::submitButton( $message , $attributes );
266 * Returns a row from the history printout.
268 * @todo document some more, and maybe clean up the code (some params redundant?)
270 * @param Row $row The database row corresponding to the previous line.
271 * @param mixed $next The database row corresponding to the next line.
272 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
273 * @param $notificationtimestamp
274 * @param bool $latest Whether this row corresponds to the page's latest revision.
275 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
276 * @return string HTML output for the row
278 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
279 $latest = false, $firstInList = false )
281 global $wgUser, $wgLang;
282 $rev = new Revision( $row );
283 $rev->setTitle( $this->mTitle
);
285 $curlink = $this->curLink( $rev, $latest );
286 $lastlink = $this->lastLink( $rev, $next, $counter );
287 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
288 $link = $this->revLink( $rev );
291 $s = "($curlink) ($lastlink) $arbitrary";
293 if( $wgUser->isAllowed( 'deleterevision' ) ) {
294 // Hide JS by default for non-JS browsing
295 $hidden = array( 'style' => 'display:none' );
296 // If revision was hidden from sysops
297 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
298 $del = Xml
::check( 'deleterevisions', false,
299 $hidden +
array('disabled' => 'disabled') );
300 $del .= Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
301 '(' . $this->message
['rev-delundel'] . ')' );
302 // Otherwise, show the link...
305 $jsCall = "updateShowHideForm($id,this.checked)";
306 $del = Xml
::check( 'showhiderevisions', false,
308 'onchange' => $jsCall,
309 'id' => "mw-revdel-$id" ) );
311 'type' => 'revision',
312 'target' => $this->mTitle
->getPrefixedDbkey(),
313 'ids' => $rev->getId() );
314 $del .= $this->mSkin
->revDeleteLink( $query,
315 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
321 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
323 if( $rev->isMinor() ) {
324 $s .= ' ' . Xml
::element( 'abbr', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
327 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
328 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
331 $s .= $this->mSkin
->revComment( $rev, false, true );
333 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
334 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
339 if( !is_null( $next ) && is_object( $next ) ) {
340 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
341 $tools[] = '<span class="mw-rollback-link">'.
342 $this->mSkin
->buildRollbackLink( $rev ).'</span>';
345 if( $this->mTitle
->quickUserCan( 'edit' )
346 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
347 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
349 # Create undo tooltip for the first (=latest) line only
350 $undoTooltip = $latest
351 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
353 $undolink = $this->mSkin
->link(
355 wfMsgHtml( 'editundo' ),
357 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
358 array( 'known', 'noclasses' )
360 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
365 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
369 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
370 $classes = array_merge( $classes, $newClasses );
371 $s .= " $tagSummary";
373 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
375 $classes = implode( ' ', $classes );
377 return "<li class=\"$classes\">$s</li>\n";
381 * Create a link to view this revision of the page
382 * @param Revision $rev
385 function revLink( $rev ) {
387 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
388 $date = htmlspecialchars( $date );
389 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
390 $link = $this->mSkin
->link(
394 array( 'oldid' => $rev->getId() ),
395 array( 'known', 'noclasses' )
398 $link = "<span class=\"history-deleted\">$date</span>";
404 * Create a diff-to-current link for this revision for this page
405 * @param Revision $rev
406 * @param Bool $latest, this is the latest revision of the page?
409 function curLink( $rev, $latest ) {
410 $cur = $this->message
['cur'];
411 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
414 return $this->mSkin
->link(
419 'diff' => $this->mTitle
->getLatestRevID(),
420 'oldid' => $rev->getId()
422 array( 'known', 'noclasses' )
428 * Create a diff-to-previous link for this revision for this page.
429 * @param Revision $prevRev, the previous revision
430 * @param mixed $next, the newer revision
431 * @param int $counter, what row on the history list this is
434 function lastLink( $prevRev, $next, $counter ) {
435 $last = $this->message
['last'];
436 # $next may either be a Row, null, or "unkown"
437 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
438 if( is_null($next) ) {
439 # Probably no next row
441 } elseif( $next === 'unknown' ) {
442 # Next row probably exists but is unknown, use an oldid=prev link
443 return $this->mSkin
->link(
448 'diff' => $prevRev->getId(),
451 array( 'known', 'noclasses' )
453 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
456 return $this->mSkin
->link(
461 'diff' => $prevRev->getId(),
462 'oldid' => $next->rev_id
464 array( 'known', 'noclasses' )
470 * Create radio buttons for page history
472 * @param object $rev Revision
473 * @param bool $firstInList Is this version the first one?
474 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
475 * @return string HTML output for the radio buttons
477 function diffButtons( $rev, $firstInList, $counter ) {
478 if( $this->linesonpage
> 1 ) {
480 $radio = array( 'type' => 'radio', 'value' => $id );
481 /** @todo: move title texts to javascript */
483 $first = Xml
::element( 'input',
484 array_merge( $radio, array(
485 'style' => 'visibility:hidden',
487 'id' => 'mw-oldid-null' ) )
489 $checkmark = array( 'checked' => 'checked' );
491 # Check visibility of old revisions
492 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
493 $radio['disabled'] = 'disabled';
494 $checkmark = array(); // We will check the next possible one
495 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
496 $checkmark = array( 'checked' => 'checked' );
497 $this->mOldIdChecked
= $id;
499 $checkmark = array();
501 $first = Xml
::element( 'input',
502 array_merge( $radio, $checkmark, array(
504 'id' => "mw-oldid-$id" ) ) );
505 $checkmark = array();
507 $second = Xml
::element( 'input',
508 array_merge( $radio, $checkmark, array(
510 'id' => "mw-diff-$id" ) ) );
511 return $first . $second;
518 * Fetch an array of revisions, specified by a given limit, offset and
519 * direction. This is now only used by the feeds. It was previously
520 * used by the main UI but that's now handled by the pager.
522 function fetchRevisions($limit, $offset, $direction) {
523 $dbr = wfGetDB( DB_SLAVE
);
525 if( $direction == PageHistory
::DIR_PREV
)
526 list($dirs, $oper) = array("ASC", ">=");
527 else /* $direction == PageHistory::DIR_NEXT */
528 list($dirs, $oper) = array("DESC", "<=");
531 $offsets = array("rev_timestamp $oper '$offset'");
535 $page_id = $this->mTitle
->getArticleID();
537 return $dbr->select( 'revision',
538 Revision
::selectFields(),
539 array_merge(array("rev_page=$page_id"), $offsets),
541 array( 'ORDER BY' => "rev_timestamp $dirs",
542 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
547 * Output a subscription feed listing recent edits to this page.
548 * @param string $type
550 function feed( $type ) {
551 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
552 if( !FeedUtils
::checkFeedOutput($type) ) {
556 $feed = new $wgFeedClasses[$type](
557 $this->mTitle
->getPrefixedText() . ' - ' .
558 wfMsgForContent( 'history-feed-title' ),
559 wfMsgForContent( 'history-feed-description' ),
560 $this->mTitle
->getFullUrl( array( 'action' => 'history' ) )
563 // Get a limit on number of feed entries. Provide a sane default
564 // of 10 if none is defined (but limit to $wgFeedLimit max)
565 $limit = $wgRequest->getInt( 'limit', 10 );
566 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
569 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
573 foreach( $items as $row ) {
574 $feed->outItem( $this->feedItem( $row ) );
577 $feed->outItem( $this->feedEmpty() );
582 function feedEmpty() {
585 wfMsgForContent( 'nohistory' ),
586 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
587 $this->mTitle
->getFullUrl(),
588 wfTimestamp( TS_MW
),
590 $this->mTitle
->getTalkPage()->getFullUrl()
595 * Generate a FeedItem object from a given revision table row
596 * Borrows Recent Changes' feed generation functions for formatting;
597 * includes a diff to the previous revision (if any).
602 function feedItem( $row ) {
603 $rev = new Revision( $row );
604 $rev->setTitle( $this->mTitle
);
605 $text = FeedUtils
::formatDiffRow(
607 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
609 $rev->getTimestamp(),
612 if( $rev->getComment() == '' ) {
614 $ts = $rev->getTimestamp();
615 $title = wfMsgForContent( 'history-feed-item-nocomment',
617 $wgContLang->timeanddate( $ts ),
618 $wgContLang->date( $ts ),
619 $wgContLang->time( $ts ) );
621 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) .
622 FeedItem
::stripComment( $rev->getComment() );
627 $this->mTitle
->getFullUrl( array(
628 'diff' => $rev->getId(),
631 $rev->getTimestamp(),
633 $this->mTitle
->getTalkPage()->getFullUrl()
641 class PageHistoryPager
extends ReverseChronologicalPager
{
642 public $mLastRow = false, $mPageHistory, $mTitle;
644 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
645 parent
::__construct();
646 $this->mPageHistory
= $pageHistory;
647 $this->mTitle
=& $this->mPageHistory
->mTitle
;
648 $this->tagFilter
= $tagFilter;
649 $this->getDateCond( $year, $month );
652 function getQueryInfo() {
654 'tables' => array('revision'),
655 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
656 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
657 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
658 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
660 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
661 $queryInfo['fields'],
663 $queryInfo['join_conds'],
664 $queryInfo['options'],
666 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
670 function getIndexField() {
671 return 'rev_timestamp';
674 function formatRow( $row ) {
675 if( $this->mLastRow
) {
676 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
677 $firstInList = $this->mCounter
== 1;
678 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
679 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
683 $this->mLastRow
= $row;
687 function getStartBody() {
688 $this->mLastRow
= false;
693 function getEndBody() {
694 if( $this->mLastRow
) {
695 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
696 $firstInList = $this->mCounter
== 1;
697 if( $this->mIsBackwards
) {
698 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
699 if( $this->mOffset
== '' ) {
705 # The next row is the past-the-end row
706 $next = $this->mPastTheEndRow
;
708 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
709 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );