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 = Title
::makeTitle( NS_SPECIAL
, '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 = Title
::makeTitle( NS_SPECIAL
, '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' );
218 /** @todo document */
219 function revLink( $rev ) {
221 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
222 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
223 $link = $this->mSkin
->makeKnownLinkObj(
224 $this->mTitle
, $date, "oldid=" . $rev->getId() );
228 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
229 return '<span class="history-deleted">' . $link . '</span>';
234 /** @todo document */
235 function curLink( $rev, $latest ) {
236 $cur = wfMsgExt( 'cur', array( 'escape') );
237 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
240 return $this->mSkin
->makeKnownLinkObj(
242 'diff=' . $this->getLatestID() .
243 "&oldid=" . $rev->getId() );
247 /** @todo document */
248 function lastLink( $rev, $next, $counter ) {
249 $last = wfMsgExt( 'last', array( 'escape' ) );
250 if ( is_null( $next ) ) {
251 # Probably no next row
253 } elseif ( $next === 'unknown' ) {
254 # Next row probably exists but is unknown, use an oldid=prev link
255 return $this->mSkin
->makeKnownLinkObj(
258 "diff=" . $rev->getId() . "&oldid=prev" );
259 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
262 return $this->mSkin
->makeKnownLinkObj(
265 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
269 "tabindex={$counter}"*/ );
273 /** @todo document */
274 function diffButtons( $rev, $firstInList, $counter ) {
275 if( $this->linesonpage
> 1) {
278 'value' => $rev->getId(),
279 # do we really need to flood this on every item?
280 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
283 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
284 $radio['disabled'] = 'disabled';
287 /** @todo: move title texts to javascript */
288 if ( $firstInList ) {
289 $first = wfElement( 'input', array_merge(
292 'style' => 'visibility:hidden',
293 'name' => 'oldid' ) ) );
294 $checkmark = array( 'checked' => 'checked' );
296 if( $counter == 2 ) {
297 $checkmark = array( 'checked' => 'checked' );
299 $checkmark = array();
301 $first = wfElement( 'input', array_merge(
304 array( 'name' => 'oldid' ) ) );
305 $checkmark = array();
307 $second = wfElement( 'input', array_merge(
310 array( 'name' => 'diff' ) ) );
311 return $first . $second;
317 /** @todo document */
318 function getLatestId() {
319 if( is_null( $this->mLatestId
) ) {
320 $id = $this->mTitle
->getArticleID();
321 $db =& wfGetDB(DB_SLAVE
);
322 $this->mLatestId
= $db->selectField( 'page',
324 array( 'page_id' => $id ),
325 'PageHistory::getLatestID' );
327 return $this->mLatestId
;
331 * Fetch an array of revisions, specified by a given limit, offset and
332 * direction. This is now only used by the feeds. It was previously
333 * used by the main UI but that's now handled by the pager.
335 function fetchRevisions($limit, $offset, $direction) {
336 $fname = 'PageHistory::fetchRevisions';
338 $dbr =& wfGetDB( DB_SLAVE
);
340 if ($direction == PageHistory
::DIR_PREV
)
341 list($dirs, $oper) = array("ASC", ">=");
342 else /* $direction == PageHistory::DIR_NEXT */
343 list($dirs, $oper) = array("DESC", "<=");
346 $offsets = array("rev_timestamp $oper '$offset'");
350 $page_id = $this->mTitle
->getArticleID();
354 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
355 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
356 array_merge(array("rev_page=$page_id"), $offsets),
358 array('ORDER BY' => "rev_timestamp $dirs",
359 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
363 while (($obj = $dbr->fetchObject($res)) != NULL)
369 /** @todo document */
370 function getNotificationTimestamp() {
371 global $wgUser, $wgShowUpdatedMarker;
372 $fname = 'PageHistory::getNotficationTimestamp';
374 if ($this->mNotificationTimestamp
!== NULL)
375 return $this->mNotificationTimestamp
;
377 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
378 return $this->mNotificationTimestamp
= false;
380 $dbr =& wfGetDB(DB_SLAVE
);
382 $this->mNotificationTimestamp
= $dbr->selectField(
384 'wl_notificationtimestamp',
385 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
386 'wl_title' => $this->mTitle
->getDBkey(),
387 'wl_user' => $wgUser->getID()
391 // Don't use the special value reserved for telling whether the field is filled
392 if ( is_null( $this->mNotificationTimestamp
) ) {
393 $this->mNotificationTimestamp
= false;
396 return $this->mNotificationTimestamp
;
400 * Output a subscription feed listing recent edits to this page.
401 * @param string $type
403 function feed( $type ) {
404 require_once 'SpecialRecentchanges.php';
406 global $wgFeedClasses;
407 if( !isset( $wgFeedClasses[$type] ) ) {
409 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
413 $feed = new $wgFeedClasses[$type](
414 $this->mTitle
->getPrefixedText() . ' - ' .
415 wfMsgForContent( 'history-feed-title' ),
416 wfMsgForContent( 'history-feed-description' ),
417 $this->mTitle
->getFullUrl( 'action=history' ) );
419 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
422 foreach( $items as $row ) {
423 $feed->outItem( $this->feedItem( $row ) );
426 $feed->outItem( $this->feedEmpty() );
431 function feedEmpty() {
434 wfMsgForContent( 'nohistory' ),
435 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
436 $this->mTitle
->getFullUrl(),
437 wfTimestamp( TS_MW
),
439 $this->mTitle
->getTalkPage()->getFullUrl() );
443 * Generate a FeedItem object from a given revision table row
444 * Borrows Recent Changes' feed generation functions for formatting;
445 * includes a diff to the previous revision (if any).
450 function feedItem( $row ) {
451 $rev = new Revision( $row );
452 $rev->setTitle( $this->mTitle
);
453 $text = rcFormatDiffRow( $this->mTitle
,
454 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
456 $rev->getTimestamp(),
457 $rev->getComment() );
459 if( $rev->getComment() == '' ) {
461 $title = wfMsgForContent( 'history-feed-item-nocomment',
463 $wgContLang->timeanddate( $rev->getTimestamp() ) );
465 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
471 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
472 $rev->getTimestamp(),
474 $this->mTitle
->getTalkPage()->getFullUrl() );
478 * Quickie hack... strip out wikilinks to more legible form from the comment.
480 function stripComment( $text ) {
481 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
486 class PageHistoryPager
extends ReverseChronologicalPager
{
487 public $mLastRow = false, $mPageHistory;
489 function __construct( $pageHistory ) {
490 parent
::__construct();
491 $this->mPageHistory
= $pageHistory;
494 function getQueryInfo() {
496 'tables' => 'revision',
497 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
498 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
499 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
500 'options' => array( 'USE INDEX' => 'page_timestamp' )
504 function getIndexField() {
505 return 'rev_timestamp';
508 function formatRow( $row ) {
509 if ( $this->mLastRow
) {
510 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
511 $firstInList = $this->mCounter
== 1;
512 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
513 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
517 $this->mLastRow
= $row;
521 function getStartBody() {
522 $this->mLastRow
= false;
527 function getEndBody() {
528 if ( $this->mLastRow
) {
529 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
530 $firstInList = $this->mCounter
== 1;
531 if ( $this->mIsBackwards
) {
532 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
533 if ( $this->mOffset
== '' ) {
539 # The next row is the past-the-end row
540 $next = $this->mPastTheEndRow
;
542 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
543 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );