* (bug 2644) "cur" diff links in page history should specify current ID
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @package MediaWiki
7 */
8
9 include_once ( "SpecialValidate.php" );
10
11 define("DIR_PREV", 0);
12 define("DIR_NEXT", 1);
13
14 /**
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.
18 *
19 * Construct it by passing in an Article, and call $h->history() to print the
20 * history.
21 *
22 * @package MediaWiki
23 */
24
25 class PageHistory {
26 var $mArticle, $mTitle, $mSkin;
27 var $lastdate;
28 var $linesonpage;
29 var $mNotificationTimestamp;
30
31 /**
32 * Construct a new PageHistory.
33 *
34 * @param Article $article
35 * @returns nothing
36 */
37 function PageHistory($article) {
38 global $wgUser;
39
40 $this->mArticle =& $article;
41 $this->mTitle =& $article->mTitle;
42 $this->mNotificationTimestamp = NULL;
43 $this->mSkin = $wgUser->getSkin();
44 }
45
46 /**
47 * Print the history page for an article.
48 *
49 * @returns nothing
50 */
51 function history() {
52 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
53 $wgTitle, $wgUseValidation;
54
55 /*
56 * Allow client caching.
57 */
58
59 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
60 /* Client cache fresh and headers sent, nothing more to do. */
61 return;
62
63 $fname = 'PageHistory::history';
64 wfProfileIn( $fname );
65
66 /*
67 * Setup page variables.
68 */
69 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
70 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
71 $wgOut->setArticleFlag( false );
72 $wgOut->setArticleRelated( true );
73 $wgOut->setRobotpolicy( 'noindex,nofollow' );
74
75 /*
76 * Fail if article doesn't exist.
77 */
78 $id = $this->mTitle->getArticleID();
79 if( $id == 0 ) {
80 $wgOut->addWikiText( 'nohistory' );
81 wfProfileOut( $fname );
82 return;
83 }
84
85 /*
86 * Extract limit, the number of revisions to show, and
87 * offset, the timestamp to begin at, from the URL.
88 */
89 $limit = $wgRequest->getInt('limit', 50);
90 $offset = $wgRequest->getText('offset');
91
92 /* Offset must be an integral. */
93 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
94 $offset = 0;
95
96 /*
97 * "go=last" means to jump to the last history page.
98 */
99 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
100 switch ($gowhere) {
101 case "first":
102 if (($lastid = $this->getLastOffsetForPaging($id, $limit)) === NULL)
103 break;
104 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset={$lastid}");
105 break;
106 default:
107 $gourl = NULL;
108 }
109
110 if (!is_null($gourl)) {
111 $wgOut->redirect($gourl);
112 return;
113 }
114 }
115
116 /*
117 * Fetch revisions.
118 *
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.
122 */
123 $direction = $this->getDirection();
124 $revisions = $this->fetchRevisions($limit, $offset, $direction);
125 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
126
127 /*
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).
130 *
131 * linesonpage stores the actual number of lines.
132 */
133 if (count($revisions) < $limit + 1)
134 $this->linesonpage = count($revisions);
135 else
136 $this->linesonpage = count($revisions) - 1;
137
138 /* Un-reverse revisions */
139 if ($direction == DIR_PREV)
140 $revisions = array_reverse($revisions);
141
142 /*
143 * Print the top navbar.
144 */
145 $s = $navbar;
146 $s .= $this->beginHistoryList();
147 $counter = 1;
148
149 /*
150 * Print each revision, excluding the one-past-the-end, if any.
151 */
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);
156 $counter++;
157 }
158
159 /*
160 * End navbar.
161 */
162 $s .= $this->endHistoryList();
163 $s .= $navbar;
164
165 /*
166 * Article validation line.
167 */
168 if ($wgUseValidation)
169 $s .= "<p>" . Validation::link2statistics ( $this->mArticle ) . "</p>" ;
170
171 $wgOut->addHTML( $s );
172 wfProfileOut( $fname );
173 }
174
175 function beginHistoryList() {
176 global $wgTitle;
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">';
184 return $s;
185 }
186
187 function endHistoryList() {
188 $last = wfMsg( 'last' );
189
190 $s = '</ul>';
191 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
192 $s .= '</form>';
193 return $s;
194 }
195
196 function submitButton( $bits = array() ) {
197 return ( $this->linesonpage > 0 )
198 ? wfElement( 'input', array_merge( $bits,
199 array(
200 'class' => 'historysubmit',
201 'type' => 'submit',
202 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
203 'title' => wfMsg( 'tooltip-compareselectedversions' ),
204 'value' => wfMsg( 'compareselectedversions' ),
205 ) ) )
206 : '';
207 }
208
209 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
210 global $wgLang, $wgContLang;
211
212 static $message;
213 if( !isset( $message ) ) {
214 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
215 $message[$msg] = wfMsg( $msg );
216 }
217 }
218
219 $link = $this->revLink( $row );
220
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 ) );
226 } else {
227 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
228 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
229 }
230
231 $s = '<li>';
232 if( $row->rev_deleted ) {
233 $s .= '<span class="deleted">';
234 }
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>";
239
240 if( $row->rev_minor_edit ) {
241 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
242 }
243
244 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
245 if ($this->getNotificationTimestamp() && ($row->rev_timestamp >= $this->getNotificationTimestamp())) {
246 $s .= wfMsg( 'updatedmarker' );
247 }
248 if( $row->rev_deleted ) {
249 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
250 }
251 $s .= '</li>';
252
253 return $s;
254 }
255
256 function revLink( $row ) {
257 global $wgUser, $wgLang;
258 $date = $wgLang->timeanddate( $row->rev_timestamp, true );
259 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
260 return $date;
261 } else {
262 return $this->mSkin->makeKnownLinkObj(
263 $this->mTitle,
264 $date,
265 'oldid='.$row->rev_id );
266 }
267 }
268
269 function curLink( $row, $latest ) {
270 global $wgUser;
271 $cur = htmlspecialchars( wfMsg( 'cur' ) );
272 if( $latest
273 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
274 return $cur;
275 } else {
276 return $this->mSkin->makeKnownLinkObj(
277 $this->mTitle,
278 $cur,
279 'diff=' . $this->getLatestID($this->mTitle->getArticleID())
280 . '&oldid=' . $row->rev_id );
281 }
282 }
283
284 function lastLink( $row, $next, $counter ) {
285 global $wgUser;
286 $last = htmlspecialchars( wfMsg( 'last' ) );
287 if( is_null( $next )
288 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
289 return $last;
290 } else {
291 return $this->mSkin->makeKnownLinkObj(
292 $this->mTitle,
293 $last,
294 "diff={$row->rev_id}&oldid={$next->rev_id}",
295 '',
296 '',
297 ' tabindex="'.$counter.'"' );
298 }
299 }
300
301 function diffButtons( $row, $latest, $counter ) {
302 global $wgUser;
303 if( $this->linesonpage > 1) {
304 $radio = array(
305 'type' => 'radio',
306 'value' => $row->rev_id,
307 'title' => wfMsg( 'selectolderversionfordiff' )
308 );
309 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
310 $radio['disabled'] = 'disabled';
311 }
312
313 # XXX: move title texts to javascript
314 if ( $latest ) {
315 $first = wfElement( 'input', array_merge(
316 $radio,
317 array(
318 'style' => 'visibility:hidden',
319 'name' => 'oldid' ) ) );
320 $checkmark = array( 'checked' => 'checked' );
321 } else {
322 if( $counter == 2 ) {
323 $checkmark = array( 'checked' => 'checked' );
324 } else {
325 $checkmark = array();
326 }
327 $first = wfElement( 'input', array_merge(
328 $radio,
329 $checkmark,
330 array( 'name' => 'oldid' ) ) );
331 $checkmark = array();
332 }
333 $second = wfElement( 'input', array_merge(
334 $radio,
335 $checkmark,
336 array( 'name' => 'diff' ) ) );
337 return $first . $second;
338 } else {
339 return '';
340 }
341 }
342
343 function getLatestOffset($id) {
344 return $this->getExtremeOffset( $id, 'max' );
345 }
346
347 function getEarliestOffset($id) {
348 return $this->getExtremeOffset( $id, 'min' );
349 }
350
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' );
357 }
358
359 function getLatestID( $id ) {
360 $db =& wfGetDB(DB_SLAVE);
361 return $db->selectField( 'revision',
362 "max(rev_id)",
363 array( 'rev_page' => $id ),
364 'PageHistory::getLatestID' );
365 }
366
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 );
374
375 $last = null;
376 while( $obj = $db->fetchObject( $res ) ) {
377 $last = $obj->rev_timestamp;
378 }
379 $db->freeResult( $res );
380 return $last;
381 }
382
383 function getDirection() {
384 global $wgRequest;
385
386 if ($wgRequest->getText("dir") == "prev")
387 return DIR_PREV;
388 else
389 return DIR_NEXT;
390 }
391
392 function fetchRevisions($limit, $offset, $direction) {
393 global $wgUser, $wgShowUpdatedMarker;
394
395 /* Check one extra row to see whether we need to show 'next' and diff links */
396 $limitplus = $limit + 1;
397
398 $namespace = $this->mTitle->getNamespace();
399 $title = $this->mTitle->getText();
400 $uid = $wgUser->getID();
401 $db =& wfGetDB( DB_SLAVE );
402
403 $use_index = $db->useIndexClause( 'page_timestamp' );
404 $revision = $db->tableName( 'revision' );
405
406 $limits = $offsets = "";
407
408 if ($direction == DIR_PREV)
409 list($dirs, $oper) = array("ASC", ">=");
410 else /* $direction = DIR_NEXT */
411 list($dirs, $oper) = array("DESC", "<=");
412
413 if ($offset)
414 $offsets .= " AND rev_timestamp $oper '$offset' ";
415
416 if ($limit)
417 $limits .= " LIMIT $limitplus ";
418 $page_id = $this->mTitle->getArticleID();
419
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 " .
424 $offsets .
425 "ORDER BY rev_timestamp $dirs " .
426 $limits;
427 $res = $db->query($sql, "PageHistory::fetchRevisions");
428
429 $result = array();
430 while (($obj = $db->fetchObject($res)) != NULL)
431 $result[] = $obj;
432
433 return $result;
434 }
435
436 function getNotificationTimestamp() {
437 global $wgUser, $wgShowUpdatedMarker;
438
439 if ($this->mNotificationTimestamp !== NULL)
440 return $this->mNotificationTimestamp;
441
442 if ($wgUser->getID() == 0 || !$wgShowUpdatedMarker)
443 return $this->mNotificationTimestamp = false;
444
445 $db =& wfGetDB(DB_SLAVE);
446
447 $this->mNotificationTimestamp = $db->selectField(
448 'watchlist',
449 'wl_notificationtimestamp',
450 array( 'wl_namespace' => $this->mTitle->getNamespace(),
451 'wl_title' => $this->mTitle->getDBkey(),
452 'wl_user' => $wgUser->getID()
453 ),
454 "PageHistory::getNotficationTimestamp");
455
456 return $this->mNotificationTimestamp;
457 }
458
459 function makeNavbar($revisions, $offset, $limit, $direction) {
460 global $wgTitle, $wgLang;
461
462 $revisions = array_slice($revisions, 0, $limit);
463
464 $pageid = $this->mTitle->getArticleID();
465 $latestTimestamp = $this->getLatestOffset( $pageid );
466 $earliestTimestamp = $this->getEarliestOffset( $pageid );
467
468 /*
469 * When we're displaying previous revisions, we need to reverse
470 * the array, because it's queried in reverse order.
471 */
472 if ($direction == DIR_PREV)
473 $revisions = array_reverse($revisions);
474
475 /*
476 * lowts is the timestamp of the first revision on this page.
477 * hights is the timestamp of the last revision.
478 */
479
480 $lowts = $hights = 0;
481
482 if( count( $revisions ) ) {
483 $latestShown = $revisions[0]->rev_timestamp;
484 $earliestShown = $revisions[count($revisions) - 1]->rev_timestamp;
485 }
486
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");
491
492 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$latestShown}&limit={$limit}");
493 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$earliestShown}&limit={$limit}");
494
495 $urls = array();
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>";
499 }
500
501 $bits = implode($urls, ' | ');
502
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>";
507 } else {
508 $prevtext = wfMsgHtml("prevn", $limit);
509 }
510
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>";
515 } else {
516 $nexttext = wfMsgHtml("nextn", $limit);
517 }
518
519 $firstlast = "($lasttext | $firsttext)";
520
521 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
522 }
523 }
524
525 ?>