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( $this->mTitle
->getPrefixedText() );
65 $wgOut->setArticleFlag( false );
66 $wgOut->setArticleRelated( true );
67 $wgOut->setRobotpolicy( 'noindex,nofollow' );
68 $wgOut->setSyndicated( true );
70 $logPage = SpecialPage
::getTitleFor( 'Log' );
71 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
73 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
74 $wgOut->setSubtitle( $subtitle );
76 $feedType = $wgRequest->getVal( 'feed' );
78 wfProfileOut( $fname );
79 return $this->feed( $feedType );
83 * Fail if article doesn't exist.
85 if( !$this->mTitle
->exists() ) {
86 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
87 wfProfileOut( $fname );
93 * "go=first" means to jump to the last (earliest) history page.
94 * This is deprecated, it no longer appears in the user interface
96 if ( $wgRequest->getText("go") == 'first' ) {
97 $limit = $wgRequest->getInt( 'limit', 50 );
98 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
102 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
107 $pager = new PageHistoryPager( $this );
108 $this->linesonpage
= $pager->getNumRows();
110 $pager->getNavigationBar() .
111 $this->beginHistoryList() .
113 $this->endHistoryList() .
114 $pager->getNavigationBar()
116 wfProfileOut( $fname );
119 /** @todo document */
120 function beginHistoryList() {
122 $this->lastdate
= '';
123 $s = wfMsgExt( 'histlegend', array( 'parse') );
124 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
125 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
127 // The following line is SUPPOSED to have double-quotes around the
128 // $prefixedkey variable, because htmlspecialchars() doesn't escape
131 // On at least two occasions people have changed it to single-quotes,
132 // which creates invalid HTML and incorrect display of the resulting
135 // Please do not break this a third time. Thank you for your kind
136 // consideration and cooperation.
138 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
140 $s .= $this->submitButton();
141 $s .= '<ul id="pagehistory">' . "\n";
145 /** @todo document */
146 function endHistoryList() {
148 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
153 /** @todo document */
154 function submitButton( $bits = array() ) {
155 return ( $this->linesonpage
> 0 )
156 ?
wfElement( 'input', array_merge( $bits,
158 'class' => 'historysubmit',
160 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
161 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
162 'value' => wfMsg( 'compareselectedversions' ),
168 * Returns a row from the history printout.
170 * @todo document some more, and maybe clean up the code (some params redundant?)
172 * @param object $row The database row corresponding to the line (or is it the previous line?).
173 * @param object $next The database row corresponding to the next line (or is it this one?).
174 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
175 * @param $notificationtimestamp
176 * @param bool $latest Whether this row corresponds to the page's latest revision.
177 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
178 * @return string HTML output for the row
180 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
181 global $wgUser, $wgLang;
182 $rev = new Revision( $row );
183 $rev->setTitle( $this->mTitle
);
186 $curlink = $this->curLink( $rev, $latest );
187 $lastlink = $this->lastLink( $rev, $next, $counter );
188 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
189 $link = $this->revLink( $rev );
191 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
192 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
194 $s .= "($curlink) ($lastlink) $arbitrary";
196 if( $wgUser->isAllowed( 'deleterevision' ) ) {
197 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
199 // We don't currently handle well changing the top revision's settings
200 $del = wfMsgHtml( 'rev-delundel' );
201 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
202 // If revision was hidden from sysops
203 $del = wfMsgHtml( 'rev-delundel' );
205 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
206 wfMsg( 'rev-delundel' ),
207 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
208 '&oldid=' . urlencode( $rev->getId() ) );
210 $s .= " (<small>$del</small>) ";
214 #getUser is safe, but this avoids making the invalid untargeted contribs links
215 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
216 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
218 $s .= " <span class='history-user'>$user</span>";
220 if( $row->rev_minor_edit
) {
221 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
224 if (!is_null($size = $rev->getSize())) {
226 $stxt = wfMsgHtml('historyempty');
228 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
229 $s .= " <span class=\"history-size\">$stxt</span>";
232 #getComment is safe, but this is better formatted
233 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
234 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
235 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
237 $s .= $this->mSkin
->revComment( $rev );
240 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
241 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
243 #add blurb about text having been deleted
244 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
245 $s .= ' ' . wfMsgHtml( 'deletedrev' );
247 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
248 $s .= ' '.$this->mSkin
->generateRollback( $rev );
251 if ( $wgUser->isAllowed( 'rollback' ) && !is_null($next)) {
252 $undolink = $this->mSkin
->makeKnownLinkObj(
253 $this->mTitle
, '('.htmlspecialchars( wfMsg( 'editundo' ) ).')',
254 'action=edit&undoafter=' . $next->rev_id
. '&undo=' . $rev->getId(),
256 $s .= ' <span class="mw-history-undo"> '."$undolink </span>";
259 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
266 /** @todo document */
267 function revLink( $rev ) {
269 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
270 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
271 $link = $this->mSkin
->makeKnownLinkObj(
272 $this->mTitle
, $date, "oldid=" . $rev->getId() );
276 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
277 return '<span class="history-deleted">' . $link . '</span>';
282 /** @todo document */
283 function curLink( $rev, $latest ) {
284 $cur = wfMsgExt( 'cur', array( 'escape') );
285 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
288 return $this->mSkin
->makeKnownLinkObj(
290 'diff=' . $this->getLatestID() .
291 "&oldid=" . $rev->getId() );
295 /** @todo document */
296 function lastLink( $rev, $next, $counter ) {
297 $last = wfMsgExt( 'last', array( 'escape' ) );
298 if ( is_null( $next ) ) {
299 # Probably no next row
301 } elseif ( $next === 'unknown' ) {
302 # Next row probably exists but is unknown, use an oldid=prev link
303 return $this->mSkin
->makeKnownLinkObj(
306 "diff=" . $rev->getId() . "&oldid=prev" );
307 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
310 return $this->mSkin
->makeKnownLinkObj(
313 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
317 "tabindex={$counter}"*/ );
321 /** @todo document */
322 function diffButtons( $rev, $firstInList, $counter ) {
323 if( $this->linesonpage
> 1) {
326 'value' => $rev->getId(),
327 # do we really need to flood this on every item?
328 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
331 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
332 $radio['disabled'] = 'disabled';
335 /** @todo: move title texts to javascript */
336 if ( $firstInList ) {
337 $first = wfElement( 'input', array_merge(
340 'style' => 'visibility:hidden',
341 'name' => 'oldid' ) ) );
342 $checkmark = array( 'checked' => 'checked' );
344 if( $counter == 2 ) {
345 $checkmark = array( 'checked' => 'checked' );
347 $checkmark = array();
349 $first = wfElement( 'input', array_merge(
352 array( 'name' => 'oldid' ) ) );
353 $checkmark = array();
355 $second = wfElement( 'input', array_merge(
358 array( 'name' => 'diff' ) ) );
359 return $first . $second;
365 /** @todo document */
366 function getLatestId() {
367 if( is_null( $this->mLatestId
) ) {
368 $id = $this->mTitle
->getArticleID();
369 $db = wfGetDB(DB_SLAVE
);
370 $this->mLatestId
= $db->selectField( 'page',
372 array( 'page_id' => $id ),
373 'PageHistory::getLatestID' );
375 return $this->mLatestId
;
379 * Fetch an array of revisions, specified by a given limit, offset and
380 * direction. This is now only used by the feeds. It was previously
381 * used by the main UI but that's now handled by the pager.
383 function fetchRevisions($limit, $offset, $direction) {
384 $fname = 'PageHistory::fetchRevisions';
386 $dbr = wfGetDB( DB_SLAVE
);
388 if ($direction == PageHistory
::DIR_PREV
)
389 list($dirs, $oper) = array("ASC", ">=");
390 else /* $direction == PageHistory::DIR_NEXT */
391 list($dirs, $oper) = array("DESC", "<=");
394 $offsets = array("rev_timestamp $oper '$offset'");
398 $page_id = $this->mTitle
->getArticleID();
402 Revision
::selectFields(),
403 array_merge(array("rev_page=$page_id"), $offsets),
405 array('ORDER BY' => "rev_timestamp $dirs",
406 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
410 while (($obj = $dbr->fetchObject($res)) != NULL)
416 /** @todo document */
417 function getNotificationTimestamp() {
418 global $wgUser, $wgShowUpdatedMarker;
419 $fname = 'PageHistory::getNotficationTimestamp';
421 if ($this->mNotificationTimestamp
!== NULL)
422 return $this->mNotificationTimestamp
;
424 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
425 return $this->mNotificationTimestamp
= false;
427 $dbr = wfGetDB(DB_SLAVE
);
429 $this->mNotificationTimestamp
= $dbr->selectField(
431 'wl_notificationtimestamp',
432 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
433 'wl_title' => $this->mTitle
->getDBkey(),
434 'wl_user' => $wgUser->getID()
438 // Don't use the special value reserved for telling whether the field is filled
439 if ( is_null( $this->mNotificationTimestamp
) ) {
440 $this->mNotificationTimestamp
= false;
443 return $this->mNotificationTimestamp
;
447 * Output a subscription feed listing recent edits to this page.
448 * @param string $type
450 function feed( $type ) {
451 require_once 'SpecialRecentchanges.php';
453 global $wgFeedClasses;
454 if( !isset( $wgFeedClasses[$type] ) ) {
456 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
460 $feed = new $wgFeedClasses[$type](
461 $this->mTitle
->getPrefixedText() . ' - ' .
462 wfMsgForContent( 'history-feed-title' ),
463 wfMsgForContent( 'history-feed-description' ),
464 $this->mTitle
->getFullUrl( 'action=history' ) );
466 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
469 foreach( $items as $row ) {
470 $feed->outItem( $this->feedItem( $row ) );
473 $feed->outItem( $this->feedEmpty() );
478 function feedEmpty() {
481 wfMsgForContent( 'nohistory' ),
482 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
483 $this->mTitle
->getFullUrl(),
484 wfTimestamp( TS_MW
),
486 $this->mTitle
->getTalkPage()->getFullUrl() );
490 * Generate a FeedItem object from a given revision table row
491 * Borrows Recent Changes' feed generation functions for formatting;
492 * includes a diff to the previous revision (if any).
497 function feedItem( $row ) {
498 $rev = new Revision( $row );
499 $rev->setTitle( $this->mTitle
);
500 $text = rcFormatDiffRow( $this->mTitle
,
501 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
503 $rev->getTimestamp(),
504 $rev->getComment() );
506 if( $rev->getComment() == '' ) {
508 $title = wfMsgForContent( 'history-feed-item-nocomment',
510 $wgContLang->timeanddate( $rev->getTimestamp() ) );
512 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
518 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
519 $rev->getTimestamp(),
521 $this->mTitle
->getTalkPage()->getFullUrl() );
525 * Quickie hack... strip out wikilinks to more legible form from the comment.
527 function stripComment( $text ) {
528 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
536 class PageHistoryPager
extends ReverseChronologicalPager
{
537 public $mLastRow = false, $mPageHistory;
539 function __construct( $pageHistory ) {
540 parent
::__construct();
541 $this->mPageHistory
= $pageHistory;
544 function getQueryInfo() {
546 'tables' => 'revision',
547 'fields' => Revision
::selectFields(),
548 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
549 'options' => array( 'USE INDEX' => 'page_timestamp' )
553 function getIndexField() {
554 return 'rev_timestamp';
557 function formatRow( $row ) {
558 if ( $this->mLastRow
) {
559 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
560 $firstInList = $this->mCounter
== 1;
561 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
562 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
566 $this->mLastRow
= $row;
570 function getStartBody() {
571 $this->mLastRow
= false;
576 function getEndBody() {
577 if ( $this->mLastRow
) {
578 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
579 $firstInList = $this->mCounter
== 1;
580 if ( $this->mIsBackwards
) {
581 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
582 if ( $this->mOffset
== '' ) {
588 # The next row is the past-the-end row
589 $next = $this->mPastTheEndRow
;
591 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
592 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );