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 );
68 * Setup page variables.
70 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
71 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
72 $wgOut->setArticleFlag( false );
73 $wgOut->setArticleRelated( true );
74 $wgOut->setRobotpolicy( 'noindex,nofollow' );
77 * Fail if article doesn't exist.
79 $id = $this->mTitle
->getArticleID();
81 $wgOut->addWikiText( 'nohistory' );
82 wfProfileOut( $fname );
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', 50);
91 $offset = $wgRequest->getText('offset');
93 /* Offset must be an integral. */
94 if (!strlen($offset) ||
!preg_match("/^[0-9]+$/", $offset))
98 * "go=last" means to jump to the last history page.
100 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
103 if (($lastid = $this->getLastOffsetForPaging($id, $limit)) === NULL)
105 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset={$lastid}");
111 if (!is_null($gourl)) {
112 $wgOut->redirect($gourl);
120 * If the user clicked "previous", we retrieve the revisions backwards,
121 * then reverse them. This is to avoid needing to know the timestamp of
122 * previous revisions when generating the URL.
124 $direction = $this->getDirection();
125 $revisions = $this->fetchRevisions($limit, $offset, $direction);
126 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
129 * We fetch one more revision than needed to get the timestamp of the
130 * one after this page (and to know if it exists).
132 * linesonpage stores the actual number of lines.
134 if (count($revisions) < $limit +
1)
135 $this->linesonpage
= count($revisions);
137 $this->linesonpage
= count($revisions) - 1;
139 /* Un-reverse revisions */
140 if ($direction == DIR_PREV
)
141 $revisions = array_reverse($revisions);
144 * Print the top navbar.
147 $s .= $this->beginHistoryList();
151 * Print each revision, excluding the one-past-the-end, if any.
153 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
154 $first = !$i && $offset == 0;
155 $next = isset( $revisions[$i +
1] ) ?
$revisions[$i +
1 ] : null;
156 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $first);
163 $s .= $this->endHistoryList();
167 * Article validation line.
169 if ($wgUseValidation)
170 $s .= '<p>' . Validation
::link2statistics ( $this->mArticle
) . '</p>' ;
172 $wgOut->addHTML( $s );
173 wfProfileOut( $fname );
176 /** @todo document */
177 function beginHistoryList() {
179 $this->lastdate
= '';
180 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
181 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
182 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
183 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
184 $s .= $this->submitButton();
185 $s .= '<ul id="pagehistory">';
189 /** @todo document */
190 function endHistoryList() {
191 $last = wfMsg( 'last' );
194 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
199 /** @todo document */
200 function submitButton( $bits = array() ) {
201 return ( $this->linesonpage
> 0 )
202 ?
wfElement( 'input', array_merge( $bits,
204 'class' => 'historysubmit',
206 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
207 'title' => wfMsg( 'tooltip-compareselectedversions' ),
208 'value' => wfMsg( 'compareselectedversions' ),
213 /** @todo document */
214 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
215 global $wgLang, $wgContLang;
218 if( !isset( $message ) ) {
219 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
220 $message[$msg] = wfMsg( $msg );
224 $link = $this->revLink( $row );
226 if ( 0 == $row->rev_user
) {
227 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
228 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
229 htmlspecialchars( $row->rev_user_text
),
230 'target=' . urlencode( $row->rev_user_text
) );
232 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
233 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
237 if( $row->rev_deleted
) {
238 $s .= '<span class="deleted">';
240 $curlink = $this->curLink( $row, $latest );
241 $lastlink = $this->lastLink( $row, $next, $counter );
242 $arbitrary = $this->diffButtons( $row, $latest, $counter );
243 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
245 if( $row->rev_minor_edit
) {
246 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
249 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
250 if ($this->getNotificationTimestamp() && ($row->rev_timestamp
>= $this->getNotificationTimestamp())) {
251 $s .= wfMsg( 'updatedmarker' );
253 if( $row->rev_deleted
) {
254 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
261 /** @todo document */
262 function revLink( $row ) {
263 global $wgUser, $wgLang;
264 $date = $wgLang->timeanddate( $row->rev_timestamp
, true );
265 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
268 return $this->mSkin
->makeKnownLinkObj(
271 'oldid='.$row->rev_id
);
275 /** @todo document */
276 function curLink( $row, $latest ) {
278 $cur = htmlspecialchars( wfMsg( 'cur' ) );
280 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
283 return $this->mSkin
->makeKnownLinkObj(
286 'diff=' . $this->getLatestID($this->mTitle
->getArticleID())
287 . '&oldid=' . $row->rev_id
);
291 /** @todo document */
292 function lastLink( $row, $next, $counter ) {
294 $last = htmlspecialchars( wfMsg( 'last' ) );
296 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
299 return $this->mSkin
->makeKnownLinkObj(
302 "diff={$row->rev_id}&oldid={$next->rev_id}",
305 ' tabindex="'.$counter.'"' );
309 /** @todo document */
310 function diffButtons( $row, $latest, $counter ) {
312 if( $this->linesonpage
> 1) {
315 'value' => $row->rev_id
,
316 'title' => wfMsg( 'selectolderversionfordiff' )
318 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
319 $radio['disabled'] = 'disabled';
322 # XXX: move title texts to javascript
324 $first = wfElement( 'input', array_merge(
327 'style' => 'visibility:hidden',
328 'name' => 'oldid' ) ) );
329 $checkmark = array( 'checked' => 'checked' );
331 if( $counter == 2 ) {
332 $checkmark = array( 'checked' => 'checked' );
334 $checkmark = array();
336 $first = wfElement( 'input', array_merge(
339 array( 'name' => 'oldid' ) ) );
340 $checkmark = array();
342 $second = wfElement( 'input', array_merge(
345 array( 'name' => 'diff' ) ) );
346 return $first . $second;
352 /** @todo document */
353 function getLatestOffset($id) {
354 return $this->getExtremeOffset( $id, 'max' );
357 /** @todo document */
358 function getEarliestOffset($id) {
359 return $this->getExtremeOffset( $id, 'min' );
362 /** @todo document */
363 function getExtremeOffset( $id, $func ) {
364 $db =& wfGetDB(DB_SLAVE
);
365 return $db->selectField( 'revision',
366 "$func(rev_timestamp)",
367 array( 'rev_page' => $id ),
368 'PageHistory::getExtremeOffset' );
371 /** @todo document */
372 function getLatestID( $id ) {
373 $db =& wfGetDB(DB_SLAVE
);
374 return $db->selectField( 'revision',
376 array( 'rev_page' => $id ),
377 'PageHistory::getLatestID' );
380 /** @todo document */
381 function getLastOffsetForPaging( $id, $step = 50 ) {
382 $db =& wfGetDB(DB_SLAVE
);
383 $revision = $db->tableName( 'revision' );
384 $sql = "SELECT rev_timestamp FROM $revision WHERE rev_page = $id " .
385 "ORDER BY rev_timestamp ASC LIMIT $step";
386 $res = $db->query( $sql, "PageHistory::getLastOffsetForPaging" );
387 $n = $db->numRows( $res );
390 while( $obj = $db->fetchObject( $res ) ) {
391 $last = $obj->rev_timestamp
;
393 $db->freeResult( $res );
397 /** @todo document */
398 function getDirection() {
401 if ($wgRequest->getText("dir") == "prev")
407 /** @todo document */
408 function fetchRevisions($limit, $offset, $direction) {
409 global $wgUser, $wgShowUpdatedMarker;
411 /* Check one extra row to see whether we need to show 'next' and diff links */
412 $limitplus = $limit +
1;
414 $namespace = $this->mTitle
->getNamespace();
415 $title = $this->mTitle
->getText();
416 $uid = $wgUser->getID();
417 $db =& wfGetDB( DB_SLAVE
);
419 $use_index = $db->useIndexClause( 'page_timestamp' );
420 $revision = $db->tableName( 'revision' );
422 $limits = $offsets = "";
424 if ($direction == DIR_PREV
)
425 list($dirs, $oper) = array("ASC", ">=");
426 else /* $direction = DIR_NEXT */
427 list($dirs, $oper) = array("DESC", "<=");
430 $offsets .= " AND rev_timestamp $oper '$offset' ";
433 $limits .= " LIMIT $limitplus ";
434 $page_id = $this->mTitle
->getArticleID();
436 $sql = "SELECT rev_id,rev_user," .
437 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
438 "FROM $revision $use_index " .
439 "WHERE rev_page=$page_id " .
441 "ORDER BY rev_timestamp $dirs " .
443 $res = $db->query($sql, "PageHistory::fetchRevisions");
446 while (($obj = $db->fetchObject($res)) != NULL)
452 /** @todo document */
453 function getNotificationTimestamp() {
454 global $wgUser, $wgShowUpdatedMarker;
456 if ($this->mNotificationTimestamp
!== NULL)
457 return $this->mNotificationTimestamp
;
459 if ($wgUser->getID() == 0 ||
!$wgShowUpdatedMarker)
460 return $this->mNotificationTimestamp
= false;
462 $db =& wfGetDB(DB_SLAVE
);
464 $this->mNotificationTimestamp
= $db->selectField(
466 'wl_notificationtimestamp',
467 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
468 'wl_title' => $this->mTitle
->getDBkey(),
469 'wl_user' => $wgUser->getID()
471 "PageHistory::getNotficationTimestamp");
473 return $this->mNotificationTimestamp
;
476 /** @todo document */
477 function makeNavbar($revisions, $offset, $limit, $direction) {
478 global $wgTitle, $wgLang;
480 $revisions = array_slice($revisions, 0, $limit);
482 $pageid = $this->mTitle
->getArticleID();
483 $latestTimestamp = $this->getLatestOffset( $pageid );
484 $earliestTimestamp = $this->getEarliestOffset( $pageid );
487 * When we're displaying previous revisions, we need to reverse
488 * the array, because it's queried in reverse order.
490 if ($direction == DIR_PREV
)
491 $revisions = array_reverse($revisions);
494 * lowts is the timestamp of the first revision on this page.
495 * hights is the timestamp of the last revision.
498 $lowts = $hights = 0;
500 if( count( $revisions ) ) {
501 $latestShown = $revisions[0]->rev_timestamp
;
502 $earliestShown = $revisions[count($revisions) - 1]->rev_timestamp
;
505 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
506 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
507 $firsttext = wfMsgHtml('histfirst');
508 $lasttext = wfMsgHtml('histlast');
510 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$latestShown}&limit={$limit}");
511 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$earliestShown}&limit={$limit}");
514 foreach (array(20, 50, 100, 250, 500) as $num) {
515 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
516 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
519 $bits = implode($urls, ' | ');
521 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
522 if( $latestShown < $latestTimestamp ) {
523 $prevtext = "<a href=\"$prevurl\">".wfMsgHtml("prevn", $limit)."</a>";
524 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
526 $prevtext = wfMsgHtml("prevn", $limit);
529 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
530 if( $earliestShown > $earliestTimestamp ) {
531 $nexttext = "<a href=\"$nexturl\">".wfMsgHtml("nextn", $limit)."</a>";
532 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
534 $nexttext = wfMsgHtml("nextn", $limit);
537 $firstlast = "($lasttext | $firsttext)";
539 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);