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 $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' ),
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 ) {
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' );
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>) ";
211 $s .= " $link <span class='history-user'>$user</span>";
213 if( $row->rev_minor_edit
) {
214 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
217 $s .= $this->mSkin
->revComment( $rev );
218 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
219 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
221 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
222 $s .= ' ' . wfMsgHtml( 'deletedrev' );
224 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
225 $s .= ' '.$this->mSkin
->generateRollback( $rev );
232 /** @todo document */
233 function revLink( $rev ) {
235 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
236 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
237 $link = $this->mSkin
->makeKnownLinkObj(
238 $this->mTitle
, $date, "oldid=" . $rev->getId() );
242 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
243 return '<span class="history-deleted">' . $link . '</span>';
248 /** @todo document */
249 function curLink( $rev, $latest ) {
250 $cur = wfMsgExt( 'cur', array( 'escape') );
251 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
254 return $this->mSkin
->makeKnownLinkObj(
256 'diff=' . $this->getLatestID() .
257 "&oldid=" . $rev->getId() );
261 /** @todo document */
262 function lastLink( $rev, $next, $counter ) {
263 $last = wfMsgExt( 'last', array( 'escape' ) );
264 if ( is_null( $next ) ) {
265 # Probably no next row
267 } elseif ( $next === 'unknown' ) {
268 # Next row probably exists but is unknown, use an oldid=prev link
269 return $this->mSkin
->makeKnownLinkObj(
272 "diff=" . $rev->getId() . "&oldid=prev" );
273 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
276 return $this->mSkin
->makeKnownLinkObj(
279 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
283 "tabindex={$counter}"*/ );
287 /** @todo document */
288 function diffButtons( $rev, $firstInList, $counter ) {
289 if( $this->linesonpage
> 1) {
292 'value' => $rev->getId(),
293 # do we really need to flood this on every item?
294 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
297 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
298 $radio['disabled'] = 'disabled';
301 /** @todo: move title texts to javascript */
302 if ( $firstInList ) {
303 $first = wfElement( 'input', array_merge(
306 'style' => 'visibility:hidden',
307 'name' => 'oldid' ) ) );
308 $checkmark = array( 'checked' => 'checked' );
310 if( $counter == 2 ) {
311 $checkmark = array( 'checked' => 'checked' );
313 $checkmark = array();
315 $first = wfElement( 'input', array_merge(
318 array( 'name' => 'oldid' ) ) );
319 $checkmark = array();
321 $second = wfElement( 'input', array_merge(
324 array( 'name' => 'diff' ) ) );
325 return $first . $second;
331 /** @todo document */
332 function getLatestId() {
333 if( is_null( $this->mLatestId
) ) {
334 $id = $this->mTitle
->getArticleID();
335 $db =& wfGetDB(DB_SLAVE
);
336 $this->mLatestId
= $db->selectField( 'page',
338 array( 'page_id' => $id ),
339 'PageHistory::getLatestID' );
341 return $this->mLatestId
;
345 * Fetch an array of revisions, specified by a given limit, offset and
346 * direction. This is now only used by the feeds. It was previously
347 * used by the main UI but that's now handled by the pager.
349 function fetchRevisions($limit, $offset, $direction) {
350 $fname = 'PageHistory::fetchRevisions';
352 $dbr =& wfGetDB( DB_SLAVE
);
354 if ($direction == PageHistory
::DIR_PREV
)
355 list($dirs, $oper) = array("ASC", ">=");
356 else /* $direction == PageHistory::DIR_NEXT */
357 list($dirs, $oper) = array("DESC", "<=");
360 $offsets = array("rev_timestamp $oper '$offset'");
364 $page_id = $this->mTitle
->getArticleID();
368 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
369 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
370 array_merge(array("rev_page=$page_id"), $offsets),
372 array('ORDER BY' => "rev_timestamp $dirs",
373 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
377 while (($obj = $dbr->fetchObject($res)) != NULL)
383 /** @todo document */
384 function getNotificationTimestamp() {
385 global $wgUser, $wgShowUpdatedMarker;
386 $fname = 'PageHistory::getNotficationTimestamp';
388 if ($this->mNotificationTimestamp
!== NULL)
389 return $this->mNotificationTimestamp
;
391 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
392 return $this->mNotificationTimestamp
= false;
394 $dbr =& wfGetDB(DB_SLAVE
);
396 $this->mNotificationTimestamp
= $dbr->selectField(
398 'wl_notificationtimestamp',
399 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
400 'wl_title' => $this->mTitle
->getDBkey(),
401 'wl_user' => $wgUser->getID()
405 // Don't use the special value reserved for telling whether the field is filled
406 if ( is_null( $this->mNotificationTimestamp
) ) {
407 $this->mNotificationTimestamp
= false;
410 return $this->mNotificationTimestamp
;
414 * Output a subscription feed listing recent edits to this page.
415 * @param string $type
417 function feed( $type ) {
418 require_once 'SpecialRecentchanges.php';
420 global $wgFeedClasses;
421 if( !isset( $wgFeedClasses[$type] ) ) {
423 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
427 $feed = new $wgFeedClasses[$type](
428 $this->mTitle
->getPrefixedText() . ' - ' .
429 wfMsgForContent( 'history-feed-title' ),
430 wfMsgForContent( 'history-feed-description' ),
431 $this->mTitle
->getFullUrl( 'action=history' ) );
433 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
436 foreach( $items as $row ) {
437 $feed->outItem( $this->feedItem( $row ) );
440 $feed->outItem( $this->feedEmpty() );
445 function feedEmpty() {
448 wfMsgForContent( 'nohistory' ),
449 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
450 $this->mTitle
->getFullUrl(),
451 wfTimestamp( TS_MW
),
453 $this->mTitle
->getTalkPage()->getFullUrl() );
457 * Generate a FeedItem object from a given revision table row
458 * Borrows Recent Changes' feed generation functions for formatting;
459 * includes a diff to the previous revision (if any).
464 function feedItem( $row ) {
465 $rev = new Revision( $row );
466 $rev->setTitle( $this->mTitle
);
467 $text = rcFormatDiffRow( $this->mTitle
,
468 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
470 $rev->getTimestamp(),
471 $rev->getComment() );
473 if( $rev->getComment() == '' ) {
475 $title = wfMsgForContent( 'history-feed-item-nocomment',
477 $wgContLang->timeanddate( $rev->getTimestamp() ) );
479 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
485 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
486 $rev->getTimestamp(),
488 $this->mTitle
->getTalkPage()->getFullUrl() );
492 * Quickie hack... strip out wikilinks to more legible form from the comment.
494 function stripComment( $text ) {
495 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
500 class PageHistoryPager
extends ReverseChronologicalPager
{
501 public $mLastRow = false, $mPageHistory;
503 function __construct( $pageHistory ) {
504 parent
::__construct();
505 $this->mPageHistory
= $pageHistory;
508 function getQueryInfo() {
510 'tables' => 'revision',
511 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
512 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
513 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
514 'options' => array( 'USE INDEX' => 'page_timestamp' )
518 function getIndexField() {
519 return 'rev_timestamp';
522 function formatRow( $row ) {
523 if ( $this->mLastRow
) {
524 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
525 $firstInList = $this->mCounter
== 1;
526 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
527 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
531 $this->mLastRow
= $row;
535 function getStartBody() {
536 $this->mLastRow
= false;
541 function getEndBody() {
542 if ( $this->mLastRow
) {
543 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
544 $firstInList = $this->mCounter
== 1;
545 if ( $this->mIsBackwards
) {
546 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
547 if ( $this->mOffset
== '' ) {
553 # The next row is the past-the-end row
554 $next = $this->mPastTheEndRow
;
556 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
557 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );