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 $msgs = array( 'cur', 'last', 'pipe-separator' );
54 foreach( $msgs as $msg ) {
55 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
61 * Print the history page for an article.
66 global $wgOut, $wgRequest, $wgScript;
69 * Allow client caching.
71 if( $wgOut->checkLastModified( $this->article
->getTouched() ) )
72 return; // Client cache fresh and headers sent, nothing more to do.
74 wfProfileIn( __METHOD__
);
77 * Setup page variables.
79 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
80 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
81 $wgOut->setArticleFlag( false );
82 $wgOut->setArticleRelated( true );
83 $wgOut->setRobotPolicy( 'noindex,nofollow' );
84 $wgOut->setSyndicated( true );
85 $wgOut->setFeedAppendQuery( 'action=history' );
86 $wgOut->addScriptFile( 'history.js' );
88 $logPage = SpecialPage
::getTitleFor( 'Log' );
89 $logLink = $this->skin
->link(
91 wfMsgHtml( 'viewpagelogs' ),
93 array( 'page' => $this->title
->getPrefixedText() ),
94 array( 'known', 'noclasses' )
96 $wgOut->setSubtitle( $logLink );
98 $feedType = $wgRequest->getVal( 'feed' );
100 wfProfileOut( __METHOD__
);
101 return $this->feed( $feedType );
105 * Fail if article doesn't exist.
107 if( !$this->title
->exists() ) {
108 $wgOut->addWikiMsg( 'nohistory' );
109 # show deletion/move log if there is an entry
110 LogEventsList
::showLogExtract(
112 array( 'delete', 'move' ),
113 $this->title
->getPrefixedText(),
116 'conds' => array( "log_action != 'revision'" ),
117 'showIfEmpty' => false,
118 'msgKey' => array( 'moveddeleted-notice' )
121 wfProfileOut( __METHOD__
);
126 * Add date selector to quickly get to a certain time
128 $year = $wgRequest->getInt( 'year' );
129 $month = $wgRequest->getInt( 'month' );
130 $tagFilter = $wgRequest->getVal( 'tagfilter' );
131 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
133 * Option to show only revisions that have been (partially) hidden via RevisionDelete
134 * Note that this can run a *long* time if there are many revisions to look at.
135 * We use "isBigDeletion" to determine if the history is too big to go through.
136 * Additionally, only users with 'deleterevision' right can filter for deleted edits.
138 if ( $this->title
->userCan( 'deleterevision' ) && ( !$this->article
->isBigDeletion() ||
$this->title
->userCan( 'bigdelete' ) ) ) {
139 $conds = ( $wgRequest->getBool( 'deleted' ) ) ?
array("rev_deleted != '0'") : array();
140 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ), 'deleted', '', $wgRequest->getBool( 'deleted' ) ) . "\n";
142 else { # Don't filter and don't add the checkbox for filtering
147 $action = htmlspecialchars( $wgScript );
149 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
151 wfMsg( 'history-fieldset-title' ),
153 array( 'id' => 'mw-history-search' )
155 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
156 Xml
::hidden( 'action', 'history' ) . "\n" .
157 Xml
::dateMenu( $year, $month ) . ' ' .
158 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
160 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
164 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
169 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
171 $pager->getNavigationBar() .
173 $pager->getNavigationBar()
176 wfProfileOut( __METHOD__
);
180 * Fetch an array of revisions, specified by a given limit, offset and
181 * direction. This is now only used by the feeds. It was previously
182 * used by the main UI but that's now handled by the pager.
184 function fetchRevisions($limit, $offset, $direction) {
185 $dbr = wfGetDB( DB_SLAVE
);
187 if( $direction == HistoryPage
::DIR_PREV
)
188 list($dirs, $oper) = array("ASC", ">=");
189 else /* $direction == HistoryPage::DIR_NEXT */
190 list($dirs, $oper) = array("DESC", "<=");
193 $offsets = array("rev_timestamp $oper '$offset'");
197 $page_id = $this->title
->getArticleID();
199 return $dbr->select( 'revision',
200 Revision
::selectFields(),
201 array_merge(array("rev_page=$page_id"), $offsets),
203 array( 'ORDER BY' => "rev_timestamp $dirs",
204 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
209 * Output a subscription feed listing recent edits to this page.
210 * @param string $type
212 function feed( $type ) {
213 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
214 if( !FeedUtils
::checkFeedOutput($type) ) {
218 $feed = new $wgFeedClasses[$type](
219 $this->title
->getPrefixedText() . ' - ' .
220 wfMsgForContent( 'history-feed-title' ),
221 wfMsgForContent( 'history-feed-description' ),
222 $this->title
->getFullUrl( 'action=history' )
225 // Get a limit on number of feed entries. Provide a sane default
226 // of 10 if none is defined (but limit to $wgFeedLimit max)
227 $limit = $wgRequest->getInt( 'limit', 10 );
228 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
231 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
235 foreach( $items as $row ) {
236 $feed->outItem( $this->feedItem( $row ) );
239 $feed->outItem( $this->feedEmpty() );
244 function feedEmpty() {
247 wfMsgForContent( 'nohistory' ),
248 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
249 $this->title
->getFullUrl(),
250 wfTimestamp( TS_MW
),
252 $this->title
->getTalkPage()->getFullUrl()
257 * Generate a FeedItem object from a given revision table row
258 * Borrows Recent Changes' feed generation functions for formatting;
259 * includes a diff to the previous revision (if any).
264 function feedItem( $row ) {
265 $rev = new Revision( $row );
266 $rev->setTitle( $this->title
);
267 $text = FeedUtils
::formatDiffRow(
269 $this->title
->getPreviousRevisionID( $rev->getId() ),
271 $rev->getTimestamp(),
274 if( $rev->getComment() == '' ) {
276 $title = wfMsgForContent( 'history-feed-item-nocomment',
278 $wgContLang->timeanddate( $rev->getTimestamp() ),
279 $wgContLang->date( $rev->getTimestamp() ),
280 $wgContLang->time( $rev->getTimestamp() )
283 $title = $rev->getUserText() .
284 wfMsgForContent( 'colon-separator' ) .
285 FeedItem
::stripComment( $rev->getComment() );
290 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
291 $rev->getTimestamp(),
293 $this->title
->getTalkPage()->getFullUrl()
301 class HistoryPager
extends ReverseChronologicalPager
{
302 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
303 protected $oldIdChecked;
305 function __construct( $historyPage, $year='', $month='', $tagFilter = '', $conds = array() ) {
306 parent
::__construct();
307 $this->historyPage
= $historyPage;
308 $this->title
= $this->historyPage
->title
;
309 $this->tagFilter
= $tagFilter;
310 $this->getDateCond( $year, $month );
311 $this->conds
= $conds;
314 // For hook compatibility...
315 function getArticle() {
316 return $this->historyPage
->getArticle();
319 function getQueryInfo() {
321 'tables' => array('revision'),
322 'fields' => Revision
::selectFields(),
323 'conds' => array_merge( array('rev_page' => $this->historyPage
->title
->getArticleID() ), $this->conds
),
324 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
325 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
327 ChangeTags
::modifyDisplayQuery(
328 $queryInfo['tables'],
329 $queryInfo['fields'],
331 $queryInfo['join_conds'],
332 $queryInfo['options'],
335 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
339 function getIndexField() {
340 return 'rev_timestamp';
343 function formatRow( $row ) {
344 if( $this->lastRow
) {
345 $latest = ($this->counter
== 1 && $this->mIsFirst
);
346 $firstInList = $this->counter
== 1;
347 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
348 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
352 $this->lastRow
= $row;
357 * Creates begin of history list with a submit button
359 * @return string HTML output
361 function getStartBody() {
362 global $wgScript, $wgUser, $wgOut, $wgContLang;
363 $this->lastRow
= false;
365 $this->oldIdChecked
= 0;
367 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
368 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
369 'id' => 'mw-history-compare' ) ) . "\n";
370 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
371 $s .= Xml
::hidden( 'action', 'historysubmit' ) . "\n";
373 $this->buttons
= '<div>';
374 if( $wgUser->isAllowed('deleterevision') ) {
375 $float = $wgContLang->alignEnd();
376 # Note bug #20966, <button> is non-standard in IE<8
377 $this->buttons
.= Xml
::element( 'button',
380 'name' => 'revisiondelete',
382 'style' => "float: $float;",
383 'class' => 'mw-history-revisiondelete-button',
385 wfMsg( 'showhideselectedversions' )
388 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
390 'class' => 'historysubmit',
391 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
392 'title' => wfMsg( 'tooltip-compareselectedversions' ),
395 $this->buttons
.= '</div>';
396 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
400 function getEndBody() {
401 if( $this->lastRow
) {
402 $latest = $this->counter
== 1 && $this->mIsFirst
;
403 $firstInList = $this->counter
== 1;
404 if( $this->mIsBackwards
) {
405 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
406 if( $this->mOffset
== '' ) {
412 # The next row is the past-the-end row
413 $next = $this->mPastTheEndRow
;
415 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
416 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
421 # Add second buttons only if there is more than one rev
422 if( $this->getNumRows() > 2 ) {
423 $s .= $this->buttons
;
430 * Creates a submit button
432 * @param array $attributes attributes
433 * @return string HTML output for the submit button
435 function submitButton($message, $attributes = array() ) {
436 # Disable submit button if history has 1 revision only
437 if( $this->getNumRows() > 1 ) {
438 return Xml
::submitButton( $message , $attributes );
445 * Returns a row from the history printout.
447 * @todo document some more, and maybe clean up the code (some params redundant?)
449 * @param Row $row The database row corresponding to the previous line.
450 * @param mixed $next The database row corresponding to the next line.
451 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
452 * @param $notificationtimestamp
453 * @param bool $latest Whether this row corresponds to the page's latest revision.
454 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
455 * @return string HTML output for the row
457 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
458 $latest = false, $firstInList = false )
460 global $wgUser, $wgLang;
461 $rev = new Revision( $row );
462 $rev->setTitle( $this->title
);
464 $curlink = $this->curLink( $rev, $latest );
465 $lastlink = $this->lastLink( $rev, $next, $counter );
466 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
467 $histLinks = Html
::rawElement(
469 array( 'class' => 'mw-history-histlinks' ),
470 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
472 $s = $histLinks . $diffButtons;
474 $link = $this->revLink( $rev );
478 // User can delete revisions...
479 if( $wgUser->isAllowed( 'deleterevision' ) ) {
480 // If revision was hidden from sysops, disable the checkbox
481 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
482 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
483 // Otherwise, enable the checkbox...
485 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => 'ids['.$rev->getId().']' ) );
487 // User can only view deleted revisions...
488 } else if( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
489 // If revision was hidden from sysops, disable the link
490 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
491 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
492 // Otherwise, show the link...
494 $query = array( 'type' => 'revision',
495 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
496 $del .= $this->getSkin()->revDeleteLink( $query,
497 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
500 if( $del ) $s .= " $del ";
503 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
505 if( $rev->isMinor() ) {
506 $s .= ' ' . ChangesList
::flag( 'minor' );
509 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
510 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
513 $s .= $this->getSkin()->revComment( $rev, false, true );
515 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
516 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
521 # Rollback and undo links
522 if( !is_null( $next ) && is_object( $next ) ) {
523 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
524 $tools[] = '<span class="mw-rollback-link">'.
525 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
528 if( $this->title
->quickUserCan( 'edit' )
529 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
530 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
532 # Create undo tooltip for the first (=latest) line only
533 $undoTooltip = $latest
534 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
536 $undolink = $this->getSkin()->link(
538 wfMsgHtml( 'editundo' ),
542 'undoafter' => $next->rev_id
,
543 'undo' => $rev->getId()
545 array( 'known', 'noclasses' )
547 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
552 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
556 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
557 $classes = array_merge( $classes, $newClasses );
558 $s .= " $tagSummary";
560 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
564 $attribs['class'] = implode( ' ', $classes );
567 return Xml
::tags( 'li', $attribs, $s ) . "\n";
571 * Create a link to view this revision of the page
572 * @param Revision $rev
575 function revLink( $rev ) {
577 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
578 $date = htmlspecialchars( $date );
579 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
580 $link = $this->getSkin()->link(
584 array( 'oldid' => $rev->getId() ),
585 array( 'known', 'noclasses' )
588 $link = "<span class=\"history-deleted\">$date</span>";
594 * Create a diff-to-current link for this revision for this page
595 * @param Revision $rev
596 * @param Bool $latest, this is the latest revision of the page?
599 function curLink( $rev, $latest ) {
600 $cur = $this->historyPage
->message
['cur'];
601 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
604 return $this->getSkin()->link(
609 'diff' => $this->title
->getLatestRevID(),
610 'oldid' => $rev->getId()
612 array( 'known', 'noclasses' )
618 * Create a diff-to-previous link for this revision for this page.
619 * @param Revision $prevRev, the previous revision
620 * @param mixed $next, the newer revision
621 * @param int $counter, what row on the history list this is
624 function lastLink( $prevRev, $next, $counter ) {
625 $last = $this->historyPage
->message
['last'];
626 # $next may either be a Row, null, or "unkown"
627 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
628 if( is_null($next) ) {
629 # Probably no next row
631 } elseif( $next === 'unknown' ) {
632 # Next row probably exists but is unknown, use an oldid=prev link
633 return $this->getSkin()->link(
638 'diff' => $prevRev->getId(),
641 array( 'known', 'noclasses' )
643 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
646 return $this->getSkin()->link(
651 'diff' => $prevRev->getId(),
652 'oldid' => $next->rev_id
654 array( 'known', 'noclasses' )
660 * Create radio buttons for page history
662 * @param object $rev Revision
663 * @param bool $firstInList Is this version the first one?
664 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
665 * @return string HTML output for the radio buttons
667 function diffButtons( $rev, $firstInList, $counter ) {
668 if( $this->getNumRows() > 1 ) {
670 $radio = array( 'type' => 'radio', 'value' => $id );
671 /** @todo: move title texts to javascript */
673 $first = Xml
::element( 'input',
674 array_merge( $radio, array(
675 'style' => 'visibility:hidden',
677 'id' => 'mw-oldid-null' ) )
679 $checkmark = array( 'checked' => 'checked' );
681 # Check visibility of old revisions
682 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
683 $radio['disabled'] = 'disabled';
684 $checkmark = array(); // We will check the next possible one
685 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
686 $checkmark = array( 'checked' => 'checked' );
687 $this->oldIdChecked
= $id;
689 $checkmark = array();
691 $first = Xml
::element( 'input',
692 array_merge( $radio, $checkmark, array(
694 'id' => "mw-oldid-$id" ) ) );
695 $checkmark = array();
697 $second = Xml
::element( 'input',
698 array_merge( $radio, $checkmark, array(
700 'id' => "mw-diff-$id" ) ) );
701 return $first . $second;
709 * Backwards-compatibility aliases
711 class PageHistory
extends HistoryPage
{}
712 class PageHistoryPager
extends HistoryPager
{}