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();
41 $this->preCacheMessages();
45 * As we use the same small set of messages in various methods and that
46 * they are called often, we call them once and save them in $this->message
48 function preCacheMessages() {
49 // Precache various messages
50 if( !isset( $this->message
) ) {
51 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
52 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
58 * Print the history page for an article.
63 global $wgOut, $wgRequest, $wgTitle;
66 * Allow client caching.
69 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
70 /* Client cache fresh and headers sent, nothing more to do. */
73 $fname = 'PageHistory::history';
74 wfProfileIn( $fname );
77 * Setup page variables.
79 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
80 $wgOut->setArticleFlag( false );
81 $wgOut->setArticleRelated( true );
82 $wgOut->setRobotpolicy( 'noindex,nofollow' );
83 $wgOut->setSyndicated( true );
85 $logPage = SpecialPage
::getTitleFor( 'Log' );
86 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
88 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
89 $wgOut->setSubtitle( $subtitle );
91 $feedType = $wgRequest->getVal( 'feed' );
93 wfProfileOut( $fname );
94 return $this->feed( $feedType );
98 * Fail if article doesn't exist.
100 if( !$this->mTitle
->exists() ) {
101 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
102 wfProfileOut( $fname );
108 * "go=first" means to jump to the last (earliest) history page.
109 * This is deprecated, it no longer appears in the user interface
111 if ( $wgRequest->getText("go") == 'first' ) {
112 $limit = $wgRequest->getInt( 'limit', 50 );
113 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
120 $pager = new PageHistoryPager( $this );
121 $this->linesonpage
= $pager->getNumRows();
123 $pager->getNavigationBar() .
124 $this->beginHistoryList() .
126 $this->endHistoryList() .
127 $pager->getNavigationBar()
129 wfProfileOut( $fname );
132 /** @todo document */
133 function beginHistoryList() {
135 $this->lastdate
= '';
136 $s = wfMsgExt( 'histlegend', array( 'parse') );
137 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
138 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
140 // The following line is SUPPOSED to have double-quotes around the
141 // $prefixedkey variable, because htmlspecialchars() doesn't escape
144 // On at least two occasions people have changed it to single-quotes,
145 // which creates invalid HTML and incorrect display of the resulting
148 // Please do not break this a third time. Thank you for your kind
149 // consideration and cooperation.
151 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
153 $s .= $this->submitButton();
154 $s .= '<ul id="pagehistory">' . "\n";
158 /** @todo document */
159 function endHistoryList() {
161 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
166 /** @todo document */
167 function submitButton( $bits = array() ) {
168 return ( $this->linesonpage
> 0 )
169 ?
wfElement( 'input', array_merge( $bits,
171 'class' => 'historysubmit',
173 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
174 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
175 'value' => wfMsg( '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 $s .= "($curlink) ($lastlink) $arbitrary";
206 if( $wgUser->isAllowed( 'deleterevision' ) ) {
207 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
209 // We don't currently handle well changing the top revision's settings
210 $del = $this->message
['rev-delundel'];
211 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
212 // If revision was hidden from sysops
213 $del = $this->message
['rev-delundel'];
215 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
216 $this->message
['rev-delundel'],
217 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
218 '&oldid=' . urlencode( $rev->getId() ) );
219 // Bolden oversighted content
220 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
221 $del = "<strong>$del</strong>";
223 $s .= " <tt>(<small>$del</small>)</tt> ";
227 $s .= $this->mSkin
->revUserTools( $rev, true);
229 if( $row->rev_minor_edit
) {
230 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
233 if (!is_null($size = $rev->getSize())) {
235 $stxt = wfMsgHtml('historyempty');
237 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
238 $s .= " <span class=\"history-size\">$stxt</span>";
241 #getComment is safe, but this is better formatted
242 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
243 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
244 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
246 $s .= $this->mSkin
->revComment( $rev );
249 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
250 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
252 #add blurb about text having been deleted
253 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
254 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
256 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
257 $s .= ' '.$this->mSkin
->generateRollback( $rev );
264 /** @todo document */
265 function revLink( $rev ) {
267 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
268 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
269 $link = $this->mSkin
->makeKnownLinkObj(
270 $this->mTitle
, $date, "oldid=" . $rev->getId() );
274 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
275 return '<span class="history-deleted">' . $link . '</span>';
280 /** @todo document */
281 function curLink( $rev, $latest ) {
282 $cur = $this->message
['cur'];
283 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
286 return $this->mSkin
->makeKnownLinkObj(
288 'diff=' . $this->getLatestID() .
289 "&oldid=" . $rev->getId() );
293 /** @todo document */
294 function lastLink( $rev, $next, $counter ) {
295 $last = $this->message
['last'];
296 if ( is_null( $next ) ) {
297 # Probably no next row
299 } elseif ( $next === 'unknown' ) {
300 # Next row probably exists but is unknown, use an oldid=prev link
301 return $this->mSkin
->makeKnownLinkObj(
304 "diff=" . $rev->getId() . "&oldid=prev" );
305 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
308 return $this->mSkin
->makeKnownLinkObj(
311 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
315 "tabindex={$counter}"*/ );
319 /** @todo document */
320 function diffButtons( $rev, $firstInList, $counter ) {
321 if( $this->linesonpage
> 1) {
324 'value' => $rev->getId(),
325 # do we really need to flood this on every item?
326 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
329 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
330 $radio['disabled'] = 'disabled';
333 /** @todo: move title texts to javascript */
334 if ( $firstInList ) {
335 $first = wfElement( 'input', array_merge(
338 'style' => 'visibility:hidden',
339 'name' => 'oldid' ) ) );
340 $checkmark = array( 'checked' => 'checked' );
342 if( $counter == 2 ) {
343 $checkmark = array( 'checked' => 'checked' );
345 $checkmark = array();
347 $first = wfElement( 'input', array_merge(
350 array( 'name' => 'oldid' ) ) );
351 $checkmark = array();
353 $second = wfElement( 'input', array_merge(
356 array( 'name' => 'diff' ) ) );
357 return $first . $second;
363 /** @todo document */
364 function getLatestId() {
365 if( is_null( $this->mLatestId
) ) {
366 $id = $this->mTitle
->getArticleID();
367 $db = wfGetDB(DB_SLAVE
);
368 $this->mLatestId
= $db->selectField( 'page',
370 array( 'page_id' => $id ),
371 'PageHistory::getLatestID' );
373 return $this->mLatestId
;
377 * Fetch an array of revisions, specified by a given limit, offset and
378 * direction. This is now only used by the feeds. It was previously
379 * used by the main UI but that's now handled by the pager.
381 function fetchRevisions($limit, $offset, $direction) {
382 $fname = 'PageHistory::fetchRevisions';
384 $dbr = wfGetDB( DB_SLAVE
);
386 if ($direction == PageHistory
::DIR_PREV
)
387 list($dirs, $oper) = array("ASC", ">=");
388 else /* $direction == PageHistory::DIR_NEXT */
389 list($dirs, $oper) = array("DESC", "<=");
392 $offsets = array("rev_timestamp $oper '$offset'");
396 $page_id = $this->mTitle
->getArticleID();
400 Revision
::selectFields(),
401 array_merge(array("rev_page=$page_id"), $offsets),
403 array('ORDER BY' => "rev_timestamp $dirs",
404 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
408 while (($obj = $dbr->fetchObject($res)) != NULL)
414 /** @todo document */
415 function getNotificationTimestamp() {
416 global $wgUser, $wgShowUpdatedMarker;
417 $fname = 'PageHistory::getNotficationTimestamp';
419 if ($this->mNotificationTimestamp
!== NULL)
420 return $this->mNotificationTimestamp
;
422 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
423 return $this->mNotificationTimestamp
= false;
425 $dbr = wfGetDB(DB_SLAVE
);
427 $this->mNotificationTimestamp
= $dbr->selectField(
429 'wl_notificationtimestamp',
430 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
431 'wl_title' => $this->mTitle
->getDBkey(),
432 'wl_user' => $wgUser->getID()
436 // Don't use the special value reserved for telling whether the field is filled
437 if ( is_null( $this->mNotificationTimestamp
) ) {
438 $this->mNotificationTimestamp
= false;
441 return $this->mNotificationTimestamp
;
445 * Output a subscription feed listing recent edits to this page.
446 * @param string $type
448 function feed( $type ) {
449 require_once 'SpecialRecentchanges.php';
451 global $wgFeedClasses;
452 if( !isset( $wgFeedClasses[$type] ) ) {
454 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
458 $feed = new $wgFeedClasses[$type](
459 $this->mTitle
->getPrefixedText() . ' - ' .
460 wfMsgForContent( 'history-feed-title' ),
461 wfMsgForContent( 'history-feed-description' ),
462 $this->mTitle
->getFullUrl( 'action=history' ) );
464 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
467 foreach( $items as $row ) {
468 $feed->outItem( $this->feedItem( $row ) );
471 $feed->outItem( $this->feedEmpty() );
476 function feedEmpty() {
479 wfMsgForContent( 'nohistory' ),
480 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
481 $this->mTitle
->getFullUrl(),
482 wfTimestamp( TS_MW
),
484 $this->mTitle
->getTalkPage()->getFullUrl() );
488 * Generate a FeedItem object from a given revision table row
489 * Borrows Recent Changes' feed generation functions for formatting;
490 * includes a diff to the previous revision (if any).
495 function feedItem( $row ) {
496 $rev = new Revision( $row );
497 $rev->setTitle( $this->mTitle
);
498 $text = rcFormatDiffRow( $this->mTitle
,
499 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
501 $rev->getTimestamp(),
502 $rev->getComment() );
504 if( $rev->getComment() == '' ) {
506 $title = wfMsgForContent( 'history-feed-item-nocomment',
508 $wgContLang->timeanddate( $rev->getTimestamp() ) );
510 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
516 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
517 $rev->getTimestamp(),
519 $this->mTitle
->getTalkPage()->getFullUrl() );
523 * Quickie hack... strip out wikilinks to more legible form from the comment.
525 function stripComment( $text ) {
526 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
531 class PageHistoryPager
extends ReverseChronologicalPager
{
532 public $mLastRow = false, $mPageHistory;
534 function __construct( $pageHistory ) {
535 parent
::__construct();
536 $this->mPageHistory
= $pageHistory;
539 function getQueryInfo() {
541 'tables' => 'revision',
542 'fields' => Revision
::selectFields(),
543 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
544 'options' => array( 'USE INDEX' => 'page_timestamp' )
548 function getIndexField() {
549 return 'rev_timestamp';
552 function formatRow( $row ) {
553 if ( $this->mLastRow
) {
554 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
555 $firstInList = $this->mCounter
== 1;
556 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
557 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
561 $this->mLastRow
= $row;
565 function getStartBody() {
566 $this->mLastRow
= false;
571 function getEndBody() {
572 if ( $this->mLastRow
) {
573 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
574 $firstInList = $this->mCounter
== 1;
575 if ( $this->mIsBackwards
) {
576 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
577 if ( $this->mOffset
== '' ) {
583 # The next row is the past-the-end row
584 $next = $this->mPastTheEndRow
;
586 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
587 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );