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 /** Contains the Article object. Passed on construction. */
24 /** The $article title object. Found on construction. */
26 /** Shortcut to the user Skin object. */
30 * Construct a new HistoryPage.
32 * @param $article Article
34 function __construct( $article ) {
36 $this->article
= $article;
37 $this->title
= $article->getTitle();
38 $this->skin
= $wgUser->getSkin();
39 $this->preCacheMessages();
42 /** Get the Article object we are working on. */
43 public function getArticle() {
44 return $this->article
;
47 /** Get the Title object. */
48 public function getTitle() {
53 * As we use the same small set of messages in various methods and that
54 * they are called often, we call them once and save them in $this->message
56 private function preCacheMessages() {
57 // Precache various messages
58 if ( !isset( $this->message
) ) {
59 $msgs = array( 'cur', 'last', 'pipe-separator' );
60 foreach ( $msgs as $msg ) {
61 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
67 * Print the history page for an article.
71 global $wgOut, $wgRequest, $wgScript;
74 * Allow client caching.
76 if ( $wgOut->checkLastModified( $this->article
->getTouched() ) )
77 return; // Client cache fresh and headers sent, nothing more to do.
79 wfProfileIn( __METHOD__
);
81 // Setup page variables.
82 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title
->getPrefixedText() ) );
83 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
84 $wgOut->setArticleFlag( false );
85 $wgOut->setArticleRelated( true );
86 $wgOut->setRobotPolicy( 'noindex,nofollow' );
87 $wgOut->setSyndicated( true );
88 $wgOut->setFeedAppendQuery( 'action=history' );
89 $wgOut->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
91 // Creation of a subtitle link pointing to [[Special:Log]]
92 $logPage = SpecialPage
::getTitleFor( 'Log' );
93 $logLink = $this->skin
->link(
95 wfMsgHtml( 'viewpagelogs' ),
97 array( 'page' => $this->title
->getPrefixedText() ),
98 array( 'known', 'noclasses' )
100 $wgOut->setSubtitle( $logLink );
102 // Handle atom/RSS feeds.
103 $feedType = $wgRequest->getVal( 'feed' );
105 wfProfileOut( __METHOD__
);
106 return $this->feed( $feedType );
109 // Fail nicely if article doesn't exist.
110 if ( !$this->title
->exists() ) {
111 $wgOut->addWikiMsg( 'nohistory' );
112 # show deletion/move log if there is an entry
113 LogEventsList
::showLogExtract(
115 array( 'delete', 'move' ),
116 $this->title
->getPrefixedText(),
119 'conds' => array( "log_action != 'revision'" ),
120 'showIfEmpty' => false,
121 'msgKey' => array( 'moveddeleted-notice' )
124 wfProfileOut( __METHOD__
);
129 * Add date selector to quickly get to a certain time
131 $year = $wgRequest->getInt( 'year' );
132 $month = $wgRequest->getInt( 'month' );
133 $tagFilter = $wgRequest->getVal( 'tagfilter' );
134 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
137 * Option to show only revisions that have been (partially) hidden via RevisionDelete
139 if ( $wgRequest->getBool( 'deleted' ) ) {
140 $conds = array( "rev_deleted != '0'" );
144 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
145 'deleted', 'mw-show-deleted-only', $wgRequest->getBool( 'deleted' ) ) . "\n";
147 // Add the general form
148 $action = htmlspecialchars( $wgScript );
150 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
152 wfMsg( 'history-fieldset-title' ),
154 array( 'id' => 'mw-history-search' )
156 Html
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
157 Html
::hidden( 'action', 'history' ) . "\n" .
158 Xml
::dateMenu( $year, $month ) . ' ' .
159 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
161 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
165 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
167 // Create and output the list.
168 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
170 $pager->getNavigationBar() .
172 $pager->getNavigationBar()
174 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
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 * @param $limit Integer: the limit number of revisions to get
185 * @param $offset Integer
186 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
187 * @return ResultWrapper
189 function fetchRevisions( $limit, $offset, $direction ) {
190 $dbr = wfGetDB( DB_SLAVE
);
192 if ( $direction == HistoryPage
::DIR_PREV
) {
193 list( $dirs, $oper ) = array( "ASC", ">=" );
194 } else { /* $direction == HistoryPage::DIR_NEXT */
195 list( $dirs, $oper ) = array( "DESC", "<=" );
199 $offsets = array( "rev_timestamp $oper '$offset'" );
204 $page_id = $this->title
->getArticleID();
206 return $dbr->select( 'revision',
207 Revision
::selectFields(),
208 array_merge( array( "rev_page=$page_id" ), $offsets ),
210 array( 'ORDER BY' => "rev_timestamp $dirs",
211 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
216 * Output a subscription feed listing recent edits to this page.
218 * @param $type String: feed type
220 function feed( $type ) {
221 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
222 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
226 $feed = new $wgFeedClasses[$type](
227 $this->title
->getPrefixedText() . ' - ' .
228 wfMsgForContent( 'history-feed-title' ),
229 wfMsgForContent( 'history-feed-description' ),
230 $this->title
->getFullUrl( 'action=history' )
233 // Get a limit on number of feed entries. Provide a sane default
234 // of 10 if none is defined (but limit to $wgFeedLimit max)
235 $limit = $wgRequest->getInt( 'limit', 10 );
236 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
239 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
241 // Generate feed elements enclosed between header and footer.
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;
313 protected $preventClickjacking = false;
315 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
316 parent
::__construct();
317 $this->historyPage
= $historyPage;
318 $this->title
= $this->historyPage
->getTitle();
319 $this->tagFilter
= $tagFilter;
320 $this->getDateCond( $year, $month );
321 $this->conds
= $conds;
324 // For hook compatibility...
325 function getArticle() {
326 return $this->historyPage
->getArticle();
329 function getTitle() {
333 function getSqlComment() {
334 if ( $this->conds
) {
335 return 'history page filtered'; // potentially slow, see CR r58153
337 return 'history page unfiltered';
341 function getQueryInfo() {
343 'tables' => array( 'revision' ),
344 'fields' => Revision
::selectFields(),
345 'conds' => array_merge(
346 array( 'rev_page' => $this->title
->getArticleID() ),
348 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
349 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
351 ChangeTags
::modifyDisplayQuery(
352 $queryInfo['tables'],
353 $queryInfo['fields'],
355 $queryInfo['join_conds'],
356 $queryInfo['options'],
359 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
363 function getIndexField() {
364 return 'rev_timestamp';
367 function formatRow( $row ) {
368 if ( $this->lastRow
) {
369 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
370 $firstInList = $this->counter
== 1;
372 $s = $this->historyLine( $this->lastRow
, $row,
373 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
377 $this->lastRow
= $row;
382 * Creates begin of history list with a submit button
384 * @return string HTML output
386 function getStartBody() {
387 global $wgScript, $wgUser, $wgOut;
388 $this->lastRow
= false;
390 $this->oldIdChecked
= 0;
392 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
393 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
394 'id' => 'mw-history-compare' ) ) . "\n";
395 $s .= Html
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
396 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
398 $s .= '<div>' . $this->submitButton( wfMsg( 'compareselectedversions' ),
399 array( 'class' => 'historysubmit' ) ) . "\n";
401 $this->buttons
= '<div>';
402 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions' ),
403 array( 'class' => 'historysubmit' )
404 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
407 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
408 $s .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
410 $this->buttons
.= '</div>';
411 $s .= '</div><ul id="pagehistory">' . "\n";
415 private function getRevisionButton( $name, $msg ) {
416 $this->preventClickjacking();
417 # Note bug #20966, <button> is non-standard in IE<8
418 $element = Html
::element( 'button',
423 'class' => "mw-history-$name-button mw-float-end",
427 $this->buttons
.= $element;
431 function getEndBody() {
432 if ( $this->lastRow
) {
433 $latest = $this->counter
== 1 && $this->mIsFirst
;
434 $firstInList = $this->counter
== 1;
435 if ( $this->mIsBackwards
) {
436 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
437 if ( $this->mOffset
== '' ) {
443 # The next row is the past-the-end row
444 $next = $this->mPastTheEndRow
;
447 $s = $this->historyLine( $this->lastRow
, $next,
448 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
453 # Add second buttons only if there is more than one rev
454 if ( $this->getNumRows() > 2 ) {
455 $s .= $this->buttons
;
462 * Creates a submit button
464 * @param $message String: text of the submit button, will be escaped
465 * @param $attributes Array: attributes
466 * @return String: HTML output for the submit button
468 function submitButton( $message, $attributes = array() ) {
469 # Disable submit button if history has 1 revision only
470 if ( $this->getNumRows() > 1 ) {
471 return Xml
::submitButton( $message , $attributes );
478 * Returns a row from the history printout.
480 * @todo document some more, and maybe clean up the code (some params redundant?)
482 * @param $row Object: the database row corresponding to the previous line.
483 * @param $next Mixed: the database row corresponding to the next line.
484 * @param $notificationtimestamp
485 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
486 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
487 * @return String: HTML output for the row
489 function historyLine( $row, $next, $notificationtimestamp = false,
490 $latest = false, $firstInList = false )
492 global $wgUser, $wgLang;
493 $rev = new Revision( $row );
494 $rev->setTitle( $this->title
);
496 $curlink = $this->curLink( $rev, $latest );
497 $lastlink = $this->lastLink( $rev, $next );
498 $diffButtons = $this->diffButtons( $rev, $firstInList );
499 $histLinks = Html
::rawElement(
501 array( 'class' => 'mw-history-histlinks' ),
502 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
504 $s = $histLinks . $diffButtons;
506 $link = $this->revLink( $rev );
510 // Show checkboxes for each revision
511 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
512 $this->preventClickjacking();
513 // If revision was hidden from sysops, disable the checkbox
514 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
515 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
516 // Otherwise, enable the checkbox...
518 $del = Xml
::check( 'showhiderevisions', false,
519 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
521 // User can only view deleted revisions...
522 } elseif ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
523 // If revision was hidden from sysops, disable the link
524 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
525 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
526 // Otherwise, show the link...
528 $query = array( 'type' => 'revision',
529 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
530 $del .= $this->getSkin()->revDeleteLink( $query,
531 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
538 $dirmark = $wgLang->getDirMark();
542 $s .= " <span class='history-user'>" .
543 $this->getSkin()->revUserTools( $rev, true ) . "</span>";
546 if ( $rev->isMinor() ) {
547 $s .= ' ' . ChangesList
::flag( 'minor' );
550 if ( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
551 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
554 $s .= $this->getSkin()->revComment( $rev, false, true );
556 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
557 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
562 # Rollback and undo links
563 if ( !is_null( $next ) && is_object( $next ) ) {
564 if ( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
565 $this->preventClickjacking();
566 $tools[] = '<span class="mw-rollback-link">' .
567 $this->getSkin()->buildRollbackLink( $rev ) . '</span>';
570 if ( $this->title
->quickUserCan( 'edit' )
571 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
572 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
574 # Create undo tooltip for the first (=latest) line only
575 $undoTooltip = $latest
576 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
578 $undolink = $this->getSkin()->link(
580 wfMsgHtml( 'editundo' ),
584 'undoafter' => $next->rev_id
,
585 'undo' => $rev->getId()
587 array( 'known', 'noclasses' )
589 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
594 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
598 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
599 $classes = array_merge( $classes, $newClasses );
600 $s .= " $tagSummary";
602 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
606 $attribs['class'] = implode( ' ', $classes );
609 return Xml
::tags( 'li', $attribs, $s ) . "\n";
613 * Create a link to view this revision of the page
615 * @param $rev Revision
618 function revLink( $rev ) {
620 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $rev->getTimestamp() ), true );
621 $date = htmlspecialchars( $date );
622 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
623 $link = $this->getSkin()->link(
627 array( 'oldid' => $rev->getId() ),
628 array( 'known', 'noclasses' )
633 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
634 $link = "<span class=\"history-deleted\">$link</span>";
640 * Create a diff-to-current link for this revision for this page
642 * @param $rev Revision
643 * @param $latest Boolean: this is the latest revision of the page?
646 function curLink( $rev, $latest ) {
647 $cur = $this->historyPage
->message
['cur'];
648 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
651 return $this->getSkin()->link(
656 'diff' => $this->title
->getLatestRevID(),
657 'oldid' => $rev->getId()
659 array( 'known', 'noclasses' )
665 * Create a diff-to-previous link for this revision for this page.
667 * @param $prevRev Revision: the previous revision
668 * @param $next Mixed: the newer revision
671 function lastLink( $prevRev, $next ) {
672 $last = $this->historyPage
->message
['last'];
673 # $next may either be a Row, null, or "unkown"
674 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
675 if ( is_null( $next ) ) {
676 # Probably no next row
678 } elseif ( $next === 'unknown' ) {
679 # Next row probably exists but is unknown, use an oldid=prev link
680 return $this->getSkin()->link(
685 'diff' => $prevRev->getId(),
688 array( 'known', 'noclasses' )
690 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
)
691 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
) )
695 return $this->getSkin()->link(
700 'diff' => $prevRev->getId(),
701 'oldid' => $next->rev_id
703 array( 'known', 'noclasses' )
709 * Create radio buttons for page history
711 * @param $rev Revision object
712 * @param $firstInList Boolean: is this version the first one?
714 * @return String: HTML output for the radio buttons
716 function diffButtons( $rev, $firstInList ) {
717 if ( $this->getNumRows() > 1 ) {
719 $radio = array( 'type' => 'radio', 'value' => $id );
720 /** @todo: move title texts to javascript */
721 if ( $firstInList ) {
722 $first = Xml
::element( 'input',
723 array_merge( $radio, array(
724 'style' => 'visibility:hidden',
726 'id' => 'mw-oldid-null' ) )
728 $checkmark = array( 'checked' => 'checked' );
730 # Check visibility of old revisions
731 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
732 $radio['disabled'] = 'disabled';
733 $checkmark = array(); // We will check the next possible one
734 } elseif ( !$this->oldIdChecked
) {
735 $checkmark = array( 'checked' => 'checked' );
736 $this->oldIdChecked
= $id;
738 $checkmark = array();
740 $first = Xml
::element( 'input',
741 array_merge( $radio, $checkmark, array(
743 'id' => "mw-oldid-$id" ) ) );
744 $checkmark = array();
746 $second = Xml
::element( 'input',
747 array_merge( $radio, $checkmark, array(
749 'id' => "mw-diff-$id" ) ) );
750 return $first . $second;
757 * This is called if a write operation is possible from the generated HTML
759 function preventClickjacking( $enable = true ) {
760 $this->preventClickjacking
= $enable;
764 * Get the "prevent clickjacking" flag
766 function getPreventClickjacking() {
767 return $this->preventClickjacking
;
772 * Backwards-compatibility aliases
774 class PageHistory
extends HistoryPage
{}
775 class PageHistoryPager
extends HistoryPager
{}