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();
44 $this->defaultLimit
= 50;
48 * Print the history page for an article.
53 global $wgOut, $wgRequest, $wgTitle;
56 * Allow client caching.
59 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
60 /* Client cache fresh and headers sent, nothing more to do. */
63 $fname = 'PageHistory::history';
64 wfProfileIn( $fname );
67 * Setup page variables.
69 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
70 $wgOut->setArticleFlag( false );
71 $wgOut->setArticleRelated( true );
72 $wgOut->setRobotpolicy( 'noindex,nofollow' );
73 $wgOut->setSyndicated( true );
75 $logPage = Title
::makeTitle( NS_SPECIAL
, 'Log' );
76 $logLink = $this->mSkin
->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
78 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
79 $wgOut->setSubtitle( $subtitle );
81 $feedType = $wgRequest->getVal( 'feed' );
83 wfProfileOut( $fname );
84 return $this->feed( $feedType );
88 * Fail if article doesn't exist.
90 if( !$this->mTitle
->exists() ) {
91 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
92 wfProfileOut( $fname );
96 $dbr =& wfGetDB(DB_SLAVE
);
99 * Extract limit, the number of revisions to show, and
100 * offset, the timestamp to begin at, from the URL.
102 $limit = $wgRequest->getInt('limit', $this->defaultLimit
);
104 $limit = $this->defaultLimit
;
105 } elseif ( $limit > 50000 ) {
107 # Any more than this and we'll probably get an out of memory error
111 $offset = $wgRequest->getText('offset');
113 /* Offset must be an integral. */
114 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
116 # $offset = $dbr->timestamp($offset);
117 $dboffset = $offset === 0 ?
0 : $dbr->timestamp($offset);
119 * "go=last" means to jump to the last history page.
121 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
125 if (($lastid = $this->getLastOffsetForPaging($this->mTitle
->getArticleID(), $limit)) === NULL)
127 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
128 wfTimestamp(TS_MW
, $lastid));
132 if (!is_null($gourl)) {
133 $wgOut->redirect($gourl);
141 * If the user clicked "previous", we retrieve the revisions backwards,
142 * then reverse them. This is to avoid needing to know the timestamp of
143 * previous revisions when generating the URL.
145 $direction = $this->getDirection();
146 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
147 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
150 * We fetch one more revision than needed to get the timestamp of the
151 * one after this page (and to know if it exists).
153 * linesonpage stores the actual number of lines.
155 if (count($revisions) < $limit +
1)
156 $this->linesonpage
= count($revisions);
158 $this->linesonpage
= count($revisions) - 1;
160 /* Un-reverse revisions */
161 if ($direction == PageHistory
::DIR_PREV
)
162 $revisions = array_reverse($revisions);
165 * Print the top navbar.
168 $s .= $this->beginHistoryList();
172 * Print each revision, excluding the one-past-the-end, if any.
174 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
175 $latest = !$i && $offset == 0;
177 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
178 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
185 $s .= $this->endHistoryList();
188 $wgOut->addHTML( $s );
189 wfProfileOut( $fname );
192 /** @todo document */
193 function beginHistoryList() {
195 $this->lastdate
= '';
196 $s = wfMsgExt( 'histlegend', array( 'parse') );
197 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
198 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
200 // The following line is SUPPOSED to have double-quotes around the
201 // $prefixedkey variable, because htmlspecialchars() doesn't escape
204 // On at least two occasions people have changed it to single-quotes,
205 // which creates invalid HTML and incorrect display of the resulting
208 // Please do not break this a third time. Thank you for your kind
209 // consideration and cooperation.
211 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
213 $s .= $this->submitButton();
214 $s .= '<ul id="pagehistory">' . "\n";
218 /** @todo document */
219 function endHistoryList() {
221 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
226 /** @todo document */
227 function submitButton( $bits = array() ) {
228 return ( $this->linesonpage
> 0 )
229 ?
wfElement( 'input', array_merge( $bits,
231 'class' => 'historysubmit',
233 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
234 'title' => wfMsg( 'tooltip-compareselectedversions' ),
235 'value' => wfMsg( 'compareselectedversions' ),
240 /** @todo document */
241 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
243 $rev = new Revision( $row );
244 $rev->setTitle( $this->mTitle
);
247 $curlink = $this->curLink( $rev, $latest );
248 $lastlink = $this->lastLink( $rev, $next, $counter );
249 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
250 $link = $this->revLink( $rev );
252 $user = $this->mSkin
->userLink( $rev->getUser(), $rev->getUserText() )
253 . $this->mSkin
->userToolLinks( $rev->getUser(), $rev->getUserText() );
255 $s .= "($curlink) ($lastlink) $arbitrary";
257 if( $wgUser->isAllowed( 'deleterevision' ) ) {
258 $revdel = Title
::makeTitle( NS_SPECIAL
, 'Revisiondelete' );
260 // We don't currently handle well changing the top revision's settings
261 $del = wfMsgHtml( 'rev-delundel' );
263 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
264 wfMsg( 'rev-delundel' ),
265 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
266 '&oldid=' . urlencode( $rev->getId() ) );
268 $s .= "(<small>$del</small>) ";
271 $s .= " $link <span class='history-user'>$user</span>";
273 if( $row->rev_minor_edit
) {
274 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
277 $s .= $this->mSkin
->revComment( $rev );
278 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
279 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
281 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
282 $s .= ' ' . wfMsgHtml( 'deletedrev' );
289 /** @todo document */
290 function revLink( $rev ) {
292 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
293 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
294 $link = $this->mSkin
->makeKnownLinkObj(
295 $this->mTitle
, $date, "oldid=" . $rev->getId() );
299 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
300 return '<span class="history-deleted">' . $link . '</span>';
305 /** @todo document */
306 function curLink( $rev, $latest ) {
307 $cur = wfMsgExt( 'cur', array( 'escape') );
308 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
311 return $this->mSkin
->makeKnownLinkObj(
313 'diff=' . $this->getLatestID() .
314 "&oldid=" . $rev->getId() );
318 /** @todo document */
319 function lastLink( $rev, $next, $counter ) {
320 $last = wfMsgExt( 'last', array( 'escape' ) );
321 if( is_null( $next ) ) {
322 if( $rev->getTimestamp() == $this->getEarliestOffset() ) {
325 // Cut off by paging; there are more behind us...
326 return $this->mSkin
->makeKnownLinkObj(
329 "diff=" . $rev->getId() . "&oldid=prev" );
331 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
334 return $this->mSkin
->makeKnownLinkObj(
337 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
341 "tabindex={$counter}"*/ );
345 /** @todo document */
346 function diffButtons( $rev, $firstInList, $counter ) {
347 if( $this->linesonpage
> 1) {
350 'value' => $rev->getId(),
351 # do we really need to flood this on every item?
352 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
355 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
356 $radio['disabled'] = 'disabled';
359 /** @todo: move title texts to javascript */
360 if ( $firstInList ) {
361 $first = wfElement( 'input', array_merge(
364 'style' => 'visibility:hidden',
365 'name' => 'oldid' ) ) );
366 $checkmark = array( 'checked' => 'checked' );
368 if( $counter == 2 ) {
369 $checkmark = array( 'checked' => 'checked' );
371 $checkmark = array();
373 $first = wfElement( 'input', array_merge(
376 array( 'name' => 'oldid' ) ) );
377 $checkmark = array();
379 $second = wfElement( 'input', array_merge(
382 array( 'name' => 'diff' ) ) );
383 return $first . $second;
389 /** @todo document */
390 function getLatestOffset( $id = null ) {
391 if ( $id === null) $id = $this->mTitle
->getArticleID();
392 return $this->getExtremeOffset( $id, 'max' );
395 /** @todo document */
396 function getEarliestOffset( $id = null ) {
397 if ( $id === null) $id = $this->mTitle
->getArticleID();
398 return $this->getExtremeOffset( $id, 'min' );
401 /** @todo document */
402 function getExtremeOffset( $id, $func ) {
403 $db =& wfGetDB(DB_SLAVE
);
404 return $db->selectField( 'revision',
405 "$func(rev_timestamp)",
406 array( 'rev_page' => $id ),
407 'PageHistory::getExtremeOffset' );
410 /** @todo document */
411 function getLatestId() {
412 if( is_null( $this->mLatestId
) ) {
413 $id = $this->mTitle
->getArticleID();
414 $db =& wfGetDB(DB_SLAVE
);
415 $this->mLatestId
= $db->selectField( 'revision',
417 array( 'rev_page' => $id ),
418 'PageHistory::getLatestID' );
420 return $this->mLatestId
;
423 /** @todo document */
424 function getLastOffsetForPaging( $id, $step ) {
425 $fname = 'PageHistory::getLastOffsetForPaging';
427 $dbr =& wfGetDB(DB_SLAVE
);
433 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
435 $n = $dbr->numRows( $res );
437 while( $obj = $dbr->fetchObject( $res ) ) {
438 $last = $obj->rev_timestamp
;
440 $dbr->freeResult( $res );
445 * @return returns the direction of browsing watchlist
447 function getDirection() {
449 if ($wgRequest->getText("dir") == "prev")
450 return PageHistory
::DIR_PREV
;
452 return PageHistory
::DIR_NEXT
;
455 /** @todo document */
456 function fetchRevisions($limit, $offset, $direction) {
457 $fname = 'PageHistory::fetchRevisions';
459 $dbr =& wfGetDB( DB_SLAVE
);
461 if ($direction == PageHistory
::DIR_PREV
)
462 list($dirs, $oper) = array("ASC", ">=");
463 else /* $direction == PageHistory::DIR_NEXT */
464 list($dirs, $oper) = array("DESC", "<=");
467 $offsets = array("rev_timestamp $oper '$offset'");
471 $page_id = $this->mTitle
->getArticleID();
475 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
476 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
477 array_merge(array("rev_page=$page_id"), $offsets),
479 array('ORDER BY' => "rev_timestamp $dirs",
480 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
484 while (($obj = $dbr->fetchObject($res)) != NULL)
490 /** @todo document */
491 function getNotificationTimestamp() {
492 global $wgUser, $wgShowUpdatedMarker;
493 $fname = 'PageHistory::getNotficationTimestamp';
495 if ($this->mNotificationTimestamp
!== NULL)
496 return $this->mNotificationTimestamp
;
498 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
499 return $this->mNotificationTimestamp
= false;
501 $dbr =& wfGetDB(DB_SLAVE
);
503 $this->mNotificationTimestamp
= $dbr->selectField(
505 'wl_notificationtimestamp',
506 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
507 'wl_title' => $this->mTitle
->getDBkey(),
508 'wl_user' => $wgUser->getID()
512 // Don't use the special value reserved for telling whether the field is filled
513 if ( is_null( $this->mNotificationTimestamp
) ) {
514 $this->mNotificationTimestamp
= false;
517 return $this->mNotificationTimestamp
;
520 /** @todo document */
521 function makeNavbar($revisions, $offset, $limit, $direction) {
524 $revisions = array_slice($revisions, 0, $limit);
526 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
527 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
530 * When we're displaying previous revisions, we need to reverse
531 * the array, because it's queried in reverse order.
533 if ($direction == PageHistory
::DIR_PREV
)
534 $revisions = array_reverse($revisions);
537 * lowts is the timestamp of the first revision on this page.
538 * hights is the timestamp of the last revision.
541 $lowts = $hights = 0;
543 if( count( $revisions ) ) {
544 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
545 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
548 $earliestShown = null;
551 /* Don't announce the limit everywhere if it's the default */
552 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
555 foreach (array(20, 50, 100, 250, 500) as $num) {
556 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
557 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
560 $bits = implode($urls, ' | ');
562 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
563 if( $latestShown < $latestTimestamp ) {
564 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
565 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
566 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
567 array( 'limit' => $usefulLimit ) );
569 $prevtext = wfMsgHtml("prevn", $limit);
570 $lasttext = wfMsgHtml('histlast');
573 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
574 if( $earliestShown > $earliestTimestamp ) {
575 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
576 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
577 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
578 array( 'go' => 'first', 'limit' => $usefulLimit ) );
580 $nexttext = wfMsgHtml("nextn", $limit);
581 $firsttext = wfMsgHtml('histfirst');
584 $firstlast = "($lasttext | $firsttext)";
586 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
589 function MakeLink($text, $query = NULL) {
590 if ( $query === null ) return $text;
591 return $this->mSkin
->makeKnownLinkObj(
592 $this->mTitle
, $text,
593 wfArrayToCGI( $query, array( 'action' => 'history' )));
598 * Output a subscription feed listing recent edits to this page.
599 * @param string $type
601 function feed( $type ) {
602 require_once 'SpecialRecentchanges.php';
604 global $wgFeedClasses;
605 if( !isset( $wgFeedClasses[$type] ) ) {
607 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
611 $feed = new $wgFeedClasses[$type](
612 $this->mTitle
->getPrefixedText() . ' - ' .
613 wfMsgForContent( 'history-feed-title' ),
614 wfMsgForContent( 'history-feed-description' ),
615 $this->mTitle
->getFullUrl( 'action=history' ) );
617 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
620 foreach( $items as $row ) {
621 $feed->outItem( $this->feedItem( $row ) );
624 $feed->outItem( $this->feedEmpty() );
629 function feedEmpty() {
632 wfMsgForContent( 'nohistory' ),
633 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
634 $this->mTitle
->getFullUrl(),
635 wfTimestamp( TS_MW
),
637 $this->mTitle
->getTalkPage()->getFullUrl() );
641 * Generate a FeedItem object from a given revision table row
642 * Borrows Recent Changes' feed generation functions for formatting;
643 * includes a diff to the previous revision (if any).
648 function feedItem( $row ) {
649 $rev = new Revision( $row );
650 $rev->setTitle( $this->mTitle
);
651 $text = rcFormatDiffRow( $this->mTitle
,
652 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
654 $rev->getTimestamp(),
655 $rev->getComment() );
657 if( $rev->getComment() == '' ) {
659 $title = wfMsgForContent( 'history-feed-item-nocomment',
661 $wgContLang->timeanddate( $rev->getTimestamp() ) );
663 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
669 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
670 $rev->getTimestamp(),
672 $this->mTitle
->getTalkPage()->getFullUrl() );
676 * Quickie hack... strip out wikilinks to more legible form from the comment.
678 function stripComment( $text ) {
679 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );