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()
225 * Generate a FeedItem object from a given revision table row
226 * Borrows Recent Changes' feed generation functions for formatting;
227 * includes a diff to the previous revision (if any).
232 function feedItem( $row ) {
233 $rev = new Revision( $row );
234 $rev->setTitle( $this->title
);
235 $text = FeedUtils
::formatDiffRow(
237 $this->title
->getPreviousRevisionID( $rev->getId() ),
239 $rev->getTimestamp(),
242 if( $rev->getComment() == '' ) {
244 $title = wfMsgForContent( 'history-feed-item-nocomment',
246 $wgContLang->timeanddate( $rev->getTimestamp() ) );
248 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
253 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
254 $rev->getTimestamp(),
256 $this->title
->getTalkPage()->getFullUrl()
265 class HistoryPager
extends ReverseChronologicalPager
{
266 public $lastRow = false, $counter, $historyPage, $title, $buttons;
267 protected $oldIdChecked;
269 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
270 parent
::__construct();
271 $this->historyPage
= $historyPage;
272 $this->title
= $this->historyPage
->title
;
273 $this->tagFilter
= $tagFilter;
274 $this->getDateCond( $year, $month );
277 // For hook compatibility...
278 function getArticle() {
279 return $this->historyPage
->getArticle();
282 function getQueryInfo() {
284 'tables' => array('revision'),
285 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
286 'conds' => array('rev_page' => $this->historyPage
->title
->getArticleID() ),
287 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
288 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
290 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
291 $queryInfo['fields'],
293 $queryInfo['join_conds'],
294 $queryInfo['options'],
296 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
300 function getIndexField() {
301 return 'rev_timestamp';
304 function formatRow( $row ) {
305 if( $this->lastRow
) {
306 $latest = ($this->counter
== 1 && $this->mIsFirst
);
307 $firstInList = $this->counter
== 1;
308 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
309 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
313 $this->lastRow
= $row;
318 * Creates begin of history list with a submit button
320 * @return string HTML output
322 function getStartBody() {
323 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut;
324 $this->lastRow
= false;
326 $this->oldIdChecked
= 0;
328 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
329 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
330 'id' => 'mw-history-compare' ) ) . "\n";
331 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
333 $this->buttons
= '<div>';
334 if( $wgUser->isAllowed('deleterevision') ) {
335 $this->buttons
.= Xml
::element( 'button',
339 'value' => 'revisiondelete',
340 'style' => 'float: right',
342 wfMsg( 'showhideselectedversions' )
345 if( $wgEnableHtmlDiff ) {
346 $this->buttons
.= Xml
::element( 'button',
349 'name' => 'htmldiff',
351 'class' => 'historysubmit',
352 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
353 'title' => wfMsg( 'tooltip-compareselectedversions' ),
355 wfMsg( 'visualcomparison')
357 $this->buttons
.= $this->submitButton( wfMsg( 'wikicodecomparison'),
359 'class' => 'historysubmit',
360 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
361 'title' => wfMsg( 'tooltip-compareselectedversions' ),
365 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
367 'class' => 'historysubmit',
368 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
369 'title' => wfMsg( 'tooltip-compareselectedversions' ),
373 $this->buttons
.= '</div>';
374 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
378 function getEndBody() {
379 if( $this->lastRow
) {
380 $latest = $this->counter
== 1 && $this->mIsFirst
;
381 $firstInList = $this->counter
== 1;
382 if( $this->mIsBackwards
) {
383 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
384 if( $this->mOffset
== '' ) {
390 # The next row is the past-the-end row
391 $next = $this->mPastTheEndRow
;
393 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
394 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
399 $s .= $this->buttons
;
405 * Creates a submit button
407 * @param array $attributes attributes
408 * @return string HTML output for the submit button
410 function submitButton($message, $attributes = array() ) {
411 # Disable submit button if history has 1 revision only
412 if( $this->getNumRows() > 1 ) {
413 return Xml
::submitButton( $message , $attributes );
420 * Returns a row from the history printout.
422 * @todo document some more, and maybe clean up the code (some params redundant?)
424 * @param Row $row The database row corresponding to the previous line.
425 * @param mixed $next The database row corresponding to the next line.
426 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
427 * @param $notificationtimestamp
428 * @param bool $latest Whether this row corresponds to the page's latest revision.
429 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
430 * @return string HTML output for the row
432 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
433 $latest = false, $firstInList = false )
435 global $wgUser, $wgLang;
436 $rev = new Revision( $row );
437 $rev->setTitle( $this->title
);
439 $curlink = $this->curLink( $rev, $latest );
440 $lastlink = $this->lastLink( $rev, $next, $counter );
441 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
442 $link = $this->revLink( $rev );
445 $s = "($curlink) ($lastlink) $diffButtons";
447 if( $wgUser->isAllowed( 'deleterevision' ) ) {
448 // If revision was hidden from sysops
449 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
450 $del = Xml
::check( 'deleterevisions', false, array('disabled' => 'disabled') );
451 // Otherwise, show the link...
454 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
460 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
462 if( $rev->isMinor() ) {
463 $s .= ' ' . ChangesList
::flag( 'minor' );
466 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
467 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
470 $s .= $this->getSkin()->revComment( $rev, false, true );
472 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
473 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
478 # Rollback and undo links
479 if( !is_null( $next ) && is_object( $next ) ) {
480 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
481 $tools[] = '<span class="mw-rollback-link">'.
482 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
485 if( $this->title
->quickUserCan( 'edit' )
486 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
487 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
489 # Create undo tooltip for the first (=latest) line only
490 $undoTooltip = $latest
491 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
493 $undolink = $this->getSkin()->link(
495 wfMsgHtml( 'editundo' ),
497 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
498 array( 'known', 'noclasses' )
500 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
505 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
509 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
510 $classes = array_merge( $classes, $newClasses );
511 $s .= " $tagSummary";
513 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
517 $attribs['class'] = implode( ' ', $classes );
520 return Xml
::tags( 'li', $attribs, $s ) . "\n";
524 * Create a link to view this revision of the page
525 * @param Revision $rev
528 function revLink( $rev ) {
530 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
531 $date = htmlspecialchars( $date );
532 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
533 $link = $this->getSkin()->link(
537 array( 'oldid' => $rev->getId() ),
538 array( 'known', 'noclasses' )
541 $link = "<span class=\"history-deleted\">$date</span>";
547 * Create a diff-to-current link for this revision for this page
548 * @param Revision $rev
549 * @param Bool $latest, this is the latest revision of the page?
552 function curLink( $rev, $latest ) {
553 $cur = $this->historyPage
->message
['cur'];
554 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
557 return $this->getSkin()->link(
562 'diff' => $this->title
->getLatestRevID(),
563 'oldid' => $rev->getId()
565 array( 'known', 'noclasses' )
571 * Create a diff-to-previous link for this revision for this page.
572 * @param Revision $prevRev, the previous revision
573 * @param mixed $next, the newer revision
574 * @param int $counter, what row on the history list this is
577 function lastLink( $prevRev, $next, $counter ) {
578 $last = $this->historyPage
->message
['last'];
579 # $next may either be a Row, null, or "unkown"
580 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
581 if( is_null($next) ) {
582 # Probably no next row
584 } elseif( $next === 'unknown' ) {
585 # Next row probably exists but is unknown, use an oldid=prev link
586 return $this->getSkin()->link(
591 'diff' => $prevRev->getId(),
594 array( 'known', 'noclasses' )
596 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
599 return $this->getSkin()->link(
604 'diff' => $prevRev->getId(),
605 'oldid' => $next->rev_id
607 array( 'known', 'noclasses' )
613 * Create radio buttons for page history
615 * @param object $rev Revision
616 * @param bool $firstInList Is this version the first one?
617 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
618 * @return string HTML output for the radio buttons
620 function diffButtons( $rev, $firstInList, $counter ) {
621 if( $this->getNumRows() > 1 ) {
623 $radio = array( 'type' => 'radio', 'value' => $id );
624 /** @todo: move title texts to javascript */
626 $first = Xml
::element( 'input',
627 array_merge( $radio, array(
628 'style' => 'visibility:hidden',
630 'id' => 'mw-oldid-null' ) )
632 $checkmark = array( 'checked' => 'checked' );
634 # Check visibility of old revisions
635 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
636 $radio['disabled'] = 'disabled';
637 $checkmark = array(); // We will check the next possible one
638 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
639 $checkmark = array( 'checked' => 'checked' );
640 $this->oldIdChecked
= $id;
642 $checkmark = array();
644 $first = Xml
::element( 'input',
645 array_merge( $radio, $checkmark, array(
647 'id' => "mw-oldid-$id" ) ) );
648 $checkmark = array();
650 $second = Xml
::element( 'input',
651 array_merge( $radio, $checkmark, array(
653 'id' => "mw-diff-$id" ) ) );
654 return $first . $second;
662 * Backwards-compatibility aliases
664 class PageHistory
extends HistoryPage
{}
665 class PageHistoryPager
extends HistoryPager
{}