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" ) );
103 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
108 $pager = new PageHistoryPager( $this );
109 $this->linesonpage
= $pager->getNumRows();
111 $pager->getNavigationBar() .
112 $this->beginHistoryList() .
114 $this->endHistoryList() .
115 $pager->getNavigationBar()
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' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
163 'value' => wfMsg( 'compareselectedversions' ),
169 * Returns a row from the history printout.
171 * @todo document some more, and maybe clean up the code (some params redundant?)
173 * @param object $row The database row corresponding to the line (or is it the previous line?).
174 * @param object $next The database row corresponding to the next line (or is it this one?).
175 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
176 * @param $notificationtimestamp
177 * @param bool $latest Whether this row corresponds to the page's latest revision.
178 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
179 * @return string HTML output for the row
181 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
182 global $wgUser, $wgLang;
183 $rev = new Revision( $row );
184 $rev->setTitle( $this->mTitle
);
187 $curlink = $this->curLink( $rev, $latest );
188 $lastlink = $this->lastLink( $rev, $next, $counter );
189 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
190 $link = $this->revLink( $rev );
192 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
193 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
195 $s .= "($curlink) ($lastlink) $arbitrary";
197 if( $wgUser->isAllowed( 'deleterevision' ) ) {
198 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
200 // We don't currently handle well changing the top revision's settings
201 $del = wfMsgHtml( 'rev-delundel' );
202 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
203 // If revision was hidden from sysops
204 $del = wfMsgHtml( 'rev-delundel' );
206 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
207 wfMsg( 'rev-delundel' ),
208 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
209 '&oldid=' . urlencode( $rev->getId() ) );
211 $s .= " (<small>$del</small>) ";
215 #getUser is safe, but this avoids making the invalid untargeted contribs links
216 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
217 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
219 $s .= " <span class='history-user'>$user</span>";
221 if( $row->rev_minor_edit
) {
222 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
225 if (!is_null($size = $rev->getSize())) {
227 $stxt = wfMsgHtml('historyempty');
229 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
230 $s .= " <span class=\"history-size\">$stxt</span>";
233 #getComment is safe, but this is better formatted
234 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
235 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
236 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
238 $s .= $this->mSkin
->revComment( $rev );
241 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
242 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
244 #add blurb about text having been deleted
245 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
246 $s .= ' ' . wfMsgHtml( 'deletedrev' );
248 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
249 $s .= ' '.$this->mSkin
->generateRollback( $rev );
252 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
259 /** @todo document */
260 function revLink( $rev ) {
262 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
263 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
264 $link = $this->mSkin
->makeKnownLinkObj(
265 $this->mTitle
, $date, "oldid=" . $rev->getId() );
269 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
270 return '<span class="history-deleted">' . $link . '</span>';
275 /** @todo document */
276 function curLink( $rev, $latest ) {
277 $cur = wfMsgExt( 'cur', array( 'escape') );
278 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
281 return $this->mSkin
->makeKnownLinkObj(
283 'diff=' . $this->getLatestID() .
284 "&oldid=" . $rev->getId() );
288 /** @todo document */
289 function lastLink( $rev, $next, $counter ) {
290 $last = wfMsgExt( 'last', array( 'escape' ) );
291 if ( is_null( $next ) ) {
292 # Probably no next row
294 } elseif ( $next === 'unknown' ) {
295 # Next row probably exists but is unknown, use an oldid=prev link
296 return $this->mSkin
->makeKnownLinkObj(
299 "diff=" . $rev->getId() . "&oldid=prev" );
300 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
303 return $this->mSkin
->makeKnownLinkObj(
306 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
310 "tabindex={$counter}"*/ );
314 /** @todo document */
315 function diffButtons( $rev, $firstInList, $counter ) {
316 if( $this->linesonpage
> 1) {
319 'value' => $rev->getId(),
320 # do we really need to flood this on every item?
321 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
324 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
325 $radio['disabled'] = 'disabled';
328 /** @todo: move title texts to javascript */
329 if ( $firstInList ) {
330 $first = wfElement( 'input', array_merge(
333 'style' => 'visibility:hidden',
334 'name' => 'oldid' ) ) );
335 $checkmark = array( 'checked' => 'checked' );
337 if( $counter == 2 ) {
338 $checkmark = array( 'checked' => 'checked' );
340 $checkmark = array();
342 $first = wfElement( 'input', array_merge(
345 array( 'name' => 'oldid' ) ) );
346 $checkmark = array();
348 $second = wfElement( 'input', array_merge(
351 array( 'name' => 'diff' ) ) );
352 return $first . $second;
358 /** @todo document */
359 function getLatestId() {
360 if( is_null( $this->mLatestId
) ) {
361 $id = $this->mTitle
->getArticleID();
362 $db = wfGetDB(DB_SLAVE
);
363 $this->mLatestId
= $db->selectField( 'page',
365 array( 'page_id' => $id ),
366 'PageHistory::getLatestID' );
368 return $this->mLatestId
;
372 * Fetch an array of revisions, specified by a given limit, offset and
373 * direction. This is now only used by the feeds. It was previously
374 * used by the main UI but that's now handled by the pager.
376 function fetchRevisions($limit, $offset, $direction) {
377 $fname = 'PageHistory::fetchRevisions';
379 $dbr = wfGetDB( DB_SLAVE
);
381 if ($direction == PageHistory
::DIR_PREV
)
382 list($dirs, $oper) = array("ASC", ">=");
383 else /* $direction == PageHistory::DIR_NEXT */
384 list($dirs, $oper) = array("DESC", "<=");
387 $offsets = array("rev_timestamp $oper '$offset'");
391 $page_id = $this->mTitle
->getArticleID();
395 Revision
::selectFields(),
396 array_merge(array("rev_page=$page_id"), $offsets),
398 array('ORDER BY' => "rev_timestamp $dirs",
399 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
403 while (($obj = $dbr->fetchObject($res)) != NULL)
409 /** @todo document */
410 function getNotificationTimestamp() {
411 global $wgUser, $wgShowUpdatedMarker;
412 $fname = 'PageHistory::getNotficationTimestamp';
414 if ($this->mNotificationTimestamp
!== NULL)
415 return $this->mNotificationTimestamp
;
417 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
418 return $this->mNotificationTimestamp
= false;
420 $dbr = wfGetDB(DB_SLAVE
);
422 $this->mNotificationTimestamp
= $dbr->selectField(
424 'wl_notificationtimestamp',
425 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
426 'wl_title' => $this->mTitle
->getDBkey(),
427 'wl_user' => $wgUser->getID()
431 // Don't use the special value reserved for telling whether the field is filled
432 if ( is_null( $this->mNotificationTimestamp
) ) {
433 $this->mNotificationTimestamp
= false;
436 return $this->mNotificationTimestamp
;
440 * Output a subscription feed listing recent edits to this page.
441 * @param string $type
443 function feed( $type ) {
444 require_once 'SpecialRecentchanges.php';
446 global $wgFeedClasses;
447 if( !isset( $wgFeedClasses[$type] ) ) {
449 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
453 $feed = new $wgFeedClasses[$type](
454 $this->mTitle
->getPrefixedText() . ' - ' .
455 wfMsgForContent( 'history-feed-title' ),
456 wfMsgForContent( 'history-feed-description' ),
457 $this->mTitle
->getFullUrl( 'action=history' ) );
459 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
462 foreach( $items as $row ) {
463 $feed->outItem( $this->feedItem( $row ) );
466 $feed->outItem( $this->feedEmpty() );
471 function feedEmpty() {
474 wfMsgForContent( 'nohistory' ),
475 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
476 $this->mTitle
->getFullUrl(),
477 wfTimestamp( TS_MW
),
479 $this->mTitle
->getTalkPage()->getFullUrl() );
483 * Generate a FeedItem object from a given revision table row
484 * Borrows Recent Changes' feed generation functions for formatting;
485 * includes a diff to the previous revision (if any).
490 function feedItem( $row ) {
491 $rev = new Revision( $row );
492 $rev->setTitle( $this->mTitle
);
493 $text = rcFormatDiffRow( $this->mTitle
,
494 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
496 $rev->getTimestamp(),
497 $rev->getComment() );
499 if( $rev->getComment() == '' ) {
501 $title = wfMsgForContent( 'history-feed-item-nocomment',
503 $wgContLang->timeanddate( $rev->getTimestamp() ) );
505 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
511 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
512 $rev->getTimestamp(),
514 $this->mTitle
->getTalkPage()->getFullUrl() );
518 * Quickie hack... strip out wikilinks to more legible form from the comment.
520 function stripComment( $text ) {
521 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
526 class PageHistoryPager
extends ReverseChronologicalPager
{
527 public $mLastRow = false, $mPageHistory;
529 function __construct( $pageHistory ) {
530 parent
::__construct();
531 $this->mPageHistory
= $pageHistory;
534 function getQueryInfo() {
536 'tables' => 'revision',
537 'fields' => Revision
::selectFields(),
538 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
539 'options' => array( 'USE INDEX' => 'page_timestamp' )
543 function getIndexField() {
544 return 'rev_timestamp';
547 function formatRow( $row ) {
548 if ( $this->mLastRow
) {
549 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
550 $firstInList = $this->mCounter
== 1;
551 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
552 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
556 $this->mLastRow
= $row;
560 function getStartBody() {
561 $this->mLastRow
= false;
566 function getEndBody() {
567 if ( $this->mLastRow
) {
568 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
569 $firstInList = $this->mCounter
== 1;
570 if ( $this->mIsBackwards
) {
571 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
572 if ( $this->mOffset
== '' ) {
578 # The next row is the past-the-end row
579 $next = $this->mPastTheEndRow
;
581 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
582 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );