5 * Split off from Article.php and Skin.php, 2003-12-22
10 include_once ( 'SpecialValidate.php' );
12 define('DIR_PREV', 0);
13 define('DIR_NEXT', 1);
16 * This class handles printing the history page for an article. In order to
17 * be efficient, it uses timestamps rather than offsets for paging, to avoid
18 * costly LIMIT,offset queries.
20 * Construct it by passing in an Article, and call $h->history() to print the
27 var $mArticle, $mTitle, $mSkin;
30 var $mNotificationTimestamp;
33 * Construct a new PageHistory.
35 * @param Article $article
38 function PageHistory($article) {
41 $this->mArticle
=& $article;
42 $this->mTitle
=& $article->mTitle
;
43 $this->mNotificationTimestamp
= NULL;
44 $this->mSkin
= $wgUser->getSkin();
48 * Print the history page for an article.
53 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
54 $wgTitle, $wgUseValidation;
57 * Allow client caching.
60 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
61 /* Client cache fresh and headers sent, nothing more to do. */
64 $fname = 'PageHistory::history';
65 wfProfileIn( $fname );
67 $dbr = wfGetDB(DB_SLAVE
);
70 * Setup page variables.
72 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
73 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
74 $wgOut->setArticleFlag( false );
75 $wgOut->setArticleRelated( true );
76 $wgOut->setRobotpolicy( 'noindex,nofollow' );
79 * Fail if article doesn't exist.
81 $id = $this->mTitle
->getArticleID();
83 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
84 wfProfileOut( $fname );
89 * Extract limit, the number of revisions to show, and
90 * offset, the timestamp to begin at, from the URL.
92 $limit = $wgRequest->getInt('limit', 50);
93 $offset = $wgRequest->getText('offset');
95 /* Offset must be an integral. */
96 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
98 # $offset = $dbr->timestamp($offset);
99 $dboffset = $dbr->timestamp($offset);
100 wfdebug("offset=[$offset] dboffset=[$dboffset]\n");
102 * "go=last" means to jump to the last history page.
104 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
107 if (($lastid = $this->getLastOffsetForPaging($id, $limit)) === NULL)
109 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
110 wfTimestamp(TS_MW
, $lastid));
116 if (!is_null($gourl)) {
117 $wgOut->redirect($gourl);
125 * If the user clicked "previous", we retrieve the revisions backwards,
126 * then reverse them. This is to avoid needing to know the timestamp of
127 * previous revisions when generating the URL.
129 $direction = $this->getDirection();
130 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
131 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
134 * We fetch one more revision than needed to get the timestamp of the
135 * one after this page (and to know if it exists).
137 * linesonpage stores the actual number of lines.
139 if (count($revisions) < $limit +
1)
140 $this->linesonpage
= count($revisions);
142 $this->linesonpage
= count($revisions) - 1;
144 /* Un-reverse revisions */
145 if ($direction == DIR_PREV
)
146 $revisions = array_reverse($revisions);
149 * Print the top navbar.
152 $s .= $this->beginHistoryList();
156 * Print each revision, excluding the one-past-the-end, if any.
158 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
159 $first = !$i && $offset == 0;
160 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
161 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $first);
168 $s .= $this->endHistoryList();
172 * Article validation line.
174 if ($wgUseValidation)
175 $s .= '<p>' . Validation
::getStatisticsLink( $this->mArticle
) . '</p>' ;
177 $wgOut->addHTML( $s );
178 wfProfileOut( $fname );
181 /** @todo document */
182 function beginHistoryList() {
184 $this->lastdate
= '';
185 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
186 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
187 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
188 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
189 $s .= $this->submitButton();
190 $s .= '<ul id="pagehistory">';
194 /** @todo document */
195 function endHistoryList() {
196 $last = wfMsg( 'last' );
199 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
204 /** @todo document */
205 function submitButton( $bits = array() ) {
206 return ( $this->linesonpage
> 0 )
207 ?
wfElement( 'input', array_merge( $bits,
209 'class' => 'historysubmit',
211 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
212 'title' => wfMsg( 'tooltip-compareselectedversions' ),
213 'value' => wfMsg( 'compareselectedversions' ),
218 /** @todo document */
219 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
220 global $wgLang, $wgContLang;
223 if( !isset( $message ) ) {
224 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
225 $message[$msg] = wfMsg( $msg );
229 $link = $this->revLink( $row );
231 if ( 0 == $row->rev_user
) {
232 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
233 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
234 htmlspecialchars( $row->rev_user_text
),
235 'target=' . urlencode( $row->rev_user_text
) );
237 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
238 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
242 if( $row->rev_deleted
) {
243 $s .= '<span class="deleted">';
245 $curlink = $this->curLink( $row, $latest );
246 $lastlink = $this->lastLink( $row, $next, $counter );
247 $arbitrary = $this->diffButtons( $row, $latest, $counter );
248 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
250 if( $row->rev_minor_edit
) {
251 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
254 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
255 if ($this->getNotificationTimestamp() && ($row->rev_timestamp
>= $this->getNotificationTimestamp())) {
256 $s .= wfMsg( 'updatedmarker' );
258 if( $row->rev_deleted
) {
259 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
266 /** @todo document */
267 function revLink( $row ) {
268 global $wgUser, $wgLang;
269 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->rev_timestamp
), true );
270 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
273 return $this->mSkin
->makeKnownLinkObj(
276 'oldid='.$row->rev_id
);
280 /** @todo document */
281 function curLink( $row, $latest ) {
283 $cur = htmlspecialchars( wfMsg( 'cur' ) );
285 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
288 return $this->mSkin
->makeKnownLinkObj(
291 'diff=' . $this->getLatestID($this->mTitle
->getArticleID())
292 . '&oldid=' . $row->rev_id
);
296 /** @todo document */
297 function lastLink( $row, $next, $counter ) {
299 $last = htmlspecialchars( wfMsg( 'last' ) );
301 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
304 return $this->mSkin
->makeKnownLinkObj(
307 "diff={$row->rev_id}&oldid={$next->rev_id}",
310 ' tabindex="'.$counter.'"' );
314 /** @todo document */
315 function diffButtons( $row, $latest, $counter ) {
317 if( $this->linesonpage
> 1) {
320 'value' => $row->rev_id
,
321 'title' => wfMsg( 'selectolderversionfordiff' )
323 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
324 $radio['disabled'] = 'disabled';
327 # XXX: move title texts to javascript
329 $first = wfElement( 'input', array_merge(
332 'style' => 'visibility:hidden',
333 'name' => 'oldid' ) ) );
334 $checkmark = array( 'checked' => 'checked' );
336 if( $counter == 2 ) {
337 $checkmark = array( 'checked' => 'checked' );
339 $checkmark = array();
341 $first = wfElement( 'input', array_merge(
344 array( 'name' => 'oldid' ) ) );
345 $checkmark = array();
347 $second = wfElement( 'input', array_merge(
350 array( 'name' => 'diff' ) ) );
351 return $first . $second;
357 /** @todo document */
358 function getLatestOffset($id) {
359 return $this->getExtremeOffset( $id, 'max' );
362 /** @todo document */
363 function getEarliestOffset($id) {
364 return $this->getExtremeOffset( $id, 'min' );
367 /** @todo document */
368 function getExtremeOffset( $id, $func ) {
369 $db =& wfGetDB(DB_SLAVE
);
370 return $db->selectField( 'revision',
371 "$func(rev_timestamp)",
372 array( 'rev_page' => $id ),
373 'PageHistory::getExtremeOffset' );
376 /** @todo document */
377 function getLatestID( $id ) {
378 $db =& wfGetDB(DB_SLAVE
);
379 return $db->selectField( 'revision',
381 array( 'rev_page' => $id ),
382 'PageHistory::getLatestID' );
385 /** @todo document */
386 function getLastOffsetForPaging( $id, $step = 50 ) {
387 $db =& wfGetDB(DB_SLAVE
);
388 $revision = $db->tableName( 'revision' );
389 $sql = "SELECT rev_timestamp FROM $revision WHERE rev_page = $id " .
390 "ORDER BY rev_timestamp ASC";
391 $sql = $db->limitResult($sql, $step, 0);
392 $res = $db->query( $sql, "PageHistory::getLastOffsetForPaging" );
393 $n = $db->numRows( $res );
396 while( $obj = $db->fetchObject( $res ) ) {
397 $last = $obj->rev_timestamp
;
399 $db->freeResult( $res );
403 /** @todo document */
404 function getDirection() {
407 if ($wgRequest->getText("dir") == "prev")
413 /** @todo document */
414 function fetchRevisions($limit, $offset, $direction) {
415 global $wgUser, $wgShowUpdatedMarker;
417 /* Check one extra row to see whether we need to show 'next' and diff links */
418 $limitplus = $limit +
1;
420 $namespace = $this->mTitle
->getNamespace();
421 $title = $this->mTitle
->getText();
422 $uid = $wgUser->getID();
423 $db =& wfGetDB( DB_SLAVE
);
425 $use_index = $db->useIndexClause( 'page_timestamp' );
426 $revision = $db->tableName( 'revision' );
428 $limits = $offsets = "";
430 if ($direction == DIR_PREV
)
431 list($dirs, $oper) = array("ASC", ">=");
432 else /* $direction = DIR_NEXT */
433 list($dirs, $oper) = array("DESC", "<=");
436 $offsets .= " AND rev_timestamp $oper '$offset' ";
438 $page_id = $this->mTitle
->getArticleID();
440 $sql = "SELECT rev_id,rev_user," .
441 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
442 "FROM $revision $use_index " .
443 "WHERE rev_page=$page_id " .
445 "ORDER BY rev_timestamp $dirs ";
447 $sql = $db->limitResult($sql, $limitplus, 0);
448 $res = $db->query($sql, "PageHistory::fetchRevisions");
451 while (($obj = $db->fetchObject($res)) != NULL)
457 /** @todo document */
458 function getNotificationTimestamp() {
459 global $wgUser, $wgShowUpdatedMarker;
461 if ($this->mNotificationTimestamp
!== NULL)
462 return $this->mNotificationTimestamp
;
464 if ($wgUser->getID() == 0 ||
!$wgShowUpdatedMarker)
465 return $this->mNotificationTimestamp
= false;
467 $db =& wfGetDB(DB_SLAVE
);
469 $this->mNotificationTimestamp
= $db->selectField(
471 'wl_notificationtimestamp',
472 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
473 'wl_title' => $this->mTitle
->getDBkey(),
474 'wl_user' => $wgUser->getID()
476 "PageHistory::getNotficationTimestamp");
478 return $this->mNotificationTimestamp
;
481 /** @todo document */
482 function makeNavbar($revisions, $offset, $limit, $direction) {
483 global $wgTitle, $wgLang;
485 $revisions = array_slice($revisions, 0, $limit);
487 $pageid = $this->mTitle
->getArticleID();
488 $latestTimestamp = wfTimestamp(TS_MW
, $this->getLatestOffset( $pageid ));
489 $earliestTimestamp = wfTimestamp(TS_MW
, $this->getEarliestOffset( $pageid ));
492 * When we're displaying previous revisions, we need to reverse
493 * the array, because it's queried in reverse order.
495 if ($direction == DIR_PREV
)
496 $revisions = array_reverse($revisions);
499 * lowts is the timestamp of the first revision on this page.
500 * hights is the timestamp of the last revision.
503 $lowts = $hights = 0;
505 if( count( $revisions ) ) {
506 $latestShown = wfTimestamp(TS_MW
, $revisions[0]->rev_timestamp
);
507 $earliestShown = wfTimestamp(TS_MW
, $revisions[count($revisions) - 1]->rev_timestamp
);
510 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
511 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
512 $firsttext = wfMsgHtml('histfirst');
513 $lasttext = wfMsgHtml('histlast');
515 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset=$latestShown&limit={$limit}");
516 $nexturl = $wgTitle->escapeLocalURL("action=history&offset=$earliestShown&limit={$limit}");
519 foreach (array(20, 50, 100, 250, 500) as $num) {
520 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
521 "action=history&offset=" . wfTimestamp(TS_MW
, $offset) .
522 "&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
525 $bits = implode($urls, ' | ');
527 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
528 if( $latestShown < $latestTimestamp ) {
529 $prevtext = "<a href=\"$prevurl\">".wfMsgHtml("prevn", $limit)."</a>";
530 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
532 $prevtext = wfMsgHtml("prevn", $limit);
535 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
536 if( $earliestShown > $earliestTimestamp ) {
537 $nexttext = "<a href=\"$nexturl\">".wfMsgHtml("nextn", $limit)."</a>";
538 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
540 $nexttext = wfMsgHtml("nextn", $limit);
543 $firstlast = "($lasttext | $firsttext)";
545 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);