3329684313aea6852934d1b0d374be8cda618ca1
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->addHTML( wfMsg( '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->getLastOffset($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);
161 $s .= $this->endHistoryList();
165 * Article validation line.
167 if ($wgUseValidation)
168 $s .= "<p>" . Validation
::link2statistics ( $this->mArticle
) . "</p>" ;
170 $wgOut->addHTML( $s );
171 wfProfileOut( $fname );
174 function beginHistoryList() {
176 $this->lastdate
= '';
177 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
178 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
179 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
180 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
181 $s .= $this->submitButton();
182 $s .= '<ul id="pagehistory">';
186 function endHistoryList() {
187 $last = wfMsg( 'last' );
190 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
195 function submitButton( $bits = array() ) {
196 return ( $this->linesonpage
> 0 )
197 ?
wfElement( 'input', array_merge( $bits,
199 'class' => 'historysubmit',
201 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
202 'title' => wfMsg( 'tooltip-compareselectedversions' ),
203 'value' => wfMsg( 'compareselectedversions' ),
208 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
209 global $wgLang, $wgContLang;
212 if( !isset( $message ) ) {
213 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
214 $message[$msg] = wfMsg( $msg );
218 $link = $this->revLink( $row );
220 if ( 0 == $row->rev_user
) {
221 $contribsPage =& Title
::makeTitle( NS_SPECIAL
, 'Contributions' );
222 $ul = $this->mSkin
->makeKnownLinkObj( $contribsPage,
223 htmlspecialchars( $row->rev_user_text
),
224 'target=' . urlencode( $row->rev_user_text
) );
226 $userPage =& Title
::makeTitle( NS_USER
, $row->rev_user_text
);
227 $ul = $this->mSkin
->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text
) );
231 if( $row->rev_deleted
) {
232 $s .= '<span class="deleted">';
234 $curlink = $this->curLink( $row, $latest );
235 $lastlink = $this->lastLink( $row, $next, $counter );
236 $arbitrary = $this->diffButtons( $row, $latest, $counter );
237 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
239 if( $row->rev_minor_edit
) {
240 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
243 $s .= $this->mSkin
->commentBlock( $row->rev_comment
, $this->mTitle
);
244 if ($this->getNotificationTimestamp() && ($row->rev_timestamp
>= $this->getNotificationTimestamp())) {
245 $s .= wfMsg( 'updatedmarker' );
247 if( $row->rev_deleted
) {
248 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
255 function revLink( $row ) {
256 global $wgUser, $wgLang;
257 $date = $wgLang->timeanddate( $row->rev_timestamp
, true );
258 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
261 return $this->mSkin
->makeKnownLinkObj(
264 'oldid='.$row->rev_id
);
268 function curLink( $row, $latest ) {
270 $cur = htmlspecialchars( wfMsg( 'cur' ) );
272 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
275 return $this->mSkin
->makeKnownLinkObj(
278 'diff=0&oldid=' . $row->rev_id
);
282 function lastLink( $row, $next, $counter ) {
284 $last = htmlspecialchars( wfMsg( 'last' ) );
286 ||
( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) ) {
289 return $this->mSkin
->makeKnownLinkObj(
292 "diff={$row->rev_id}&oldid={$next->rev_id}",
295 ' tabindex="'.$counter.'"' );
299 function diffButtons( $row, $latest, $counter ) {
301 if( $this->linesonpage
> 1) {
304 'value' => $row->rev_id
,
305 'title' => wfMsg( 'selectolderversionfordiff' )
307 if( $row->rev_deleted
&& !$wgUser->isAllowed( 'undelete' ) ) {
308 $radio['disabled'] = 'disabled';
311 # XXX: move title texts to javascript
313 $first = wfElement( 'input', array_merge(
316 'style' => 'visibility:hidden',
317 'name' => 'oldid' ) ) );
318 $checkmark = array( 'checked' => 'checked' );
320 if( $counter == 2 ) {
321 $checkmark = array( 'checked' => 'checked' );
323 $checkmark = array();
325 $first = wfElement( 'input', array_merge(
328 array( 'name' => 'oldid' ) ) );
329 $checkmark = array();
331 $second = wfElement( 'input', array_merge(
334 array( 'name' => 'diff' ) ) );
335 return $first . $second;
341 function getLastOffset($id, $step = 50) {
342 $db =& wfGetDB(DB_SLAVE
);
343 $sql = "SELECT rev_timestamp FROM revision WHERE rev_page = $id " .
344 "ORDER BY rev_timestamp ASC LIMIT $step";
345 $res = $db->query($sql, "getLastOffset");
346 $n = $db->numRows($res);
352 $obj = $db->fetchObject($res);
353 return $obj->rev_timestamp
;
356 function getDirection() {
359 if ($wgRequest->getText("dir") == "prev")
365 function fetchRevisions($limit, $offset, $direction) {
366 global $wgUser, $wgShowUpdatedMarker;
368 /* Check one extra row to see whether we need to show 'next' and diff links */
369 $limitplus = $limit +
1;
371 $namespace = $this->mTitle
->getNamespace();
372 $title = $this->mTitle
->getText();
373 $uid = $wgUser->getID();
374 $db =& wfGetDB( DB_SLAVE
);
376 $use_index = $db->useIndexClause( 'page_timestamp' );
377 $revision = $db->tableName( 'revision' );
379 $limits = $offsets = "";
381 if ($direction == DIR_PREV
)
382 list($dirs, $oper) = array("ASC", ">=");
383 else /* $direction = DIR_NEXT */
384 list($dirs, $oper) = array("DESC", "<=");
387 $offsets .= " AND rev_timestamp $oper '$offset' ";
390 $limits .= " LIMIT $limitplus ";
391 $page_id = $this->mTitle
->getArticleID();
393 $sql = "SELECT rev_id,rev_user," .
394 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
395 "FROM $revision $use_index " .
396 "WHERE rev_page=$page_id " .
398 "ORDER BY rev_timestamp $dirs " .
400 $res = $db->query($sql, "PageHistory::fetchRevisions");
403 while (($obj = $db->fetchObject($res)) != NULL)
405 wfdebug("limits=$limits offset=$offsets got=".count($result)."\n");
410 function getNotificationTimestamp() {
411 global $wgUser, $wgShowUpdatedMarker;
413 if ($this->mNotificationTimestamp
!== NULL)
414 return $this->mNotificationTimestamp
;
416 if ($wgUser->getID() == 0 ||
!$wgShowUpdatedMarker)
417 return $this->mNotificationTimestamp
= false;
419 $db =& wfGetDB(DB_SLAVE
);
421 $this->mNotificationTimestamp
= $db->selectField(
423 'wl_notificationtimestamp',
424 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
425 'wl_title' => $this->mTitle
->getDBkey(),
426 'wl_user' => $wgUser->getID()
428 "PageHistory::getNotficationTimestamp");
430 return $this->mNotificationTimestamp
;
433 function makeNavbar($revisions, $offset, $limit, $direction) {
434 global $wgTitle, $wgLang;
437 * lowts is the timestamp of the first revision on this page.
438 * hights is the timestamp of the last revision.
440 $atend = !(count($revisions) > $limit);
441 wfDebug("offset=$offset limit=$limit dir=$direction count=".count($revisions)."\n");
442 $revisions = array_slice($revisions, 0, $limit);
445 * When we're displaying previous revisions, we need to reverse
446 * the array, because it's queried in reverse order.
448 if ($direction == DIR_PREV
)
449 $revisions = array_reverse($revisions);
451 $lowts = $hights = 0;
453 if (count($revisions)) {
454 $lowts = $revisions[0]->rev_timestamp
;
455 $hights = $revisions[count($revisions) - 1]->rev_timestamp
;
458 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
459 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
460 $firsttext = wfMsg("histfirst");
461 $lasttext = wfMsg("histlast");
463 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$lowts}&limit={$limit}");
464 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$hights}&limit={$limit}");
467 foreach (array(20, 50, 100, 250, 500) as $num) {
468 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
469 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
471 $bits = implode($urls, ' | ');
472 if (($direction == DIR_NEXT
&& $offset) ||
($direction == DIR_PREV
&& !$atend)) {
473 $prevtext = "<a href=\"$prevurl\">".wfMsg("prevn", $limit)."</a>";
474 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
475 } else $prevtext = wfMsg("prevn", $limit);
477 if (!$atend ||
$direction == DIR_PREV
) {
478 $nexttext = "<a href=\"$nexturl\">".wfMsg("nextn", $limit)."</a>";
479 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
480 } else $nexttext = wfMsg("nextn", $limit);
482 $firstlast = "($firsttext | $lasttext)";
484 return "$firstlast " . wfMsg("viewprevnext", $prevtext, $nexttext, $bits);