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' ),
170 * Returns a row from the history printout.
172 * @todo document some more, and maybe clean up the code (some params redundant?)
174 * @param object $row The database row corresponding to the line (or is it the previous line?).
175 * @param object $next The database row corresponding to the next line (or is it this one?).
176 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
177 * @param $notificationtimestamp
178 * @param bool $latest Whether this row corresponds to the page's latest revision.
179 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
180 * @return string HTML output for the row
182 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
184 $rev = new Revision( $row );
185 $rev->setTitle( $this->mTitle
);
188 $curlink = $this->curLink( $rev, $latest );
189 $lastlink = $this->lastLink( $rev, $next, $counter );
190 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
191 $link = $this->revLink( $rev );
193 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
194 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
196 $s .= "($curlink) ($lastlink) $arbitrary";
198 if( $wgUser->isAllowed( 'deleterevision' ) ) {
199 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
201 // We don't currently handle well changing the top revision's settings
202 $del = wfMsgHtml( 'rev-delundel' );
204 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
205 wfMsg( 'rev-delundel' ),
206 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
207 '&oldid=' . urlencode( $rev->getId() ) );
209 $s .= "(<small>$del</small>) ";
212 $s .= " $link <span class='history-user'>$user</span>";
214 if( $row->rev_minor_edit
) {
215 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
218 $s .= $this->mSkin
->revComment( $rev );
219 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
220 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
222 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
223 $s .= ' ' . wfMsgHtml( 'deletedrev' );
225 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
226 $s .= ' '.$this->mSkin
->generateRollback( $rev );
233 /** @todo document */
234 function revLink( $rev ) {
236 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
237 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
238 $link = $this->mSkin
->makeKnownLinkObj(
239 $this->mTitle
, $date, "oldid=" . $rev->getId() );
243 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
244 return '<span class="history-deleted">' . $link . '</span>';
249 /** @todo document */
250 function curLink( $rev, $latest ) {
251 $cur = wfMsgExt( 'cur', array( 'escape') );
252 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
255 return $this->mSkin
->makeKnownLinkObj(
257 'diff=' . $this->getLatestID() .
258 "&oldid=" . $rev->getId() );
262 /** @todo document */
263 function lastLink( $rev, $next, $counter ) {
264 $last = wfMsgExt( 'last', array( 'escape' ) );
265 if ( is_null( $next ) ) {
266 # Probably no next row
268 } elseif ( $next === 'unknown' ) {
269 # Next row probably exists but is unknown, use an oldid=prev link
270 return $this->mSkin
->makeKnownLinkObj(
273 "diff=" . $rev->getId() . "&oldid=prev" );
274 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
277 return $this->mSkin
->makeKnownLinkObj(
280 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
284 "tabindex={$counter}"*/ );
288 /** @todo document */
289 function diffButtons( $rev, $firstInList, $counter ) {
290 if( $this->linesonpage
> 1) {
293 'value' => $rev->getId(),
294 # do we really need to flood this on every item?
295 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
298 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
299 $radio['disabled'] = 'disabled';
302 /** @todo: move title texts to javascript */
303 if ( $firstInList ) {
304 $first = wfElement( 'input', array_merge(
307 'style' => 'visibility:hidden',
308 'name' => 'oldid' ) ) );
309 $checkmark = array( 'checked' => 'checked' );
311 if( $counter == 2 ) {
312 $checkmark = array( 'checked' => 'checked' );
314 $checkmark = array();
316 $first = wfElement( 'input', array_merge(
319 array( 'name' => 'oldid' ) ) );
320 $checkmark = array();
322 $second = wfElement( 'input', array_merge(
325 array( 'name' => 'diff' ) ) );
326 return $first . $second;
332 /** @todo document */
333 function getLatestId() {
334 if( is_null( $this->mLatestId
) ) {
335 $id = $this->mTitle
->getArticleID();
336 $db =& wfGetDB(DB_SLAVE
);
337 $this->mLatestId
= $db->selectField( 'page',
339 array( 'page_id' => $id ),
340 'PageHistory::getLatestID' );
342 return $this->mLatestId
;
346 * Fetch an array of revisions, specified by a given limit, offset and
347 * direction. This is now only used by the feeds. It was previously
348 * used by the main UI but that's now handled by the pager.
350 function fetchRevisions($limit, $offset, $direction) {
351 $fname = 'PageHistory::fetchRevisions';
353 $dbr =& wfGetDB( DB_SLAVE
);
355 if ($direction == PageHistory
::DIR_PREV
)
356 list($dirs, $oper) = array("ASC", ">=");
357 else /* $direction == PageHistory::DIR_NEXT */
358 list($dirs, $oper) = array("DESC", "<=");
361 $offsets = array("rev_timestamp $oper '$offset'");
365 $page_id = $this->mTitle
->getArticleID();
369 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
370 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
371 array_merge(array("rev_page=$page_id"), $offsets),
373 array('ORDER BY' => "rev_timestamp $dirs",
374 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
378 while (($obj = $dbr->fetchObject($res)) != NULL)
384 /** @todo document */
385 function getNotificationTimestamp() {
386 global $wgUser, $wgShowUpdatedMarker;
387 $fname = 'PageHistory::getNotficationTimestamp';
389 if ($this->mNotificationTimestamp
!== NULL)
390 return $this->mNotificationTimestamp
;
392 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
393 return $this->mNotificationTimestamp
= false;
395 $dbr =& wfGetDB(DB_SLAVE
);
397 $this->mNotificationTimestamp
= $dbr->selectField(
399 'wl_notificationtimestamp',
400 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
401 'wl_title' => $this->mTitle
->getDBkey(),
402 'wl_user' => $wgUser->getID()
406 // Don't use the special value reserved for telling whether the field is filled
407 if ( is_null( $this->mNotificationTimestamp
) ) {
408 $this->mNotificationTimestamp
= false;
411 return $this->mNotificationTimestamp
;
415 * Output a subscription feed listing recent edits to this page.
416 * @param string $type
418 function feed( $type ) {
419 require_once 'SpecialRecentchanges.php';
421 global $wgFeedClasses;
422 if( !isset( $wgFeedClasses[$type] ) ) {
424 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
428 $feed = new $wgFeedClasses[$type](
429 $this->mTitle
->getPrefixedText() . ' - ' .
430 wfMsgForContent( 'history-feed-title' ),
431 wfMsgForContent( 'history-feed-description' ),
432 $this->mTitle
->getFullUrl( 'action=history' ) );
434 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
437 foreach( $items as $row ) {
438 $feed->outItem( $this->feedItem( $row ) );
441 $feed->outItem( $this->feedEmpty() );
446 function feedEmpty() {
449 wfMsgForContent( 'nohistory' ),
450 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
451 $this->mTitle
->getFullUrl(),
452 wfTimestamp( TS_MW
),
454 $this->mTitle
->getTalkPage()->getFullUrl() );
458 * Generate a FeedItem object from a given revision table row
459 * Borrows Recent Changes' feed generation functions for formatting;
460 * includes a diff to the previous revision (if any).
465 function feedItem( $row ) {
466 $rev = new Revision( $row );
467 $rev->setTitle( $this->mTitle
);
468 $text = rcFormatDiffRow( $this->mTitle
,
469 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
471 $rev->getTimestamp(),
472 $rev->getComment() );
474 if( $rev->getComment() == '' ) {
476 $title = wfMsgForContent( 'history-feed-item-nocomment',
478 $wgContLang->timeanddate( $rev->getTimestamp() ) );
480 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
486 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
487 $rev->getTimestamp(),
489 $this->mTitle
->getTalkPage()->getFullUrl() );
493 * Quickie hack... strip out wikilinks to more legible form from the comment.
495 function stripComment( $text ) {
496 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
501 class PageHistoryPager
extends ReverseChronologicalPager
{
502 public $mLastRow = false, $mPageHistory;
504 function __construct( $pageHistory ) {
505 parent
::__construct();
506 $this->mPageHistory
= $pageHistory;
509 function getQueryInfo() {
511 'tables' => 'revision',
512 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
513 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
514 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
515 'options' => array( 'USE INDEX' => 'page_timestamp' )
519 function getIndexField() {
520 return 'rev_timestamp';
523 function formatRow( $row ) {
524 if ( $this->mLastRow
) {
525 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
526 $firstInList = $this->mCounter
== 1;
527 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
528 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
532 $this->mLastRow
= $row;
536 function getStartBody() {
537 $this->mLastRow
= false;
542 function getEndBody() {
543 if ( $this->mLastRow
) {
544 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
545 $firstInList = $this->mCounter
== 1;
546 if ( $this->mIsBackwards
) {
547 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
548 if ( $this->mOffset
== '' ) {
554 # The next row is the past-the-end row
555 $next = $this->mPastTheEndRow
;
557 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
558 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );