5 * Split off from Article.php and Skin.php, 2003-12-22
10 * This class handles printing the history page for an article. In order to
11 * be efficient, it uses timestamps rather than offsets for paging, to avoid
12 * costly LIMIT,offset queries.
14 * Construct it by passing in an Article, and call $h->history() to print the
24 var $mArticle, $mTitle, $mSkin;
27 var $mNotificationTimestamp;
28 var $mLatestId = null;
31 * Construct a new PageHistory.
33 * @param Article $article
36 function PageHistory($article) {
39 $this->mArticle
=& $article;
40 $this->mTitle
=& $article->mTitle
;
41 $this->mNotificationTimestamp
= NULL;
42 $this->mSkin
= $wgUser->getSkin();
46 * Print the history page for an article.
51 global $wgOut, $wgRequest, $wgTitle;
54 * Allow client caching.
57 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
58 /* Client cache fresh and headers sent, nothing more to do. */
61 $fname = 'PageHistory::history';
62 wfProfileIn( $fname );
65 * Setup page variables.
67 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
68 $wgOut->setArticleFlag( false );
69 $wgOut->setArticleRelated( true );
70 $wgOut->setRobotpolicy( 'noindex,nofollow' );
71 $wgOut->setSyndicated( true );
73 $logPage = SpecialPage
::getTitleFor( 'Log' );
74 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
76 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
77 $wgOut->setSubtitle( $subtitle );
79 $feedType = $wgRequest->getVal( 'feed' );
81 wfProfileOut( $fname );
82 return $this->feed( $feedType );
86 * Fail if article doesn't exist.
88 if( !$this->mTitle
->exists() ) {
89 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
90 wfProfileOut( $fname );
96 * "go=first" means to jump to the last (earliest) history page.
97 * This is deprecated, it no longer appears in the user interface
99 if ( $wgRequest->getText("go") == 'first' ) {
100 $limit = $wgRequest->getInt( 'limit', 50 );
101 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
108 $pager = new PageHistoryPager( $this );
109 $navbar = $pager->getNavigationBar();
110 $this->linesonpage
= $pager->getNumRows();
112 $pager->getNavigationBar() .
113 $this->beginHistoryList() .
115 $this->endHistoryList() .
116 $pager->getNavigationBar()
118 wfProfileOut( $fname );
121 /** @todo document */
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";
147 /** @todo document */
148 function endHistoryList() {
150 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
155 /** @todo document */
156 function submitButton( $bits = array() ) {
157 return ( $this->linesonpage
> 0 )
158 ?
wfElement( 'input', array_merge( $bits,
160 'class' => 'historysubmit',
162 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
163 'title' => wfMsg( 'tooltip-compareselectedversions' ),
164 'value' => wfMsg( 'compareselectedversions' ),
169 /** @todo document */
170 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
172 $rev = new Revision( $row );
173 $rev->setTitle( $this->mTitle
);
176 $curlink = $this->curLink( $rev, $latest );
177 $lastlink = $this->lastLink( $rev, $next, $counter );
178 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
179 $link = $this->revLink( $rev );
181 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
182 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
184 $s .= "($curlink) ($lastlink) $arbitrary";
186 if( $wgUser->isAllowed( 'deleterevision' ) ) {
187 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
189 // We don't currently handle well changing the top revision's settings
190 $del = wfMsgHtml( 'rev-delundel' );
192 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
193 wfMsg( 'rev-delundel' ),
194 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
195 '&oldid=' . urlencode( $rev->getId() ) );
197 $s .= "(<small>$del</small>) ";
200 $s .= " $link <span class='history-user'>$user</span>";
202 if( $row->rev_minor_edit
) {
203 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
206 $s .= $this->mSkin
->revComment( $rev );
207 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
208 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
210 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
211 $s .= ' ' . wfMsgHtml( 'deletedrev' );
213 if( $wgUser->isAllowed( 'rollback' ) && $firstInList ) {
214 $s .= ' '.$this->mSkin
->generateRollback( $rev );
221 /** @todo document */
222 function revLink( $rev ) {
224 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
225 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
226 $link = $this->mSkin
->makeKnownLinkObj(
227 $this->mTitle
, $date, "oldid=" . $rev->getId() );
231 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
232 return '<span class="history-deleted">' . $link . '</span>';
237 /** @todo document */
238 function curLink( $rev, $latest ) {
239 $cur = wfMsgExt( 'cur', array( 'escape') );
240 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
243 return $this->mSkin
->makeKnownLinkObj(
245 'diff=' . $this->getLatestID() .
246 "&oldid=" . $rev->getId() );
250 /** @todo document */
251 function lastLink( $rev, $next, $counter ) {
252 $last = wfMsgExt( 'last', array( 'escape' ) );
253 if ( is_null( $next ) ) {
254 # Probably no next row
256 } elseif ( $next === 'unknown' ) {
257 # Next row probably exists but is unknown, use an oldid=prev link
258 return $this->mSkin
->makeKnownLinkObj(
261 "diff=" . $rev->getId() . "&oldid=prev" );
262 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
265 return $this->mSkin
->makeKnownLinkObj(
268 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
272 "tabindex={$counter}"*/ );
276 /** @todo document */
277 function diffButtons( $rev, $firstInList, $counter ) {
278 if( $this->linesonpage
> 1) {
281 'value' => $rev->getId(),
282 # do we really need to flood this on every item?
283 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
286 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
287 $radio['disabled'] = 'disabled';
290 /** @todo: move title texts to javascript */
291 if ( $firstInList ) {
292 $first = wfElement( 'input', array_merge(
295 'style' => 'visibility:hidden',
296 'name' => 'oldid' ) ) );
297 $checkmark = array( 'checked' => 'checked' );
299 if( $counter == 2 ) {
300 $checkmark = array( 'checked' => 'checked' );
302 $checkmark = array();
304 $first = wfElement( 'input', array_merge(
307 array( 'name' => 'oldid' ) ) );
308 $checkmark = array();
310 $second = wfElement( 'input', array_merge(
313 array( 'name' => 'diff' ) ) );
314 return $first . $second;
320 /** @todo document */
321 function getLatestId() {
322 if( is_null( $this->mLatestId
) ) {
323 $id = $this->mTitle
->getArticleID();
324 $db =& wfGetDB(DB_SLAVE
);
325 $this->mLatestId
= $db->selectField( 'page',
327 array( 'page_id' => $id ),
328 'PageHistory::getLatestID' );
330 return $this->mLatestId
;
334 * Fetch an array of revisions, specified by a given limit, offset and
335 * direction. This is now only used by the feeds. It was previously
336 * used by the main UI but that's now handled by the pager.
338 function fetchRevisions($limit, $offset, $direction) {
339 $fname = 'PageHistory::fetchRevisions';
341 $dbr =& wfGetDB( DB_SLAVE
);
343 if ($direction == PageHistory
::DIR_PREV
)
344 list($dirs, $oper) = array("ASC", ">=");
345 else /* $direction == PageHistory::DIR_NEXT */
346 list($dirs, $oper) = array("DESC", "<=");
349 $offsets = array("rev_timestamp $oper '$offset'");
353 $page_id = $this->mTitle
->getArticleID();
357 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
358 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
359 array_merge(array("rev_page=$page_id"), $offsets),
361 array('ORDER BY' => "rev_timestamp $dirs",
362 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
366 while (($obj = $dbr->fetchObject($res)) != NULL)
372 /** @todo document */
373 function getNotificationTimestamp() {
374 global $wgUser, $wgShowUpdatedMarker;
375 $fname = 'PageHistory::getNotficationTimestamp';
377 if ($this->mNotificationTimestamp
!== NULL)
378 return $this->mNotificationTimestamp
;
380 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
381 return $this->mNotificationTimestamp
= false;
383 $dbr =& wfGetDB(DB_SLAVE
);
385 $this->mNotificationTimestamp
= $dbr->selectField(
387 'wl_notificationtimestamp',
388 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
389 'wl_title' => $this->mTitle
->getDBkey(),
390 'wl_user' => $wgUser->getID()
394 // Don't use the special value reserved for telling whether the field is filled
395 if ( is_null( $this->mNotificationTimestamp
) ) {
396 $this->mNotificationTimestamp
= false;
399 return $this->mNotificationTimestamp
;
403 * Output a subscription feed listing recent edits to this page.
404 * @param string $type
406 function feed( $type ) {
407 require_once 'SpecialRecentchanges.php';
409 global $wgFeedClasses;
410 if( !isset( $wgFeedClasses[$type] ) ) {
412 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
416 $feed = new $wgFeedClasses[$type](
417 $this->mTitle
->getPrefixedText() . ' - ' .
418 wfMsgForContent( 'history-feed-title' ),
419 wfMsgForContent( 'history-feed-description' ),
420 $this->mTitle
->getFullUrl( 'action=history' ) );
422 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
425 foreach( $items as $row ) {
426 $feed->outItem( $this->feedItem( $row ) );
429 $feed->outItem( $this->feedEmpty() );
434 function feedEmpty() {
437 wfMsgForContent( 'nohistory' ),
438 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
439 $this->mTitle
->getFullUrl(),
440 wfTimestamp( TS_MW
),
442 $this->mTitle
->getTalkPage()->getFullUrl() );
446 * Generate a FeedItem object from a given revision table row
447 * Borrows Recent Changes' feed generation functions for formatting;
448 * includes a diff to the previous revision (if any).
453 function feedItem( $row ) {
454 $rev = new Revision( $row );
455 $rev->setTitle( $this->mTitle
);
456 $text = rcFormatDiffRow( $this->mTitle
,
457 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
459 $rev->getTimestamp(),
460 $rev->getComment() );
462 if( $rev->getComment() == '' ) {
464 $title = wfMsgForContent( 'history-feed-item-nocomment',
466 $wgContLang->timeanddate( $rev->getTimestamp() ) );
468 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
474 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
475 $rev->getTimestamp(),
477 $this->mTitle
->getTalkPage()->getFullUrl() );
481 * Quickie hack... strip out wikilinks to more legible form from the comment.
483 function stripComment( $text ) {
484 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
489 class PageHistoryPager
extends ReverseChronologicalPager
{
490 public $mLastRow = false, $mPageHistory;
492 function __construct( $pageHistory ) {
493 parent
::__construct();
494 $this->mPageHistory
= $pageHistory;
497 function getQueryInfo() {
499 'tables' => 'revision',
500 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
501 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
502 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
503 'options' => array( 'USE INDEX' => 'page_timestamp' )
507 function getIndexField() {
508 return 'rev_timestamp';
511 function formatRow( $row ) {
512 if ( $this->mLastRow
) {
513 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
514 $firstInList = $this->mCounter
== 1;
515 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
516 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
520 $this->mLastRow
= $row;
524 function getStartBody() {
525 $this->mLastRow
= false;
530 function getEndBody() {
531 if ( $this->mLastRow
) {
532 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
533 $firstInList = $this->mCounter
== 1;
534 if ( $this->mIsBackwards
) {
535 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
536 if ( $this->mOffset
== '' ) {
542 # The next row is the past-the-end row
543 $next = $this->mPastTheEndRow
;
545 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
546 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );