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. */
28 * Construct a new HistoryPage.
30 * @param $article Article
32 function __construct( $article ) {
33 $this->article
= $article;
34 $this->title
= $article->getTitle();
35 $this->preCacheMessages();
38 /** Get the Article object we are working on. */
39 public function getArticle() {
40 return $this->article
;
43 /** Get the Title object. */
44 public function getTitle() {
49 * As we use the same small set of messages in various methods and that
50 * they are called often, we call them once and save them in $this->message
52 private function preCacheMessages() {
53 // Precache various messages
54 if ( !isset( $this->message
) ) {
55 $msgs = array( 'cur', 'last', 'pipe-separator' );
56 foreach ( $msgs as $msg ) {
57 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
63 * Print the history page for an article.
67 global $wgOut, $wgRequest, $wgScript;
70 * Allow client caching.
72 if ( $wgOut->checkLastModified( $this->article
->getTouched() ) )
73 return; // Client cache fresh and headers sent, nothing more to do.
75 wfProfileIn( __METHOD__
);
77 // 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->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
87 // Creation of a subtitle link pointing to [[Special:Log]]
88 $logPage = SpecialPage
::getTitleFor( 'Log' );
89 $logLink = Linker
::linkKnown(
91 wfMsgHtml( 'viewpagelogs' ),
93 array( 'page' => $this->title
->getPrefixedText() )
95 $wgOut->setSubtitle( $logLink );
97 // Handle atom/RSS feeds.
98 $feedType = $wgRequest->getVal( 'feed' );
100 wfProfileOut( __METHOD__
);
101 return $this->feed( $feedType );
104 // Fail nicely 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 );
132 * Option to show only revisions that have been (partially) hidden via RevisionDelete
134 if ( $wgRequest->getBool( 'deleted' ) ) {
135 $conds = array( "rev_deleted != '0'" );
139 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
140 'deleted', 'mw-show-deleted-only', $wgRequest->getBool( 'deleted' ) ) . "\n";
142 // Add the general form
143 $action = htmlspecialchars( $wgScript );
145 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
147 wfMsg( 'history-fieldset-title' ),
149 array( 'id' => 'mw-history-search' )
151 Html
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
152 Html
::hidden( 'action', 'history' ) . "\n" .
153 Xml
::dateMenu( $year, $month ) . ' ' .
154 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
156 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
160 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
162 // Create and output the list.
163 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
165 $pager->getNavigationBar() .
167 $pager->getNavigationBar()
169 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
171 wfProfileOut( __METHOD__
);
175 * Fetch an array of revisions, specified by a given limit, offset and
176 * direction. This is now only used by the feeds. It was previously
177 * used by the main UI but that's now handled by the pager.
179 * @param $limit Integer: the limit number of revisions to get
180 * @param $offset Integer
181 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
182 * @return ResultWrapper
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", "<=" );
194 $offsets = array( "rev_timestamp $oper '$offset'" );
199 $page_id = $this->title
->getArticleID();
201 return $dbr->select( 'revision',
202 Revision
::selectFields(),
203 array_merge( array( "rev_page=$page_id" ), $offsets ),
205 array( 'ORDER BY' => "rev_timestamp $dirs",
206 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
211 * Output a subscription feed listing recent edits to this page.
213 * @param $type String: feed type
215 function feed( $type ) {
216 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
217 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
221 $feed = new $wgFeedClasses[$type](
222 $this->title
->getPrefixedText() . ' - ' .
223 wfMsgForContent( 'history-feed-title' ),
224 wfMsgForContent( 'history-feed-description' ),
225 $this->title
->getFullUrl( 'action=history' )
228 // Get a limit on number of feed entries. Provide a sane default
229 // of 10 if none is defined (but limit to $wgFeedLimit max)
230 $limit = $wgRequest->getInt( 'limit', 10 );
231 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
234 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
236 // Generate feed elements enclosed between header and footer.
239 foreach ( $items as $row ) {
240 $feed->outItem( $this->feedItem( $row ) );
243 $feed->outItem( $this->feedEmpty() );
248 function feedEmpty() {
251 wfMsgForContent( 'nohistory' ),
252 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
253 $this->title
->getFullUrl(),
254 wfTimestamp( TS_MW
),
256 $this->title
->getTalkPage()->getFullUrl()
261 * Generate a FeedItem object from a given revision table row
262 * Borrows Recent Changes' feed generation functions for formatting;
263 * includes a diff to the previous revision (if any).
265 * @param $row Object: database row
268 function feedItem( $row ) {
269 $rev = new Revision( $row );
270 $rev->setTitle( $this->title
);
271 $text = FeedUtils
::formatDiffRow(
273 $this->title
->getPreviousRevisionID( $rev->getId() ),
275 $rev->getTimestamp(),
278 if ( $rev->getComment() == '' ) {
280 $title = wfMsgForContent( 'history-feed-item-nocomment',
282 $wgContLang->timeanddate( $rev->getTimestamp() ),
283 $wgContLang->date( $rev->getTimestamp() ),
284 $wgContLang->time( $rev->getTimestamp() )
287 $title = $rev->getUserText() .
288 wfMsgForContent( 'colon-separator' ) .
289 FeedItem
::stripComment( $rev->getComment() );
294 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
295 $rev->getTimestamp(),
297 $this->title
->getTalkPage()->getFullUrl()
305 class HistoryPager
extends ReverseChronologicalPager
{
306 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
307 protected $oldIdChecked;
308 protected $preventClickjacking = false;
310 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
311 parent
::__construct();
312 $this->historyPage
= $historyPage;
313 $this->title
= $this->historyPage
->getTitle();
314 $this->tagFilter
= $tagFilter;
315 $this->getDateCond( $year, $month );
316 $this->conds
= $conds;
319 // For hook compatibility...
320 function getArticle() {
321 return $this->historyPage
->getArticle();
324 function getTitle() {
328 function getSqlComment() {
329 if ( $this->conds
) {
330 return 'history page filtered'; // potentially slow, see CR r58153
332 return 'history page unfiltered';
336 function getQueryInfo() {
338 'tables' => array( 'revision' ),
339 'fields' => Revision
::selectFields(),
340 'conds' => array_merge(
341 array( 'rev_page' => $this->title
->getArticleID() ),
343 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
344 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
346 ChangeTags
::modifyDisplayQuery(
347 $queryInfo['tables'],
348 $queryInfo['fields'],
350 $queryInfo['join_conds'],
351 $queryInfo['options'],
354 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
358 function getIndexField() {
359 return 'rev_timestamp';
362 function formatRow( $row ) {
363 if ( $this->lastRow
) {
364 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
365 $firstInList = $this->counter
== 1;
367 $s = $this->historyLine( $this->lastRow
, $row,
368 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
372 $this->lastRow
= $row;
377 * Creates begin of history list with a submit button
379 * @return string HTML output
381 function getStartBody() {
382 global $wgScript, $wgUser, $wgOut;
383 $this->lastRow
= false;
385 $this->oldIdChecked
= 0;
387 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
388 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
389 'id' => 'mw-history-compare' ) ) . "\n";
390 $s .= Html
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
391 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
393 $s .= '<div>' . $this->submitButton( wfMsg( 'compareselectedversions' ),
394 array( 'class' => 'historysubmit' ) ) . "\n";
396 $this->buttons
= '<div>';
397 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions' ),
398 array( 'class' => 'historysubmit' )
399 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
402 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
403 $s .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
405 $this->buttons
.= '</div>';
406 $s .= '</div><ul id="pagehistory">' . "\n";
410 private function getRevisionButton( $name, $msg ) {
411 $this->preventClickjacking();
412 # Note bug #20966, <button> is non-standard in IE<8
413 $element = Html
::element( 'button',
418 'class' => "mw-history-$name-button",
422 $this->buttons
.= $element;
426 function getEndBody() {
427 if ( $this->lastRow
) {
428 $latest = $this->counter
== 1 && $this->mIsFirst
;
429 $firstInList = $this->counter
== 1;
430 if ( $this->mIsBackwards
) {
431 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
432 if ( $this->mOffset
== '' ) {
438 # The next row is the past-the-end row
439 $next = $this->mPastTheEndRow
;
442 $s = $this->historyLine( $this->lastRow
, $next,
443 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
448 # Add second buttons only if there is more than one rev
449 if ( $this->getNumRows() > 2 ) {
450 $s .= $this->buttons
;
457 * Creates a submit button
459 * @param $message String: text of the submit button, will be escaped
460 * @param $attributes Array: attributes
461 * @return String: HTML output for the submit button
463 function submitButton( $message, $attributes = array() ) {
464 # Disable submit button if history has 1 revision only
465 if ( $this->getNumRows() > 1 ) {
466 return Xml
::submitButton( $message , $attributes );
473 * Returns a row from the history printout.
475 * @todo document some more, and maybe clean up the code (some params redundant?)
477 * @param $row Object: the database row corresponding to the previous line.
478 * @param $next Mixed: the database row corresponding to the next line.
479 * @param $notificationtimestamp
480 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
481 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
482 * @return String: HTML output for the row
484 function historyLine( $row, $next, $notificationtimestamp = false,
485 $latest = false, $firstInList = false )
487 global $wgUser, $wgLang;
488 $rev = new Revision( $row );
489 $rev->setTitle( $this->title
);
491 $curlink = $this->curLink( $rev, $latest );
492 $lastlink = $this->lastLink( $rev, $next );
493 $diffButtons = $this->diffButtons( $rev, $firstInList );
494 $histLinks = Html
::rawElement(
496 array( 'class' => 'mw-history-histlinks' ),
497 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
499 $s = $histLinks . $diffButtons;
501 $link = $this->revLink( $rev );
505 // Show checkboxes for each revision
506 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
507 $this->preventClickjacking();
508 // If revision was hidden from sysops, disable the checkbox
509 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
510 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
511 // Otherwise, enable the checkbox...
513 $del = Xml
::check( 'showhiderevisions', false,
514 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
516 // User can only view deleted revisions...
517 } elseif ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
518 // If revision was hidden from sysops, disable the link
519 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
520 $cdel = Linker
::revDeleteLinkDisabled( false );
521 // Otherwise, show the link...
523 $query = array( 'type' => 'revision',
524 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
525 $del .= Linker
::revDeleteLink( $query,
526 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
533 $dirmark = $wgLang->getDirMark();
537 $s .= " <span class='history-user'>" .
538 Linker
::revUserTools( $rev, true ) . "</span>";
541 if ( $rev->isMinor() ) {
542 $s .= ' ' . ChangesList
::flag( 'minor' );
545 if ( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
546 $s .= ' ' . Linker
::formatRevisionSize( $size );
549 $s .= Linker
::revComment( $rev, false, true );
551 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
552 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
557 # Rollback and undo links
558 if ( !is_null( $next ) && is_object( $next ) ) {
559 if ( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
560 $this->preventClickjacking();
561 $tools[] = '<span class="mw-rollback-link">' .
562 Linker
::buildRollbackLink( $rev ) . '</span>';
565 if ( $this->title
->quickUserCan( 'edit' )
566 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
567 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
569 # Create undo tooltip for the first (=latest) line only
570 $undoTooltip = $latest
571 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
573 $undolink = Linker
::linkKnown(
575 wfMsgHtml( 'editundo' ),
579 'undoafter' => $next->rev_id
,
580 'undo' => $rev->getId()
583 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
588 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
592 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
593 $classes = array_merge( $classes, $newClasses );
594 $s .= " $tagSummary";
596 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
600 $attribs['class'] = implode( ' ', $classes );
603 return Xml
::tags( 'li', $attribs, $s ) . "\n";
607 * Create a link to view this revision of the page
609 * @param $rev Revision
612 function revLink( $rev ) {
614 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $rev->getTimestamp() ), true );
615 $date = htmlspecialchars( $date );
616 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
617 $link = Linker
::linkKnown(
621 array( 'oldid' => $rev->getId() )
626 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
627 $link = "<span class=\"history-deleted\">$link</span>";
633 * Create a diff-to-current link for this revision for this page
635 * @param $rev Revision
636 * @param $latest Boolean: this is the latest revision of the page?
639 function curLink( $rev, $latest ) {
640 $cur = $this->historyPage
->message
['cur'];
641 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
644 return Linker
::linkKnown(
649 'diff' => $this->title
->getLatestRevID(),
650 'oldid' => $rev->getId()
657 * Create a diff-to-previous link for this revision for this page.
659 * @param $prevRev Revision: the previous revision
660 * @param $next Mixed: the newer revision
663 function lastLink( $prevRev, $next ) {
664 $last = $this->historyPage
->message
['last'];
665 # $next may either be a Row, null, or "unkown"
666 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
667 if ( is_null( $next ) ) {
668 # Probably no next row
670 } elseif ( $next === 'unknown' ) {
671 # Next row probably exists but is unknown, use an oldid=prev link
672 return Linker
::linkKnown(
677 'diff' => $prevRev->getId(),
681 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
)
682 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
) )
686 return Linker
::linkKnown(
691 'diff' => $prevRev->getId(),
692 'oldid' => $next->rev_id
699 * Create radio buttons for page history
701 * @param $rev Revision object
702 * @param $firstInList Boolean: is this version the first one?
704 * @return String: HTML output for the radio buttons
706 function diffButtons( $rev, $firstInList ) {
707 if ( $this->getNumRows() > 1 ) {
709 $radio = array( 'type' => 'radio', 'value' => $id );
710 /** @todo: move title texts to javascript */
711 if ( $firstInList ) {
712 $first = Xml
::element( 'input',
713 array_merge( $radio, array(
714 'style' => 'visibility:hidden',
716 'id' => 'mw-oldid-null' ) )
718 $checkmark = array( 'checked' => 'checked' );
720 # Check visibility of old revisions
721 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
722 $radio['disabled'] = 'disabled';
723 $checkmark = array(); // We will check the next possible one
724 } elseif ( !$this->oldIdChecked
) {
725 $checkmark = array( 'checked' => 'checked' );
726 $this->oldIdChecked
= $id;
728 $checkmark = array();
730 $first = Xml
::element( 'input',
731 array_merge( $radio, $checkmark, array(
733 'id' => "mw-oldid-$id" ) ) );
734 $checkmark = array();
736 $second = Xml
::element( 'input',
737 array_merge( $radio, $checkmark, array(
739 'id' => "mw-diff-$id" ) ) );
740 return $first . $second;
747 * This is called if a write operation is possible from the generated HTML
749 function preventClickjacking( $enable = true ) {
750 $this->preventClickjacking
= $enable;
754 * Get the "prevent clickjacking" flag
756 function getPreventClickjacking() {
757 return $this->preventClickjacking
;
762 * Backwards-compatibility aliases
764 class PageHistory
extends HistoryPage
{}
765 class PageHistoryPager
extends HistoryPager
{}