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
18 class HistoryPage
extends ContextSource
{
22 /** Contains the Page object. Passed on construction. */
26 * Construct a new HistoryPage.
28 * @param $article Article
30 function __construct( Page
$page, IContextSource
$context ) {
31 $this->article
= $page;
32 $this->context
= clone $context; // don't clobber the main context
33 $this->context
->setTitle( $page->getTitle() ); // must match
34 $this->preCacheMessages();
38 * Get the Article object we are working on.
41 public function getArticle() {
42 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 private 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 $wgScript, $wgUseFileCache;
65 $out = $this->getOutput();
66 $request = $this->getRequest();
69 * Allow client caching.
71 if ( $out->checkLastModified( $this->article
->getTouched() ) ) {
72 return; // Client cache fresh and headers sent, nothing more to do.
75 wfProfileIn( __METHOD__
);
77 # Fill in the file cache if not set already
78 if ( $wgUseFileCache && HTMLFileCache
::useFileCache( $this->getContext() ) ) {
79 $cache = HTMLFileCache
::newFromTitle( $this->getTitle(), 'history' );
80 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
81 ob_start( array( &$cache, 'saveToFileCache' ) );
85 // Setup page variables.
86 $out->setPageTitle( wfMsg( 'history-title', $this->getTitle()->getPrefixedText() ) );
87 $out->setPageTitleActionText( wfMsg( 'history_short' ) );
88 $out->setArticleFlag( false );
89 $out->setArticleRelated( true );
90 $out->setRobotPolicy( 'noindex,nofollow' );
91 $out->setFeedAppendQuery( 'action=history' );
92 $out->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
94 // Creation of a subtitle link pointing to [[Special:Log]]
95 $logPage = SpecialPage
::getTitleFor( 'Log' );
96 $logLink = Linker
::linkKnown(
98 wfMsgHtml( 'viewpagelogs' ),
100 array( 'page' => $this->getTitle()->getPrefixedText() )
102 $out->setSubtitle( $logLink );
104 // Handle atom/RSS feeds.
105 $feedType = $request->getVal( 'feed' );
107 wfProfileOut( __METHOD__
);
108 return $this->feed( $feedType );
111 // Fail nicely if article doesn't exist.
112 if ( !$this->getTitle()->exists() ) {
113 $out->addWikiMsg( 'nohistory' );
114 # show deletion/move log if there is an entry
115 LogEventsList
::showLogExtract(
117 array( 'delete', 'move' ),
121 'conds' => array( "log_action != 'revision'" ),
122 'showIfEmpty' => false,
123 'msgKey' => array( 'moveddeleted-notice' )
126 wfProfileOut( __METHOD__
);
131 * Add date selector to quickly get to a certain time
133 $year = $request->getInt( 'year' );
134 $month = $request->getInt( 'month' );
135 $tagFilter = $request->getVal( 'tagfilter' );
136 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
139 * Option to show only revisions that have been (partially) hidden via RevisionDelete
141 if ( $request->getBool( 'deleted' ) ) {
142 $conds = array( "rev_deleted != '0'" );
146 $checkDeleted = Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
147 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
149 // Add the general form
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 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBKey() ) . "\n" .
159 Html
::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
) );
169 // Create and output the list.
170 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
172 $pager->getNavigationBar() .
174 $pager->getNavigationBar()
176 $out->preventClickjacking( $pager->getPreventClickjacking() );
178 wfProfileOut( __METHOD__
);
182 * Fetch an array of revisions, specified by a given limit, offset and
183 * direction. This is now only used by the feeds. It was previously
184 * used by the main UI but that's now handled by the pager.
186 * @param $limit Integer: the limit number of revisions to get
187 * @param $offset Integer
188 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
189 * @return ResultWrapper
191 function fetchRevisions( $limit, $offset, $direction ) {
192 $dbr = wfGetDB( DB_SLAVE
);
194 if ( $direction == HistoryPage
::DIR_PREV
) {
195 list( $dirs, $oper ) = array( "ASC", ">=" );
196 } else { /* $direction == HistoryPage::DIR_NEXT */
197 list( $dirs, $oper ) = array( "DESC", "<=" );
201 $offsets = array( "rev_timestamp $oper '$offset'" );
206 $page_id = $this->getTitle()->getArticleID();
208 return $dbr->select( 'revision',
209 Revision
::selectFields(),
210 array_merge( array( "rev_page=$page_id" ), $offsets ),
212 array( 'ORDER BY' => "rev_timestamp $dirs",
213 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
218 * Output a subscription feed listing recent edits to this page.
220 * @param $type String: feed type
222 function feed( $type ) {
223 global $wgFeedClasses, $wgFeedLimit;
224 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
227 $request = $this->getRequest();
229 $feed = new $wgFeedClasses[$type](
230 $this->getTitle()->getPrefixedText() . ' - ' .
231 wfMsgForContent( 'history-feed-title' ),
232 wfMsgForContent( 'history-feed-description' ),
233 $this->getTitle()->getFullUrl( 'action=history' )
236 // Get a limit on number of feed entries. Provide a sane default
237 // of 10 if none is defined (but limit to $wgFeedLimit max)
238 $limit = $request->getInt( 'limit', 10 );
239 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
242 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
244 // Generate feed elements enclosed between header and footer.
247 foreach ( $items as $row ) {
248 $feed->outItem( $this->feedItem( $row ) );
251 $feed->outItem( $this->feedEmpty() );
256 function feedEmpty() {
258 wfMsgForContent( 'nohistory' ),
259 $this->getOutput()->parse( wfMsgForContent( 'history-feed-empty' ) ),
260 $this->getTitle()->getFullUrl(),
261 wfTimestamp( TS_MW
),
263 $this->getTitle()->getTalkPage()->getFullUrl()
268 * Generate a FeedItem object from a given revision table row
269 * Borrows Recent Changes' feed generation functions for formatting;
270 * includes a diff to the previous revision (if any).
272 * @param $row Object: database row
275 function feedItem( $row ) {
276 $rev = new Revision( $row );
277 $rev->setTitle( $this->getTitle() );
278 $text = FeedUtils
::formatDiffRow(
280 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
282 $rev->getTimestamp(),
285 if ( $rev->getComment() == '' ) {
287 $title = wfMsgForContent( 'history-feed-item-nocomment',
289 $wgContLang->timeanddate( $rev->getTimestamp() ),
290 $wgContLang->date( $rev->getTimestamp() ),
291 $wgContLang->time( $rev->getTimestamp() )
294 $title = $rev->getUserText() .
295 wfMsgForContent( 'colon-separator' ) .
296 FeedItem
::stripComment( $rev->getComment() );
301 $this->getTitle()->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
302 $rev->getTimestamp(),
304 $this->getTitle()->getTalkPage()->getFullUrl()
312 class HistoryPager
extends ReverseChronologicalPager
{
313 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
314 protected $oldIdChecked;
315 protected $preventClickjacking = false;
317 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
318 parent
::__construct();
319 $this->historyPage
= $historyPage;
320 $this->title
= $this->historyPage
->getTitle();
321 $this->tagFilter
= $tagFilter;
322 $this->getDateCond( $year, $month );
323 $this->conds
= $conds;
326 // For hook compatibility...
327 function getArticle() {
328 return $this->historyPage
->getArticle();
331 function getTitle() {
335 function getSqlComment() {
336 if ( $this->conds
) {
337 return 'history page filtered'; // potentially slow, see CR r58153
339 return 'history page unfiltered';
343 function getQueryInfo() {
345 'tables' => array( 'revision' ),
346 'fields' => Revision
::selectFields(),
347 'conds' => array_merge(
348 array( 'rev_page' => $this->title
->getArticleID() ),
350 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
351 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
353 ChangeTags
::modifyDisplayQuery(
354 $queryInfo['tables'],
355 $queryInfo['fields'],
357 $queryInfo['join_conds'],
358 $queryInfo['options'],
361 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
365 function getIndexField() {
366 return 'rev_timestamp';
369 function formatRow( $row ) {
370 if ( $this->lastRow
) {
371 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
372 $firstInList = $this->counter
== 1;
374 $s = $this->historyLine( $this->lastRow
, $row,
375 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
379 $this->lastRow
= $row;
384 * Creates begin of history list with a submit button
386 * @return string HTML output
388 function getStartBody() {
389 global $wgScript, $wgUser, $wgOut;
390 $this->lastRow
= false;
392 $this->oldIdChecked
= 0;
394 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
395 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
396 'id' => 'mw-history-compare' ) ) . "\n";
397 $s .= Html
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
398 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
400 $s .= '<div>' . $this->submitButton( wfMsg( 'compareselectedversions' ),
401 array( 'class' => 'historysubmit' ) ) . "\n";
403 $this->buttons
= '<div>';
404 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions' ),
405 array( 'class' => 'historysubmit' )
406 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
409 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
410 $s .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
412 $this->buttons
.= '</div>';
413 $s .= '</div><ul id="pagehistory">' . "\n";
417 private function getRevisionButton( $name, $msg ) {
418 $this->preventClickjacking();
419 # Note bug #20966, <button> is non-standard in IE<8
420 $element = Html
::element( 'button',
425 'class' => "mw-history-$name-button",
429 $this->buttons
.= $element;
433 function getEndBody() {
434 if ( $this->lastRow
) {
435 $latest = $this->counter
== 1 && $this->mIsFirst
;
436 $firstInList = $this->counter
== 1;
437 if ( $this->mIsBackwards
) {
438 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
439 if ( $this->mOffset
== '' ) {
445 # The next row is the past-the-end row
446 $next = $this->mPastTheEndRow
;
449 $s = $this->historyLine( $this->lastRow
, $next,
450 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
455 # Add second buttons only if there is more than one rev
456 if ( $this->getNumRows() > 2 ) {
457 $s .= $this->buttons
;
464 * Creates a submit button
466 * @param $message String: text of the submit button, will be escaped
467 * @param $attributes Array: attributes
468 * @return String: HTML output for the submit button
470 function submitButton( $message, $attributes = array() ) {
471 # Disable submit button if history has 1 revision only
472 if ( $this->getNumRows() > 1 ) {
473 return Xml
::submitButton( $message , $attributes );
480 * Returns a row from the history printout.
482 * @todo document some more, and maybe clean up the code (some params redundant?)
484 * @param $row Object: the database row corresponding to the previous line.
485 * @param $next Mixed: the database row corresponding to the next line.
486 * @param $notificationtimestamp
487 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
488 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
489 * @return String: HTML output for the row
491 function historyLine( $row, $next, $notificationtimestamp = false,
492 $latest = false, $firstInList = false )
494 global $wgUser, $wgLang;
495 $rev = new Revision( $row );
496 $rev->setTitle( $this->title
);
498 $curlink = $this->curLink( $rev, $latest );
499 $lastlink = $this->lastLink( $rev, $next );
500 $diffButtons = $this->diffButtons( $rev, $firstInList );
501 $histLinks = Html
::rawElement(
503 array( 'class' => 'mw-history-histlinks' ),
504 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
506 $s = $histLinks . $diffButtons;
508 $link = $this->revLink( $rev );
512 // Show checkboxes for each revision
513 if ( $wgUser->isAllowed( 'deleterevision' ) ) {
514 $this->preventClickjacking();
515 // If revision was hidden from sysops, disable the checkbox
516 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
517 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
518 // Otherwise, enable the checkbox...
520 $del = Xml
::check( 'showhiderevisions', false,
521 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
523 // User can only view deleted revisions...
524 } elseif ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
525 // If revision was hidden from sysops, disable the link
526 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
527 $cdel = Linker
::revDeleteLinkDisabled( false );
528 // Otherwise, show the link...
530 $query = array( 'type' => 'revision',
531 'target' => $this->title
->getPrefixedDbkey(), 'ids' => $rev->getId() );
532 $del .= Linker
::revDeleteLink( $query,
533 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
540 $dirmark = $wgLang->getDirMark();
544 $s .= " <span class='history-user'>" .
545 Linker
::revUserTools( $rev, true ) . "</span>";
548 if ( $rev->isMinor() ) {
549 $s .= ' ' . ChangesList
::flag( 'minor' );
552 if ( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
553 $s .= ' ' . Linker
::formatRevisionSize( $size );
556 $s .= Linker
::revComment( $rev, false, true );
558 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
559 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
564 # Rollback and undo links
565 if ( !is_null( $next ) && is_object( $next ) ) {
566 if ( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
567 $this->preventClickjacking();
568 $tools[] = '<span class="mw-rollback-link">' .
569 Linker
::buildRollbackLink( $rev ) . '</span>';
572 if ( $this->title
->quickUserCan( 'edit' )
573 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
574 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
576 # Create undo tooltip for the first (=latest) line only
577 $undoTooltip = $latest
578 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
580 $undolink = Linker
::linkKnown(
582 wfMsgHtml( 'editundo' ),
586 'undoafter' => $next->rev_id
,
587 'undo' => $rev->getId()
590 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
595 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
599 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
600 $classes = array_merge( $classes, $newClasses );
601 $s .= " $tagSummary";
603 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
607 $attribs['class'] = implode( ' ', $classes );
610 return Xml
::tags( 'li', $attribs, $s ) . "\n";
614 * Create a link to view this revision of the page
616 * @param $rev Revision
619 function revLink( $rev ) {
621 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $rev->getTimestamp() ), true );
622 $date = htmlspecialchars( $date );
623 if ( $rev->userCan( Revision
::DELETED_TEXT
) ) {
624 $link = Linker
::linkKnown(
628 array( 'oldid' => $rev->getId() )
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 Linker
::linkKnown(
656 'diff' => $this->title
->getLatestRevID(),
657 'oldid' => $rev->getId()
664 * Create a diff-to-previous link for this revision for this page.
666 * @param $prevRev Revision: the previous revision
667 * @param $next Mixed: the newer revision
670 function lastLink( $prevRev, $next ) {
671 $last = $this->historyPage
->message
['last'];
672 # $next may either be a Row, null, or "unkown"
673 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
674 if ( is_null( $next ) ) {
675 # Probably no next row
677 } elseif ( $next === 'unknown' ) {
678 # Next row probably exists but is unknown, use an oldid=prev link
679 return Linker
::linkKnown(
684 'diff' => $prevRev->getId(),
688 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
)
689 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
) )
693 return Linker
::linkKnown(
698 'diff' => $prevRev->getId(),
699 'oldid' => $next->rev_id
706 * Create radio buttons for page history
708 * @param $rev Revision object
709 * @param $firstInList Boolean: is this version the first one?
711 * @return String: HTML output for the radio buttons
713 function diffButtons( $rev, $firstInList ) {
714 if ( $this->getNumRows() > 1 ) {
716 $radio = array( 'type' => 'radio', 'value' => $id );
717 /** @todo: move title texts to javascript */
718 if ( $firstInList ) {
719 $first = Xml
::element( 'input',
720 array_merge( $radio, array(
721 'style' => 'visibility:hidden',
723 'id' => 'mw-oldid-null' ) )
725 $checkmark = array( 'checked' => 'checked' );
727 # Check visibility of old revisions
728 if ( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
729 $radio['disabled'] = 'disabled';
730 $checkmark = array(); // We will check the next possible one
731 } elseif ( !$this->oldIdChecked
) {
732 $checkmark = array( 'checked' => 'checked' );
733 $this->oldIdChecked
= $id;
735 $checkmark = array();
737 $first = Xml
::element( 'input',
738 array_merge( $radio, $checkmark, array(
740 'id' => "mw-oldid-$id" ) ) );
741 $checkmark = array();
743 $second = Xml
::element( 'input',
744 array_merge( $radio, $checkmark, array(
746 'id' => "mw-diff-$id" ) ) );
747 return $first . $second;
754 * This is called if a write operation is possible from the generated HTML
756 function preventClickjacking( $enable = true ) {
757 $this->preventClickjacking
= $enable;
761 * Get the "prevent clickjacking" flag
763 function getPreventClickjacking() {
764 return $this->preventClickjacking
;
769 * Backwards-compatibility aliases
771 class PageHistory
extends HistoryPage
{}
772 class PageHistoryPager
extends HistoryPager
{}