5 * Split off from Article.php and Skin.php, 2003-12-22
10 define('DIR_NEXT', 1);
13 * This class handles printing the history page for an article. In order to
14 * be efficient, it uses timestamps rather than offsets for paging, to avoid
15 * costly LIMIT,offset queries.
17 * 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' );
76 * Fail if article doesn't exist.
78 if( !$this->mTitle
->exists() ) {
79 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
80 wfProfileOut( $fname );
84 $dbr =& wfGetDB(DB_SLAVE
);
87 * Extract limit, the number of revisions to show, and
88 * offset, the timestamp to begin at, from the URL.
90 $limit = $wgRequest->getInt('limit', $this->defaultLimit
);
91 $offset = $wgRequest->getText('offset');
93 /* Offset must be an integral. */
94 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
96 # $offset = $dbr->timestamp($offset);
97 $dboffset = $offset === 0 ?
0 : $dbr->timestamp($offset);
99 * "go=last" means to jump to the last history page.
101 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
104 if (($lastid = $this->getLastOffsetForPaging($this->mTitle
->getArticleID(), $limit)) === NULL)
106 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
107 wfTimestamp(TS_MW
, $lastid));
113 if (!is_null($gourl)) {
114 $wgOut->redirect($gourl);
122 * If the user clicked "previous", we retrieve the revisions backwards,
123 * then reverse them. This is to avoid needing to know the timestamp of
124 * previous revisions when generating the URL.
126 $direction = $this->getDirection();
127 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
128 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
131 * We fetch one more revision than needed to get the timestamp of the
132 * one after this page (and to know if it exists).
134 * linesonpage stores the actual number of lines.
136 if (count($revisions) < $limit +
1)
137 $this->linesonpage
= count($revisions);
139 $this->linesonpage
= count($revisions) - 1;
141 /* Un-reverse revisions */
142 if ($direction == DIR_PREV
)
143 $revisions = array_reverse($revisions);
146 * Print the top navbar.
149 $s .= $this->beginHistoryList();
153 * Print each revision, excluding the one-past-the-end, if any.
155 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
156 $latest = !$i && $offset == 0;
158 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
159 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
166 $s .= $this->endHistoryList();
169 $wgOut->addHTML( $s );
170 wfProfileOut( $fname );
173 /** @todo document */
174 function beginHistoryList() {
176 $this->lastdate
= '';
177 $s = wfMsgWikiHtml( 'histlegend' );
178 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
179 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
181 // The following line is SUPPOSED to have double-quotes around the
182 // $prefixedkey variable, because htmlspecialchars() doesn't escape
185 // On at least two occasions people have changed it to single-quotes,
186 // which creates invalid HTML and incorrect display of the resulting
189 // Please do not break this a third time. Thank you for your kind
190 // consideration and cooperation.
192 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
194 $s .= $this->submitButton();
195 $s .= '<ul id="pagehistory">' . "\n";
199 /** @todo document */
200 function endHistoryList() {
201 $last = wfMsg( 'last' );
204 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
209 /** @todo document */
210 function submitButton( $bits = array() ) {
211 return ( $this->linesonpage
> 0 )
212 ?
wfElement( 'input', array_merge( $bits,
214 'class' => 'historysubmit',
216 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
217 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
218 'value' => wfMsgHtml( 'compareselectedversions' ),
223 /** @todo document */
224 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
226 $rev = new Revision( $row );
229 $curlink = $this->curLink( $rev, $latest );
230 $lastlink = $this->lastLink( $rev, $next, $counter );
231 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
232 $link = $this->revLink( $rev );
233 $user = $this->mSkin
->revUserLink( $rev );
235 $s .= "($curlink) ($lastlink) $arbitrary";
237 if( $wgUser->isAllowed( 'deleterevision' ) ) {
238 $revdel = Title
::makeTitle( NS_SPECIAL
, 'Revisiondelete' );
240 // We don't currently handle well changing the top revision's settings
241 $del = wfMsgHtml( 'rev-delundel' );
243 $del = $this->mSkin
->makeKnownLinkObj( $revdel,
244 wfMsg( 'rev-delundel' ),
245 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
246 '&oldid=' . urlencode( $rev->getId() ) );
248 $s .= "(<small>$del</small>) ";
251 $s .= " $link <span class='history-user'>$user</span>";
253 if( $row->rev_minor_edit
) {
254 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
257 $s .= $this->mSkin
->revComment( $rev );
258 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
259 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
261 if( $row->rev_deleted
& MW_REV_DELETED_TEXT
) {
262 $s .= ' ' . wfMsgHtml( 'deletedrev' );
269 /** @todo document */
270 function revLink( $rev ) {
271 global $wgUser, $wgLang;
272 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
273 if( $rev->userCan( MW_REV_DELETED_TEXT
) ) {
274 $link = $this->mSkin
->makeKnownLinkObj(
275 $this->mTitle
, $date, "oldid=" . $rev->getId() );
279 if( $rev->isDeleted( MW_REV_DELETED_TEXT
) ) {
280 return '<span class="history-deleted">' . $link . '</span>';
285 /** @todo document */
286 function curLink( $rev, $latest ) {
288 $cur = wfMsgHtml( 'cur' );
289 if( $latest ||
!$rev->userCan( MW_REV_DELETED_TEXT
) ) {
292 return $this->mSkin
->makeKnownLinkObj(
294 'diff=' . $this->getLatestID() .
295 "&oldid=" . $rev->getId() );
299 /** @todo document */
300 function lastLink( $rev, $next, $counter ) {
302 $last = htmlspecialchars( wfMsg( 'last' ) );
303 if( is_null( $next ) ) {
304 if( $rev->getTimestamp() == $this->getEarliestOffset() ) {
307 // Cut off by paging; there are more behind us...
308 return $this->mSkin
->makeKnownLinkObj(
311 "diff=" . $rev->getId() . "&oldid=prev" );
313 } elseif( !$rev->userCan( MW_REV_DELETED_TEXT
) ) {
316 return $this->mSkin
->makeKnownLinkObj(
319 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
323 "tabindex={$counter}"*/ );
327 /** @todo document */
328 function diffButtons( $rev, $firstInList, $counter ) {
330 if( $this->linesonpage
> 1) {
333 'value' => $rev->getId(),
334 # do we really need to flood this on every item?
335 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
338 if( !$rev->userCan( MW_REV_DELETED_TEXT
) ) {
339 $radio['disabled'] = 'disabled';
342 /** @todo: move title texts to javascript */
343 if ( $firstInList ) {
344 $first = wfElement( 'input', array_merge(
347 'style' => 'visibility:hidden',
348 'name' => 'oldid' ) ) );
349 $checkmark = array( 'checked' => 'checked' );
351 if( $counter == 2 ) {
352 $checkmark = array( 'checked' => 'checked' );
354 $checkmark = array();
356 $first = wfElement( 'input', array_merge(
359 array( 'name' => 'oldid' ) ) );
360 $checkmark = array();
362 $second = wfElement( 'input', array_merge(
365 array( 'name' => 'diff' ) ) );
366 return $first . $second;
372 /** @todo document */
373 function getLatestOffset( $id = null ) {
374 if ( $id === null) $id = $this->mTitle
->getArticleID();
375 return $this->getExtremeOffset( $id, 'max' );
378 /** @todo document */
379 function getEarliestOffset( $id = null ) {
380 if ( $id === null) $id = $this->mTitle
->getArticleID();
381 return $this->getExtremeOffset( $id, 'min' );
384 /** @todo document */
385 function getExtremeOffset( $id, $func ) {
386 $db =& wfGetDB(DB_SLAVE
);
387 return $db->selectField( 'revision',
388 "$func(rev_timestamp)",
389 array( 'rev_page' => $id ),
390 'PageHistory::getExtremeOffset' );
393 /** @todo document */
394 function getLatestId() {
395 if( is_null( $this->mLatestId
) ) {
396 $id = $this->mTitle
->getArticleID();
397 $db =& wfGetDB(DB_SLAVE
);
398 $this->mLatestId
= $db->selectField( 'revision',
400 array( 'rev_page' => $id ),
401 'PageHistory::getLatestID' );
403 return $this->mLatestId
;
406 /** @todo document */
407 function getLastOffsetForPaging( $id, $step ) {
408 $fname = 'PageHistory::getLastOffsetForPaging';
410 $dbr =& wfGetDB(DB_SLAVE
);
416 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
418 $n = $dbr->numRows( $res );
420 while( $obj = $dbr->fetchObject( $res ) ) {
421 $last = $obj->rev_timestamp
;
423 $dbr->freeResult( $res );
428 * @return returns the direction of browsing watchlist
430 function getDirection() {
432 if ($wgRequest->getText("dir") == "prev")
438 /** @todo document */
439 function fetchRevisions($limit, $offset, $direction) {
440 $fname = 'PageHistory::fetchRevisions';
442 $dbr =& wfGetDB( DB_SLAVE
);
444 if ($direction == DIR_PREV
)
445 list($dirs, $oper) = array("ASC", ">=");
446 else /* $direction == DIR_NEXT */
447 list($dirs, $oper) = array("DESC", "<=");
450 $offsets = array("rev_timestamp $oper '$offset'");
454 $page_id = $this->mTitle
->getArticleID();
458 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
459 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
460 array_merge(array("rev_page=$page_id"), $offsets),
462 array('ORDER BY' => "rev_timestamp $dirs",
463 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
467 while (($obj = $dbr->fetchObject($res)) != NULL)
473 /** @todo document */
474 function getNotificationTimestamp() {
475 global $wgUser, $wgShowUpdatedMarker;
476 $fname = 'PageHistory::getNotficationTimestamp';
478 if ($this->mNotificationTimestamp
!== NULL)
479 return $this->mNotificationTimestamp
;
481 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
482 return $this->mNotificationTimestamp
= false;
484 $dbr =& wfGetDB(DB_SLAVE
);
486 $this->mNotificationTimestamp
= $dbr->selectField(
488 'wl_notificationtimestamp',
489 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
490 'wl_title' => $this->mTitle
->getDBkey(),
491 'wl_user' => $wgUser->getID()
495 return $this->mNotificationTimestamp
;
498 /** @todo document */
499 function makeNavbar($revisions, $offset, $limit, $direction) {
502 $revisions = array_slice($revisions, 0, $limit);
504 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset());
505 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset());
508 * When we're displaying previous revisions, we need to reverse
509 * the array, because it's queried in reverse order.
511 if ($direction == DIR_PREV
)
512 $revisions = array_reverse($revisions);
515 * lowts is the timestamp of the first revision on this page.
516 * hights is the timestamp of the last revision.
519 $lowts = $hights = 0;
521 if( count( $revisions ) ) {
522 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
523 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
526 /* Don't announce the limit everywhere if it's the default */
527 $usefulLimit = $limit == $this->defaultLimit ?
'' : $limit;
530 foreach (array(20, 50, 100, 250, 500) as $num) {
531 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
532 array('offset' => $offset == 0 ?
'' : wfTimestamp(TS_MW
, $offset), 'limit' => $num, ) );
535 $bits = implode($urls, ' | ');
537 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
538 if( $latestShown < $latestTimestamp ) {
539 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
540 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
541 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
542 array( 'limit' => $usefulLimit ) );
544 $prevtext = wfMsgHtml("prevn", $limit);
545 $lasttext = wfMsgHtml('histlast');
548 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
549 if( $earliestShown > $earliestTimestamp ) {
550 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
551 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
552 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
553 array( 'go' => 'first', 'limit' => $usefulLimit ) );
555 $nexttext = wfMsgHtml("nextn", $limit);
556 $firsttext = wfMsgHtml('histfirst');
559 $firstlast = "($lasttext | $firsttext)";
561 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
564 function MakeLink($text, $query = NULL) {
565 if ( $query === null ) return $text;
566 return $this->mSkin
->makeKnownLinkObj(
567 $this->mTitle
, $text,
568 wfArrayToCGI( $query, array( 'action' => 'history' )));