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 $article, $title, $skin;
25 * Construct a new HistoryPage.
27 * @param Article $article
30 function __construct( $article ) {
32 $this->article
= $article;
33 $this->title
= $article->getTitle();
34 $this->skin
= $wgUser->getSkin();
35 $this->preCacheMessages();
38 function getArticle() {
39 return $this->article
;
47 * As we use the same small set of messages in various methods and that
48 * they are called often, we call them once and save them in $this->message
50 function preCacheMessages() {
51 // Precache various messages
52 if( !isset( $this->message
) ) {
53 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
54 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
60 * Print the history page for an article.
65 global $wgOut, $wgRequest, $wgScript;
68 * Allow client caching.
70 if( $wgOut->checkLastModified( $this->article
->getTouched() ) )
71 return; // Client cache fresh and headers sent, nothing more to do.
73 wfProfileIn( __METHOD__
);
76 * Setup page variables.
78 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
79 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
80 $wgOut->setArticleFlag( false );
81 $wgOut->setArticleRelated( true );
82 $wgOut->setRobotPolicy( 'noindex,nofollow' );
83 $wgOut->setSyndicated( true );
84 $wgOut->setFeedAppendQuery( 'action=history' );
85 $wgOut->addScriptFile( 'history.js' );
87 $logPage = SpecialPage
::getTitleFor( 'Log' );
88 $logLink = $this->skin
->link(
90 wfMsgHtml( 'viewpagelogs' ),
92 array( 'page' => $this->title
->getPrefixedText() ),
93 array( 'known', 'noclasses' )
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->title
->exists() ) {
107 $wgOut->addWikiMsg( 'nohistory' );
108 $this->article
->showLogs(); // show deletion/move log if there is an entry
109 wfProfileOut( __METHOD__
);
114 * Add date selector to quickly get to a certain time
116 $year = $wgRequest->getInt( 'year' );
117 $month = $wgRequest->getInt( 'month' );
118 $tagFilter = $wgRequest->getVal( 'tagfilter' );
119 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
121 $action = htmlspecialchars( $wgScript );
123 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
124 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
125 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
126 Xml
::hidden( 'action', 'history' ) . "\n" .
127 xml
::dateMenu( $year, $month ) . ' ' .
128 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
129 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
133 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
138 $pager = new HistoryPager( $this, $year, $month, $tagFilter );
140 $pager->getNavigationBar() .
142 $pager->getNavigationBar()
145 wfProfileOut( __METHOD__
);
149 * Fetch an array of revisions, specified by a given limit, offset and
150 * direction. This is now only used by the feeds. It was previously
151 * used by the main UI but that's now handled by the pager.
153 function fetchRevisions($limit, $offset, $direction) {
154 $dbr = wfGetDB( DB_SLAVE
);
156 if( $direction == HistoryPage
::DIR_PREV
)
157 list($dirs, $oper) = array("ASC", ">=");
158 else /* $direction == HistoryPage::DIR_NEXT */
159 list($dirs, $oper) = array("DESC", "<=");
162 $offsets = array("rev_timestamp $oper '$offset'");
166 $page_id = $this->title
->getArticleID();
168 return $dbr->select( 'revision',
169 Revision
::selectFields(),
170 array_merge(array("rev_page=$page_id"), $offsets),
172 array( 'ORDER BY' => "rev_timestamp $dirs",
173 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
178 * Output a subscription feed listing recent edits to this page.
179 * @param string $type
181 function feed( $type ) {
182 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
183 if( !FeedUtils
::checkFeedOutput($type) ) {
187 $feed = new $wgFeedClasses[$type](
188 $this->title
->getPrefixedText() . ' - ' .
189 wfMsgForContent( 'history-feed-title' ),
190 wfMsgForContent( 'history-feed-description' ),
191 $this->title
->getFullUrl( 'action=history' ) );
193 // Get a limit on number of feed entries. Provide a sane default
194 // of 10 if none is defined (but limit to $wgFeedLimit max)
195 $limit = $wgRequest->getInt( 'limit', 10 );
196 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
199 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
203 foreach( $items as $row ) {
204 $feed->outItem( $this->feedItem( $row ) );
207 $feed->outItem( $this->feedEmpty() );
212 function feedEmpty() {
215 wfMsgForContent( 'nohistory' ),
216 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
217 $this->title
->getFullUrl(),
218 wfTimestamp( TS_MW
),
220 $this->title
->getTalkPage()->getFullUrl() );
224 * Generate a FeedItem object from a given revision table row
225 * Borrows Recent Changes' feed generation functions for formatting;
226 * includes a diff to the previous revision (if any).
231 function feedItem( $row ) {
232 $rev = new Revision( $row );
233 $rev->setTitle( $this->title
);
234 $text = FeedUtils
::formatDiffRow( $this->title
,
235 $this->title
->getPreviousRevisionID( $rev->getId() ),
237 $rev->getTimestamp(),
238 $rev->getComment() );
240 if( $rev->getComment() == '' ) {
242 $title = wfMsgForContent( 'history-feed-item-nocomment',
244 $wgContLang->timeanddate( $rev->getTimestamp() ) );
246 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
252 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
253 $rev->getTimestamp(),
255 $this->title
->getTalkPage()->getFullUrl() );
263 class HistoryPager
extends ReverseChronologicalPager
{
264 public $lastRow = false, $counter, $historyPage, $title, $buttons;
265 protected $oldIdChecked;
267 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
268 parent
::__construct();
269 $this->historyPage
= $historyPage;
270 $this->title
= $this->historyPage
->title
;
271 $this->tagFilter
= $tagFilter;
272 $this->getDateCond( $year, $month );
275 // For hook compatibility...
276 function getArticle() {
277 return $this->historyPage
->getArticle();
280 function getQueryInfo() {
282 'tables' => array('revision'),
283 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
284 'conds' => array('rev_page' => $this->historyPage
->title
->getArticleID() ),
285 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
286 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
288 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
289 $queryInfo['fields'],
291 $queryInfo['join_conds'],
292 $queryInfo['options'],
294 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
298 function getIndexField() {
299 return 'rev_timestamp';
302 function formatRow( $row ) {
303 if( $this->lastRow
) {
304 $latest = $this->counter
== 1 && $this->mIsFirst
;
305 $firstInList = $this->counter
== 1;
306 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
307 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
311 $this->lastRow
= $row;
316 * Creates begin of history list with a submit button
318 * @return string HTML output
320 function getStartBody() {
321 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut;
322 $this->lastRow
= false;
324 $this->oldIdChecked
= 0;
326 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
327 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
328 'id' => 'mw-history-compare' ) ) . "\n";
329 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
331 $this->buttons
= '<div>';
332 if( $wgUser->isAllowed('deleterevision') ) {
333 $this->buttons
.= Xml
::element( 'button',
337 'value' => 'revisiondelete',
338 'style' => 'float: right',
340 wfMsg( 'showhideselectedversions' )
343 if( $wgEnableHtmlDiff ) {
344 $this->buttons
.= Xml
::element( 'button',
347 'name' => 'htmldiff',
349 'class' => 'historysubmit',
350 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
351 'title' => wfMsg( 'tooltip-compareselectedversions' ),
353 wfMsg( 'visualcomparison')
355 $this->buttons
.= $this->submitButton( wfMsg( 'wikicodecomparison'),
357 'class' => 'historysubmit',
358 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
359 'title' => wfMsg( 'tooltip-compareselectedversions' ),
363 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
365 'class' => 'historysubmit',
366 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
367 'title' => wfMsg( 'tooltip-compareselectedversions' ),
371 $this->buttons
.= '</div>';
372 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
376 function getEndBody() {
377 if( $this->lastRow
) {
378 $latest = $this->counter
== 1 && $this->mIsFirst
;
379 $firstInList = $this->counter
== 1;
380 if( $this->mIsBackwards
) {
381 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
382 if( $this->mOffset
== '' ) {
388 # The next row is the past-the-end row
389 $next = $this->mPastTheEndRow
;
391 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
392 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
397 $s .= $this->buttons
;
403 * Creates a submit button
405 * @param array $attributes attributes
406 * @return string HTML output for the submit button
408 function submitButton($message, $attributes = array() ) {
409 # Disable submit button if history has 1 revision only
410 if( $this->getNumRows() > 1 ) {
411 return Xml
::submitButton( $message , $attributes );
418 * Returns a row from the history printout.
420 * @todo document some more, and maybe clean up the code (some params redundant?)
422 * @param Row $row The database row corresponding to the previous line.
423 * @param mixed $next The database row corresponding to the next line.
424 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
425 * @param $notificationtimestamp
426 * @param bool $latest Whether this row corresponds to the page's latest revision.
427 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
428 * @return string HTML output for the row
430 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
431 $latest = false, $firstInList = false )
433 global $wgUser, $wgLang;
434 $rev = new Revision( $row );
435 $rev->setTitle( $this->title
);
437 $curlink = $this->curLink( $rev, $latest );
438 $lastlink = $this->lastLink( $rev, $next, $counter );
439 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
440 $link = $this->revLink( $rev );
443 $s = "($curlink) ($lastlink) $diffButtons";
445 if( $wgUser->isAllowed( 'deleterevision' ) ) {
446 // If revision was hidden from sysops
447 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
448 $del = Xml
::check( 'deleterevisions', false, array('disabled' => 'disabled') );
449 $del .= Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
450 '(' . $this->historyPage
->message
['rev-delundel'] . ')' );
451 // Otherwise, show the link...
454 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
456 'type' => 'revision',
457 'target' => $this->title
->getPrefixedDbkey(),
459 $del .= $this->getSkin()->revDeleteLink( $query,
460 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
466 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
468 if( $rev->isMinor() ) {
469 $s .= ' ' . ChangesList
::flag( 'minor' );
472 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
473 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
476 $s .= $this->getSkin()->revComment( $rev, false, true );
478 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
479 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
484 if( !is_null( $next ) && is_object( $next ) ) {
485 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
486 $tools[] = '<span class="mw-rollback-link">'.
487 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
490 if( $this->title
->quickUserCan( 'edit' )
491 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
492 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
494 # Create undo tooltip for the first (=latest) line only
495 $undoTooltip = $latest
496 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
498 $undolink = $this->getSkin()->link(
500 wfMsgHtml( 'editundo' ),
502 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
503 array( 'known', 'noclasses' )
505 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
510 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
514 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
515 $classes = array_merge( $classes, $newClasses );
516 $s .= " $tagSummary";
518 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
522 $attribs['class'] = implode( ' ', $classes );
525 return Xml
::tags( 'li', $attribs, $s ) . "\n";
529 * Create a link to view this revision of the page
530 * @param Revision $rev
533 function revLink( $rev ) {
535 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
536 $date = htmlspecialchars( $date );
537 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
538 $link = $this->getSkin()->link(
542 array( 'oldid' => $rev->getId() ),
543 array( 'known', 'noclasses' )
546 $link = "<span class=\"history-deleted\">$date</span>";
552 * Create a diff-to-current link for this revision for this page
553 * @param Revision $rev
554 * @param Bool $latest, this is the latest revision of the page?
557 function curLink( $rev, $latest ) {
558 $cur = $this->historyPage
->message
['cur'];
559 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
562 return $this->getSkin()->link(
567 'diff' => $this->title
->getLatestRevID(),
568 'oldid' => $rev->getId()
570 array( 'known', 'noclasses' )
576 * Create a diff-to-previous link for this revision for this page.
577 * @param Revision $prevRev, the previous revision
578 * @param mixed $next, the newer revision
579 * @param int $counter, what row on the history list this is
582 function lastLink( $prevRev, $next, $counter ) {
583 $last = $this->historyPage
->message
['last'];
584 # $next may either be a Row, null, or "unkown"
585 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
586 if( is_null($next) ) {
587 # Probably no next row
589 } elseif( $next === 'unknown' ) {
590 # Next row probably exists but is unknown, use an oldid=prev link
591 return $this->getSkin()->link(
596 'diff' => $prevRev->getId(),
599 array( 'known', 'noclasses' )
601 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
604 return $this->getSkin()->link(
609 'diff' => $prevRev->getId(),
610 'oldid' => $next->rev_id
612 array( 'known', 'noclasses' )
618 * Create radio buttons for page history
620 * @param object $rev Revision
621 * @param bool $firstInList Is this version the first one?
622 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
623 * @return string HTML output for the radio buttons
625 function diffButtons( $rev, $firstInList, $counter ) {
626 if( $this->getNumRows() > 1 ) {
628 $radio = array( 'type' => 'radio', 'value' => $id );
629 /** @todo: move title texts to javascript */
631 $first = Xml
::element( 'input',
632 array_merge( $radio, array(
633 'style' => 'visibility:hidden',
635 'id' => 'mw-oldid-null' ) )
637 $checkmark = array( 'checked' => 'checked' );
639 # Check visibility of old revisions
640 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
641 $radio['disabled'] = 'disabled';
642 $checkmark = array(); // We will check the next possible one
643 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
644 $checkmark = array( 'checked' => 'checked' );
645 $this->oldIdChecked
= $id;
647 $checkmark = array();
649 $first = Xml
::element( 'input',
650 array_merge( $radio, $checkmark, array(
652 'id' => "mw-oldid-$id" ) ) );
653 $checkmark = array();
655 $second = Xml
::element( 'input',
656 array_merge( $radio, $checkmark, array(
658 'id' => "mw-diff-$id" ) ) );
659 return $first . $second;
667 * Backwards-compatibility aliases
669 class PageHistory
extends HistoryPage
{}
670 class PageHistoryPager
extends HistoryPager
{}