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
21 var $mArticle, $mTitle, $mSkin;
24 var $mNotificationTimestamp;
25 var $mLatestId = null;
28 * Construct a new PageHistory.
30 * @param Article $article
33 function __construct($article) {
36 $this->mArticle
=& $article;
37 $this->mTitle
=& $article->mTitle
;
38 $this->mNotificationTimestamp
= NULL;
39 $this->mSkin
= $wgUser->getSkin();
43 * Print the history page for an article.
48 global $wgOut, $wgRequest, $wgTitle;
51 * Allow client caching.
54 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
55 /* Client cache fresh and headers sent, nothing more to do. */
58 $fname = 'PageHistory::history';
59 wfProfileIn( $fname );
62 * Setup page variables.
64 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle
->getPrefixedText() ) );
65 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
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() );
73 $wgOut->setSubtitle( $logLink );
75 $feedType = $wgRequest->getVal( 'feed' );
77 wfProfileOut( $fname );
78 return $this->feed( $feedType );
82 * Fail if article doesn't exist.
84 if( !$this->mTitle
->exists() ) {
85 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
86 wfProfileOut( $fname );
91 * "go=first" means to jump to the last (earliest) history page.
92 * This is deprecated, it no longer appears in the user interface
94 if ( $wgRequest->getText("go") == 'first' ) {
95 $limit = $wgRequest->getInt( 'limit', 50 );
96 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
100 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
105 $pager = new PageHistoryPager( $this );
106 $this->linesonpage
= $pager->getNumRows();
108 $pager->getNavigationBar() .
109 $this->beginHistoryList() .
111 $this->endHistoryList() .
112 $pager->getNavigationBar()
114 wfProfileOut( $fname );
117 /** @todo document */
118 function beginHistoryList() {
120 $this->lastdate
= '';
121 $s = wfMsgExt( 'histlegend', array( 'parse') );
122 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
123 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
125 // The following line is SUPPOSED to have double-quotes around the
126 // $prefixedkey variable, because htmlspecialchars() doesn't escape
129 // On at least two occasions people have changed it to single-quotes,
130 // which creates invalid HTML and incorrect display of the resulting
133 // Please do not break this a third time. Thank you for your kind
134 // consideration and cooperation.
136 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
138 $s .= $this->submitButton();
139 $s .= '<ul id="pagehistory">' . "\n";
143 /** @todo document */
144 function endHistoryList() {
146 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
151 /** @todo document */
152 function submitButton( $bits = array() ) {
153 return ( $this->linesonpage
> 0 )
154 ?
wfElement( 'input', array_merge( $bits,
156 'class' => 'historysubmit',
158 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
159 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
160 'value' => wfMsg( 'compareselectedversions' ),
166 * Returns a row from the history printout.
168 * @todo document some more, and maybe clean up the code (some params redundant?)
170 * @param object $row The database row corresponding to the line (or is it the previous line?).
171 * @param object $next The database row corresponding to the next line (or is it this one?).
172 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
173 * @param $notificationtimestamp
174 * @param bool $latest Whether this row corresponds to the page's latest revision.
175 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
176 * @return string HTML output for the row
178 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
179 global $wgUser, $wgLang;
180 $rev = new Revision( $row );
181 $rev->setTitle( $this->mTitle
);
184 $curlink = $this->curLink( $rev, $latest );
185 $lastlink = $this->lastLink( $rev, $next, $counter );
186 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
187 $link = $this->revLink( $rev );
189 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
190 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
192 $s .= "($curlink) ($lastlink) $arbitrary";
194 if( $wgUser->isAllowed( 'deleterevision' ) ) {
195 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
197 // We don't currently handle well changing the top revision's settings
198 $del = wfMsgHtml( 'rev-delundel' );
199 } else if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
200 // If revision was hidden from sysops
201 $del = wfMsgHtml( 'rev-delundel' );
203 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
204 wfMsg( 'rev-delundel' ),
205 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
206 '&oldid=' . urlencode( $rev->getId() ) );
208 $s .= " (<small>$del</small>) ";
212 #getUser is safe, but this avoids making the invalid untargeted contribs links
213 if( $row->rev_deleted
& Revision
::DELETED_USER
) {
214 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
216 $s .= " <span class='history-user'>$user</span>";
218 if( $row->rev_minor_edit
) {
219 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
222 if ( !is_null( $size = $rev->getSize() ) ) {
224 $stxt = wfMsgHtml( 'historyempty' );
226 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
227 $s .= " <span class=\"history-size\">$stxt</span>";
230 #getComment is safe, but this is better formatted
231 if( $rev->isDeleted( Revision
::DELETED_COMMENT
) ) {
232 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
233 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
235 $s .= $this->mSkin
->revComment( $rev );
238 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
239 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
241 #add blurb about text having been deleted
242 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
243 $s .= ' ' . wfMsgHtml( 'deletedrev' );
248 if ( !is_null( $next ) && is_object( $next ) ) {
249 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
250 $tools[] = '<span class="mw-rollback-link">'
251 . $this->mSkin
->buildRollbackLink( $rev )
255 $undolink = $this->mSkin
->makeKnownLinkObj(
257 wfMsgHtml( 'editundo' ),
258 'action=edit&undoafter=' . $next->rev_id
. '&undo=' . $rev->getId()
260 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
264 $s .= ' (' . implode( ' | ', $tools ) . ')';
267 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
274 /** @todo document */
275 function revLink( $rev ) {
277 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
278 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
279 $link = $this->mSkin
->makeKnownLinkObj(
280 $this->mTitle
, $date, "oldid=" . $rev->getId() );
284 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
285 return '<span class="history-deleted">' . $link . '</span>';
290 /** @todo document */
291 function curLink( $rev, $latest ) {
292 $cur = wfMsgExt( 'cur', array( 'escape') );
293 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
296 return $this->mSkin
->makeKnownLinkObj(
298 'diff=' . $this->getLatestID() .
299 "&oldid=" . $rev->getId() );
303 /** @todo document */
304 function lastLink( $rev, $next, $counter ) {
305 $last = wfMsgExt( 'last', array( 'escape' ) );
306 if ( is_null( $next ) ) {
307 # Probably no next row
309 } elseif ( $next === 'unknown' ) {
310 # Next row probably exists but is unknown, use an oldid=prev link
311 return $this->mSkin
->makeKnownLinkObj(
314 "diff=" . $rev->getId() . "&oldid=prev" );
315 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
318 return $this->mSkin
->makeKnownLinkObj(
321 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
325 "tabindex={$counter}"*/ );
330 * Create radio buttons for page history
332 * @param object $rev Revision
333 * @param bool $firstInList Is this version the first one?
334 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
335 * @return string HTML output for the radio buttons
337 function diffButtons( $rev, $firstInList, $counter ) {
338 if( $this->linesonpage
> 1) {
341 'value' => $rev->getId(),
344 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
345 $radio['disabled'] = 'disabled';
348 /** @todo: move title texts to javascript */
349 if ( $firstInList ) {
350 $first = Xml
::element( 'input', array_merge(
353 'style' => 'visibility:hidden',
354 'name' => 'oldid' ) ) );
355 $checkmark = array( 'checked' => 'checked' );
357 if( $counter == 2 ) {
358 $checkmark = array( 'checked' => 'checked' );
360 $checkmark = array();
362 $first = Xml
::element( 'input', array_merge(
365 array( 'name' => 'oldid' ) ) );
366 $checkmark = array();
368 $second = Xml
::element( 'input', array_merge(
371 array( 'name' => 'diff' ) ) );
372 return $first . $second;
378 /** @todo document */
379 function getLatestId() {
380 if( is_null( $this->mLatestId
) ) {
381 $id = $this->mTitle
->getArticleID();
382 $db = wfGetDB(DB_SLAVE
);
383 $this->mLatestId
= $db->selectField( 'page',
385 array( 'page_id' => $id ),
386 'PageHistory::getLatestID' );
388 return $this->mLatestId
;
392 * Fetch an array of revisions, specified by a given limit, offset and
393 * direction. This is now only used by the feeds. It was previously
394 * used by the main UI but that's now handled by the pager.
396 function fetchRevisions($limit, $offset, $direction) {
397 $fname = 'PageHistory::fetchRevisions';
399 $dbr = wfGetDB( DB_SLAVE
);
401 if ($direction == PageHistory
::DIR_PREV
)
402 list($dirs, $oper) = array("ASC", ">=");
403 else /* $direction == PageHistory::DIR_NEXT */
404 list($dirs, $oper) = array("DESC", "<=");
407 $offsets = array("rev_timestamp $oper '$offset'");
411 $page_id = $this->mTitle
->getArticleID();
415 Revision
::selectFields(),
416 array_merge(array("rev_page=$page_id"), $offsets),
418 array('ORDER BY' => "rev_timestamp $dirs",
419 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
423 while (($obj = $dbr->fetchObject($res)) != NULL)
429 /** @todo document */
430 function getNotificationTimestamp() {
431 global $wgUser, $wgShowUpdatedMarker;
432 $fname = 'PageHistory::getNotficationTimestamp';
434 if ($this->mNotificationTimestamp
!== NULL)
435 return $this->mNotificationTimestamp
;
437 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
438 return $this->mNotificationTimestamp
= false;
440 $dbr = wfGetDB(DB_SLAVE
);
442 $this->mNotificationTimestamp
= $dbr->selectField(
444 'wl_notificationtimestamp',
445 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
446 'wl_title' => $this->mTitle
->getDBkey(),
447 'wl_user' => $wgUser->getID()
451 // Don't use the special value reserved for telling whether the field is filled
452 if ( is_null( $this->mNotificationTimestamp
) ) {
453 $this->mNotificationTimestamp
= false;
456 return $this->mNotificationTimestamp
;
460 * Output a subscription feed listing recent edits to this page.
461 * @param string $type
463 function feed( $type ) {
464 require_once 'SpecialRecentchanges.php';
466 global $wgFeedClasses;
467 if( !isset( $wgFeedClasses[$type] ) ) {
469 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
473 $feed = new $wgFeedClasses[$type](
474 $this->mTitle
->getPrefixedText() . ' - ' .
475 wfMsgForContent( 'history-feed-title' ),
476 wfMsgForContent( 'history-feed-description' ),
477 $this->mTitle
->getFullUrl( 'action=history' ) );
479 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
482 foreach( $items as $row ) {
483 $feed->outItem( $this->feedItem( $row ) );
486 $feed->outItem( $this->feedEmpty() );
491 function feedEmpty() {
494 wfMsgForContent( 'nohistory' ),
495 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
496 $this->mTitle
->getFullUrl(),
497 wfTimestamp( TS_MW
),
499 $this->mTitle
->getTalkPage()->getFullUrl() );
503 * Generate a FeedItem object from a given revision table row
504 * Borrows Recent Changes' feed generation functions for formatting;
505 * includes a diff to the previous revision (if any).
510 function feedItem( $row ) {
511 $rev = new Revision( $row );
512 $rev->setTitle( $this->mTitle
);
513 $text = rcFormatDiffRow( $this->mTitle
,
514 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
516 $rev->getTimestamp(),
517 $rev->getComment() );
519 if( $rev->getComment() == '' ) {
521 $title = wfMsgForContent( 'history-feed-item-nocomment',
523 $wgContLang->timeanddate( $rev->getTimestamp() ) );
525 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
531 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
532 $rev->getTimestamp(),
534 $this->mTitle
->getTalkPage()->getFullUrl() );
538 * Quickie hack... strip out wikilinks to more legible form from the comment.
540 function stripComment( $text ) {
541 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
549 class PageHistoryPager
extends ReverseChronologicalPager
{
550 public $mLastRow = false, $mPageHistory;
552 function __construct( $pageHistory ) {
553 parent
::__construct();
554 $this->mPageHistory
= $pageHistory;
557 function getQueryInfo() {
559 'tables' => 'revision',
560 'fields' => Revision
::selectFields(),
561 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
562 'options' => array( 'USE INDEX' => 'page_timestamp' )
566 function getIndexField() {
567 return 'rev_timestamp';
570 function formatRow( $row ) {
571 if ( $this->mLastRow
) {
572 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
573 $firstInList = $this->mCounter
== 1;
574 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
575 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
579 $this->mLastRow
= $row;
583 function getStartBody() {
584 $this->mLastRow
= false;
589 function getEndBody() {
590 if ( $this->mLastRow
) {
591 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
592 $firstInList = $this->mCounter
== 1;
593 if ( $this->mIsBackwards
) {
594 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
595 if ( $this->mOffset
== '' ) {
601 # The next row is the past-the-end row
602 $next = $this->mPastTheEndRow
;
604 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
605 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );