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) {
112 if (($lastid = $this->getLastOffsetForPaging($this->mTitle
->getArticleID(), $limit)) === NULL)
114 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
115 wfTimestamp(TS_MW
, $lastid));
119 if (!is_null($gourl)) {
120 $wgOut->redirect($gourl);
128 * If the user clicked "previous", we retrieve the revisions backwards,
129 * then reverse them. This is to avoid needing to know the timestamp of
130 * previous revisions when generating the URL.
132 $direction = $this->getDirection();
133 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
134 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
137 * We fetch one more revision than needed to get the timestamp of the
138 * one after this page (and to know if it exists).
140 * linesonpage stores the actual number of lines.
142 if (count($revisions) < $limit +
1)
143 $this->linesonpage
= count($revisions);
145 $this->linesonpage
= count($revisions) - 1;
147 /* Un-reverse revisions */
148 if ($direction == PageHistory
::DIR_PREV
)
149 $revisions = array_reverse($revisions);
152 * Print the top navbar.
155 $s .= $this->beginHistoryList();
159 * Print each revision, excluding the one-past-the-end, if any.
161 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
162 $latest = !$i && $offset == 0;
164 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
165 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
172 $s .= $this->endHistoryList();
175 $wgOut->addHTML( $s );
176 wfProfileOut( $fname );
179 /** @todo document */
180 function beginHistoryList() {
182 $this->lastdate
= '';
183 $s = wfMsgExt( 'histlegend', array( 'parse') );
184 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
185 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
187 // The following line is SUPPOSED to have double-quotes around the
188 // $prefixedkey variable, because htmlspecialchars() doesn't escape
191 // On at least two occasions people have changed it to single-quotes,
192 // which creates invalid HTML and incorrect display of the resulting
195 // Please do not break this a third time. Thank you for your kind
196 // consideration and cooperation.
198 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
200 $s .= $this->submitButton();
201 $s .= '<ul id="pagehistory">' . "\n";
205 /** @todo document */
206 function endHistoryList() {
208 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
213 /** @todo document */
214 function submitButton( $bits = array() ) {
215 return ( $this->linesonpage
> 0 )
216 ?
wfElement( 'input', array_merge( $bits,
218 'class' => 'historysubmit',
220 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
221 'title' => wfMsg( 'tooltip-compareselectedversions' ),
222 'value' => wfMsg( 'compareselectedversions' ),
227 /** @todo document */
228 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
230 $rev = new Revision( $row );
231 $rev->setTitle( $this->mTitle
);
234 $curlink = $this->curLink( $rev, $latest );
235 $lastlink = $this->lastLink( $rev, $next, $counter );
236 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
237 $link = $this->revLink( $rev );
238 $user = $this->mSkin
->revUserLink( $rev );
240 $s .= "($curlink) ($lastlink) $arbitrary";
242 if( $wgUser->isAllowed( 'deleterevision' ) ) {
243 $revdel = Title
::makeTitle( NS_SPECIAL
, 'Revisiondelete' );
245 // We don't currently handle well changing the top revision's settings
246 $del = wfMsgHtml( 'rev-delundel' );
248 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
249 wfMsg( 'rev-delundel' ),
250 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
251 '&oldid=' . urlencode( $rev->getId() ) );
253 $s .= "(<small>$del</small>) ";
256 $s .= " $link <span class='history-user'>$user</span>";
258 if( $row->rev_minor_edit
) {
259 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
262 $s .= $this->mSkin
->revComment( $rev );
263 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
264 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
266 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
267 $s .= ' ' . wfMsgHtml( 'deletedrev' );
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 if( $rev->getTimestamp() == $this->getEarliestOffset() ) {
310 // Cut off by paging; there are more behind us...
311 return $this->mSkin
->makeKnownLinkObj(
314 "diff=" . $rev->getId() . "&oldid=prev" );
316 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
319 return $this->mSkin
->makeKnownLinkObj(
322 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
326 "tabindex={$counter}"*/ );
330 /** @todo document */
331 function diffButtons( $rev, $firstInList, $counter ) {
332 if( $this->linesonpage
> 1) {
335 'value' => $rev->getId(),
336 # do we really need to flood this on every item?
337 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
340 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
341 $radio['disabled'] = 'disabled';
344 /** @todo: move title texts to javascript */
345 if ( $firstInList ) {
346 $first = wfElement( 'input', array_merge(
349 'style' => 'visibility:hidden',
350 'name' => 'oldid' ) ) );
351 $checkmark = array( 'checked' => 'checked' );
353 if( $counter == 2 ) {
354 $checkmark = array( 'checked' => 'checked' );
356 $checkmark = array();
358 $first = wfElement( 'input', array_merge(
361 array( 'name' => 'oldid' ) ) );
362 $checkmark = array();
364 $second = wfElement( 'input', array_merge(
367 array( 'name' => 'diff' ) ) );
368 return $first . $second;
374 /** @todo document */
375 function getLatestOffset( $id = null ) {
376 if ( $id === null) $id = $this->mTitle
->getArticleID();
377 return $this->getExtremeOffset( $id, 'max' );
380 /** @todo document */
381 function getEarliestOffset( $id = null ) {
382 if ( $id === null) $id = $this->mTitle
->getArticleID();
383 return $this->getExtremeOffset( $id, 'min' );
386 /** @todo document */
387 function getExtremeOffset( $id, $func ) {
388 $db =& wfGetDB(DB_SLAVE
);
389 return $db->selectField( 'revision',
390 "$func(rev_timestamp)",
391 array( 'rev_page' => $id ),
392 'PageHistory::getExtremeOffset' );
395 /** @todo document */
396 function getLatestId() {
397 if( is_null( $this->mLatestId
) ) {
398 $id = $this->mTitle
->getArticleID();
399 $db =& wfGetDB(DB_SLAVE
);
400 $this->mLatestId
= $db->selectField( 'revision',
402 array( 'rev_page' => $id ),
403 'PageHistory::getLatestID' );
405 return $this->mLatestId
;
408 /** @todo document */
409 function getLastOffsetForPaging( $id, $step ) {
410 $fname = 'PageHistory::getLastOffsetForPaging';
412 $dbr =& wfGetDB(DB_SLAVE
);
418 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
420 $n = $dbr->numRows( $res );
422 while( $obj = $dbr->fetchObject( $res ) ) {
423 $last = $obj->rev_timestamp
;
425 $dbr->freeResult( $res );
430 * @return returns the direction of browsing watchlist
432 function getDirection() {
434 if ($wgRequest->getText("dir") == "prev")
435 return PageHistory
::DIR_PREV
;
437 return PageHistory
::DIR_NEXT
;
440 /** @todo document */
441 function fetchRevisions($limit, $offset, $direction) {
442 $fname = 'PageHistory::fetchRevisions';
444 $dbr =& wfGetDB( DB_SLAVE
);
446 if ($direction == PageHistory
::DIR_PREV
)
447 list($dirs, $oper) = array("ASC", ">=");
448 else /* $direction == PageHistory::DIR_NEXT */
449 list($dirs, $oper) = array("DESC", "<=");
452 $offsets = array("rev_timestamp $oper '$offset'");
456 $page_id = $this->mTitle
->getArticleID();
460 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
461 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
462 array_merge(array("rev_page=$page_id"), $offsets),
464 array('ORDER BY' => "rev_timestamp $dirs",
465 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
469 while (($obj = $dbr->fetchObject($res)) != NULL)
475 /** @todo document */
476 function getNotificationTimestamp() {
477 global $wgUser, $wgShowUpdatedMarker;
478 $fname = 'PageHistory::getNotficationTimestamp';
480 if ($this->mNotificationTimestamp
!== NULL)
481 return $this->mNotificationTimestamp
;
483 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
484 return $this->mNotificationTimestamp
= false;
486 $dbr =& wfGetDB(DB_SLAVE
);
488 $this->mNotificationTimestamp
= $dbr->selectField(
490 'wl_notificationtimestamp',
491 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
492 'wl_title' => $this->mTitle
->getDBkey(),
493 'wl_user' => $wgUser->getID()
497 // Don't use the special value reserved for telling whether the field is filled
498 if ( is_null( $this->mNotificationTimestamp
) ) {
499 $this->mNotificationTimestamp
= false;
502 return $this->mNotificationTimestamp
;
505 /** @todo document */
506 function makeNavbar($revisions, $offset, $limit, $direction) {
509 $revisions = array_slice($revisions, 0, $limit);
511 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
512 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
515 * When we're displaying previous revisions, we need to reverse
516 * the array, because it's queried in reverse order.
518 if ($direction == PageHistory
::DIR_PREV
)
519 $revisions = array_reverse($revisions);
522 * lowts is the timestamp of the first revision on this page.
523 * hights is the timestamp of the last revision.
526 $lowts = $hights = 0;
528 if( count( $revisions ) ) {
529 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
530 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
533 $earliestShown = null;
536 /* Don't announce the limit everywhere if it's the default */
537 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
540 foreach (array(20, 50, 100, 250, 500) as $num) {
541 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
542 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
545 $bits = implode($urls, ' | ');
547 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
548 if( $latestShown < $latestTimestamp ) {
549 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
550 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
551 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
552 array( 'limit' => $usefulLimit ) );
554 $prevtext = wfMsgHtml("prevn", $limit);
555 $lasttext = wfMsgHtml('histlast');
558 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
559 if( $earliestShown > $earliestTimestamp ) {
560 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
561 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
562 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
563 array( 'go' => 'first', 'limit' => $usefulLimit ) );
565 $nexttext = wfMsgHtml("nextn", $limit);
566 $firsttext = wfMsgHtml('histfirst');
569 $firstlast = "($lasttext | $firsttext)";
571 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
574 function MakeLink($text, $query = NULL) {
575 if ( $query === null ) return $text;
576 return $this->mSkin
->makeKnownLinkObj(
577 $this->mTitle
, $text,
578 wfArrayToCGI( $query, array( 'action' => 'history' )));
583 * Output a subscription feed listing recent edits to this page.
584 * @param string $type
586 function feed( $type ) {
587 require_once 'SpecialRecentchanges.php';
589 global $wgFeedClasses;
590 if( !isset( $wgFeedClasses[$type] ) ) {
592 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
596 $feed = new $wgFeedClasses[$type](
597 $this->mTitle
->getPrefixedText() . ' - ' .
598 wfMsgForContent( 'history-feed-title' ),
599 wfMsgForContent( 'history-feed-description' ),
600 $this->mTitle
->getFullUrl( 'action=history' ) );
602 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
605 foreach( $items as $row ) {
606 $feed->outItem( $this->feedItem( $row ) );
609 $feed->outItem( $this->feedEmpty() );
614 function feedEmpty() {
617 wfMsgForContent( 'nohistory' ),
618 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
619 $this->mTitle
->getFullUrl(),
620 wfTimestamp( TS_MW
),
622 $this->mTitle
->getTalkPage()->getFullUrl() );
626 * Generate a FeedItem object from a given revision table row
627 * Borrows Recent Changes' feed generation functions for formatting;
628 * includes a diff to the previous revision (if any).
633 function feedItem( $row ) {
634 $rev = new Revision( $row );
635 $rev->setTitle( $this->mTitle
);
636 $text = rcFormatDiffRow( $this->mTitle
,
637 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
639 $rev->getTimestamp(),
640 $rev->getComment() );
642 if( $rev->getComment() == '' ) {
644 $title = wfMsgForContent( 'history-feed-item-nocomment',
646 $wgContLang->timeanddate( $rev->getTimestamp() ) );
648 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
654 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
655 $rev->getTimestamp(),
657 $this->mTitle
->getTalkPage()->getFullUrl() );
661 * Quickie hack... strip out wikilinks to more legible form from the comment.
663 function stripComment( $text ) {
664 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );