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
29 function __construct( $article ) {
31 $this->article
= $article;
32 $this->title
= $article->getTitle();
33 $this->skin
= $wgUser->getSkin();
34 $this->preCacheMessages();
37 function getArticle() {
38 return $this->article
;
46 * As we use the same small set of messages in various methods and that
47 * they are called often, we call them once and save them in $this->message
49 function preCacheMessages() {
50 // Precache various messages
51 if( !isset( $this->message
) ) {
52 $msgs = array( 'cur', 'last', 'pipe-separator' );
53 foreach( $msgs as $msg ) {
54 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
60 * Print the history page for an article.
64 global $wgOut, $wgRequest, $wgScript;
67 * Allow client caching.
69 if( $wgOut->checkLastModified( $this->article
->getTouched() ) )
70 return; // Client cache fresh and headers sent, nothing more to do.
72 wfProfileIn( __METHOD__
);
75 * Setup page variables.
77 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
78 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
79 $wgOut->setArticleFlag( false );
80 $wgOut->setArticleRelated( true );
81 $wgOut->setRobotPolicy( 'noindex,nofollow' );
82 $wgOut->setSyndicated( true );
83 $wgOut->setFeedAppendQuery( 'action=history' );
84 $wgOut->addScriptFile( 'history.js' );
86 $logPage = SpecialPage
::getTitleFor( 'Log' );
87 $logLink = $this->skin
->link(
89 wfMsgHtml( 'viewpagelogs' ),
91 array( 'page' => $this->title
->getPrefixedText() ),
92 array( 'known', 'noclasses' )
94 $wgOut->setSubtitle( $logLink );
96 $feedType = $wgRequest->getVal( 'feed' );
98 wfProfileOut( __METHOD__
);
99 return $this->feed( $feedType );
103 * Fail if article doesn't exist.
105 if( !$this->title
->exists() ) {
106 $wgOut->addWikiMsg( 'nohistory' );
107 # show deletion/move log if there is an entry
108 LogEventsList
::showLogExtract(
110 array( 'delete', 'move' ),
111 $this->title
->getPrefixedText(),
114 'conds' => array( "log_action != 'revision'" ),
115 'showIfEmpty' => false,
116 'msgKey' => array( 'moveddeleted-notice' )
119 wfProfileOut( __METHOD__
);
124 * Add date selector to quickly get to a certain time
126 $year = $wgRequest->getInt( 'year' );
127 $month = $wgRequest->getInt( 'month' );
128 $tagFilter = $wgRequest->getVal( 'tagfilter' );
129 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
131 * Option to show only revisions that have been (partially) hidden via RevisionDelete
132 * Note that this can run a *long* time if there are many revisions to look at.
133 * We use "isBigDeletion" to determine if the history is too big to go through.
134 * Additionally, only users with 'deleterevision' right can filter for deleted edits.
136 if ( $this->title
->userCan( 'deleterevision' )
137 && ( !$this->article
->isBigDeletion() ||
$this->title
->userCan( 'bigdelete' ) ) )
139 $conds = ( $wgRequest->getBool( 'deleted' ) )
140 ?
array("rev_deleted != '0'")
142 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
143 'deleted', 'mw-show-deleted-only', $wgRequest->getBool( 'deleted' ) ) . "\n";
145 else { # Don't filter and don't add the checkbox for filtering
150 $action = htmlspecialchars( $wgScript );
152 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
154 wfMsg( 'history-fieldset-title' ),
156 array( 'id' => 'mw-history-search' )
158 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
159 Xml
::hidden( 'action', 'history' ) . "\n" .
160 Xml
::dateMenu( $year, $month ) . ' ' .
161 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
163 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
167 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
172 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
174 $pager->getNavigationBar() .
176 $pager->getNavigationBar()
179 wfProfileOut( __METHOD__
);
183 * Fetch an array of revisions, specified by a given limit, offset and
184 * direction. This is now only used by the feeds. It was previously
185 * used by the main UI but that's now handled by the pager.
187 * @param $limit Integer: the limit number of revisions to get
188 * @param $offset Integer
189 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
190 * @return ResultWrapper
192 function fetchRevisions( $limit, $offset, $direction ) {
193 $dbr = wfGetDB( DB_SLAVE
);
195 if( $direction == HistoryPage
::DIR_PREV
)
196 list($dirs, $oper) = array("ASC", ">=");
197 else /* $direction == HistoryPage::DIR_NEXT */
198 list($dirs, $oper) = array("DESC", "<=");
201 $offsets = array("rev_timestamp $oper '$offset'");
205 $page_id = $this->title
->getArticleID();
207 return $dbr->select( 'revision',
208 Revision
::selectFields(),
209 array_merge(array("rev_page=$page_id"), $offsets),
211 array( 'ORDER BY' => "rev_timestamp $dirs",
212 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
217 * Output a subscription feed listing recent edits to this page.
219 * @param $type String: feed type
221 function feed( $type ) {
222 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
223 if( !FeedUtils
::checkFeedOutput($type) ) {
227 $feed = new $wgFeedClasses[$type](
228 $this->title
->getPrefixedText() . ' - ' .
229 wfMsgForContent( 'history-feed-title' ),
230 wfMsgForContent( 'history-feed-description' ),
231 $this->title
->getFullUrl( 'action=history' )
234 // Get a limit on number of feed entries. Provide a sane default
235 // of 10 if none is defined (but limit to $wgFeedLimit max)
236 $limit = $wgRequest->getInt( 'limit', 10 );
237 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
240 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
244 foreach( $items as $row ) {
245 $feed->outItem( $this->feedItem( $row ) );
248 $feed->outItem( $this->feedEmpty() );
253 function feedEmpty() {
256 wfMsgForContent( 'nohistory' ),
257 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
258 $this->title
->getFullUrl(),
259 wfTimestamp( TS_MW
),
261 $this->title
->getTalkPage()->getFullUrl()
266 * Generate a FeedItem object from a given revision table row
267 * Borrows Recent Changes' feed generation functions for formatting;
268 * includes a diff to the previous revision (if any).
270 * @param $row Object: database row
273 function feedItem( $row ) {
274 $rev = new Revision( $row );
275 $rev->setTitle( $this->title
);
276 $text = FeedUtils
::formatDiffRow(
278 $this->title
->getPreviousRevisionID( $rev->getId() ),
280 $rev->getTimestamp(),
283 if( $rev->getComment() == '' ) {
285 $title = wfMsgForContent( 'history-feed-item-nocomment',
287 $wgContLang->timeanddate( $rev->getTimestamp() ),
288 $wgContLang->date( $rev->getTimestamp() ),
289 $wgContLang->time( $rev->getTimestamp() )
292 $title = $rev->getUserText() .
293 wfMsgForContent( 'colon-separator' ) .
294 FeedItem
::stripComment( $rev->getComment() );
299 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
300 $rev->getTimestamp(),
302 $this->title
->getTalkPage()->getFullUrl()
310 class HistoryPager
extends ReverseChronologicalPager
{
311 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
312 protected $oldIdChecked;
314 function __construct( $historyPage, $year='', $month='', $tagFilter = '', $conds = array() ) {
315 parent
::__construct();
316 $this->historyPage
= $historyPage;
317 $this->title
= $this->historyPage
->title
;
318 $this->tagFilter
= $tagFilter;
319 $this->getDateCond( $year, $month );
320 $this->conds
= $conds;
323 // For hook compatibility...
324 function getArticle() {
325 return $this->historyPage
->getArticle();
328 function getQueryInfo() {
330 'tables' => array('revision'),
331 'fields' => Revision
::selectFields(),
332 'conds' => array_merge(
333 array( 'rev_page' => $this->historyPage
->title
->getArticleID() ),
335 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
336 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
338 ChangeTags
::modifyDisplayQuery(
339 $queryInfo['tables'],
340 $queryInfo['fields'],
342 $queryInfo['join_conds'],
343 $queryInfo['options'],
346 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
350 function getIndexField() {
351 return 'rev_timestamp';
354 function formatRow( $row ) {
355 if( $this->lastRow
) {
356 $latest = ($this->counter
== 1 && $this->mIsFirst
);
357 $firstInList = $this->counter
== 1;
358 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
359 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
363 $this->lastRow
= $row;
368 * Creates begin of history list with a submit button
370 * @return string HTML output
372 function getStartBody() {
373 global $wgScript, $wgUser, $wgOut, $wgContLang;
374 $this->lastRow
= false;
376 $this->oldIdChecked
= 0;
378 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
379 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
380 'id' => 'mw-history-compare' ) ) . "\n";
381 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
382 $s .= Xml
::hidden( 'action', 'historysubmit' ) . "\n";
384 $this->buttons
= '<div>';
385 if( $wgUser->isAllowed('deleterevision') ) {
386 $float = $wgContLang->alignEnd();
387 # Note bug #20966, <button> is non-standard in IE<8
388 $this->buttons
.= Xml
::element( 'button',
391 'name' => 'revisiondelete',
393 'style' => "float: $float;",
394 'class' => 'mw-history-revisiondelete-button',
396 wfMsg( 'showhideselectedversions' )
399 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
401 'class' => 'historysubmit',
402 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
403 'title' => wfMsg( 'tooltip-compareselectedversions' ),
406 $this->buttons
.= '</div>';
407 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
411 function getEndBody() {
412 if( $this->lastRow
) {
413 $latest = $this->counter
== 1 && $this->mIsFirst
;
414 $firstInList = $this->counter
== 1;
415 if( $this->mIsBackwards
) {
416 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
417 if( $this->mOffset
== '' ) {
423 # The next row is the past-the-end row
424 $next = $this->mPastTheEndRow
;
426 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
427 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
432 # Add second buttons only if there is more than one rev
433 if( $this->getNumRows() > 2 ) {
434 $s .= $this->buttons
;
441 * Creates a submit button
443 * @param $message String: text of the submit button, will be escaped
444 * @param $attributes Array: attributes
445 * @return String: HTML output for the submit button
447 function submitButton( $message, $attributes = array() ) {
448 # Disable submit button if history has 1 revision only
449 if( $this->getNumRows() > 1 ) {
450 return Xml
::submitButton( $message , $attributes );
457 * Returns a row from the history printout.
459 * @todo document some more, and maybe clean up the code (some params redundant?)
461 * @param $row Object: the database row corresponding to the previous line.
462 * @param $next Mixed: the database row corresponding to the next line.
463 * @param $counter Integer: apparently a counter of what row number we're at, counted from the top row = 1.
464 * @param $notificationtimestamp
465 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
466 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
467 * @return String: HTML output for the row
469 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
470 $latest = false, $firstInList = false )
472 global $wgUser, $wgLang;
473 $rev = new Revision( $row );
474 $rev->setTitle( $this->title
);
476 $curlink = $this->curLink( $rev, $latest );
477 $lastlink = $this->lastLink( $rev, $next, $counter );
478 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
479 $histLinks = Html
::rawElement(
481 array( 'class' => 'mw-history-histlinks' ),
482 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
484 $s = $histLinks . $diffButtons;
486 $link = $this->revLink( $rev );
490 // User can delete revisions...
491 if( $wgUser->isAllowed( 'deleterevision' ) ) {
492 // If revision was hidden from sysops, disable the checkbox
493 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
494 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
495 // Otherwise, enable the checkbox...
497 $del = Xml
::check( 'showhiderevisions', false,
498 array( 'name' => 'ids['.$rev->getId().']' ) );
500 // User can only view deleted revisions...
501 } else if( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
502 // If revision was hidden from sysops, disable the link
503 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
504 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
505 // Otherwise, show the link...
507 $query = array( 'type' => 'revision',
508 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
509 $del .= $this->getSkin()->revDeleteLink( $query,
510 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
513 if( $del ) $s .= " $del ";
516 $s .= " <span class='history-user'>" .
517 $this->getSkin()->revUserTools( $rev, true ) . "</span>";
519 if( $rev->isMinor() ) {
520 $s .= ' ' . ChangesList
::flag( 'minor' );
523 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
524 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
527 $s .= $this->getSkin()->revComment( $rev, false, true );
529 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
530 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
535 # Rollback and undo links
536 if( !is_null( $next ) && is_object( $next ) ) {
537 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
538 $tools[] = '<span class="mw-rollback-link">'.
539 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
542 if( $this->title
->quickUserCan( 'edit' )
543 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
544 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
546 # Create undo tooltip for the first (=latest) line only
547 $undoTooltip = $latest
548 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
550 $undolink = $this->getSkin()->link(
552 wfMsgHtml( 'editundo' ),
556 'undoafter' => $next->rev_id
,
557 'undo' => $rev->getId()
559 array( 'known', 'noclasses' )
561 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
566 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
570 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
571 $classes = array_merge( $classes, $newClasses );
572 $s .= " $tagSummary";
574 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
578 $attribs['class'] = implode( ' ', $classes );
581 return Xml
::tags( 'li', $attribs, $s ) . "\n";
585 * Create a link to view this revision of the page
587 * @param $rev Revision
590 function revLink( $rev ) {
592 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
593 $date = htmlspecialchars( $date );
594 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
595 $link = $this->getSkin()->link(
599 array( 'oldid' => $rev->getId() ),
600 array( 'known', 'noclasses' )
603 $link = "<span class=\"history-deleted\">$date</span>";
609 * Create a diff-to-current link for this revision for this page
611 * @param $rev Revision
612 * @param $latest Boolean: this is the latest revision of the page?
615 function curLink( $rev, $latest ) {
616 $cur = $this->historyPage
->message
['cur'];
617 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
620 return $this->getSkin()->link(
625 'diff' => $this->title
->getLatestRevID(),
626 'oldid' => $rev->getId()
628 array( 'known', 'noclasses' )
634 * Create a diff-to-previous link for this revision for this page.
636 * @param $prevRev Revision: the previous revision
637 * @param $next Mixed: the newer revision
638 * @param $counter Integer: what row on the history list this is
641 function lastLink( $prevRev, $next, $counter ) {
642 $last = $this->historyPage
->message
['last'];
643 # $next may either be a Row, null, or "unkown"
644 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
645 if( is_null($next) ) {
646 # Probably no next row
648 } elseif( $next === 'unknown' ) {
649 # Next row probably exists but is unknown, use an oldid=prev link
650 return $this->getSkin()->link(
655 'diff' => $prevRev->getId(),
658 array( 'known', 'noclasses' )
660 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
)
661 ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) )
665 return $this->getSkin()->link(
670 'diff' => $prevRev->getId(),
671 'oldid' => $next->rev_id
673 array( 'known', 'noclasses' )
679 * Create radio buttons for page history
681 * @param $rev Revision object
682 * @param $firstInList Boolean: is this version the first one?
683 * @param $counter Integer: a counter of what row number we're at, counted from the top row = 1.
684 * @return String: HTML output for the radio buttons
686 function diffButtons( $rev, $firstInList, $counter ) {
687 if( $this->getNumRows() > 1 ) {
689 $radio = array( 'type' => 'radio', 'value' => $id );
690 /** @todo: move title texts to javascript */
692 $first = Xml
::element( 'input',
693 array_merge( $radio, array(
694 'style' => 'visibility:hidden',
696 'id' => 'mw-oldid-null' ) )
698 $checkmark = array( 'checked' => 'checked' );
700 # Check visibility of old revisions
701 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
702 $radio['disabled'] = 'disabled';
703 $checkmark = array(); // We will check the next possible one
704 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
705 $checkmark = array( 'checked' => 'checked' );
706 $this->oldIdChecked
= $id;
708 $checkmark = array();
710 $first = Xml
::element( 'input',
711 array_merge( $radio, $checkmark, array(
713 'id' => "mw-oldid-$id" ) ) );
714 $checkmark = array();
716 $second = Xml
::element( 'input',
717 array_merge( $radio, $checkmark, array(
719 'id' => "mw-diff-$id" ) ) );
720 return $first . $second;
728 * Backwards-compatibility aliases
730 class PageHistory
extends HistoryPage
{}
731 class PageHistoryPager
extends HistoryPager
{}