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 $mArticle, $mTitle, $mSkin;
25 var $mNotificationTimestamp;
26 var $mLatestId = null;
29 * Construct a new PageHistory.
31 * @param Article $article
34 function __construct($article) {
37 $this->mArticle
=& $article;
38 $this->mTitle
=& $article->mTitle
;
39 $this->mNotificationTimestamp
= NULL;
40 $this->mSkin
= $wgUser->getSkin();
41 $this->preCacheMessages();
44 function getArticle() {
45 return $this->mArticle
;
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 function preCacheMessages() {
57 // Precache various messages
58 if( !isset( $this->message
) ) {
59 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
60 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
66 * Print the history page for an article.
71 global $wgOut, $wgRequest, $wgTitle, $wgScript;
74 * Allow client caching.
76 if( $wgOut->checkLastModified( $this->mArticle
->getTouched() ) )
77 /* Client cache fresh and headers sent, nothing more to do. */
80 wfProfileIn( __METHOD__
);
83 * Setup page variables.
85 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle
->getPrefixedText() ) );
86 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
87 $wgOut->setArticleFlag( false );
88 $wgOut->setArticleRelated( true );
89 $wgOut->setRobotPolicy( 'noindex,nofollow' );
90 $wgOut->setSyndicated( true );
91 $wgOut->setFeedAppendQuery( 'action=history' );
92 $wgOut->addScriptFile( 'history.js' );
94 $logPage = SpecialPage
::getTitleFor( 'Log' );
95 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
96 $wgOut->setSubtitle( $logLink );
98 $feedType = $wgRequest->getVal( 'feed' );
100 wfProfileOut( __METHOD__
);
101 return $this->feed( $feedType );
105 * Fail if article doesn't exist.
107 if( !$this->mTitle
->exists() ) {
108 $wgOut->addWikiMsg( 'nohistory' );
109 wfProfileOut( __METHOD__
);
114 * "go=first" means to jump to the last (earliest) history page.
115 * This is deprecated, it no longer appears in the user interface
117 if ( $wgRequest->getText("go") == 'first' ) {
118 $limit = $wgRequest->getInt( 'limit', 50 );
120 if( $limit > $wgFeedLimit ) {
121 $limit = $wgFeedLimit;
123 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
128 * Add date selector to quickly get to a certain time
130 $year = $wgRequest->getInt( 'year' );
131 $month = $wgRequest->getInt( 'month' );
133 $action = htmlspecialchars( $wgScript );
135 Xml
::fieldset( wfMsg( 'history-search' ), false, array( 'id' => 'mw-history-search' ) ) .
136 "<form action=\"$action\" method=\"get\">" .
137 Xml
::hidden( 'title', $this->mTitle
->getPrefixedDBKey() ) . "\n" .
138 Xml
::hidden( 'action', 'history' ) . "\n" .
139 $this->getDateMenu( $year, $month ) . ' ' .
140 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
144 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
149 $pager = new PageHistoryPager( $this, $year, $month );
150 $this->linesonpage
= $pager->getNumRows();
152 $pager->getNavigationBar() .
153 $this->beginHistoryList() .
155 $this->endHistoryList() .
156 $pager->getNavigationBar()
158 wfProfileOut( __METHOD__
);
162 * @return string Formatted HTML
166 private function getDateMenu( $year, $month ) {
167 # Offset overrides year/month selection
168 if( $month && $month !== -1 ) {
169 $encMonth = intval( $month );
174 $encYear = intval( $year );
175 } else if( $encMonth ) {
176 $thisMonth = intval( gmdate( 'n' ) );
177 $thisYear = intval( gmdate( 'Y' ) );
178 if( intval($encMonth) > $thisMonth ) {
181 $encYear = $thisYear;
185 return Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
186 Xml
::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) .
188 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
189 Xml
::monthSelector( $encMonth, -1 );
193 * Creates begin of history list with a submit button
195 * @return string HTML output
197 function beginHistoryList() {
198 global $wgTitle, $wgScript, $wgEnableHtmlDiff;
199 $this->lastdate
= '';
200 $s = wfMsgExt( 'histlegend', array( 'parse') );
201 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript ) );
202 $s .= Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() );
203 if($wgEnableHtmlDiff){
204 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
206 'name' => 'htmldiff',
207 'class' => 'historysubmit',
208 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
209 'title' => wfMsg( 'tooltip-compareselectedversions' ),
212 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
214 'class' => 'historysubmit',
215 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
216 'title' => wfMsg( 'tooltip-compareselectedversions' ),
220 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
222 'class' => 'historysubmit',
223 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
224 'title' => wfMsg( 'tooltip-compareselectedversions' ),
228 $s .= '<ul id="pagehistory">' . "\n";
233 * Creates end of history list with a submit button
235 * @return string HTML output
237 function endHistoryList() {
238 global $wgEnableHtmlDiff;
240 if($wgEnableHtmlDiff){
241 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
243 'name' => 'htmldiff',
244 'class' => 'historysubmit',
245 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
246 'title' => wfMsg( 'tooltip-compareselectedversions' ),
249 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
251 'class' => 'historysubmit',
252 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
253 'title' => wfMsg( 'tooltip-compareselectedversions' ),
257 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
259 'class' => 'historysubmit',
260 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
261 'title' => wfMsg( 'tooltip-compareselectedversions' ),
270 * Creates a submit button
272 * @param array $attributes attributes
273 * @return string HTML output for the submit button
275 function submitButton($message, $attributes = array() ) {
276 # Disable submit button if history has 1 revision only
277 if ( $this->linesonpage
> 1 ) {
278 return Xml
::submitButton( $message , $attributes );
285 * Returns a row from the history printout.
287 * @todo document some more, and maybe clean up the code (some params redundant?)
289 * @param Row $row The database row corresponding to the previous line.
290 * @param mixed $next The database row corresponding to the next line.
291 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
292 * @param $notificationtimestamp
293 * @param bool $latest Whether this row corresponds to the page's latest revision.
294 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
295 * @return string HTML output for the row
297 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
298 global $wgUser, $wgLang;
299 $rev = new Revision( $row );
300 $rev->setTitle( $this->mTitle
);
303 $curlink = $this->curLink( $rev, $latest );
304 $lastlink = $this->lastLink( $rev, $next, $counter );
305 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
306 $link = $this->revLink( $rev );
308 $s .= "($curlink) ($lastlink) $arbitrary";
310 if( $wgUser->isAllowed( 'deleterevision' ) ) {
311 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
313 // We don't currently handle well changing the top revision's settings
314 $del = $this->message
['rev-delundel'];
315 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
316 // If revision was hidden from sysops
317 $del = $this->message
['rev-delundel'];
319 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
320 $this->message
['rev-delundel'],
321 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
322 '&oldid=' . urlencode( $rev->getId() ) );
323 // Bolden oversighted content
324 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
325 $del = "<strong>$del</strong>";
327 $s .= " <tt>(<small>$del</small>)</tt> ";
331 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
333 if( $row->rev_minor_edit
) {
334 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
337 if ( !is_null( $size = $rev->getSize() ) && $rev->userCan( Revision
::DELETED_TEXT
) ) {
338 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
341 $s .= $this->mSkin
->revComment( $rev, false, true );
343 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
344 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
346 #add blurb about text having been deleted
347 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
348 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
353 if ( !is_null( $next ) && is_object( $next ) ) {
354 if( !$this->mTitle
->getUserPermissionsErrors( 'rollback', $wgUser )
355 && !$this->mTitle
->getUserPermissionsErrors( 'edit', $wgUser )
357 $tools[] = '<span class="mw-rollback-link">'
358 . $this->mSkin
->buildRollbackLink( $rev )
362 if( $this->mTitle
->quickUserCan( 'edit' ) &&
363 !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
364 !$next->rev_deleted
& Revision
::DELETED_TEXT
) {
366 # Create undo tooltip for the first (=latest) line only
367 $undoTooltip = $latest
368 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
370 $undolink = $this->mSkin
->link(
372 wfMsgHtml( 'editundo' ),
374 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
375 array( 'known', 'noclasses' )
377 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
382 $s .= ' (' . implode( ' | ', $tools ) . ')';
385 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
387 return "<li>$s</li>\n";
391 * Create a link to view this revision of the page
392 * @param Revision $rev
395 function revLink( $rev ) {
397 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
398 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
399 $link = $this->mSkin
->makeKnownLinkObj(
400 $this->mTitle
, $date, "oldid=" . $rev->getId() );
404 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
405 return '<span class="history-deleted">' . $link . '</span>';
411 * Create a diff-to-current link for this revision for this page
412 * @param Revision $rev
413 * @param Bool $latest, this is the latest revision of the page?
416 function curLink( $rev, $latest ) {
417 $cur = $this->message
['cur'];
418 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
421 return $this->mSkin
->makeKnownLinkObj(
423 'diff=' . $this->getLatestID() .
424 "&oldid=" . $rev->getId() );
429 * Create a diff-to-previous link for this revision for this page.
430 * @param Revision $prevRev, the previous revision
431 * @param mixed $next, the newer revision
432 * @param int $counter, what row on the history list this is
435 function lastLink( $prevRev, $next, $counter ) {
436 $last = $this->message
['last'];
437 # $next may either be a Row, null, or "unkown"
438 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
439 if( is_null($next) ) {
440 # Probably no next row
442 } elseif( $next === 'unknown' ) {
443 # Next row probably exists but is unknown, use an oldid=prev link
444 return $this->mSkin
->makeKnownLinkObj(
447 "diff=" . $prevRev->getId() . "&oldid=prev" );
448 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
451 return $this->mSkin
->makeKnownLinkObj(
454 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}"
458 "tabindex={$counter}"*/ );
463 * Create radio buttons for page history
465 * @param object $rev Revision
466 * @param bool $firstInList Is this version the first one?
467 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
468 * @return string HTML output for the radio buttons
470 function diffButtons( $rev, $firstInList, $counter ) {
471 if( $this->linesonpage
> 1) {
474 'value' => $rev->getId(),
477 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
478 $radio['disabled'] = 'disabled';
481 /** @todo: move title texts to javascript */
482 if ( $firstInList ) {
483 $first = Xml
::element( 'input', array_merge(
486 'style' => 'visibility:hidden',
487 'name' => 'oldid' ) ) );
488 $checkmark = array( 'checked' => 'checked' );
490 if( $counter == 2 ) {
491 $checkmark = array( 'checked' => 'checked' );
493 $checkmark = array();
495 $first = Xml
::element( 'input', array_merge(
498 array( 'name' => 'oldid' ) ) );
499 $checkmark = array();
501 $second = Xml
::element( 'input', array_merge(
504 array( 'name' => 'diff' ) ) );
505 return $first . $second;
511 /** @todo document */
512 function getLatestId() {
513 if( is_null( $this->mLatestId
) ) {
514 $id = $this->mTitle
->getArticleID();
515 $db = wfGetDB( DB_SLAVE
);
516 $this->mLatestId
= $db->selectField( 'page',
518 array( 'page_id' => $id ),
521 return $this->mLatestId
;
525 * Fetch an array of revisions, specified by a given limit, offset and
526 * direction. This is now only used by the feeds. It was previously
527 * used by the main UI but that's now handled by the pager.
529 function fetchRevisions($limit, $offset, $direction) {
530 $dbr = wfGetDB( DB_SLAVE
);
532 if ($direction == PageHistory
::DIR_PREV
)
533 list($dirs, $oper) = array("ASC", ">=");
534 else /* $direction == PageHistory::DIR_NEXT */
535 list($dirs, $oper) = array("DESC", "<=");
538 $offsets = array("rev_timestamp $oper '$offset'");
542 $page_id = $this->mTitle
->getArticleID();
546 Revision
::selectFields(),
547 array_merge(array("rev_page=$page_id"), $offsets),
549 array('ORDER BY' => "rev_timestamp $dirs",
550 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
554 /** @todo document */
555 function getNotificationTimestamp() {
556 global $wgUser, $wgShowUpdatedMarker;
558 if ($this->mNotificationTimestamp
!== NULL)
559 return $this->mNotificationTimestamp
;
561 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
562 return $this->mNotificationTimestamp
= false;
564 $dbr = wfGetDB(DB_SLAVE
);
566 $this->mNotificationTimestamp
= $dbr->selectField(
568 'wl_notificationtimestamp',
569 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
570 'wl_title' => $this->mTitle
->getDBkey(),
571 'wl_user' => $wgUser->getId()
575 // Don't use the special value reserved for telling whether the field is filled
576 if ( is_null( $this->mNotificationTimestamp
) ) {
577 $this->mNotificationTimestamp
= false;
580 return $this->mNotificationTimestamp
;
584 * Output a subscription feed listing recent edits to this page.
585 * @param string $type
587 function feed( $type ) {
588 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
589 if ( !FeedUtils
::checkFeedOutput($type) ) {
593 $feed = new $wgFeedClasses[$type](
594 $this->mTitle
->getPrefixedText() . ' - ' .
595 wfMsgForContent( 'history-feed-title' ),
596 wfMsgForContent( 'history-feed-description' ),
597 $this->mTitle
->getFullUrl( 'action=history' ) );
599 // Get a limit on number of feed entries. Provide a sane default
600 // of 10 if none is defined (but limit to $wgFeedLimit max)
601 $limit = $wgRequest->getInt( 'limit', 10 );
602 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
605 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
609 foreach( $items as $row ) {
610 $feed->outItem( $this->feedItem( $row ) );
613 $feed->outItem( $this->feedEmpty() );
618 function feedEmpty() {
621 wfMsgForContent( 'nohistory' ),
622 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
623 $this->mTitle
->getFullUrl(),
624 wfTimestamp( TS_MW
),
626 $this->mTitle
->getTalkPage()->getFullUrl() );
630 * Generate a FeedItem object from a given revision table row
631 * Borrows Recent Changes' feed generation functions for formatting;
632 * includes a diff to the previous revision (if any).
637 function feedItem( $row ) {
638 $rev = new Revision( $row );
639 $rev->setTitle( $this->mTitle
);
640 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
641 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
643 $rev->getTimestamp(),
644 $rev->getComment() );
646 if( $rev->getComment() == '' ) {
648 $title = wfMsgForContent( 'history-feed-item-nocomment',
650 $wgContLang->timeanddate( $rev->getTimestamp() ) );
652 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
658 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
659 $rev->getTimestamp(),
661 $this->mTitle
->getTalkPage()->getFullUrl() );
665 * Quickie hack... strip out wikilinks to more legible form from the comment.
667 function stripComment( $text ) {
668 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
676 class PageHistoryPager
extends ReverseChronologicalPager
{
677 public $mLastRow = false, $mPageHistory;
679 function __construct( $pageHistory, $year='', $month='' ) {
680 parent
::__construct();
681 $this->mPageHistory
= $pageHistory;
682 $this->getDateCond( $year, $month );
685 function getQueryInfo() {
687 'tables' => array('revision'),
688 'fields' => Revision
::selectFields(),
689 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
690 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') )
692 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
696 function getIndexField() {
697 return 'rev_timestamp';
700 function formatRow( $row ) {
701 if ( $this->mLastRow
) {
702 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
703 $firstInList = $this->mCounter
== 1;
704 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
705 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
709 $this->mLastRow
= $row;
713 function getStartBody() {
714 $this->mLastRow
= false;
719 function getEndBody() {
720 if ( $this->mLastRow
) {
721 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
722 $firstInList = $this->mCounter
== 1;
723 if ( $this->mIsBackwards
) {
724 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
725 if ( $this->mOffset
== '' ) {
731 # The next row is the past-the-end row
732 $next = $this->mPastTheEndRow
;
734 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
735 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );