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();
43 * Print the history page for an article.
48 global $wgOut, $wgRequest, $wgTitle;
51 * Allow client caching.
54 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
55 /* Client cache fresh and headers sent, nothing more to do. */
58 $fname = 'PageHistory::history';
59 wfProfileIn( $fname );
62 * Setup page variables.
64 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle
->getPrefixedText() ) );
65 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
66 $wgOut->setArticleFlag( false );
67 $wgOut->setArticleRelated( true );
68 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 $wgOut->setSyndicated( true );
71 $logPage = SpecialPage
::getTitleFor( 'Log' );
72 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
73 $wgOut->setSubtitle( $logLink );
75 $feedType = $wgRequest->getVal( 'feed' );
77 wfProfileOut( $fname );
78 return $this->feed( $feedType );
82 * Fail if article doesn't exist.
84 if( !$this->mTitle
->exists() ) {
85 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
86 wfProfileOut( $fname );
91 * "go=first" means to jump to the last (earliest) history page.
92 * This is deprecated, it no longer appears in the user interface
94 if ( $wgRequest->getText("go") == 'first' ) {
95 $limit = $wgRequest->getInt( 'limit', 50 );
96 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
100 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
105 $pager = new PageHistoryPager( $this );
106 $this->linesonpage
= $pager->getNumRows();
108 $pager->getNavigationBar() .
109 $this->beginHistoryList() .
111 $this->endHistoryList() .
112 $pager->getNavigationBar()
114 wfProfileOut( $fname );
118 * Creates begin of history list with a submit button
120 * @return string HTML output
122 function beginHistoryList() {
124 $this->lastdate
= '';
125 $s = wfMsgExt( 'histlegend', array( 'parse') );
126 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
127 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
129 // The following line is SUPPOSED to have double-quotes around the
130 // $prefixedkey variable, because htmlspecialchars() doesn't escape
133 // On at least two occasions people have changed it to single-quotes,
134 // which creates invalid HTML and incorrect display of the resulting
137 // Please do not break this a third time. Thank you for your kind
138 // consideration and cooperation.
140 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
142 $s .= $this->submitButton();
143 $s .= '<ul id="pagehistory">' . "\n";
148 * Creates end of history list with a submit button
150 * @return string HTML output
152 function endHistoryList() {
154 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
160 * Creates a submit button
162 * @param array $bits optional CSS ID
163 * @return string HTML output for the submit button
165 function submitButton( $bits = array() ) {
166 # Disable submit button if history has 1 revision only
167 if ( $this->linesonpage
> 1 ) {
168 return Xml
::submitButton( wfMsg( 'compareselectedversions' ),
170 'class' => 'historysubmit',
171 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
172 'title' => wfMsg( 'tooltip-compareselectedversions' ),
181 * Returns a row from the history printout.
183 * @todo document some more, and maybe clean up the code (some params redundant?)
185 * @param object $row The database row corresponding to the line (or is it the previous line?).
186 * @param object $next The database row corresponding to the next line (or is it this one?).
187 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
188 * @param $notificationtimestamp
189 * @param bool $latest Whether this row corresponds to the page's latest revision.
190 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
191 * @return string HTML output for the row
193 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
194 global $wgUser, $wgLang;
195 $rev = new Revision( $row );
196 $rev->setTitle( $this->mTitle
);
199 $curlink = $this->curLink( $rev, $latest );
200 $lastlink = $this->lastLink( $rev, $next, $counter );
201 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
202 $link = $this->revLink( $rev );
204 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
205 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
207 $s .= "($curlink) ($lastlink) $arbitrary";
209 if( $wgUser->isAllowed( 'deleterevision' ) ) {
210 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
212 // We don't currently handle well changing the top revision's settings
213 $del = wfMsgHtml( 'rev-delundel' );
214 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
215 // If revision was hidden from sysops
216 $del = wfMsgHtml( 'rev-delundel' );
218 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
219 wfMsg( 'rev-delundel' ),
220 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
221 '&oldid=' . urlencode( $rev->getId() ) );
223 $s .= " (<small>$del</small>) ";
227 #getUser is safe, but this avoids making the invalid untargeted contribs links
228 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
229 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
231 $s .= " <span class='history-user'>$user</span>";
233 if( $row->rev_minor_edit
) {
234 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
237 if ( !is_null( $size = $rev->getSize() ) ) {
239 $stxt = wfMsgHtml( 'historyempty' );
241 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
242 $s .= " <span class=\"history-size\">$stxt</span>";
245 #getComment is safe, but this is better formatted
246 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
247 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
248 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
250 $s .= $this->mSkin
->revComment( $rev );
253 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
254 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
256 #add blurb about text having been deleted
257 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
258 $s .= ' ' . wfMsgHtml( 'deletedrev' );
263 if ( !is_null( $next ) && is_object( $next ) ) {
264 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
265 $tools[] = '<span class="mw-rollback-link">'
266 . $this->mSkin
->buildRollbackLink( $rev )
270 $undolink = $this->mSkin
->makeKnownLinkObj(
272 wfMsgHtml( 'editundo' ),
273 'action=edit&undoafter=' . $next->rev_id
. '&undo=' . $rev->getId()
275 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
279 $s .= ' (' . implode( ' | ', $tools ) . ')';
282 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
289 /** @todo document */
290 function revLink( $rev ) {
292 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
293 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
294 $link = $this->mSkin
->makeKnownLinkObj(
295 $this->mTitle
, $date, "oldid=" . $rev->getId() );
299 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
300 return '<span class="history-deleted">' . $link . '</span>';
305 /** @todo document */
306 function curLink( $rev, $latest ) {
307 $cur = wfMsgExt( 'cur', array( 'escape') );
308 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
311 return $this->mSkin
->makeKnownLinkObj(
313 'diff=' . $this->getLatestID() .
314 "&oldid=" . $rev->getId() );
318 /** @todo document */
319 function lastLink( $rev, $next, $counter ) {
320 $last = wfMsgExt( 'last', array( 'escape' ) );
321 if ( is_null( $next ) ) {
322 # Probably no next row
324 } elseif ( $next === 'unknown' ) {
325 # Next row probably exists but is unknown, use an oldid=prev link
326 return $this->mSkin
->makeKnownLinkObj(
329 "diff=" . $rev->getId() . "&oldid=prev" );
330 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
333 return $this->mSkin
->makeKnownLinkObj(
336 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
340 "tabindex={$counter}"*/ );
345 * Create radio buttons for page history
347 * @param object $rev Revision
348 * @param bool $firstInList Is this version the first one?
349 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
350 * @return string HTML output for the radio buttons
352 function diffButtons( $rev, $firstInList, $counter ) {
353 if( $this->linesonpage
> 1) {
356 'value' => $rev->getId(),
359 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
360 $radio['disabled'] = 'disabled';
363 /** @todo: move title texts to javascript */
364 if ( $firstInList ) {
365 $first = Xml
::element( 'input', array_merge(
368 'style' => 'visibility:hidden',
369 'name' => 'oldid' ) ) );
370 $checkmark = array( 'checked' => 'checked' );
372 if( $counter == 2 ) {
373 $checkmark = array( 'checked' => 'checked' );
375 $checkmark = array();
377 $first = Xml
::element( 'input', array_merge(
380 array( 'name' => 'oldid' ) ) );
381 $checkmark = array();
383 $second = Xml
::element( 'input', array_merge(
386 array( 'name' => 'diff' ) ) );
387 return $first . $second;
393 /** @todo document */
394 function getLatestId() {
395 if( is_null( $this->mLatestId
) ) {
396 $id = $this->mTitle
->getArticleID();
397 $db = wfGetDB(DB_SLAVE
);
398 $this->mLatestId
= $db->selectField( 'page',
400 array( 'page_id' => $id ),
401 'PageHistory::getLatestID' );
403 return $this->mLatestId
;
407 * Fetch an array of revisions, specified by a given limit, offset and
408 * direction. This is now only used by the feeds. It was previously
409 * used by the main UI but that's now handled by the pager.
411 function fetchRevisions($limit, $offset, $direction) {
412 $fname = 'PageHistory::fetchRevisions';
414 $dbr = wfGetDB( DB_SLAVE
);
416 if ($direction == PageHistory
::DIR_PREV
)
417 list($dirs, $oper) = array("ASC", ">=");
418 else /* $direction == PageHistory::DIR_NEXT */
419 list($dirs, $oper) = array("DESC", "<=");
422 $offsets = array("rev_timestamp $oper '$offset'");
426 $page_id = $this->mTitle
->getArticleID();
430 Revision
::selectFields(),
431 array_merge(array("rev_page=$page_id"), $offsets),
433 array('ORDER BY' => "rev_timestamp $dirs",
434 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
438 while (($obj = $dbr->fetchObject($res)) != NULL)
444 /** @todo document */
445 function getNotificationTimestamp() {
446 global $wgUser, $wgShowUpdatedMarker;
447 $fname = 'PageHistory::getNotficationTimestamp';
449 if ($this->mNotificationTimestamp
!== NULL)
450 return $this->mNotificationTimestamp
;
452 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
453 return $this->mNotificationTimestamp
= false;
455 $dbr = wfGetDB(DB_SLAVE
);
457 $this->mNotificationTimestamp
= $dbr->selectField(
459 'wl_notificationtimestamp',
460 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
461 'wl_title' => $this->mTitle
->getDBkey(),
462 'wl_user' => $wgUser->getID()
466 // Don't use the special value reserved for telling whether the field is filled
467 if ( is_null( $this->mNotificationTimestamp
) ) {
468 $this->mNotificationTimestamp
= false;
471 return $this->mNotificationTimestamp
;
475 * Output a subscription feed listing recent edits to this page.
476 * @param string $type
478 function feed( $type ) {
479 require_once 'SpecialRecentchanges.php';
481 global $wgFeedClasses;
482 if( !isset( $wgFeedClasses[$type] ) ) {
484 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
488 $feed = new $wgFeedClasses[$type](
489 $this->mTitle
->getPrefixedText() . ' - ' .
490 wfMsgForContent( 'history-feed-title' ),
491 wfMsgForContent( 'history-feed-description' ),
492 $this->mTitle
->getFullUrl( 'action=history' ) );
494 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
497 foreach( $items as $row ) {
498 $feed->outItem( $this->feedItem( $row ) );
501 $feed->outItem( $this->feedEmpty() );
506 function feedEmpty() {
509 wfMsgForContent( 'nohistory' ),
510 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
511 $this->mTitle
->getFullUrl(),
512 wfTimestamp( TS_MW
),
514 $this->mTitle
->getTalkPage()->getFullUrl() );
518 * Generate a FeedItem object from a given revision table row
519 * Borrows Recent Changes' feed generation functions for formatting;
520 * includes a diff to the previous revision (if any).
525 function feedItem( $row ) {
526 $rev = new Revision( $row );
527 $rev->setTitle( $this->mTitle
);
528 $text = rcFormatDiffRow( $this->mTitle
,
529 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
531 $rev->getTimestamp(),
532 $rev->getComment() );
534 if( $rev->getComment() == '' ) {
536 $title = wfMsgForContent( 'history-feed-item-nocomment',
538 $wgContLang->timeanddate( $rev->getTimestamp() ) );
540 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
546 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
547 $rev->getTimestamp(),
549 $this->mTitle
->getTalkPage()->getFullUrl() );
553 * Quickie hack... strip out wikilinks to more legible form from the comment.
555 function stripComment( $text ) {
556 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
564 class PageHistoryPager
extends ReverseChronologicalPager
{
565 public $mLastRow = false, $mPageHistory;
567 function __construct( $pageHistory ) {
568 parent
::__construct();
569 $this->mPageHistory
= $pageHistory;
572 function getQueryInfo() {
574 'tables' => 'revision',
575 'fields' => Revision
::selectFields(),
576 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
577 'options' => array( 'USE INDEX' => 'page_timestamp' )
581 function getIndexField() {
582 return 'rev_timestamp';
585 function formatRow( $row ) {
586 if ( $this->mLastRow
) {
587 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
588 $firstInList = $this->mCounter
== 1;
589 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
590 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
594 $this->mLastRow
= $row;
598 function getStartBody() {
599 $this->mLastRow
= false;
604 function getEndBody() {
605 if ( $this->mLastRow
) {
606 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
607 $firstInList = $this->mCounter
== 1;
608 if ( $this->mIsBackwards
) {
609 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
610 if ( $this->mOffset
== '' ) {
616 # The next row is the past-the-end row
617 $next = $this->mPastTheEndRow
;
619 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
620 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );