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' );
84 $wgOut->addScriptFile( 'history.js' );
86 $logPage = SpecialPage
::getTitleFor( 'Log' );
87 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
88 $wgOut->setSubtitle( $logLink );
90 $feedType = $wgRequest->getVal( 'feed' );
92 wfProfileOut( __METHOD__
);
93 return $this->feed( $feedType );
97 * Fail if article doesn't exist.
99 if( !$this->mTitle
->exists() ) {
100 $wgOut->addWikiMsg( 'nohistory' );
101 wfProfileOut( __METHOD__
);
106 * "go=first" means to jump to the last (earliest) history page.
107 * This is deprecated, it no longer appears in the user interface
109 if ( $wgRequest->getText("go") == 'first' ) {
110 $limit = $wgRequest->getInt( 'limit', 50 );
111 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
115 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
120 $pager = new PageHistoryPager( $this );
121 $this->linesonpage
= $pager->getNumRows();
123 $pager->getNavigationBar() .
124 $this->beginHistoryList() .
126 $this->endHistoryList() .
127 $pager->getNavigationBar()
129 wfProfileOut( __METHOD__
);
133 * Creates begin of history list with a submit button
135 * @return string HTML output
137 function beginHistoryList() {
139 $this->lastdate
= '';
140 $s = wfMsgExt( 'histlegend', array( 'parse') );
141 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
142 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
144 // The following line is SUPPOSED to have double-quotes around the
145 // $prefixedkey variable, because htmlspecialchars() doesn't escape
148 // On at least two occasions people have changed it to single-quotes,
149 // which creates invalid HTML and incorrect display of the resulting
152 // Please do not break this a third time. Thank you for your kind
153 // consideration and cooperation.
155 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
157 $s .= $this->submitButton();
158 $s .= '<ul id="pagehistory">' . "\n";
163 * Creates end of history list with a submit button
165 * @return string HTML output
167 function endHistoryList() {
169 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
175 * Creates a submit button
177 * @param array $bits optional CSS ID
178 * @return string HTML output for the submit button
180 function submitButton( $bits = array() ) {
181 # Disable submit button if history has 1 revision only
182 if ( $this->linesonpage
> 1 ) {
183 return Xml
::submitButton( wfMsg( 'compareselectedversions' ),
185 'class' => 'historysubmit',
186 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
187 'title' => wfMsg( 'tooltip-compareselectedversions' ),
196 * Returns a row from the history printout.
198 * @todo document some more, and maybe clean up the code (some params redundant?)
200 * @param Row $row The database row corresponding to the previous line.
201 * @param mixed $next The database row corresponding to the next line.
202 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
203 * @param $notificationtimestamp
204 * @param bool $latest Whether this row corresponds to the page's latest revision.
205 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
206 * @return string HTML output for the row
208 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
209 global $wgUser, $wgLang;
210 $rev = new Revision( $row );
211 $rev->setTitle( $this->mTitle
);
214 $curlink = $this->curLink( $rev, $latest );
215 $lastlink = $this->lastLink( $rev, $next, $counter );
216 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
217 $link = $this->revLink( $rev );
219 $s .= "($curlink) ($lastlink) $arbitrary";
221 if( $wgUser->isAllowed( 'deleterevision' ) ) {
222 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
224 // We don't currently handle well changing the top revision's settings
225 $del = $this->message
['rev-delundel'];
226 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
227 // If revision was hidden from sysops
228 $del = $this->message
['rev-delundel'];
230 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
231 $this->message
['rev-delundel'],
232 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
233 '&oldid=' . urlencode( $rev->getId() ) );
234 // Bolden oversighted content
235 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
236 $del = "<strong>$del</strong>";
238 $s .= " <tt>(<small>$del</small>)</tt> ";
242 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
244 if( $row->rev_minor_edit
) {
245 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
248 if ( !is_null( $size = $rev->getSize() ) && $rev->userCan( Revision
::DELETED_TEXT
) ) {
250 $stxt = wfMsgHtml( 'historyempty' );
252 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
253 $s .= " <span class=\"history-size\">$stxt</span>";
256 $s .= $this->mSkin
->revComment( $rev, false, true );
258 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
259 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
261 #add blurb about text having been deleted
262 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
263 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
268 if ( !is_null( $next ) && is_object( $next ) ) {
269 if( !$this->mTitle
->getUserPermissionsErrors( 'rollback', $wgUser )
270 && !$this->mTitle
->getUserPermissionsErrors( 'edit', $wgUser )
272 $tools[] = '<span class="mw-rollback-link">'
273 . $this->mSkin
->buildRollbackLink( $rev )
277 if( $this->mTitle
->quickUserCan( 'edit' ) &&
278 !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
279 !$next->rev_deleted
& Revision
::DELETED_TEXT
) {
280 $undolink = $this->mSkin
->makeKnownLinkObj(
282 wfMsgHtml( 'editundo' ),
283 'action=edit&undoafter=' . $next->rev_id
. '&undo=' . $rev->getId()
285 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
290 $s .= ' (' . implode( ' | ', $tools ) . ')';
293 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
295 return "<li>$s</li>\n";
299 * Create a link to view this revision of the page
300 * @param Revision $rev
303 function revLink( $rev ) {
305 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
306 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
307 $link = $this->mSkin
->makeKnownLinkObj(
308 $this->mTitle
, $date, "oldid=" . $rev->getId() );
312 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
313 return '<span class="history-deleted">' . $link . '</span>';
319 * Create a diff-to-current link for this revision for this page
320 * @param Revision $rev
321 * @param Bool $latest, this is the latest revision of the page?
324 function curLink( $rev, $latest ) {
325 $cur = $this->message
['cur'];
326 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
329 return $this->mSkin
->makeKnownLinkObj(
331 'diff=' . $this->getLatestID() .
332 "&oldid=" . $rev->getId() );
337 * Create a diff-to-previous link for this revision for this page.
338 * @param Revision $prevRev, the previous revision
339 * @param mixed $next, the newer revision
340 * @param int $counter, what row on the history list this is
343 function lastLink( $prevRev, $next, $counter ) {
344 $last = $this->message
['last'];
345 # $next may either be a Row, null, or "unkown"
346 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
347 if( is_null($next) ) {
348 # Probably no next row
350 } elseif( $next === 'unknown' ) {
351 # Next row probably exists but is unknown, use an oldid=prev link
352 return $this->mSkin
->makeKnownLinkObj(
355 "diff=" . $prevRev->getId() . "&oldid=prev" );
356 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
359 return $this->mSkin
->makeKnownLinkObj(
362 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}"
366 "tabindex={$counter}"*/ );
371 * Create radio buttons for page history
373 * @param object $rev Revision
374 * @param bool $firstInList Is this version the first one?
375 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
376 * @return string HTML output for the radio buttons
378 function diffButtons( $rev, $firstInList, $counter ) {
379 if( $this->linesonpage
> 1) {
382 'value' => $rev->getId(),
385 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
386 $radio['disabled'] = 'disabled';
389 /** @todo: move title texts to javascript */
390 if ( $firstInList ) {
391 $first = Xml
::element( 'input', array_merge(
394 'style' => 'visibility:hidden',
395 'name' => 'oldid' ) ) );
396 $checkmark = array( 'checked' => 'checked' );
398 if( $counter == 2 ) {
399 $checkmark = array( 'checked' => 'checked' );
401 $checkmark = array();
403 $first = Xml
::element( 'input', array_merge(
406 array( 'name' => 'oldid' ) ) );
407 $checkmark = array();
409 $second = Xml
::element( 'input', array_merge(
412 array( 'name' => 'diff' ) ) );
413 return $first . $second;
419 /** @todo document */
420 function getLatestId() {
421 if( is_null( $this->mLatestId
) ) {
422 $id = $this->mTitle
->getArticleID();
423 $db = wfGetDB( DB_SLAVE
);
424 $this->mLatestId
= $db->selectField( 'page',
426 array( 'page_id' => $id ),
429 return $this->mLatestId
;
433 * Fetch an array of revisions, specified by a given limit, offset and
434 * direction. This is now only used by the feeds. It was previously
435 * used by the main UI but that's now handled by the pager.
437 function fetchRevisions($limit, $offset, $direction) {
438 $dbr = wfGetDB( DB_SLAVE
);
440 if ($direction == PageHistory
::DIR_PREV
)
441 list($dirs, $oper) = array("ASC", ">=");
442 else /* $direction == PageHistory::DIR_NEXT */
443 list($dirs, $oper) = array("DESC", "<=");
446 $offsets = array("rev_timestamp $oper '$offset'");
450 $page_id = $this->mTitle
->getArticleID();
454 Revision
::selectFields(),
455 array_merge(array("rev_page=$page_id"), $offsets),
457 array('ORDER BY' => "rev_timestamp $dirs",
458 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
462 while (($obj = $dbr->fetchObject($res)) != NULL)
468 /** @todo document */
469 function getNotificationTimestamp() {
470 global $wgUser, $wgShowUpdatedMarker;
472 if ($this->mNotificationTimestamp
!== NULL)
473 return $this->mNotificationTimestamp
;
475 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
476 return $this->mNotificationTimestamp
= false;
478 $dbr = wfGetDB(DB_SLAVE
);
480 $this->mNotificationTimestamp
= $dbr->selectField(
482 'wl_notificationtimestamp',
483 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
484 'wl_title' => $this->mTitle
->getDBkey(),
485 'wl_user' => $wgUser->getID()
489 // Don't use the special value reserved for telling whether the field is filled
490 if ( is_null( $this->mNotificationTimestamp
) ) {
491 $this->mNotificationTimestamp
= false;
494 return $this->mNotificationTimestamp
;
498 * Output a subscription feed listing recent edits to this page.
499 * @param string $type
501 function feed( $type ) {
502 require_once 'SpecialRecentchanges.php';
504 global $wgFeed, $wgFeedClasses;
508 $wgOut->addWikiMsg( 'feed-unavailable' );
512 if( !isset( $wgFeedClasses[$type] ) ) {
514 $wgOut->addWikiMsg( 'feed-invalid' );
518 $feed = new $wgFeedClasses[$type](
519 $this->mTitle
->getPrefixedText() . ' - ' .
520 wfMsgForContent( 'history-feed-title' ),
521 wfMsgForContent( 'history-feed-description' ),
522 $this->mTitle
->getFullUrl( 'action=history' ) );
524 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
527 foreach( $items as $row ) {
528 $feed->outItem( $this->feedItem( $row ) );
531 $feed->outItem( $this->feedEmpty() );
536 function feedEmpty() {
539 wfMsgForContent( 'nohistory' ),
540 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
541 $this->mTitle
->getFullUrl(),
542 wfTimestamp( TS_MW
),
544 $this->mTitle
->getTalkPage()->getFullUrl() );
548 * Generate a FeedItem object from a given revision table row
549 * Borrows Recent Changes' feed generation functions for formatting;
550 * includes a diff to the previous revision (if any).
555 function feedItem( $row ) {
556 $rev = new Revision( $row );
557 $rev->setTitle( $this->mTitle
);
558 $text = rcFormatDiffRow( $this->mTitle
,
559 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
561 $rev->getTimestamp(),
562 $rev->getComment() );
564 if( $rev->getComment() == '' ) {
566 $title = wfMsgForContent( 'history-feed-item-nocomment',
568 $wgContLang->timeanddate( $rev->getTimestamp() ) );
570 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
576 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
577 $rev->getTimestamp(),
579 $this->mTitle
->getTalkPage()->getFullUrl() );
583 * Quickie hack... strip out wikilinks to more legible form from the comment.
585 function stripComment( $text ) {
586 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
594 class PageHistoryPager
extends ReverseChronologicalPager
{
595 public $mLastRow = false, $mPageHistory;
597 function __construct( $pageHistory ) {
598 parent
::__construct();
599 $this->mPageHistory
= $pageHistory;
602 function getQueryInfo() {
604 'tables' => 'revision',
605 'fields' => Revision
::selectFields(),
606 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
607 'options' => array( 'USE INDEX' => 'page_timestamp' )
611 function getIndexField() {
612 return 'rev_timestamp';
615 function formatRow( $row ) {
616 if ( $this->mLastRow
) {
617 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
618 $firstInList = $this->mCounter
== 1;
619 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
620 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
624 $this->mLastRow
= $row;
628 function getStartBody() {
629 $this->mLastRow
= false;
634 function getEndBody() {
635 if ( $this->mLastRow
) {
636 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
637 $firstInList = $this->mCounter
== 1;
638 if ( $this->mIsBackwards
) {
639 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
640 if ( $this->mOffset
== '' ) {
646 # The next row is the past-the-end row
647 $next = $this->mPastTheEndRow
;
649 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
650 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );