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();
44 * Print the history page for an article.
49 global $wgOut, $wgRequest, $wgTitle;
52 * Allow client caching.
55 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
56 /* Client cache fresh and headers sent, nothing more to do. */
59 $fname = 'PageHistory::history';
60 wfProfileIn( $fname );
63 * Setup page variables.
65 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
66 $wgOut->setArticleFlag( false );
67 $wgOut->setArticleRelated( true );
68 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 $wgOut->setSyndicated( true );
71 $logPage = SpecialPage
::getTitleFor( 'Log' );
72 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
74 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
75 $wgOut->setSubtitle( $subtitle );
77 $feedType = $wgRequest->getVal( 'feed' );
79 wfProfileOut( $fname );
80 return $this->feed( $feedType );
84 * Fail if article doesn't exist.
86 if( !$this->mTitle
->exists() ) {
87 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
88 wfProfileOut( $fname );
94 * "go=first" means to jump to the last (earliest) history page.
95 * This is deprecated, it no longer appears in the user interface
97 if ( $wgRequest->getText("go") == 'first' ) {
98 $limit = $wgRequest->getInt( 'limit', 50 );
99 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
106 $pager = new PageHistoryPager( $this );
107 $this->linesonpage
= $pager->getNumRows();
109 $pager->getNavigationBar() .
110 $this->beginHistoryList() .
112 $this->endHistoryList() .
113 $pager->getNavigationBar()
115 wfProfileOut( $fname );
118 /** @todo document */
119 function beginHistoryList() {
121 $this->lastdate
= '';
122 $s = wfMsgExt( 'histlegend', array( 'parse') );
123 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
124 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
126 // The following line is SUPPOSED to have double-quotes around the
127 // $prefixedkey variable, because htmlspecialchars() doesn't escape
130 // On at least two occasions people have changed it to single-quotes,
131 // which creates invalid HTML and incorrect display of the resulting
134 // Please do not break this a third time. Thank you for your kind
135 // consideration and cooperation.
137 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
139 $s .= $this->submitButton();
140 $s .= '<ul id="pagehistory">' . "\n";
144 /** @todo document */
145 function endHistoryList() {
147 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
152 /** @todo document */
153 function submitButton( $bits = array() ) {
154 return ( $this->linesonpage
> 0 )
155 ?
wfElement( 'input', array_merge( $bits,
157 'class' => 'historysubmit',
159 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
160 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
161 'value' => wfMsg( 'compareselectedversions' ),
167 * Returns a row from the history printout.
169 * @todo document some more, and maybe clean up the code (some params redundant?)
171 * @param object $row The database row corresponding to the line (or is it the previous line?).
172 * @param object $next The database row corresponding to the next line (or is it this one?).
173 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
174 * @param $notificationtimestamp
175 * @param bool $latest Whether this row corresponds to the page's latest revision.
176 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
177 * @return string HTML output for the row
179 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
181 $rev = new Revision( $row );
182 $rev->setTitle( $this->mTitle
);
185 $curlink = $this->curLink( $rev, $latest );
186 $lastlink = $this->lastLink( $rev, $next, $counter );
187 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
188 $link = $this->revLink( $rev );
190 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
191 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
193 $s .= "($curlink) ($lastlink) $arbitrary";
195 if( $wgUser->isAllowed( 'deleterevision' ) ) {
196 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
198 // We don't currently handle well changing the top revision's settings
199 $del = wfMsgHtml( 'rev-delundel' );
201 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
202 wfMsg( 'rev-delundel' ),
203 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
204 '&oldid=' . urlencode( $rev->getId() ) );
206 $s .= "(<small>$del</small>) ";
209 #getUser is safe, but this avoids making the invalid untargeted contribs links
210 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
211 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
212 $s .= " $link <span class='history-user'>$user</span>";
214 $s .= " $link <span class='history-user'>$user</span>";
217 if( $row->rev_minor_edit
) {
218 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
221 $s .= $this->mSkin
->revComment( $rev );
222 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
223 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
225 #add blurb about having been deleted
226 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
227 $s .= ' ' . wfMsgHtml( 'deletedrev' );
229 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
230 $s .= ' '.$this->mSkin
->generateRollback( $rev );
237 /** @todo document */
238 function revLink( $rev ) {
240 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
241 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
242 $link = $this->mSkin
->makeKnownLinkObj(
243 $this->mTitle
, $date, "oldid=" . $rev->getId() );
247 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
248 return '<span class="history-deleted">' . $link . '</span>';
253 /** @todo document */
254 function curLink( $rev, $latest ) {
255 $cur = wfMsgExt( 'cur', array( 'escape') );
256 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
259 return $this->mSkin
->makeKnownLinkObj(
261 'diff=' . $this->getLatestID() .
262 "&oldid=" . $rev->getId() );
266 /** @todo document */
267 function lastLink( $rev, $next, $counter ) {
268 $last = wfMsgExt( 'last', array( 'escape' ) );
269 if ( is_null( $next ) ) {
270 # Probably no next row
272 } elseif ( $next === 'unknown' ) {
273 # Next row probably exists but is unknown, use an oldid=prev link
274 return $this->mSkin
->makeKnownLinkObj(
277 "diff=" . $rev->getId() . "&oldid=prev" );
278 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
281 return $this->mSkin
->makeKnownLinkObj(
284 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
288 "tabindex={$counter}"*/ );
292 /** @todo document */
293 function diffButtons( $rev, $firstInList, $counter ) {
294 if( $this->linesonpage
> 1) {
297 'value' => $rev->getId(),
298 # do we really need to flood this on every item?
299 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
302 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
303 $radio['disabled'] = 'disabled';
306 /** @todo: move title texts to javascript */
307 if ( $firstInList ) {
308 $first = wfElement( 'input', array_merge(
311 'style' => 'visibility:hidden',
312 'name' => 'oldid' ) ) );
313 $checkmark = array( 'checked' => 'checked' );
315 if( $counter == 2 ) {
316 $checkmark = array( 'checked' => 'checked' );
318 $checkmark = array();
320 $first = wfElement( 'input', array_merge(
323 array( 'name' => 'oldid' ) ) );
324 $checkmark = array();
326 $second = wfElement( 'input', array_merge(
329 array( 'name' => 'diff' ) ) );
330 return $first . $second;
336 /** @todo document */
337 function getLatestId() {
338 if( is_null( $this->mLatestId
) ) {
339 $id = $this->mTitle
->getArticleID();
340 $db = wfGetDB(DB_SLAVE
);
341 $this->mLatestId
= $db->selectField( 'page',
343 array( 'page_id' => $id ),
344 'PageHistory::getLatestID' );
346 return $this->mLatestId
;
350 * Fetch an array of revisions, specified by a given limit, offset and
351 * direction. This is now only used by the feeds. It was previously
352 * used by the main UI but that's now handled by the pager.
354 function fetchRevisions($limit, $offset, $direction) {
355 $fname = 'PageHistory::fetchRevisions';
357 $dbr = wfGetDB( DB_SLAVE
);
359 if ($direction == PageHistory
::DIR_PREV
)
360 list($dirs, $oper) = array("ASC", ">=");
361 else /* $direction == PageHistory::DIR_NEXT */
362 list($dirs, $oper) = array("DESC", "<=");
365 $offsets = array("rev_timestamp $oper '$offset'");
369 $page_id = $this->mTitle
->getArticleID();
373 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
374 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
375 array_merge(array("rev_page=$page_id"), $offsets),
377 array('ORDER BY' => "rev_timestamp $dirs",
378 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
382 while (($obj = $dbr->fetchObject($res)) != NULL)
388 /** @todo document */
389 function getNotificationTimestamp() {
390 global $wgUser, $wgShowUpdatedMarker;
391 $fname = 'PageHistory::getNotficationTimestamp';
393 if ($this->mNotificationTimestamp
!== NULL)
394 return $this->mNotificationTimestamp
;
396 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
397 return $this->mNotificationTimestamp
= false;
399 $dbr = wfGetDB(DB_SLAVE
);
401 $this->mNotificationTimestamp
= $dbr->selectField(
403 'wl_notificationtimestamp',
404 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
405 'wl_title' => $this->mTitle
->getDBkey(),
406 'wl_user' => $wgUser->getID()
410 // Don't use the special value reserved for telling whether the field is filled
411 if ( is_null( $this->mNotificationTimestamp
) ) {
412 $this->mNotificationTimestamp
= false;
415 return $this->mNotificationTimestamp
;
419 * Output a subscription feed listing recent edits to this page.
420 * @param string $type
422 function feed( $type ) {
423 require_once 'SpecialRecentchanges.php';
425 global $wgFeedClasses;
426 if( !isset( $wgFeedClasses[$type] ) ) {
428 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
432 $feed = new $wgFeedClasses[$type](
433 $this->mTitle
->getPrefixedText() . ' - ' .
434 wfMsgForContent( 'history-feed-title' ),
435 wfMsgForContent( 'history-feed-description' ),
436 $this->mTitle
->getFullUrl( 'action=history' ) );
438 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
441 foreach( $items as $row ) {
442 $feed->outItem( $this->feedItem( $row ) );
445 $feed->outItem( $this->feedEmpty() );
450 function feedEmpty() {
453 wfMsgForContent( 'nohistory' ),
454 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
455 $this->mTitle
->getFullUrl(),
456 wfTimestamp( TS_MW
),
458 $this->mTitle
->getTalkPage()->getFullUrl() );
462 * Generate a FeedItem object from a given revision table row
463 * Borrows Recent Changes' feed generation functions for formatting;
464 * includes a diff to the previous revision (if any).
469 function feedItem( $row ) {
470 $rev = new Revision( $row );
471 $rev->setTitle( $this->mTitle
);
472 $text = rcFormatDiffRow( $this->mTitle
,
473 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
475 $rev->getTimestamp(),
476 $rev->getComment() );
478 if( $rev->getComment() == '' ) {
480 $title = wfMsgForContent( 'history-feed-item-nocomment',
482 $wgContLang->timeanddate( $rev->getTimestamp() ) );
484 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
490 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
491 $rev->getTimestamp(),
493 $this->mTitle
->getTalkPage()->getFullUrl() );
497 * Quickie hack... strip out wikilinks to more legible form from the comment.
499 function stripComment( $text ) {
500 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
505 class PageHistoryPager
extends ReverseChronologicalPager
{
506 public $mLastRow = false, $mPageHistory;
508 function __construct( $pageHistory ) {
509 parent
::__construct();
510 $this->mPageHistory
= $pageHistory;
513 function getQueryInfo() {
515 'tables' => 'revision',
516 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
517 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
518 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
519 'options' => array( 'USE INDEX' => 'page_timestamp' )
523 function getIndexField() {
524 return 'rev_timestamp';
527 function formatRow( $row ) {
528 if ( $this->mLastRow
) {
529 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
530 $firstInList = $this->mCounter
== 1;
531 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
532 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
536 $this->mLastRow
= $row;
540 function getStartBody() {
541 $this->mLastRow
= false;
546 function getEndBody() {
547 if ( $this->mLastRow
) {
548 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
549 $firstInList = $this->mCounter
== 1;
550 if ( $this->mIsBackwards
) {
551 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
552 if ( $this->mOffset
== '' ) {
558 # The next row is the past-the-end row
559 $next = $this->mPastTheEndRow
;
561 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
562 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );