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 HistoryAction
extends FormlessAction
{
22 public function getName() {
26 public function getRestriction() {
30 public function requiresWrite() {
34 public function requiresUnblock() {
38 protected function getPageTitle() {
39 return $this->msg( 'history-title', $this->getTitle()->getPrefixedText() )->text();
42 protected function getDescription() {
43 // Creation of a subtitle link pointing to [[Special:Log]]
44 return Linker
::linkKnown(
45 SpecialPage
::getTitleFor( 'Log' ),
46 $this->msg( 'viewpagelogs' )->escaped(),
48 array( 'page' => $this->getTitle()->getPrefixedText() )
53 * Get the Article object we are working on.
56 public function getArticle() {
61 * As we use the same small set of messages in various methods and that
62 * they are called often, we call them once and save them in $this->message
64 private function preCacheMessages() {
65 // Precache various messages
66 if ( !isset( $this->message
) ) {
67 $msgs = array( 'cur', 'last', 'pipe-separator' );
68 foreach ( $msgs as $msg ) {
69 $this->message
[$msg] = $this->msg( $msg )->escaped();
75 * Print the history page for an article.
79 global $wgScript, $wgUseFileCache, $wgSquidMaxage;
81 $out = $this->getOutput();
82 $request = $this->getRequest();
85 * Allow client caching.
87 if ( $out->checkLastModified( $this->page
->getTouched() ) ) {
88 return; // Client cache fresh and headers sent, nothing more to do.
91 wfProfileIn( __METHOD__
);
93 if ( $request->getFullRequestURL() == $this->getTitle()->getInternalURL( 'action=history' ) ) {
94 $out->setSquidMaxage( $wgSquidMaxage );
97 $this->preCacheMessages();
99 # Fill in the file cache if not set already
100 if ( $wgUseFileCache && HTMLFileCache
::useFileCache( $this->getContext() ) ) {
101 $cache = HTMLFileCache
::newFromTitle( $this->getTitle(), 'history' );
102 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
103 ob_start( array( &$cache, 'saveToFileCache' ) );
107 // Setup page variables.
108 $out->setFeedAppendQuery( 'action=history' );
109 $out->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
111 // Handle atom/RSS feeds.
112 $feedType = $request->getVal( 'feed' );
114 wfProfileOut( __METHOD__
);
115 return $this->feed( $feedType );
118 // Fail nicely if article doesn't exist.
119 if ( !$this->getTitle()->exists() ) {
120 $out->addWikiMsg( 'nohistory' );
121 # show deletion/move log if there is an entry
122 LogEventsList
::showLogExtract(
124 array( 'delete', 'move' ),
128 'conds' => array( "log_action != 'revision'" ),
129 'showIfEmpty' => false,
130 'msgKey' => array( 'moveddeleted-notice' )
133 wfProfileOut( __METHOD__
);
138 * Add date selector to quickly get to a certain time
140 $year = $request->getInt( 'year' );
141 $month = $request->getInt( 'month' );
142 $tagFilter = $request->getVal( 'tagfilter' );
143 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
146 * Option to show only revisions that have been (partially) hidden via RevisionDelete
148 if ( $request->getBool( 'deleted' ) ) {
149 $conds = array( "rev_deleted != '0'" );
153 $checkDeleted = Xml
::checkLabel( $this->msg( 'history-show-deleted' )->text(),
154 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
156 // Add the general form
157 $action = htmlspecialchars( $wgScript );
159 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
161 $this->msg( 'history-fieldset-title' )->text(),
163 array( 'id' => 'mw-history-search' )
165 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBKey() ) . "\n" .
166 Html
::hidden( 'action', 'history' ) . "\n" .
167 Xml
::dateMenu( $year, $month ) . ' ' .
168 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
170 Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
174 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
176 // Create and output the list.
177 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
179 $pager->getNavigationBar() .
181 $pager->getNavigationBar()
183 $out->preventClickjacking( $pager->getPreventClickjacking() );
185 wfProfileOut( __METHOD__
);
189 * Fetch an array of revisions, specified by a given limit, offset and
190 * direction. This is now only used by the feeds. It was previously
191 * used by the main UI but that's now handled by the pager.
193 * @param $limit Integer: the limit number of revisions to get
194 * @param $offset Integer
195 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
196 * @return ResultWrapper
198 function fetchRevisions( $limit, $offset, $direction ) {
199 $dbr = wfGetDB( DB_SLAVE
);
201 if ( $direction == HistoryPage
::DIR_PREV
) {
202 list( $dirs, $oper ) = array( "ASC", ">=" );
203 } else { /* $direction == HistoryPage::DIR_NEXT */
204 list( $dirs, $oper ) = array( "DESC", "<=" );
208 $offsets = array( "rev_timestamp $oper '$offset'" );
213 $page_id = $this->getTitle()->getArticleID();
215 return $dbr->select( 'revision',
216 Revision
::selectFields(),
217 array_merge( array( "rev_page=$page_id" ), $offsets ),
219 array( 'ORDER BY' => "rev_timestamp $dirs",
220 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
225 * Output a subscription feed listing recent edits to this page.
227 * @param $type String: feed type
229 function feed( $type ) {
230 global $wgFeedClasses, $wgFeedLimit;
231 if ( !FeedUtils
::checkFeedOutput( $type ) ) {
234 $request = $this->getRequest();
236 $feed = new $wgFeedClasses[$type](
237 $this->getTitle()->getPrefixedText() . ' - ' .
238 wfMsgForContent( 'history-feed-title' ),
239 wfMsgForContent( 'history-feed-description' ),
240 $this->getTitle()->getFullUrl( 'action=history' )
243 // Get a limit on number of feed entries. Provide a sane default
244 // of 10 if none is defined (but limit to $wgFeedLimit max)
245 $limit = $request->getInt( 'limit', 10 );
246 if ( $limit > $wgFeedLimit ||
$limit < 1 ) {
249 $items = $this->fetchRevisions( $limit, 0, HistoryPage
::DIR_NEXT
);
251 // Generate feed elements enclosed between header and footer.
253 if ( $items->numRows() ) {
254 foreach ( $items as $row ) {
255 $feed->outItem( $this->feedItem( $row ) );
258 $feed->outItem( $this->feedEmpty() );
263 function feedEmpty() {
265 wfMsgForContent( 'nohistory' ),
266 $this->getOutput()->parse( wfMsgForContent( 'history-feed-empty' ) ),
267 $this->getTitle()->getFullUrl(),
268 wfTimestamp( TS_MW
),
270 $this->getTitle()->getTalkPage()->getFullUrl()
275 * Generate a FeedItem object from a given revision table row
276 * Borrows Recent Changes' feed generation functions for formatting;
277 * includes a diff to the previous revision (if any).
279 * @param $row Object: database row
282 function feedItem( $row ) {
283 $rev = new Revision( $row );
284 $rev->setTitle( $this->getTitle() );
285 $text = FeedUtils
::formatDiffRow(
287 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
289 $rev->getTimestamp(),
292 if ( $rev->getComment() == '' ) {
294 $title = wfMsgForContent( 'history-feed-item-nocomment',
296 $wgContLang->timeanddate( $rev->getTimestamp() ),
297 $wgContLang->date( $rev->getTimestamp() ),
298 $wgContLang->time( $rev->getTimestamp() )
301 $title = $rev->getUserText() .
302 wfMsgForContent( 'colon-separator' ) .
303 FeedItem
::stripComment( $rev->getComment() );
308 $this->getTitle()->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
309 $rev->getTimestamp(),
311 $this->getTitle()->getTalkPage()->getFullUrl()
319 class HistoryPager
extends ReverseChronologicalPager
{
320 public $lastRow = false, $counter, $historyPage, $buttons, $conds;
321 protected $oldIdChecked;
322 protected $preventClickjacking = false;
324 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
325 parent
::__construct( $historyPage->getContext() );
326 $this->historyPage
= $historyPage;
327 $this->tagFilter
= $tagFilter;
328 $this->getDateCond( $year, $month );
329 $this->conds
= $conds;
332 // For hook compatibility...
333 function getArticle() {
334 return $this->historyPage
->getArticle();
337 function getSqlComment() {
338 if ( $this->conds
) {
339 return 'history page filtered'; // potentially slow, see CR r58153
341 return 'history page unfiltered';
345 function getQueryInfo() {
347 'tables' => array( 'revision', 'user' ),
348 'fields' => array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
349 'conds' => array_merge(
350 array( 'rev_page' => $this->getTitle()->getArticleID() ),
352 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
353 'join_conds' => array(
354 'user' => Revision
::userJoinCond(),
355 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
357 ChangeTags
::modifyDisplayQuery(
358 $queryInfo['tables'],
359 $queryInfo['fields'],
361 $queryInfo['join_conds'],
362 $queryInfo['options'],
365 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
369 function getIndexField() {
370 return 'rev_timestamp';
373 function formatRow( $row ) {
374 if ( $this->lastRow
) {
375 $latest = ( $this->counter
== 1 && $this->mIsFirst
);
376 $firstInList = $this->counter
== 1;
378 $s = $this->historyLine( $this->lastRow
, $row,
379 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
383 $this->lastRow
= $row;
387 function doBatchLookups() {
388 # Do a link batch query
389 $this->mResult
->seek( 0 );
390 $batch = new LinkBatch();
391 foreach ( $this->mResult
as $row ) {
392 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
393 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
396 $this->mResult
->seek( 0 );
400 * Creates begin of history list with a submit button
402 * @return string HTML output
404 function getStartBody() {
406 $this->lastRow
= false;
408 $this->oldIdChecked
= 0;
410 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
411 $s = Html
::openElement( 'form', array( 'action' => $wgScript,
412 'id' => 'mw-history-compare' ) ) . "\n";
413 $s .= Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . "\n";
414 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
416 $s .= '<div>' . $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
417 array( 'class' => 'historysubmit' ) ) . "\n";
419 $this->buttons
= '<div>';
420 $this->buttons
.= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
421 array( 'class' => 'historysubmit' )
422 + Linker
::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
425 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
426 $s .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
428 $this->buttons
.= '</div>';
429 $s .= '</div><ul id="pagehistory">' . "\n";
433 private function getRevisionButton( $name, $msg ) {
434 $this->preventClickjacking();
435 # Note bug #20966, <button> is non-standard in IE<8
436 $element = Html
::element( 'button',
441 'class' => "mw-history-$name-button",
443 $this->msg( $msg )->text()
445 $this->buttons
.= $element;
449 function getEndBody() {
450 if ( $this->lastRow
) {
451 $latest = $this->counter
== 1 && $this->mIsFirst
;
452 $firstInList = $this->counter
== 1;
453 if ( $this->mIsBackwards
) {
454 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
455 if ( $this->mOffset
== '' ) {
461 # The next row is the past-the-end row
462 $next = $this->mPastTheEndRow
;
465 $s = $this->historyLine( $this->lastRow
, $next,
466 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
471 # Add second buttons only if there is more than one rev
472 if ( $this->getNumRows() > 2 ) {
473 $s .= $this->buttons
;
480 * Creates a submit button
482 * @param $message String: text of the submit button, will be escaped
483 * @param $attributes Array: attributes
484 * @return String: HTML output for the submit button
486 function submitButton( $message, $attributes = array() ) {
487 # Disable submit button if history has 1 revision only
488 if ( $this->getNumRows() > 1 ) {
489 return Xml
::submitButton( $message , $attributes );
496 * Returns a row from the history printout.
498 * @todo document some more, and maybe clean up the code (some params redundant?)
500 * @param $row Object: the database row corresponding to the previous line.
501 * @param $next Mixed: the database row corresponding to the next line. (chronologically previous)
502 * @param $notificationtimestamp
503 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
504 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
505 * @return String: HTML output for the row
507 function historyLine( $row, $next, $notificationtimestamp = false,
508 $latest = false, $firstInList = false )
510 $rev = new Revision( $row );
511 $rev->setTitle( $this->getTitle() );
513 if ( is_object( $next ) ) {
514 $prevRev = new Revision( $next );
515 $prevRev->setTitle( $this->getTitle() );
520 $curlink = $this->curLink( $rev, $latest );
521 $lastlink = $this->lastLink( $rev, $next );
522 $diffButtons = $this->diffButtons( $rev, $firstInList );
523 $histLinks = Html
::rawElement(
525 array( 'class' => 'mw-history-histlinks' ),
526 '(' . $curlink . $this->historyPage
->message
['pipe-separator'] . $lastlink . ') '
528 $s = $histLinks . $diffButtons;
530 $link = $this->revLink( $rev );
534 $user = $this->getUser();
535 // Show checkboxes for each revision
536 if ( $user->isAllowed( 'deleterevision' ) ) {
537 $this->preventClickjacking();
538 // If revision was hidden from sysops, disable the checkbox
539 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
540 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
541 // Otherwise, enable the checkbox...
543 $del = Xml
::check( 'showhiderevisions', false,
544 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
546 // User can only view deleted revisions...
547 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
548 // If revision was hidden from sysops, disable the link
549 if ( !$rev->userCan( Revision
::DELETED_RESTRICTED
, $user ) ) {
550 $cdel = Linker
::revDeleteLinkDisabled( false );
551 // Otherwise, show the link...
553 $query = array( 'type' => 'revision',
554 'target' => $this->getTitle()->getPrefixedDbkey(), 'ids' => $rev->getId() );
555 $del .= Linker
::revDeleteLink( $query,
556 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), false );
563 $lang = $this->getLanguage();
564 $dirmark = $lang->getDirMark();
568 $s .= " <span class='history-user'>" .
569 Linker
::revUserTools( $rev, true ) . "</span>";
572 if ( $rev->isMinor() ) {
573 $s .= ' ' . ChangesList
::flag( 'minor' );
577 && !$prevRev->isDeleted( Revision
::DELETED_TEXT
)
578 && !$rev->isDeleted( Revision
::DELETED_TEXT
) )
580 $sDiff = ChangesList
::showCharacterDifference( $prevRev->getSize(), $rev->getSize() );
581 $s .= ' . . ' . $sDiff . ' . . ';
584 $s .= Linker
::revComment( $rev, false, true );
586 if ( $notificationtimestamp && ( $row->rev_timestamp
>= $notificationtimestamp ) ) {
587 $s .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
592 # Rollback and undo links
594 !count( $this->getTitle()->getUserPermissionsErrors( 'edit', $this->getUser() ) ) )
596 if ( $latest && !count( $this->getTitle()->getUserPermissionsErrors( 'rollback', $this->getUser() ) ) ) {
597 $this->preventClickjacking();
598 $tools[] = '<span class="mw-rollback-link">' .
599 Linker
::buildRollbackLink( $rev ) . '</span>';
602 if ( !$rev->isDeleted( Revision
::DELETED_TEXT
)
603 && !$prevRev->isDeleted( Revision
::DELETED_TEXT
) )
605 # Create undo tooltip for the first (=latest) line only
606 $undoTooltip = $latest
607 ?
array( 'title' => $this->msg( 'tooltip-undo' )->text() )
609 $undolink = Linker
::linkKnown(
611 $this->msg( 'editundo' )->escaped(),
615 'undoafter' => $prevRev->getId(),
616 'undo' => $rev->getId()
619 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
624 $s .= ' (' . $lang->pipeList( $tools ) . ')';
628 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
629 $classes = array_merge( $classes, $newClasses );
630 $s .= " $tagSummary";
632 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
636 $attribs['class'] = implode( ' ', $classes );
639 return Xml
::tags( 'li', $attribs, $s ) . "\n";
643 * Create a link to view this revision of the page
645 * @param $rev Revision
648 function revLink( $rev ) {
649 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
650 $date = htmlspecialchars( $date );
651 if ( $rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
652 $link = Linker
::linkKnown(
656 array( 'oldid' => $rev->getId() )
661 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
662 $link = "<span class=\"history-deleted\">$link</span>";
668 * Create a diff-to-current link for this revision for this page
670 * @param $rev Revision
671 * @param $latest Boolean: this is the latest revision of the page?
674 function curLink( $rev, $latest ) {
675 $cur = $this->historyPage
->message
['cur'];
676 if ( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
679 return Linker
::linkKnown(
684 'diff' => $this->getTitle()->getLatestRevID(),
685 'oldid' => $rev->getId()
692 * Create a diff-to-previous link for this revision for this page.
694 * @param $prevRev Revision: the previous revision
695 * @param $next Mixed: the newer revision
698 function lastLink( $prevRev, $next ) {
699 $last = $this->historyPage
->message
['last'];
700 # $next may either be a Row, null, or "unkown"
701 $nextRev = is_object( $next ) ?
new Revision( $next ) : $next;
702 if ( is_null( $next ) ) {
703 # Probably no next row
705 } elseif ( $next === 'unknown' ) {
706 # Next row probably exists but is unknown, use an oldid=prev link
707 return Linker
::linkKnown(
712 'diff' => $prevRev->getId(),
716 } elseif ( !$prevRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() )
717 ||
!$nextRev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) )
721 return Linker
::linkKnown(
726 'diff' => $prevRev->getId(),
727 'oldid' => $next->rev_id
734 * Create radio buttons for page history
736 * @param $rev Revision object
737 * @param $firstInList Boolean: is this version the first one?
739 * @return String: HTML output for the radio buttons
741 function diffButtons( $rev, $firstInList ) {
742 if ( $this->getNumRows() > 1 ) {
744 $radio = array( 'type' => 'radio', 'value' => $id );
745 /** @todo: move title texts to javascript */
746 if ( $firstInList ) {
747 $first = Xml
::element( 'input',
748 array_merge( $radio, array(
749 'style' => 'visibility:hidden',
751 'id' => 'mw-oldid-null' ) )
753 $checkmark = array( 'checked' => 'checked' );
755 # Check visibility of old revisions
756 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
757 $radio['disabled'] = 'disabled';
758 $checkmark = array(); // We will check the next possible one
759 } elseif ( !$this->oldIdChecked
) {
760 $checkmark = array( 'checked' => 'checked' );
761 $this->oldIdChecked
= $id;
763 $checkmark = array();
765 $first = Xml
::element( 'input',
766 array_merge( $radio, $checkmark, array(
768 'id' => "mw-oldid-$id" ) ) );
769 $checkmark = array();
771 $second = Xml
::element( 'input',
772 array_merge( $radio, $checkmark, array(
774 'id' => "mw-diff-$id" ) ) );
775 return $first . $second;
782 * This is called if a write operation is possible from the generated HTML
784 function preventClickjacking( $enable = true ) {
785 $this->preventClickjacking
= $enable;
789 * Get the "prevent clickjacking" flag
791 function getPreventClickjacking() {
792 return $this->preventClickjacking
;
797 * Backwards-compatibility alias
799 class HistoryPage
extends HistoryAction
{
800 public function __construct( Page
$article ) { # Just to make it public
801 parent
::__construct( $article );
804 public function history() {