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
30 function __construct( $article ) {
32 $this->article
= $article;
33 $this->title
= $article->getTitle();
34 $this->skin
= $wgUser->getSkin();
35 $this->preCacheMessages();
38 function getArticle() {
39 return $this->article
;
47 * As we use the same small set of messages in various methods and that
48 * they are called often, we call them once and save them in $this->message
50 function preCacheMessages() {
51 // Precache various messages
52 if( !isset( $this->message
) ) {
53 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
54 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
60 * Print the history page for an article.
65 global $wgOut, $wgRequest, $wgScript;
68 * Allow client caching.
70 if( $wgOut->checkLastModified( $this->article
->getTouched() ) )
71 return; // Client cache fresh and headers sent, nothing more to do.
73 wfProfileIn( __METHOD__
);
76 * 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->addScriptFile( 'history.js' );
87 $logPage = SpecialPage
::getTitleFor( 'Log' );
88 $logLink = $this->skin
->link(
90 wfMsgHtml( 'viewpagelogs' ),
92 array( 'page' => $this->title
->getPrefixedText() ),
93 array( 'known', 'noclasses' )
95 $wgOut->setSubtitle( $logLink );
97 $feedType = $wgRequest->getVal( 'feed' );
99 wfProfileOut( __METHOD__
);
100 return $this->feed( $feedType );
104 * Fail if article doesn't exist.
106 if( !$this->title
->exists() ) {
107 $wgOut->addWikiMsg( 'nohistory' );
108 # show deletion/move log if there is an entry
109 LogEventsList
::showLogExtract( $wgOut, array( 'delete', 'move' ), $this->title
->getPrefixedText(), '',
111 'conds' => array( "log_action != 'revision'" ),
112 'showIfEmpty' => false,
113 'msgKey' => array( 'moveddeleted-notice' ) )
115 wfProfileOut( __METHOD__
);
120 * Add date selector to quickly get to a certain time
122 $year = $wgRequest->getInt( 'year' );
123 $month = $wgRequest->getInt( 'month' );
124 $tagFilter = $wgRequest->getVal( 'tagfilter' );
125 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
127 $action = htmlspecialchars( $wgScript );
129 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
130 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
131 Xml
::hidden( 'title', $this->title
->getPrefixedDBKey() ) . "\n" .
132 Xml
::hidden( 'action', 'history' ) . "\n" .
133 xml
::dateMenu( $year, $month ) . ' ' .
134 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
135 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
139 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article
) );
144 $pager = new HistoryPager( $this, $year, $month, $tagFilter );
146 $pager->getNavigationBar() .
148 $pager->getNavigationBar()
151 wfProfileOut( __METHOD__
);
155 * Fetch an array of revisions, specified by a given limit, offset and
156 * direction. This is now only used by the feeds. It was previously
157 * used by the main UI but that's now handled by the pager.
159 function fetchRevisions($limit, $offset, $direction) {
160 $dbr = wfGetDB( DB_SLAVE
);
162 if( $direction == HistoryPage
::DIR_PREV
)
163 list($dirs, $oper) = array("ASC", ">=");
164 else /* $direction == HistoryPage::DIR_NEXT */
165 list($dirs, $oper) = array("DESC", "<=");
168 $offsets = array("rev_timestamp $oper '$offset'");
172 $page_id = $this->title
->getArticleID();
174 return $dbr->select( 'revision',
175 Revision
::selectFields(),
176 array_merge(array("rev_page=$page_id"), $offsets),
178 array( 'ORDER BY' => "rev_timestamp $dirs",
179 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
184 * Output a subscription feed listing recent edits to this page.
185 * @param string $type
187 function feed( $type ) {
188 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
189 if( !FeedUtils
::checkFeedOutput($type) ) {
193 $feed = new $wgFeedClasses[$type](
194 $this->title
->getPrefixedText() . ' - ' .
195 wfMsgForContent( 'history-feed-title' ),
196 wfMsgForContent( 'history-feed-description' ),
197 $this->title
->getFullUrl( 'action=history' ) );
199 // Get a limit on number of feed entries. Provide a sane default
200 // of 10 if none is defined (but limit to $wgFeedLimit max)
201 $limit = $wgRequest->getInt( 'limit', 10 );
202 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
205 $items = $this->fetchRevisions($limit, 0, HistoryPage
::DIR_NEXT
);
209 foreach( $items as $row ) {
210 $feed->outItem( $this->feedItem( $row ) );
213 $feed->outItem( $this->feedEmpty() );
218 function feedEmpty() {
221 wfMsgForContent( 'nohistory' ),
222 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
223 $this->title
->getFullUrl(),
224 wfTimestamp( TS_MW
),
226 $this->title
->getTalkPage()->getFullUrl()
231 * Generate a FeedItem object from a given revision table row
232 * Borrows Recent Changes' feed generation functions for formatting;
233 * includes a diff to the previous revision (if any).
238 function feedItem( $row ) {
239 $rev = new Revision( $row );
240 $rev->setTitle( $this->title
);
241 $text = FeedUtils
::formatDiffRow(
243 $this->title
->getPreviousRevisionID( $rev->getId() ),
245 $rev->getTimestamp(),
248 if( $rev->getComment() == '' ) {
250 $title = wfMsgForContent( 'history-feed-item-nocomment',
252 $wgContLang->timeanddate( $rev->getTimestamp() ) );
254 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
259 $this->title
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
260 $rev->getTimestamp(),
262 $this->title
->getTalkPage()->getFullUrl()
271 class HistoryPager
extends ReverseChronologicalPager
{
272 public $lastRow = false, $counter, $historyPage, $title, $buttons;
273 protected $oldIdChecked;
275 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
276 parent
::__construct();
277 $this->historyPage
= $historyPage;
278 $this->title
= $this->historyPage
->title
;
279 $this->tagFilter
= $tagFilter;
280 $this->getDateCond( $year, $month );
283 // For hook compatibility...
284 function getArticle() {
285 return $this->historyPage
->getArticle();
288 function getQueryInfo() {
290 'tables' => array('revision'),
291 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
292 'conds' => array('rev_page' => $this->historyPage
->title
->getArticleID() ),
293 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
294 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
296 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
297 $queryInfo['fields'],
299 $queryInfo['join_conds'],
300 $queryInfo['options'],
302 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
306 function getIndexField() {
307 return 'rev_timestamp';
310 function formatRow( $row ) {
311 if( $this->lastRow
) {
312 $latest = ($this->counter
== 1 && $this->mIsFirst
);
313 $firstInList = $this->counter
== 1;
314 $s = $this->historyLine( $this->lastRow
, $row, $this->counter++
,
315 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
319 $this->lastRow
= $row;
324 * Creates begin of history list with a submit button
326 * @return string HTML output
328 function getStartBody() {
329 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut;
330 $this->lastRow
= false;
332 $this->oldIdChecked
= 0;
334 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
335 $s = Xml
::openElement( 'form', array( 'action' => $wgScript,
336 'id' => 'mw-history-compare' ) ) . "\n";
337 $s .= Xml
::hidden( 'title', $this->title
->getPrefixedDbKey() ) . "\n";
339 $this->buttons
= '<div>';
340 if( $wgUser->isAllowed('deleterevision') ) {
341 $this->buttons
.= Xml
::element( 'button',
345 'value' => 'revisiondelete',
346 'style' => 'float: right',
348 wfMsg( 'showhideselectedversions' )
351 if( $wgEnableHtmlDiff ) {
352 $this->buttons
.= Xml
::element( 'button',
355 'name' => 'htmldiff',
357 'class' => 'historysubmit',
358 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
359 'title' => wfMsg( 'tooltip-compareselectedversions' ),
361 wfMsg( 'visualcomparison')
363 $this->buttons
.= $this->submitButton( wfMsg( 'wikicodecomparison'),
365 'class' => 'historysubmit',
366 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
367 'title' => wfMsg( 'tooltip-compareselectedversions' ),
371 $this->buttons
.= $this->submitButton( wfMsg( 'compareselectedversions'),
373 'class' => 'historysubmit',
374 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
375 'title' => wfMsg( 'tooltip-compareselectedversions' ),
379 $this->buttons
.= '</div>';
380 $s .= $this->buttons
. '<ul id="pagehistory">' . "\n";
384 function getEndBody() {
385 if( $this->lastRow
) {
386 $latest = $this->counter
== 1 && $this->mIsFirst
;
387 $firstInList = $this->counter
== 1;
388 if( $this->mIsBackwards
) {
389 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
390 if( $this->mOffset
== '' ) {
396 # The next row is the past-the-end row
397 $next = $this->mPastTheEndRow
;
399 $s = $this->historyLine( $this->lastRow
, $next, $this->counter++
,
400 $this->title
->getNotificationTimestamp(), $latest, $firstInList );
405 $s .= $this->buttons
;
411 * Creates a submit button
413 * @param array $attributes attributes
414 * @return string HTML output for the submit button
416 function submitButton($message, $attributes = array() ) {
417 # Disable submit button if history has 1 revision only
418 if( $this->getNumRows() > 1 ) {
419 return Xml
::submitButton( $message , $attributes );
426 * Returns a row from the history printout.
428 * @todo document some more, and maybe clean up the code (some params redundant?)
430 * @param Row $row The database row corresponding to the previous line.
431 * @param mixed $next The database row corresponding to the next line.
432 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
433 * @param $notificationtimestamp
434 * @param bool $latest Whether this row corresponds to the page's latest revision.
435 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
436 * @return string HTML output for the row
438 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
439 $latest = false, $firstInList = false )
441 global $wgUser, $wgLang;
442 $rev = new Revision( $row );
443 $rev->setTitle( $this->title
);
445 $curlink = $this->curLink( $rev, $latest );
446 $lastlink = $this->lastLink( $rev, $next, $counter );
447 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
448 $link = $this->revLink( $rev );
451 $s = "($curlink) ($lastlink) $diffButtons";
453 if( $wgUser->isAllowed( 'deleterevision' ) ) {
454 // If revision was hidden from sysops
455 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
456 $del = Xml
::check( 'deleterevisions', false, array('disabled' => 'disabled') );
457 // Otherwise, show the link...
460 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
466 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
468 if( $rev->isMinor() ) {
469 $s .= ' ' . ChangesList
::flag( 'minor' );
472 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
473 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
476 $s .= $this->getSkin()->revComment( $rev, false, true );
478 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
479 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
484 # Rollback and undo links
485 if( !is_null( $next ) && is_object( $next ) ) {
486 if( $latest && $this->title
->userCan( 'rollback' ) && $this->title
->userCan( 'edit' ) ) {
487 $tools[] = '<span class="mw-rollback-link">'.
488 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
491 if( $this->title
->quickUserCan( 'edit' )
492 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
493 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
495 # Create undo tooltip for the first (=latest) line only
496 $undoTooltip = $latest
497 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
499 $undolink = $this->getSkin()->link(
501 wfMsgHtml( 'editundo' ),
503 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
504 array( 'known', 'noclasses' )
506 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
511 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
515 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
516 $classes = array_merge( $classes, $newClasses );
517 $s .= " $tagSummary";
519 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
523 $attribs['class'] = implode( ' ', $classes );
526 return Xml
::tags( 'li', $attribs, $s ) . "\n";
530 * Create a link to view this revision of the page
531 * @param Revision $rev
534 function revLink( $rev ) {
536 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
537 $date = htmlspecialchars( $date );
538 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
539 $link = $this->getSkin()->link(
543 array( 'oldid' => $rev->getId() ),
544 array( 'known', 'noclasses' )
547 $link = "<span class=\"history-deleted\">$date</span>";
553 * Create a diff-to-current link for this revision for this page
554 * @param Revision $rev
555 * @param Bool $latest, this is the latest revision of the page?
558 function curLink( $rev, $latest ) {
559 $cur = $this->historyPage
->message
['cur'];
560 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
563 return $this->getSkin()->link(
568 'diff' => $this->title
->getLatestRevID(),
569 'oldid' => $rev->getId()
571 array( 'known', 'noclasses' )
577 * Create a diff-to-previous link for this revision for this page.
578 * @param Revision $prevRev, the previous revision
579 * @param mixed $next, the newer revision
580 * @param int $counter, what row on the history list this is
583 function lastLink( $prevRev, $next, $counter ) {
584 $last = $this->historyPage
->message
['last'];
585 # $next may either be a Row, null, or "unkown"
586 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
587 if( is_null($next) ) {
588 # Probably no next row
590 } elseif( $next === 'unknown' ) {
591 # Next row probably exists but is unknown, use an oldid=prev link
592 return $this->getSkin()->link(
597 'diff' => $prevRev->getId(),
600 array( 'known', 'noclasses' )
602 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
605 return $this->getSkin()->link(
610 'diff' => $prevRev->getId(),
611 'oldid' => $next->rev_id
613 array( 'known', 'noclasses' )
619 * Create radio buttons for page history
621 * @param object $rev Revision
622 * @param bool $firstInList Is this version the first one?
623 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
624 * @return string HTML output for the radio buttons
626 function diffButtons( $rev, $firstInList, $counter ) {
627 if( $this->getNumRows() > 1 ) {
629 $radio = array( 'type' => 'radio', 'value' => $id );
630 /** @todo: move title texts to javascript */
632 $first = Xml
::element( 'input',
633 array_merge( $radio, array(
634 'style' => 'visibility:hidden',
636 'id' => 'mw-oldid-null' ) )
638 $checkmark = array( 'checked' => 'checked' );
640 # Check visibility of old revisions
641 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
642 $radio['disabled'] = 'disabled';
643 $checkmark = array(); // We will check the next possible one
644 } else if( $counter == 2 ||
!$this->oldIdChecked
) {
645 $checkmark = array( 'checked' => 'checked' );
646 $this->oldIdChecked
= $id;
648 $checkmark = array();
650 $first = Xml
::element( 'input',
651 array_merge( $radio, $checkmark, array(
653 'id' => "mw-oldid-$id" ) ) );
654 $checkmark = array();
656 $second = Xml
::element( 'input',
657 array_merge( $radio, $checkmark, array(
659 'id' => "mw-diff-$id" ) ) );
660 return $first . $second;
668 * Backwards-compatibility aliases
670 class PageHistory
extends HistoryPage
{}
671 class PageHistoryPager
extends HistoryPager
{}