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
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();
44 * Print the history page for an article.
49 global $wgOut, $wgRequest, $wgTitle;
52 * Allow client caching.
55 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
56 /* Client cache fresh and headers sent, nothing more to do. */
59 $fname = 'PageHistory::history';
60 wfProfileIn( $fname );
63 * Setup page variables.
65 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
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" ) );
106 $pager = new PageHistoryPager( $this );
107 $this->linesonpage
= $pager->getNumRows();
109 $pager->getNavigationBar() .
110 $this->beginHistoryList() .
112 $this->endHistoryList() .
113 $pager->getNavigationBar()
115 wfProfileOut( $fname );
118 /** @todo document */
119 function beginHistoryList() {
121 $this->lastdate
= '';
122 $s = wfMsgExt( 'histlegend', array( 'parse') );
123 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
124 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
126 // The following line is SUPPOSED to have double-quotes around the
127 // $prefixedkey variable, because htmlspecialchars() doesn't escape
130 // On at least two occasions people have changed it to single-quotes,
131 // which creates invalid HTML and incorrect display of the resulting
134 // Please do not break this a third time. Thank you for your kind
135 // consideration and cooperation.
137 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
139 $s .= $this->submitButton();
140 $s .= '<ul id="pagehistory">' . "\n";
144 /** @todo document */
145 function endHistoryList() {
147 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
152 /** @todo document */
153 function submitButton( $bits = array() ) {
154 return ( $this->linesonpage
> 0 )
155 ?
wfElement( 'input', array_merge( $bits,
157 'class' => 'historysubmit',
159 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
160 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
161 'value' => wfMsg( 'compareselectedversions' ),
167 * Returns a row from the history printout.
169 * @todo document some more, and maybe clean up the code (some params redundant?)
171 * @param object $row The database row corresponding to the line (or is it the previous line?).
172 * @param object $next The database row corresponding to the next line (or is it this one?).
173 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
174 * @param $notificationtimestamp
175 * @param bool $latest Whether this row corresponds to the page's latest revision.
176 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
177 * @return string HTML output for the row
179 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
181 $rev = new Revision( $row );
182 $rev->setTitle( $this->mTitle
);
185 $curlink = $this->curLink( $rev, $latest );
186 $lastlink = $this->lastLink( $rev, $next, $counter );
187 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
188 $link = $this->revLink( $rev );
190 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
191 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
193 $s .= "($curlink) ($lastlink) $arbitrary";
195 if( $wgUser->isAllowed( 'deleterevision' ) ) {
196 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
198 // We don't currently handle well changing the top revision's settings
199 $del = wfMsgHtml( 'rev-delundel' );
200 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
201 // If revision was hidden from sysops
202 $del = wfMsgHtml( 'rev-delundel' );
204 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
205 wfMsg( 'rev-delundel' ),
206 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
207 '&oldid=' . urlencode( $rev->getId() ) );
209 $s .= " (<small>$del</small>) ";
213 #getUser is safe, but this avoids making the invalid untargeted contribs links
214 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
215 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
217 $s .= " <span class='history-user'>$user</span>";
219 if( $row->rev_minor_edit
) {
220 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
223 if (!is_null($size = $rev->getSize())) {
225 $stxt = wfMsgHtml('historyempty');
227 $stxt = wfMsgHtml('historysize', $size);
228 $s .= " <span class=\"history-size\">$stxt</span>";
231 #getComment is safe, but this is better formatted
232 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
233 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
234 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
236 $s .= $this->mSkin
->revComment( $rev );
239 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
240 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
242 #add blurb about text having been deleted
243 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
244 $s .= ' ' . wfMsgHtml( 'deletedrev' );
246 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
247 $s .= ' '.$this->mSkin
->generateRollback( $rev );
254 /** @todo document */
255 function revLink( $rev ) {
257 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
258 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
259 $link = $this->mSkin
->makeKnownLinkObj(
260 $this->mTitle
, $date, "oldid=" . $rev->getId() );
264 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
265 return '<span class="history-deleted">' . $link . '</span>';
270 /** @todo document */
271 function curLink( $rev, $latest ) {
272 $cur = wfMsgExt( 'cur', array( 'escape') );
273 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
276 return $this->mSkin
->makeKnownLinkObj(
278 'diff=' . $this->getLatestID() .
279 "&oldid=" . $rev->getId() );
283 /** @todo document */
284 function lastLink( $rev, $next, $counter ) {
285 $last = wfMsgExt( 'last', array( 'escape' ) );
286 if ( is_null( $next ) ) {
287 # Probably no next row
289 } elseif ( $next === 'unknown' ) {
290 # Next row probably exists but is unknown, use an oldid=prev link
291 return $this->mSkin
->makeKnownLinkObj(
294 "diff=" . $rev->getId() . "&oldid=prev" );
295 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
298 return $this->mSkin
->makeKnownLinkObj(
301 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
305 "tabindex={$counter}"*/ );
309 /** @todo document */
310 function diffButtons( $rev, $firstInList, $counter ) {
311 if( $this->linesonpage
> 1) {
314 'value' => $rev->getId(),
315 # do we really need to flood this on every item?
316 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
319 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
320 $radio['disabled'] = 'disabled';
323 /** @todo: move title texts to javascript */
324 if ( $firstInList ) {
325 $first = wfElement( 'input', array_merge(
328 'style' => 'visibility:hidden',
329 'name' => 'oldid' ) ) );
330 $checkmark = array( 'checked' => 'checked' );
332 if( $counter == 2 ) {
333 $checkmark = array( 'checked' => 'checked' );
335 $checkmark = array();
337 $first = wfElement( 'input', array_merge(
340 array( 'name' => 'oldid' ) ) );
341 $checkmark = array();
343 $second = wfElement( 'input', array_merge(
346 array( 'name' => 'diff' ) ) );
347 return $first . $second;
353 /** @todo document */
354 function getLatestId() {
355 if( is_null( $this->mLatestId
) ) {
356 $id = $this->mTitle
->getArticleID();
357 $db = wfGetDB(DB_SLAVE
);
358 $this->mLatestId
= $db->selectField( 'page',
360 array( 'page_id' => $id ),
361 'PageHistory::getLatestID' );
363 return $this->mLatestId
;
367 * Fetch an array of revisions, specified by a given limit, offset and
368 * direction. This is now only used by the feeds. It was previously
369 * used by the main UI but that's now handled by the pager.
371 function fetchRevisions($limit, $offset, $direction) {
372 $fname = 'PageHistory::fetchRevisions';
374 $dbr = wfGetDB( DB_SLAVE
);
376 if ($direction == PageHistory
::DIR_PREV
)
377 list($dirs, $oper) = array("ASC", ">=");
378 else /* $direction == PageHistory::DIR_NEXT */
379 list($dirs, $oper) = array("DESC", "<=");
382 $offsets = array("rev_timestamp $oper '$offset'");
386 $page_id = $this->mTitle
->getArticleID();
390 Revision
::selectFields(),
391 array_merge(array("rev_page=$page_id"), $offsets),
393 array('ORDER BY' => "rev_timestamp $dirs",
394 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
398 while (($obj = $dbr->fetchObject($res)) != NULL)
404 /** @todo document */
405 function getNotificationTimestamp() {
406 global $wgUser, $wgShowUpdatedMarker;
407 $fname = 'PageHistory::getNotficationTimestamp';
409 if ($this->mNotificationTimestamp
!== NULL)
410 return $this->mNotificationTimestamp
;
412 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
413 return $this->mNotificationTimestamp
= false;
415 $dbr = wfGetDB(DB_SLAVE
);
417 $this->mNotificationTimestamp
= $dbr->selectField(
419 'wl_notificationtimestamp',
420 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
421 'wl_title' => $this->mTitle
->getDBkey(),
422 'wl_user' => $wgUser->getID()
426 // Don't use the special value reserved for telling whether the field is filled
427 if ( is_null( $this->mNotificationTimestamp
) ) {
428 $this->mNotificationTimestamp
= false;
431 return $this->mNotificationTimestamp
;
435 * Output a subscription feed listing recent edits to this page.
436 * @param string $type
438 function feed( $type ) {
439 require_once 'SpecialRecentchanges.php';
441 global $wgFeedClasses;
442 if( !isset( $wgFeedClasses[$type] ) ) {
444 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
448 $feed = new $wgFeedClasses[$type](
449 $this->mTitle
->getPrefixedText() . ' - ' .
450 wfMsgForContent( 'history-feed-title' ),
451 wfMsgForContent( 'history-feed-description' ),
452 $this->mTitle
->getFullUrl( 'action=history' ) );
454 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
457 foreach( $items as $row ) {
458 $feed->outItem( $this->feedItem( $row ) );
461 $feed->outItem( $this->feedEmpty() );
466 function feedEmpty() {
469 wfMsgForContent( 'nohistory' ),
470 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
471 $this->mTitle
->getFullUrl(),
472 wfTimestamp( TS_MW
),
474 $this->mTitle
->getTalkPage()->getFullUrl() );
478 * Generate a FeedItem object from a given revision table row
479 * Borrows Recent Changes' feed generation functions for formatting;
480 * includes a diff to the previous revision (if any).
485 function feedItem( $row ) {
486 $rev = new Revision( $row );
487 $rev->setTitle( $this->mTitle
);
488 $text = rcFormatDiffRow( $this->mTitle
,
489 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
491 $rev->getTimestamp(),
492 $rev->getComment() );
494 if( $rev->getComment() == '' ) {
496 $title = wfMsgForContent( 'history-feed-item-nocomment',
498 $wgContLang->timeanddate( $rev->getTimestamp() ) );
500 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
506 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
507 $rev->getTimestamp(),
509 $this->mTitle
->getTalkPage()->getFullUrl() );
513 * Quickie hack... strip out wikilinks to more legible form from the comment.
515 function stripComment( $text ) {
516 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
521 class PageHistoryPager
extends ReverseChronologicalPager
{
522 public $mLastRow = false, $mPageHistory;
524 function __construct( $pageHistory ) {
525 parent
::__construct();
526 $this->mPageHistory
= $pageHistory;
529 function getQueryInfo() {
531 'tables' => 'revision',
532 'fields' => Revision
::selectFields(),
533 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
534 'options' => array( 'USE INDEX' => 'page_timestamp' )
538 function getIndexField() {
539 return 'rev_timestamp';
542 function formatRow( $row ) {
543 if ( $this->mLastRow
) {
544 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
545 $firstInList = $this->mCounter
== 1;
546 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
547 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
551 $this->mLastRow
= $row;
555 function getStartBody() {
556 $this->mLastRow
= false;
561 function getEndBody() {
562 if ( $this->mLastRow
) {
563 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
564 $firstInList = $this->mCounter
== 1;
565 if ( $this->mIsBackwards
) {
566 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
567 if ( $this->mOffset
== '' ) {
573 # The next row is the past-the-end row
574 $next = $this->mPastTheEndRow
;
576 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
577 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );