5 * Split off from Article.php and Skin.php, 2003-12-22
9 include_once ( "SpecialValidate.php" );
11 define("DIR_PREV", 0);
12 define("DIR_NEXT", 1);
15 * This class handles printing the history page for an article. In order to
16 * be efficient, it uses timestamps rather than offsets for paging, to avoid
17 * costly LIMIT,offset queries.
19 * Construct it by passing in an Article, and call $h->history() to print the
26 var $mArticle, $mTitle, $mSkin;
29 var $mNotificationTimestamp;
32 * Construct a new PageHistory.
34 * @param Article $article
37 function PageHistory($article) {
40 $this->mArticle
=& $article;
41 $this->mTitle
=& $article->mTitle
;
42 $this->mNotificationTimestamp
= NULL;
43 $this->mSkin
= $wgUser->getSkin();
47 * Print the history page for an article.
52 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
53 $wgTitle, $wgUseValidation;
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 $id = $this->mTitle
->getArticleID();
80 $wgOut->addWikiText( 'nohistory' );
81 wfProfileOut( $fname );
86 * Extract limit, the number of revisions to show, and
87 * offset, the timestamp to begin at, from the URL.
89 $limit = $wgRequest->getInt('limit', 50);
90 $offset = $wgRequest->getText('offset');
92 /* Offset must be an integral. */
93 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
97 * "go=last" means to jump to the last history page.
99 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
102 if (($lastid = $this->getLastOffsetForPaging($id, $limit)) === NULL)
104 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset={$lastid}");
110 if (!is_null($gourl)) {
111 $wgOut->redirect($gourl);
119 * If the user clicked "previous", we retrieve the revisions backwards,
120 * then reverse them. This is to avoid needing to know the timestamp of
121 * previous revisions when generating the URL.
123 $direction = $this->getDirection();
124 $revisions = $this->fetchRevisions($limit, $offset, $direction);
125 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
128 * We fetch one more revision than needed to get the timestamp of the
129 * one after this page (and to know if it exists).
131 * linesonpage stores the actual number of lines.
133 if (count($revisions) < $limit +
1)
134 $this->linesonpage
= count($revisions);
136 $this->linesonpage
= count($revisions) - 1;
138 /* Un-reverse revisions */
139 if ($direction == DIR_PREV
)
140 $revisions = array_reverse($revisions);
143 * Print the top navbar.
146 $s .= $this->beginHistoryList();
150 * Print each revision, excluding the one-past-the-end, if any.
152 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
153 $first = !$i && $offset == 0;
154 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
155 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $first);
162 $s .= $this->endHistoryList();
166 * Article validation line.
168 if ($wgUseValidation)
169 $s .= "<p>" . Validation
::link2statistics ( $this->mArticle
) . "</p>" ;
171 $wgOut->addHTML( $s );
172 wfProfileOut( $fname );
175 function beginHistoryList() {
177 $this->lastdate
= '';
178 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
179 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
180 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
181 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
182 $s .= $this->submitButton();
183 $s .= '<ul id="pagehistory">';
187 function endHistoryList() {
188 $last = wfMsg( 'last' );
191 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
196 function submitButton( $bits = array() ) {
197 return ( $this->linesonpage
> 0 )
198 ?
wfElement( 'input', array_merge( $bits,
200 'class' => 'historysubmit',
202 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
203 'title' => wfMsg( 'tooltip-compareselectedversions' ),
204 'value' => wfMsg( 'compareselectedversions' ),
209 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
210 global $wgLang, $wgContLang;
213 if( !isset( $message ) ) {
214 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
215 $message[$msg] = wfMsg( $msg );
219 $link = $this->revLink( $row );
221 if ( 0 == $row->rev_user
) {
222 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
223 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
224 htmlspecialchars( $row->rev_user_text
),
225 'target=' . urlencode( $row->rev_user_text
) );
227 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
228 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
232 if( $row->rev_deleted
) {
233 $s .= '<span class="deleted">';
235 $curlink = $this->curLink( $row, $latest );
236 $lastlink = $this->lastLink( $row, $next, $counter );
237 $arbitrary = $this->diffButtons( $row, $latest, $counter );
238 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
240 if( $row->rev_minor_edit
) {
241 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
244 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
245 if ($this->getNotificationTimestamp() && ($row->rev_timestamp
>= $this->getNotificationTimestamp())) {
246 $s .= wfMsg( 'updatedmarker' );
248 if( $row->rev_deleted
) {
249 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
256 function revLink( $row ) {
257 global $wgUser, $wgLang;
258 $date = $wgLang->timeanddate( $row->rev_timestamp
, true );
259 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
262 return $this->mSkin
->makeKnownLinkObj(
265 'oldid='.$row->rev_id
);
269 function curLink( $row, $latest ) {
271 $cur = htmlspecialchars( wfMsg( 'cur' ) );
273 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
276 return $this->mSkin
->makeKnownLinkObj(
279 'diff=' . $this->getLatestID($this->mTitle
->getArticleID())
280 . '&oldid=' . $row->rev_id
);
284 function lastLink( $row, $next, $counter ) {
286 $last = htmlspecialchars( wfMsg( 'last' ) );
288 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
291 return $this->mSkin
->makeKnownLinkObj(
294 "diff={$row->rev_id}&oldid={$next->rev_id}",
297 ' tabindex="'.$counter.'"' );
301 function diffButtons( $row, $latest, $counter ) {
303 if( $this->linesonpage
> 1) {
306 'value' => $row->rev_id
,
307 'title' => wfMsg( 'selectolderversionfordiff' )
309 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
310 $radio['disabled'] = 'disabled';
313 # XXX: move title texts to javascript
315 $first = wfElement( 'input', array_merge(
318 'style' => 'visibility:hidden',
319 'name' => 'oldid' ) ) );
320 $checkmark = array( 'checked' => 'checked' );
322 if( $counter == 2 ) {
323 $checkmark = array( 'checked' => 'checked' );
325 $checkmark = array();
327 $first = wfElement( 'input', array_merge(
330 array( 'name' => 'oldid' ) ) );
331 $checkmark = array();
333 $second = wfElement( 'input', array_merge(
336 array( 'name' => 'diff' ) ) );
337 return $first . $second;
343 function getLatestOffset($id) {
344 return $this->getExtremeOffset( $id, 'max' );
347 function getEarliestOffset($id) {
348 return $this->getExtremeOffset( $id, 'min' );
351 function getExtremeOffset( $id, $func ) {
352 $db =& wfGetDB(DB_SLAVE
);
353 return $db->selectField( 'revision',
354 "$func(rev_timestamp)",
355 array( 'rev_page' => $id ),
356 'PageHistory::getExtremeOffset' );
359 function getLatestID( $id ) {
360 $db =& wfGetDB(DB_SLAVE
);
361 return $db->selectField( 'revision',
363 array( 'rev_page' => $id ),
364 'PageHistory::getLatestID' );
367 function getLastOffsetForPaging( $id, $step = 50 ) {
368 $db =& wfGetDB(DB_SLAVE
);
369 $revision = $db->tableName( 'revision' );
370 $sql = "SELECT rev_timestamp FROM $revision WHERE rev_page = $id " .
371 "ORDER BY rev_timestamp ASC LIMIT $step";
372 $res = $db->query( $sql, "PageHistory::getLastOffsetForPaging" );
373 $n = $db->numRows( $res );
376 while( $obj = $db->fetchObject( $res ) ) {
377 $last = $obj->rev_timestamp
;
379 $db->freeResult( $res );
383 function getDirection() {
386 if ($wgRequest->getText("dir") == "prev")
392 function fetchRevisions($limit, $offset, $direction) {
393 global $wgUser, $wgShowUpdatedMarker;
395 /* Check one extra row to see whether we need to show 'next' and diff links */
396 $limitplus = $limit +
1;
398 $namespace = $this->mTitle
->getNamespace();
399 $title = $this->mTitle
->getText();
400 $uid = $wgUser->getID();
401 $db =& wfGetDB( DB_SLAVE
);
403 $use_index = $db->useIndexClause( 'page_timestamp' );
404 $revision = $db->tableName( 'revision' );
406 $limits = $offsets = "";
408 if ($direction == DIR_PREV
)
409 list($dirs, $oper) = array("ASC", ">=");
410 else /* $direction = DIR_NEXT */
411 list($dirs, $oper) = array("DESC", "<=");
414 $offsets .= " AND rev_timestamp $oper '$offset' ";
417 $limits .= " LIMIT $limitplus ";
418 $page_id = $this->mTitle
->getArticleID();
420 $sql = "SELECT rev_id,rev_user," .
421 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
422 "FROM $revision $use_index " .
423 "WHERE rev_page=$page_id " .
425 "ORDER BY rev_timestamp $dirs " .
427 $res = $db->query($sql, "PageHistory::fetchRevisions");
430 while (($obj = $db->fetchObject($res)) != NULL)
436 function getNotificationTimestamp() {
437 global $wgUser, $wgShowUpdatedMarker;
439 if ($this->mNotificationTimestamp
!== NULL)
440 return $this->mNotificationTimestamp
;
442 if ($wgUser->getID() == 0 ||
!$wgShowUpdatedMarker)
443 return $this->mNotificationTimestamp
= false;
445 $db =& wfGetDB(DB_SLAVE
);
447 $this->mNotificationTimestamp
= $db->selectField(
449 'wl_notificationtimestamp',
450 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
451 'wl_title' => $this->mTitle
->getDBkey(),
452 'wl_user' => $wgUser->getID()
454 "PageHistory::getNotficationTimestamp");
456 return $this->mNotificationTimestamp
;
459 function makeNavbar($revisions, $offset, $limit, $direction) {
460 global $wgTitle, $wgLang;
462 $revisions = array_slice($revisions, 0, $limit);
464 $pageid = $this->mTitle
->getArticleID();
465 $latestTimestamp = $this->getLatestOffset( $pageid );
466 $earliestTimestamp = $this->getEarliestOffset( $pageid );
469 * When we're displaying previous revisions, we need to reverse
470 * the array, because it's queried in reverse order.
472 if ($direction == DIR_PREV
)
473 $revisions = array_reverse($revisions);
476 * lowts is the timestamp of the first revision on this page.
477 * hights is the timestamp of the last revision.
480 $lowts = $hights = 0;
482 if( count( $revisions ) ) {
483 $latestShown = $revisions[0]->rev_timestamp
;
484 $earliestShown = $revisions[count($revisions) - 1]->rev_timestamp
;
487 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
488 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
489 $firsttext = wfMsgHtml("histfirst");
490 $lasttext = wfMsgHtml("histlast");
492 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$latestShown}&limit={$limit}");
493 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$earliestShown}&limit={$limit}");
496 foreach (array(20, 50, 100, 250, 500) as $num) {
497 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
498 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
501 $bits = implode($urls, ' | ');
503 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
504 if( $latestShown < $latestTimestamp ) {
505 $prevtext = "<a href=\"$prevurl\">".wfMsgHtml("prevn", $limit)."</a>";
506 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
508 $prevtext = wfMsgHtml("prevn", $limit);
511 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
512 if( $earliestShown > $earliestTimestamp ) {
513 $nexttext = "<a href=\"$nexturl\">".wfMsgHtml("nextn", $limit)."</a>";
514 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
516 $nexttext = wfMsgHtml("nextn", $limit);
519 $firstlast = "($lasttext | $firsttext)";
521 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);