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();
111 $pager->getNavigationBar() .
112 $this->beginHistoryList() .
114 $pager->getNavigationBar() .
115 $this->endHistoryList()
117 wfProfileOut( $fname );
120 /** @todo document */
121 function beginHistoryList() {
123 $this->lastdate
= '';
124 $s = wfMsgExt( 'histlegend', array( 'parse') );
125 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
126 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
128 // The following line is SUPPOSED to have double-quotes around the
129 // $prefixedkey variable, because htmlspecialchars() doesn't escape
132 // On at least two occasions people have changed it to single-quotes,
133 // which creates invalid HTML and incorrect display of the resulting
136 // Please do not break this a third time. Thank you for your kind
137 // consideration and cooperation.
139 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
141 $s .= $this->submitButton();
142 $s .= '<ul id="pagehistory">' . "\n";
146 /** @todo document */
147 function endHistoryList() {
149 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
154 /** @todo document */
155 function submitButton( $bits = array() ) {
156 return ( $this->linesonpage
> 0 )
157 ?
wfElement( 'input', array_merge( $bits,
159 'class' => 'historysubmit',
161 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
162 'title' => wfMsg( 'tooltip-compareselectedversions' ),
163 'value' => wfMsg( 'compareselectedversions' ),
168 /** @todo document */
169 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
171 $rev = new Revision( $row );
172 $rev->setTitle( $this->mTitle
);
175 $curlink = $this->curLink( $rev, $latest );
176 $lastlink = $this->lastLink( $rev, $next, $counter );
177 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
178 $link = $this->revLink( $rev );
180 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
181 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
183 $s .= "($curlink) ($lastlink) $arbitrary";
185 if( $wgUser->isAllowed( 'deleterevision' ) ) {
186 $revdel = Title
::makeTitle( NS_SPECIAL
, 'Revisiondelete' );
188 // We don't currently handle well changing the top revision's settings
189 $del = wfMsgHtml( 'rev-delundel' );
191 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
192 wfMsg( 'rev-delundel' ),
193 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
194 '&oldid=' . urlencode( $rev->getId() ) );
196 $s .= "(<small>$del</small>) ";
199 $s .= " $link <span class='history-user'>$user</span>";
201 if( $row->rev_minor_edit
) {
202 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
205 $s .= $this->mSkin
->revComment( $rev );
206 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
207 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
209 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
210 $s .= ' ' . wfMsgHtml( 'deletedrev' );
217 /** @todo document */
218 function revLink( $rev ) {
220 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
221 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
222 $link = $this->mSkin
->makeKnownLinkObj(
223 $this->mTitle
, $date, "oldid=" . $rev->getId() );
227 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
228 return '<span class="history-deleted">' . $link . '</span>';
233 /** @todo document */
234 function curLink( $rev, $latest ) {
235 $cur = wfMsgExt( 'cur', array( 'escape') );
236 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
239 return $this->mSkin
->makeKnownLinkObj(
241 'diff=' . $this->getLatestID() .
242 "&oldid=" . $rev->getId() );
246 /** @todo document */
247 function lastLink( $rev, $next, $counter ) {
248 $last = wfMsgExt( 'last', array( 'escape' ) );
249 if ( is_null( $next ) ) {
250 # Probably no next row
252 } elseif ( $next === 'unknown' ) {
253 # Next row probably exists but is unknown, use an oldid=prev link
254 return $this->mSkin
->makeKnownLinkObj(
257 "diff=" . $rev->getId() . "&oldid=prev" );
258 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
261 return $this->mSkin
->makeKnownLinkObj(
264 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
268 "tabindex={$counter}"*/ );
272 /** @todo document */
273 function diffButtons( $rev, $firstInList, $counter ) {
274 if( $this->linesonpage
> 1) {
277 'value' => $rev->getId(),
278 # do we really need to flood this on every item?
279 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
282 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
283 $radio['disabled'] = 'disabled';
286 /** @todo: move title texts to javascript */
287 if ( $firstInList ) {
288 $first = wfElement( 'input', array_merge(
291 'style' => 'visibility:hidden',
292 'name' => 'oldid' ) ) );
293 $checkmark = array( 'checked' => 'checked' );
295 if( $counter == 2 ) {
296 $checkmark = array( 'checked' => 'checked' );
298 $checkmark = array();
300 $first = wfElement( 'input', array_merge(
303 array( 'name' => 'oldid' ) ) );
304 $checkmark = array();
306 $second = wfElement( 'input', array_merge(
309 array( 'name' => 'diff' ) ) );
310 return $first . $second;
316 /** @todo document */
317 function getLatestId() {
318 if( is_null( $this->mLatestId
) ) {
319 $id = $this->mTitle
->getArticleID();
320 $db =& wfGetDB(DB_SLAVE
);
321 $this->mLatestId
= $db->selectField( 'page',
323 array( 'page_id' => $id ),
324 'PageHistory::getLatestID' );
326 return $this->mLatestId
;
330 * Fetch an array of revisions, specified by a given limit, offset and
331 * direction. This is now only used by the feeds. It was previously
332 * used by the main UI but that's now handled by the pager.
334 function fetchRevisions($limit, $offset, $direction) {
335 $fname = 'PageHistory::fetchRevisions';
337 $dbr =& wfGetDB( DB_SLAVE
);
339 if ($direction == PageHistory
::DIR_PREV
)
340 list($dirs, $oper) = array("ASC", ">=");
341 else /* $direction == PageHistory::DIR_NEXT */
342 list($dirs, $oper) = array("DESC", "<=");
345 $offsets = array("rev_timestamp $oper '$offset'");
349 $page_id = $this->mTitle
->getArticleID();
353 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
354 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
355 array_merge(array("rev_page=$page_id"), $offsets),
357 array('ORDER BY' => "rev_timestamp $dirs",
358 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
362 while (($obj = $dbr->fetchObject($res)) != NULL)
368 /** @todo document */
369 function getNotificationTimestamp() {
370 global $wgUser, $wgShowUpdatedMarker;
371 $fname = 'PageHistory::getNotficationTimestamp';
373 if ($this->mNotificationTimestamp
!== NULL)
374 return $this->mNotificationTimestamp
;
376 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
377 return $this->mNotificationTimestamp
= false;
379 $dbr =& wfGetDB(DB_SLAVE
);
381 $this->mNotificationTimestamp
= $dbr->selectField(
383 'wl_notificationtimestamp',
384 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
385 'wl_title' => $this->mTitle
->getDBkey(),
386 'wl_user' => $wgUser->getID()
390 // Don't use the special value reserved for telling whether the field is filled
391 if ( is_null( $this->mNotificationTimestamp
) ) {
392 $this->mNotificationTimestamp
= false;
395 return $this->mNotificationTimestamp
;
399 * Output a subscription feed listing recent edits to this page.
400 * @param string $type
402 function feed( $type ) {
403 require_once 'SpecialRecentchanges.php';
405 global $wgFeedClasses;
406 if( !isset( $wgFeedClasses[$type] ) ) {
408 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
412 $feed = new $wgFeedClasses[$type](
413 $this->mTitle
->getPrefixedText() . ' - ' .
414 wfMsgForContent( 'history-feed-title' ),
415 wfMsgForContent( 'history-feed-description' ),
416 $this->mTitle
->getFullUrl( 'action=history' ) );
418 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
421 foreach( $items as $row ) {
422 $feed->outItem( $this->feedItem( $row ) );
425 $feed->outItem( $this->feedEmpty() );
430 function feedEmpty() {
433 wfMsgForContent( 'nohistory' ),
434 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
435 $this->mTitle
->getFullUrl(),
436 wfTimestamp( TS_MW
),
438 $this->mTitle
->getTalkPage()->getFullUrl() );
442 * Generate a FeedItem object from a given revision table row
443 * Borrows Recent Changes' feed generation functions for formatting;
444 * includes a diff to the previous revision (if any).
449 function feedItem( $row ) {
450 $rev = new Revision( $row );
451 $rev->setTitle( $this->mTitle
);
452 $text = rcFormatDiffRow( $this->mTitle
,
453 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
455 $rev->getTimestamp(),
456 $rev->getComment() );
458 if( $rev->getComment() == '' ) {
460 $title = wfMsgForContent( 'history-feed-item-nocomment',
462 $wgContLang->timeanddate( $rev->getTimestamp() ) );
464 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
470 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
471 $rev->getTimestamp(),
473 $this->mTitle
->getTalkPage()->getFullUrl() );
477 * Quickie hack... strip out wikilinks to more legible form from the comment.
479 function stripComment( $text ) {
480 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
485 class PageHistoryPager
extends ReverseChronologicalPager
{
486 public $mLastRow = false, $mPageHistory;
488 function __construct( $pageHistory ) {
489 parent
::__construct();
490 $this->mPageHistory
= $pageHistory;
493 function getQueryInfo() {
495 'tables' => 'revision',
496 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
497 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
498 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
499 'options' => array( 'USE INDEX' => 'page_timestamp' )
503 function getIndexField() {
504 return 'rev_timestamp';
507 function formatRow( $row ) {
508 if ( $this->mLastRow
) {
509 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
510 $firstInList = $this->mCounter
== 1;
511 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
512 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
516 $this->mLastRow
= $row;
520 function getStartBody() {
521 $this->mLastRow
= false;
523 $this->mPageHistory
->linesonpage
= $this->getNumRows();
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 );