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() );
74 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
75 $wgOut->setSubtitle( $subtitle );
77 $feedType = $wgRequest->getVal( 'feed' );
79 wfProfileOut( $fname );
80 return $this->feed( $feedType );
84 * Fail if article doesn't exist.
86 if( !$this->mTitle
->exists() ) {
87 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
88 wfProfileOut( $fname );
94 * "go=first" means to jump to the last (earliest) history page.
95 * This is deprecated, it no longer appears in the user interface
97 if ( $wgRequest->getText("go") == 'first' ) {
98 $limit = $wgRequest->getInt( 'limit', 50 );
99 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
103 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
108 $pager = new PageHistoryPager( $this );
109 $this->linesonpage
= $pager->getNumRows();
111 $pager->getNavigationBar() .
112 $this->beginHistoryList() .
114 $this->endHistoryList() .
115 $pager->getNavigationBar()
117 wfProfileOut( $fname );
120 /** @todo document */
121 function beginHistoryList() {
123 $this->lastdate
= '';
124 $s = wfMsgExt( 'histlegend', array( 'parse') );
125 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
126 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
128 // The following line is SUPPOSED to have double-quotes around the
129 // $prefixedkey variable, because htmlspecialchars() doesn't escape
132 // On at least two occasions people have changed it to single-quotes,
133 // which creates invalid HTML and incorrect display of the resulting
136 // Please do not break this a third time. Thank you for your kind
137 // consideration and cooperation.
139 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
141 $s .= $this->submitButton();
142 $s .= '<ul id="pagehistory">' . "\n";
146 /** @todo document */
147 function endHistoryList() {
149 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
154 /** @todo document */
155 function submitButton( $bits = array() ) {
156 return ( $this->linesonpage
> 0 )
157 ?
wfElement( 'input', array_merge( $bits,
159 'class' => 'historysubmit',
161 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
162 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
163 'value' => wfMsg( 'compareselectedversions' ),
169 * Returns a row from the history printout.
171 * @todo document some more, and maybe clean up the code (some params redundant?)
173 * @param object $row The database row corresponding to the line (or is it the previous line?).
174 * @param object $next The database row corresponding to the next line (or is it this one?).
175 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
176 * @param $notificationtimestamp
177 * @param bool $latest Whether this row corresponds to the page's latest revision.
178 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
179 * @return string HTML output for the row
181 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
182 global $wgUser, $wgLang;
183 $rev = new Revision( $row );
184 $rev->setTitle( $this->mTitle
);
187 $curlink = $this->curLink( $rev, $latest );
188 $lastlink = $this->lastLink( $rev, $next, $counter );
189 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
190 $link = $this->revLink( $rev );
192 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
193 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
195 $s .= "($curlink) ($lastlink) $arbitrary";
197 if( $wgUser->isAllowed( 'deleterevision' ) ) {
198 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
200 // We don't currently handle well changing the top revision's settings
201 $del = wfMsgHtml( 'rev-delundel' );
202 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
203 // If revision was hidden from sysops
204 $del = wfMsgHtml( 'rev-delundel' );
206 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
207 wfMsg( 'rev-delundel' ),
208 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
209 '&oldid=' . urlencode( $rev->getId() ) );
211 $s .= " (<small>$del</small>) ";
215 #getUser is safe, but this avoids making the invalid untargeted contribs links
216 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
217 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
219 $s .= " <span class='history-user'>$user</span>";
221 if( $row->rev_minor_edit
) {
222 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
225 if ( !is_null( $size = $rev->getSize() ) ) {
227 $stxt = wfMsgHtml( 'historyempty' );
229 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
230 $s .= " <span class=\"history-size\">$stxt</span>";
233 #getComment is safe, but this is better formatted
234 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
235 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
236 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
238 $s .= $this->mSkin
->revComment( $rev );
241 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
242 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
244 #add blurb about text having been deleted
245 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
246 $s .= ' ' . wfMsgHtml( 'deletedrev' );
251 if ( !is_null( $next ) && is_object( $next ) ) {
252 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
253 $tools[] = '<span class="mw-rollback-link">'
254 . $this->mSkin
->buildRollbackLink( $rev )
258 $undolink = $this->mSkin
->makeKnownLinkObj(
260 wfMsgHtml( 'editundo' ),
261 'action=edit&undoafter=' . $next->rev_id
. '&undo=' . $rev->getId()
263 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
267 $s .= ' (' . implode( ' | ', $tools ) . ')';
270 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
277 /** @todo document */
278 function revLink( $rev ) {
280 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
281 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
282 $link = $this->mSkin
->makeKnownLinkObj(
283 $this->mTitle
, $date, "oldid=" . $rev->getId() );
287 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
288 return '<span class="history-deleted">' . $link . '</span>';
293 /** @todo document */
294 function curLink( $rev, $latest ) {
295 $cur = wfMsgExt( 'cur', array( 'escape') );
296 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
299 return $this->mSkin
->makeKnownLinkObj(
301 'diff=' . $this->getLatestID() .
302 "&oldid=" . $rev->getId() );
306 /** @todo document */
307 function lastLink( $rev, $next, $counter ) {
308 $last = wfMsgExt( 'last', array( 'escape' ) );
309 if ( is_null( $next ) ) {
310 # Probably no next row
312 } elseif ( $next === 'unknown' ) {
313 # Next row probably exists but is unknown, use an oldid=prev link
314 return $this->mSkin
->makeKnownLinkObj(
317 "diff=" . $rev->getId() . "&oldid=prev" );
318 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
321 return $this->mSkin
->makeKnownLinkObj(
324 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
328 "tabindex={$counter}"*/ );
332 /** @todo document */
333 function diffButtons( $rev, $firstInList, $counter ) {
334 if( $this->linesonpage
> 1) {
337 'value' => $rev->getId(),
338 # do we really need to flood this on every item?
339 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
342 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
343 $radio['disabled'] = 'disabled';
346 /** @todo: move title texts to javascript */
347 if ( $firstInList ) {
348 $first = wfElement( 'input', array_merge(
351 'style' => 'visibility:hidden',
352 'name' => 'oldid' ) ) );
353 $checkmark = array( 'checked' => 'checked' );
355 if( $counter == 2 ) {
356 $checkmark = array( 'checked' => 'checked' );
358 $checkmark = array();
360 $first = wfElement( 'input', array_merge(
363 array( 'name' => 'oldid' ) ) );
364 $checkmark = array();
366 $second = wfElement( 'input', array_merge(
369 array( 'name' => 'diff' ) ) );
370 return $first . $second;
376 /** @todo document */
377 function getLatestId() {
378 if( is_null( $this->mLatestId
) ) {
379 $id = $this->mTitle
->getArticleID();
380 $db = wfGetDB(DB_SLAVE
);
381 $this->mLatestId
= $db->selectField( 'page',
383 array( 'page_id' => $id ),
384 'PageHistory::getLatestID' );
386 return $this->mLatestId
;
390 * Fetch an array of revisions, specified by a given limit, offset and
391 * direction. This is now only used by the feeds. It was previously
392 * used by the main UI but that's now handled by the pager.
394 function fetchRevisions($limit, $offset, $direction) {
395 $fname = 'PageHistory::fetchRevisions';
397 $dbr = wfGetDB( DB_SLAVE
);
399 if ($direction == PageHistory
::DIR_PREV
)
400 list($dirs, $oper) = array("ASC", ">=");
401 else /* $direction == PageHistory::DIR_NEXT */
402 list($dirs, $oper) = array("DESC", "<=");
405 $offsets = array("rev_timestamp $oper '$offset'");
409 $page_id = $this->mTitle
->getArticleID();
413 Revision
::selectFields(),
414 array_merge(array("rev_page=$page_id"), $offsets),
416 array('ORDER BY' => "rev_timestamp $dirs",
417 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
421 while (($obj = $dbr->fetchObject($res)) != NULL)
427 /** @todo document */
428 function getNotificationTimestamp() {
429 global $wgUser, $wgShowUpdatedMarker;
430 $fname = 'PageHistory::getNotficationTimestamp';
432 if ($this->mNotificationTimestamp
!== NULL)
433 return $this->mNotificationTimestamp
;
435 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
436 return $this->mNotificationTimestamp
= false;
438 $dbr = wfGetDB(DB_SLAVE
);
440 $this->mNotificationTimestamp
= $dbr->selectField(
442 'wl_notificationtimestamp',
443 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
444 'wl_title' => $this->mTitle
->getDBkey(),
445 'wl_user' => $wgUser->getID()
449 // Don't use the special value reserved for telling whether the field is filled
450 if ( is_null( $this->mNotificationTimestamp
) ) {
451 $this->mNotificationTimestamp
= false;
454 return $this->mNotificationTimestamp
;
458 * Output a subscription feed listing recent edits to this page.
459 * @param string $type
461 function feed( $type ) {
462 require_once 'SpecialRecentchanges.php';
464 global $wgFeedClasses;
465 if( !isset( $wgFeedClasses[$type] ) ) {
467 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
471 $feed = new $wgFeedClasses[$type](
472 $this->mTitle
->getPrefixedText() . ' - ' .
473 wfMsgForContent( 'history-feed-title' ),
474 wfMsgForContent( 'history-feed-description' ),
475 $this->mTitle
->getFullUrl( 'action=history' ) );
477 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
480 foreach( $items as $row ) {
481 $feed->outItem( $this->feedItem( $row ) );
484 $feed->outItem( $this->feedEmpty() );
489 function feedEmpty() {
492 wfMsgForContent( 'nohistory' ),
493 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
494 $this->mTitle
->getFullUrl(),
495 wfTimestamp( TS_MW
),
497 $this->mTitle
->getTalkPage()->getFullUrl() );
501 * Generate a FeedItem object from a given revision table row
502 * Borrows Recent Changes' feed generation functions for formatting;
503 * includes a diff to the previous revision (if any).
508 function feedItem( $row ) {
509 $rev = new Revision( $row );
510 $rev->setTitle( $this->mTitle
);
511 $text = rcFormatDiffRow( $this->mTitle
,
512 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
514 $rev->getTimestamp(),
515 $rev->getComment() );
517 if( $rev->getComment() == '' ) {
519 $title = wfMsgForContent( 'history-feed-item-nocomment',
521 $wgContLang->timeanddate( $rev->getTimestamp() ) );
523 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
529 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
530 $rev->getTimestamp(),
532 $this->mTitle
->getTalkPage()->getFullUrl() );
536 * Quickie hack... strip out wikilinks to more legible form from the comment.
538 function stripComment( $text ) {
539 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
547 class PageHistoryPager
extends ReverseChronologicalPager
{
548 public $mLastRow = false, $mPageHistory;
550 function __construct( $pageHistory ) {
551 parent
::__construct();
552 $this->mPageHistory
= $pageHistory;
555 function getQueryInfo() {
557 'tables' => 'revision',
558 'fields' => Revision
::selectFields(),
559 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
560 'options' => array( 'USE INDEX' => 'page_timestamp' )
564 function getIndexField() {
565 return 'rev_timestamp';
568 function formatRow( $row ) {
569 if ( $this->mLastRow
) {
570 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
571 $firstInList = $this->mCounter
== 1;
572 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
573 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
577 $this->mLastRow
= $row;
581 function getStartBody() {
582 $this->mLastRow
= false;
587 function getEndBody() {
588 if ( $this->mLastRow
) {
589 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
590 $firstInList = $this->mCounter
== 1;
591 if ( $this->mIsBackwards
) {
592 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
593 if ( $this->mOffset
== '' ) {
599 # The next row is the past-the-end row
600 $next = $this->mPastTheEndRow
;
602 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
603 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );