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->setSubtitle( wfMsg( 'revhistory' ) );
71 $wgOut->setArticleFlag( false );
72 $wgOut->setArticleRelated( true );
73 $wgOut->setRobotpolicy( 'noindex,nofollow' );
74 $wgOut->setSyndicated( true );
76 $feedType = $wgRequest->getVal( 'feed' );
78 wfProfileOut( $fname );
79 return $this->feed( $feedType );
83 * Fail if article doesn't exist.
85 if( !$this->mTitle
->exists() ) {
86 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
87 wfProfileOut( $fname );
91 $dbr =& wfGetDB(DB_SLAVE
);
94 * Extract limit, the number of revisions to show, and
95 * offset, the timestamp to begin at, from the URL.
97 $limit = $wgRequest->getInt('limit', $this->defaultLimit
);
98 $offset = $wgRequest->getText('offset');
100 /* Offset must be an integral. */
101 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
103 # $offset = $dbr->timestamp($offset);
104 $dboffset = $offset === 0 ?
0 : $dbr->timestamp($offset);
106 * "go=last" means to jump to the last history page.
108 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
111 if (($lastid = $this->getLastOffsetForPaging($this->mTitle
->getArticleID(), $limit)) === NULL)
113 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
114 wfTimestamp(TS_MW
, $lastid));
120 if (!is_null($gourl)) {
121 $wgOut->redirect($gourl);
129 * If the user clicked "previous", we retrieve the revisions backwards,
130 * then reverse them. This is to avoid needing to know the timestamp of
131 * previous revisions when generating the URL.
133 $direction = $this->getDirection();
134 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
135 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
138 * We fetch one more revision than needed to get the timestamp of the
139 * one after this page (and to know if it exists).
141 * linesonpage stores the actual number of lines.
143 if (count($revisions) < $limit +
1)
144 $this->linesonpage
= count($revisions);
146 $this->linesonpage
= count($revisions) - 1;
148 /* Un-reverse revisions */
149 if ($direction == PageHistory
::DIR_PREV
)
150 $revisions = array_reverse($revisions);
153 * Print the top navbar.
156 $s .= $this->beginHistoryList();
160 * Print each revision, excluding the one-past-the-end, if any.
162 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
163 $latest = !$i && $offset == 0;
165 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
166 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
173 $s .= $this->endHistoryList();
176 $wgOut->addHTML( $s );
177 wfProfileOut( $fname );
180 /** @todo document */
181 function beginHistoryList() {
183 $this->lastdate
= '';
184 $s = wfMsgExt( 'histlegend', array( 'parse') );
185 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
186 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
188 // The following line is SUPPOSED to have double-quotes around the
189 // $prefixedkey variable, because htmlspecialchars() doesn't escape
192 // On at least two occasions people have changed it to single-quotes,
193 // which creates invalid HTML and incorrect display of the resulting
196 // Please do not break this a third time. Thank you for your kind
197 // consideration and cooperation.
199 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
201 $s .= $this->submitButton();
202 $s .= '<ul id="pagehistory">' . "\n";
206 /** @todo document */
207 function endHistoryList() {
209 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
214 /** @todo document */
215 function submitButton( $bits = array() ) {
216 return ( $this->linesonpage
> 0 )
217 ?
wfElement( 'input', array_merge( $bits,
219 'class' => 'historysubmit',
221 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
222 'title' => wfMsg( 'tooltip-compareselectedversions' ),
223 'value' => wfMsg( 'compareselectedversions' ),
228 /** @todo document */
229 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
231 $rev = new Revision( $row );
232 $rev->setTitle( $this->mTitle
);
235 $curlink = $this->curLink( $rev, $latest );
236 $lastlink = $this->lastLink( $rev, $next, $counter );
237 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
238 $link = $this->revLink( $rev );
239 $user = $this->mSkin
->revUserLink( $rev );
241 $s .= "($curlink) ($lastlink) $arbitrary";
243 if( $wgUser->isAllowed( 'deleterevision' ) ) {
244 $revdel = Title
::makeTitle( NS_SPECIAL
, 'Revisiondelete' );
246 // We don't currently handle well changing the top revision's settings
247 $del = wfMsgHtml( 'rev-delundel' );
249 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
250 wfMsg( 'rev-delundel' ),
251 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
252 '&oldid=' . urlencode( $rev->getId() ) );
254 $s .= "(<small>$del</small>) ";
257 $s .= " $link <span class='history-user'>$user</span>";
259 if( $row->rev_minor_edit
) {
260 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
263 $s .= $this->mSkin
->revComment( $rev );
264 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
265 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
267 if( $row->rev_deleted
& Revision
::MW_REV_DELETED_TEXT
) {
268 $s .= ' ' . wfMsgHtml( 'deletedrev' );
275 /** @todo document */
276 function revLink( $rev ) {
278 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
279 if( $rev->userCan( Revision
::MW_REV_DELETED_TEXT
) ) {
280 $link = $this->mSkin
->makeKnownLinkObj(
281 $this->mTitle
, $date, "oldid=" . $rev->getId() );
285 if( $rev->isDeleted( Revision
::MW_REV_DELETED_TEXT
) ) {
286 return '<span class="history-deleted">' . $link . '</span>';
291 /** @todo document */
292 function curLink( $rev, $latest ) {
293 $cur = wfMsgExt( 'cur', array( 'escape') );
294 if( $latest ||
!$rev->userCan( Revision
::MW_REV_DELETED_TEXT
) ) {
297 return $this->mSkin
->makeKnownLinkObj(
299 'diff=' . $this->getLatestID() .
300 "&oldid=" . $rev->getId() );
304 /** @todo document */
305 function lastLink( $rev, $next, $counter ) {
306 $last = wfMsgExt( 'last', array( 'escape' ) );
307 if( is_null( $next ) ) {
308 if( $rev->getTimestamp() == $this->getEarliestOffset() ) {
311 // Cut off by paging; there are more behind us...
312 return $this->mSkin
->makeKnownLinkObj(
315 "diff=" . $rev->getId() . "&oldid=prev" );
317 } elseif( !$rev->userCan( Revision
::MW_REV_DELETED_TEXT
) ) {
320 return $this->mSkin
->makeKnownLinkObj(
323 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
327 "tabindex={$counter}"*/ );
331 /** @todo document */
332 function diffButtons( $rev, $firstInList, $counter ) {
333 if( $this->linesonpage
> 1) {
336 'value' => $rev->getId(),
337 # do we really need to flood this on every item?
338 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
341 if( !$rev->userCan( Revision
::MW_REV_DELETED_TEXT
) ) {
342 $radio['disabled'] = 'disabled';
345 /** @todo: move title texts to javascript */
346 if ( $firstInList ) {
347 $first = wfElement( 'input', array_merge(
350 'style' => 'visibility:hidden',
351 'name' => 'oldid' ) ) );
352 $checkmark = array( 'checked' => 'checked' );
354 if( $counter == 2 ) {
355 $checkmark = array( 'checked' => 'checked' );
357 $checkmark = array();
359 $first = wfElement( 'input', array_merge(
362 array( 'name' => 'oldid' ) ) );
363 $checkmark = array();
365 $second = wfElement( 'input', array_merge(
368 array( 'name' => 'diff' ) ) );
369 return $first . $second;
375 /** @todo document */
376 function getLatestOffset( $id = null ) {
377 if ( $id === null) $id = $this->mTitle
->getArticleID();
378 return $this->getExtremeOffset( $id, 'max' );
381 /** @todo document */
382 function getEarliestOffset( $id = null ) {
383 if ( $id === null) $id = $this->mTitle
->getArticleID();
384 return $this->getExtremeOffset( $id, 'min' );
387 /** @todo document */
388 function getExtremeOffset( $id, $func ) {
389 $db =& wfGetDB(DB_SLAVE
);
390 return $db->selectField( 'revision',
391 "$func(rev_timestamp)",
392 array( 'rev_page' => $id ),
393 'PageHistory::getExtremeOffset' );
396 /** @todo document */
397 function getLatestId() {
398 if( is_null( $this->mLatestId
) ) {
399 $id = $this->mTitle
->getArticleID();
400 $db =& wfGetDB(DB_SLAVE
);
401 $this->mLatestId
= $db->selectField( 'revision',
403 array( 'rev_page' => $id ),
404 'PageHistory::getLatestID' );
406 return $this->mLatestId
;
409 /** @todo document */
410 function getLastOffsetForPaging( $id, $step ) {
411 $fname = 'PageHistory::getLastOffsetForPaging';
413 $dbr =& wfGetDB(DB_SLAVE
);
419 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
421 $n = $dbr->numRows( $res );
423 while( $obj = $dbr->fetchObject( $res ) ) {
424 $last = $obj->rev_timestamp
;
426 $dbr->freeResult( $res );
431 * @return returns the direction of browsing watchlist
433 function getDirection() {
435 if ($wgRequest->getText("dir") == "prev")
436 return PageHistory
::DIR_PREV
;
438 return PageHistory
::DIR_NEXT
;
441 /** @todo document */
442 function fetchRevisions($limit, $offset, $direction) {
443 $fname = 'PageHistory::fetchRevisions';
445 $dbr =& wfGetDB( DB_SLAVE
);
447 if ($direction == PageHistory
::DIR_PREV
)
448 list($dirs, $oper) = array("ASC", ">=");
449 else /* $direction == PageHistory::DIR_NEXT */
450 list($dirs, $oper) = array("DESC", "<=");
453 $offsets = array("rev_timestamp $oper '$offset'");
457 $page_id = $this->mTitle
->getArticleID();
461 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
462 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
463 array_merge(array("rev_page=$page_id"), $offsets),
465 array('ORDER BY' => "rev_timestamp $dirs",
466 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
470 while (($obj = $dbr->fetchObject($res)) != NULL)
476 /** @todo document */
477 function getNotificationTimestamp() {
478 global $wgUser, $wgShowUpdatedMarker;
479 $fname = 'PageHistory::getNotficationTimestamp';
481 if ($this->mNotificationTimestamp
!== NULL)
482 return $this->mNotificationTimestamp
;
484 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
485 return $this->mNotificationTimestamp
= false;
487 $dbr =& wfGetDB(DB_SLAVE
);
489 $this->mNotificationTimestamp
= $dbr->selectField(
491 'wl_notificationtimestamp',
492 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
493 'wl_title' => $this->mTitle
->getDBkey(),
494 'wl_user' => $wgUser->getID()
498 // Don't use the special value reserved for telling whether the field is filled
499 if ( is_null( $this->mNotificationTimestamp
) ) {
500 $this->mNotificationTimestamp
= false;
503 return $this->mNotificationTimestamp
;
506 /** @todo document */
507 function makeNavbar($revisions, $offset, $limit, $direction) {
510 $revisions = array_slice($revisions, 0, $limit);
512 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
513 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
516 * When we're displaying previous revisions, we need to reverse
517 * the array, because it's queried in reverse order.
519 if ($direction == PageHistory
::DIR_PREV
)
520 $revisions = array_reverse($revisions);
523 * lowts is the timestamp of the first revision on this page.
524 * hights is the timestamp of the last revision.
527 $lowts = $hights = 0;
529 if( count( $revisions ) ) {
530 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
531 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
534 /* Don't announce the limit everywhere if it's the default */
535 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
538 foreach (array(20, 50, 100, 250, 500) as $num) {
539 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
540 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
543 $bits = implode($urls, ' | ');
545 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
546 if( $latestShown < $latestTimestamp ) {
547 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
548 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
549 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
550 array( 'limit' => $usefulLimit ) );
552 $prevtext = wfMsgHtml("prevn", $limit);
553 $lasttext = wfMsgHtml('histlast');
556 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
557 if( $earliestShown > $earliestTimestamp ) {
558 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
559 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
560 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
561 array( 'go' => 'first', 'limit' => $usefulLimit ) );
563 $nexttext = wfMsgHtml("nextn", $limit);
564 $firsttext = wfMsgHtml('histfirst');
567 $firstlast = "($lasttext | $firsttext)";
569 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
572 function MakeLink($text, $query = NULL) {
573 if ( $query === null ) return $text;
574 return $this->mSkin
->makeKnownLinkObj(
575 $this->mTitle
, $text,
576 wfArrayToCGI( $query, array( 'action' => 'history' )));
581 * Output a subscription feed listing recent edits to this page.
582 * @param string $type
584 function feed( $type ) {
585 require_once 'SpecialRecentchanges.php';
587 global $wgFeedClasses;
588 if( !isset( $wgFeedClasses[$type] ) ) {
590 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
594 $feed = new $wgFeedClasses[$type](
595 $this->mTitle
->getPrefixedText() . ' - ' .
596 wfMsgForContent( 'history-feed-title' ),
597 wfMsgForContent( 'history-feed-description' ),
598 $this->mTitle
->getFullUrl( 'action=history' ) );
600 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
603 foreach( $items as $row ) {
604 $feed->outItem( $this->feedItem( $row ) );
607 $feed->outItem( $this->feedEmpty() );
612 function feedEmpty() {
615 wfMsgForContent( 'nohistory' ),
616 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
617 $this->mTitle
->getFullUrl(),
618 wfTimestamp( TS_MW
),
620 $this->mTitle
->getTalkPage()->getFullUrl() );
624 * Generate a FeedItem object from a given revision table row
625 * Borrows Recent Changes' feed generation functions for formatting;
626 * includes a diff to the previous revision (if any).
631 function feedItem( $row ) {
632 $rev = new Revision( $row );
633 $rev->setTitle( $this->mTitle
);
634 $text = rcFormatDiffRow( $this->mTitle
,
635 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
637 $rev->getTimestamp(),
638 $rev->getComment() );
640 if( $rev->getComment() == '' ) {
642 $title = wfMsgForContent( 'history-feed-item-nocomment',
644 $wgContLang->timeanddate( $rev->getTimestamp() ) );
646 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
652 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
653 $rev->getTimestamp(),
655 $this->mTitle
->getTalkPage()->getFullUrl() );
659 * Quickie hack... strip out wikilinks to more legible form from the comment.
661 function stripComment( $text ) {
662 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );