5 * Split off from Article.php and Skin.php, 2003-12-22
9 * This class handles printing the history page for an article. In order to
10 * be efficient, it uses timestamps rather than offsets for paging, to avoid
11 * costly LIMIT,offset queries.
13 * Construct it by passing in an Article, and call $h->history() to print the
21 var $mArticle, $mTitle, $mSkin;
24 var $mNotificationTimestamp;
25 var $mLatestId = null;
28 * Construct a new PageHistory.
30 * @param Article $article
33 function __construct($article) {
36 $this->mArticle
=& $article;
37 $this->mTitle
=& $article->mTitle
;
38 $this->mNotificationTimestamp
= NULL;
39 $this->mSkin
= $wgUser->getSkin();
40 $this->preCacheMessages();
44 * As we use the same small set of messages in various methods and that
45 * they are called often, we call them once and save them in $this->message
47 function preCacheMessages() {
48 // Precache various messages
49 if( !isset( $this->message
) ) {
50 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
51 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
57 * Print the history page for an article.
62 global $wgOut, $wgRequest, $wgTitle;
65 * Allow client caching.
68 if( $wgOut->checkLastModified( $this->mArticle
->getTouched() ) )
69 /* Client cache fresh and headers sent, nothing more to do. */
72 wfProfileIn( __METHOD__
);
75 * Setup page variables.
77 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle
->getPrefixedText() ) );
78 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
79 $wgOut->setArticleFlag( false );
80 $wgOut->setArticleRelated( true );
81 $wgOut->setRobotpolicy( 'noindex,nofollow' );
82 $wgOut->setSyndicated( true );
83 $wgOut->setFeedAppendQuery( 'action=history' );
85 $logPage = SpecialPage
::getTitleFor( 'Log' );
86 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
87 $wgOut->setSubtitle( $logLink );
89 $feedType = $wgRequest->getVal( 'feed' );
91 wfProfileOut( __METHOD__
);
92 return $this->feed( $feedType );
96 * Fail if article doesn't exist.
98 if( !$this->mTitle
->exists() ) {
99 $wgOut->addWikiMsg( 'nohistory' );
100 wfProfileOut( __METHOD__
);
105 * "go=first" means to jump to the last (earliest) history page.
106 * This is deprecated, it no longer appears in the user interface
108 if ( $wgRequest->getText("go") == 'first' ) {
109 $limit = $wgRequest->getInt( 'limit', 50 );
110 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
114 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
119 $pager = new PageHistoryPager( $this );
120 $this->linesonpage
= $pager->getNumRows();
122 $pager->getNavigationBar() .
123 $this->beginHistoryList() .
125 $this->endHistoryList() .
126 $pager->getNavigationBar()
128 wfProfileOut( __METHOD__
);
132 * Creates begin of history list with a submit button
134 * @return string HTML output
136 function beginHistoryList() {
138 $this->lastdate
= '';
139 $s = wfMsgExt( 'histlegend', array( 'parse') );
140 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
141 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
143 // The following line is SUPPOSED to have double-quotes around the
144 // $prefixedkey variable, because htmlspecialchars() doesn't escape
147 // On at least two occasions people have changed it to single-quotes,
148 // which creates invalid HTML and incorrect display of the resulting
151 // Please do not break this a third time. Thank you for your kind
152 // consideration and cooperation.
154 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
156 $s .= $this->submitButton();
157 $s .= '<ul id="pagehistory">' . "\n";
162 * Creates end of history list with a submit button
164 * @return string HTML output
166 function endHistoryList() {
168 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
174 * Creates a submit button
176 * @param array $bits optional CSS ID
177 * @return string HTML output for the submit button
179 function submitButton( $bits = array() ) {
180 # Disable submit button if history has 1 revision only
181 if ( $this->linesonpage
> 1 ) {
182 return Xml
::submitButton( wfMsg( 'compareselectedversions' ),
184 'class' => 'historysubmit',
185 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
186 'title' => wfMsg( 'tooltip-compareselectedversions' ),
195 * Returns a row from the history printout.
197 * @todo document some more, and maybe clean up the code (some params redundant?)
199 * @param Row $row The database row corresponding to the previous line.
200 * @param mixed $next The database row corresponding to the next line.
201 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
202 * @param $notificationtimestamp
203 * @param bool $latest Whether this row corresponds to the page's latest revision.
204 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
205 * @return string HTML output for the row
207 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
208 global $wgUser, $wgLang;
209 $rev = new Revision( $row );
210 $rev->setTitle( $this->mTitle
);
213 $curlink = $this->curLink( $rev, $latest );
214 $lastlink = $this->lastLink( $rev, $next, $counter );
215 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
216 $link = $this->revLink( $rev );
218 $s .= "($curlink) ($lastlink) $arbitrary";
220 if( $wgUser->isAllowed( 'deleterevision' ) ) {
221 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
223 // We don't currently handle well changing the top revision's settings
224 $del = $this->message
['rev-delundel'];
225 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
226 // If revision was hidden from sysops
227 $del = $this->message
['rev-delundel'];
229 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
230 $this->message
['rev-delundel'],
231 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
232 '&oldid=' . urlencode( $rev->getId() ) );
233 // Bolden oversighted content
234 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
235 $del = "<strong>$del</strong>";
237 $s .= " <tt>(<small>$del</small>)</tt> ";
241 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
243 if( $row->rev_minor_edit
) {
244 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
247 if ( !is_null( $size = $rev->getSize() ) && $rev->userCan( Revision
::DELETED_TEXT
) ) {
249 $stxt = wfMsgHtml( 'historyempty' );
251 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
252 $s .= " <span class=\"history-size\">$stxt</span>";
255 $s .= $this->mSkin
->revComment( $rev, false, true );
257 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
258 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
260 #add blurb about text having been deleted
261 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
262 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
267 if ( !is_null( $next ) && is_object( $next ) ) {
268 if( !$this->mTitle
->getUserPermissionsErrors( 'rollback', $wgUser )
269 && !$this->mTitle
->getUserPermissionsErrors( 'edit', $wgUser )
271 $tools[] = '<span class="mw-rollback-link">'
272 . $this->mSkin
->buildRollbackLink( $rev )
276 if( $this->mTitle
->quickUserCan( 'edit' ) ) {
277 $undolink = $this->mSkin
->makeKnownLinkObj(
279 wfMsgHtml( 'editundo' ),
280 'action=edit&undoafter=' . $next->rev_id
. '&undo=' . $rev->getId()
282 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
287 $s .= ' (' . implode( ' | ', $tools ) . ')';
290 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
292 return "<li>$s</li>\n";
296 * Create a link to view this revision of the page
297 * @param Revision $rev
300 function revLink( $rev ) {
302 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
303 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
304 $link = $this->mSkin
->makeKnownLinkObj(
305 $this->mTitle
, $date, "oldid=" . $rev->getId() );
309 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
310 return '<span class="history-deleted">' . $link . '</span>';
316 * Create a diff-to-current link for this revision for this page
317 * @param Revision $rev
318 * @param Bool $latest, this is the latest revision of the page?
321 function curLink( $rev, $latest ) {
322 $cur = $this->message
['cur'];
323 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
326 return $this->mSkin
->makeKnownLinkObj(
328 'diff=' . $this->getLatestID() .
329 "&oldid=" . $rev->getId() );
334 * Create a diff-to-previous link for this revision for this page.
335 * @param Revision $prevRev, the previous revision
336 * @param mixed $next, the newer revision
337 * @param int $counter, what row on the history list this is
340 function lastLink( $prevRev, $next, $counter ) {
341 $last = $this->message
['last'];
342 # $next may either be a Row, null, or "unkown"
343 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
344 if( is_null($next) ) {
345 # Probably no next row
347 } elseif( $next === 'unknown' ) {
348 # Next row probably exists but is unknown, use an oldid=prev link
349 return $this->mSkin
->makeKnownLinkObj(
352 "diff=" . $prevRev->getId() . "&oldid=prev" );
353 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
356 return $this->mSkin
->makeKnownLinkObj(
359 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}"
363 "tabindex={$counter}"*/ );
368 * Create radio buttons for page history
370 * @param object $rev Revision
371 * @param bool $firstInList Is this version the first one?
372 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
373 * @return string HTML output for the radio buttons
375 function diffButtons( $rev, $firstInList, $counter ) {
376 if( $this->linesonpage
> 1) {
379 'value' => $rev->getId(),
382 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
383 $radio['disabled'] = 'disabled';
386 /** @todo: move title texts to javascript */
387 if ( $firstInList ) {
388 $first = Xml
::element( 'input', array_merge(
391 'style' => 'visibility:hidden',
392 'name' => 'oldid' ) ) );
393 $checkmark = array( 'checked' => 'checked' );
395 if( $counter == 2 ) {
396 $checkmark = array( 'checked' => 'checked' );
398 $checkmark = array();
400 $first = Xml
::element( 'input', array_merge(
403 array( 'name' => 'oldid' ) ) );
404 $checkmark = array();
406 $second = Xml
::element( 'input', array_merge(
409 array( 'name' => 'diff' ) ) );
410 return $first . $second;
416 /** @todo document */
417 function getLatestId() {
418 if( is_null( $this->mLatestId
) ) {
419 $id = $this->mTitle
->getArticleID();
420 $db = wfGetDB( DB_SLAVE
);
421 $this->mLatestId
= $db->selectField( 'page',
423 array( 'page_id' => $id ),
426 return $this->mLatestId
;
430 * Fetch an array of revisions, specified by a given limit, offset and
431 * direction. This is now only used by the feeds. It was previously
432 * used by the main UI but that's now handled by the pager.
434 function fetchRevisions($limit, $offset, $direction) {
435 $dbr = wfGetDB( DB_SLAVE
);
437 if ($direction == PageHistory
::DIR_PREV
)
438 list($dirs, $oper) = array("ASC", ">=");
439 else /* $direction == PageHistory::DIR_NEXT */
440 list($dirs, $oper) = array("DESC", "<=");
443 $offsets = array("rev_timestamp $oper '$offset'");
447 $page_id = $this->mTitle
->getArticleID();
451 Revision
::selectFields(),
452 array_merge(array("rev_page=$page_id"), $offsets),
454 array('ORDER BY' => "rev_timestamp $dirs",
455 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
459 while (($obj = $dbr->fetchObject($res)) != NULL)
465 /** @todo document */
466 function getNotificationTimestamp() {
467 global $wgUser, $wgShowUpdatedMarker;
469 if ($this->mNotificationTimestamp
!== NULL)
470 return $this->mNotificationTimestamp
;
472 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
473 return $this->mNotificationTimestamp
= false;
475 $dbr = wfGetDB(DB_SLAVE
);
477 $this->mNotificationTimestamp
= $dbr->selectField(
479 'wl_notificationtimestamp',
480 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
481 'wl_title' => $this->mTitle
->getDBkey(),
482 'wl_user' => $wgUser->getID()
486 // Don't use the special value reserved for telling whether the field is filled
487 if ( is_null( $this->mNotificationTimestamp
) ) {
488 $this->mNotificationTimestamp
= false;
491 return $this->mNotificationTimestamp
;
495 * Output a subscription feed listing recent edits to this page.
496 * @param string $type
498 function feed( $type ) {
499 require_once 'SpecialRecentchanges.php';
501 global $wgFeed, $wgFeedClasses;
505 $wgOut->addWikiMsg( 'feed-unavailable' );
509 if( !isset( $wgFeedClasses[$type] ) ) {
511 $wgOut->addWikiMsg( 'feed-invalid' );
515 $feed = new $wgFeedClasses[$type](
516 $this->mTitle
->getPrefixedText() . ' - ' .
517 wfMsgForContent( 'history-feed-title' ),
518 wfMsgForContent( 'history-feed-description' ),
519 $this->mTitle
->getFullUrl( 'action=history' ) );
521 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
524 foreach( $items as $row ) {
525 $feed->outItem( $this->feedItem( $row ) );
528 $feed->outItem( $this->feedEmpty() );
533 function feedEmpty() {
536 wfMsgForContent( 'nohistory' ),
537 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
538 $this->mTitle
->getFullUrl(),
539 wfTimestamp( TS_MW
),
541 $this->mTitle
->getTalkPage()->getFullUrl() );
545 * Generate a FeedItem object from a given revision table row
546 * Borrows Recent Changes' feed generation functions for formatting;
547 * includes a diff to the previous revision (if any).
552 function feedItem( $row ) {
553 $rev = new Revision( $row );
554 $rev->setTitle( $this->mTitle
);
555 $text = rcFormatDiffRow( $this->mTitle
,
556 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
558 $rev->getTimestamp(),
559 $rev->getComment() );
561 if( $rev->getComment() == '' ) {
563 $title = wfMsgForContent( 'history-feed-item-nocomment',
565 $wgContLang->timeanddate( $rev->getTimestamp() ) );
567 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
573 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
574 $rev->getTimestamp(),
576 $this->mTitle
->getTalkPage()->getFullUrl() );
580 * Quickie hack... strip out wikilinks to more legible form from the comment.
582 function stripComment( $text ) {
583 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
591 class PageHistoryPager
extends ReverseChronologicalPager
{
592 public $mLastRow = false, $mPageHistory;
594 function __construct( $pageHistory ) {
595 parent
::__construct();
596 $this->mPageHistory
= $pageHistory;
599 function getQueryInfo() {
601 'tables' => 'revision',
602 'fields' => Revision
::selectFields(),
603 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
604 'options' => array( 'USE INDEX' => 'page_timestamp' )
608 function getIndexField() {
609 return 'rev_timestamp';
612 function formatRow( $row ) {
613 if ( $this->mLastRow
) {
614 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
615 $firstInList = $this->mCounter
== 1;
616 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
617 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
621 $this->mLastRow
= $row;
625 function getStartBody() {
626 $this->mLastRow
= false;
631 function getEndBody() {
632 if ( $this->mLastRow
) {
633 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
634 $firstInList = $this->mCounter
== 1;
635 if ( $this->mIsBackwards
) {
636 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
637 if ( $this->mOffset
== '' ) {
643 # The next row is the past-the-end row
644 $next = $this->mPastTheEndRow
;
646 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
647 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );