* (bug 2627) Fix regression: diff radio button initial selection
[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=0&oldid=' . $row->rev_id );
280 }
281 }
282
283 function lastLink( $row, $next, $counter ) {
284 global $wgUser;
285 $last = htmlspecialchars( wfMsg( 'last' ) );
286 if( is_null( $next )
287 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
288 return $last;
289 } else {
290 return $this->mSkin->makeKnownLinkObj(
291 $this->mTitle,
292 $last,
293 "diff={$row->rev_id}&oldid={$next->rev_id}",
294 '',
295 '',
296 ' tabindex="'.$counter.'"' );
297 }
298 }
299
300 function diffButtons( $row, $latest, $counter ) {
301 global $wgUser;
302 if( $this->linesonpage > 1) {
303 $radio = array(
304 'type' => 'radio',
305 'value' => $row->rev_id,
306 'title' => wfMsg( 'selectolderversionfordiff' )
307 );
308 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
309 $radio['disabled'] = 'disabled';
310 }
311
312 # XXX: move title texts to javascript
313 if ( $latest ) {
314 $first = wfElement( 'input', array_merge(
315 $radio,
316 array(
317 'style' => 'visibility:hidden',
318 'name' => 'oldid' ) ) );
319 $checkmark = array( 'checked' => 'checked' );
320 } else {
321 if( $counter == 2 ) {
322 $checkmark = array( 'checked' => 'checked' );
323 } else {
324 $checkmark = array();
325 }
326 $first = wfElement( 'input', array_merge(
327 $radio,
328 $checkmark,
329 array( 'name' => 'oldid' ) ) );
330 $checkmark = array();
331 }
332 $second = wfElement( 'input', array_merge(
333 $radio,
334 $checkmark,
335 array( 'name' => 'diff' ) ) );
336 return $first . $second;
337 } else {
338 return '';
339 }
340 }
341
342 function getLatestOffset($id) {
343 return $this->getExtremeOffset( $id, 'max' );
344 }
345
346 function getEarliestOffset($id) {
347 return $this->getExtremeOffset( $id, 'min' );
348 }
349
350 function getExtremeOffset( $id, $func ) {
351 $db =& wfGetDB(DB_SLAVE);
352 return $db->selectField( 'revision',
353 "$func(rev_timestamp)",
354 array( 'rev_page' => $id ),
355 'PageHistory::getExtremeOffset' );
356 }
357
358 function getLastOffsetForPaging( $id, $step = 50 ) {
359 $db =& wfGetDB(DB_SLAVE);
360 $revision = $db->tableName( 'revision' );
361 $sql = "SELECT rev_timestamp FROM $revision WHERE rev_page = $id " .
362 "ORDER BY rev_timestamp ASC LIMIT $step";
363 $res = $db->query( $sql, "PageHistory::getLastOffsetForPaging" );
364 $n = $db->numRows( $res );
365
366 $last = null;
367 while( $obj = $db->fetchObject( $res ) ) {
368 $last = $obj->rev_timestamp;
369 }
370 $db->freeResult( $res );
371 return $last;
372 }
373
374 function getDirection() {
375 global $wgRequest;
376
377 if ($wgRequest->getText("dir") == "prev")
378 return DIR_PREV;
379 else
380 return DIR_NEXT;
381 }
382
383 function fetchRevisions($limit, $offset, $direction) {
384 global $wgUser, $wgShowUpdatedMarker;
385
386 /* Check one extra row to see whether we need to show 'next' and diff links */
387 $limitplus = $limit + 1;
388
389 $namespace = $this->mTitle->getNamespace();
390 $title = $this->mTitle->getText();
391 $uid = $wgUser->getID();
392 $db =& wfGetDB( DB_SLAVE );
393
394 $use_index = $db->useIndexClause( 'page_timestamp' );
395 $revision = $db->tableName( 'revision' );
396
397 $limits = $offsets = "";
398
399 if ($direction == DIR_PREV)
400 list($dirs, $oper) = array("ASC", ">=");
401 else /* $direction = DIR_NEXT */
402 list($dirs, $oper) = array("DESC", "<=");
403
404 if ($offset)
405 $offsets .= " AND rev_timestamp $oper '$offset' ";
406
407 if ($limit)
408 $limits .= " LIMIT $limitplus ";
409 $page_id = $this->mTitle->getArticleID();
410
411 $sql = "SELECT rev_id,rev_user," .
412 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
413 "FROM $revision $use_index " .
414 "WHERE rev_page=$page_id " .
415 $offsets .
416 "ORDER BY rev_timestamp $dirs " .
417 $limits;
418 $res = $db->query($sql, "PageHistory::fetchRevisions");
419
420 $result = array();
421 while (($obj = $db->fetchObject($res)) != NULL)
422 $result[] = $obj;
423 wfdebug("limits=$limits offset=$offsets got=".count($result)."\n");
424
425 return $result;
426 }
427
428 function getNotificationTimestamp() {
429 global $wgUser, $wgShowUpdatedMarker;
430
431 if ($this->mNotificationTimestamp !== NULL)
432 return $this->mNotificationTimestamp;
433
434 if ($wgUser->getID() == 0 || !$wgShowUpdatedMarker)
435 return $this->mNotificationTimestamp = false;
436
437 $db =& wfGetDB(DB_SLAVE);
438
439 $this->mNotificationTimestamp = $db->selectField(
440 'watchlist',
441 'wl_notificationtimestamp',
442 array( 'wl_namespace' => $this->mTitle->getNamespace(),
443 'wl_title' => $this->mTitle->getDBkey(),
444 'wl_user' => $wgUser->getID()
445 ),
446 "PageHistory::getNotficationTimestamp");
447
448 return $this->mNotificationTimestamp;
449 }
450
451 function makeNavbar($revisions, $offset, $limit, $direction) {
452 global $wgTitle, $wgLang;
453
454 $revisions = array_slice($revisions, 0, $limit);
455
456 $pageid = $this->mTitle->getArticleID();
457 $latestTimestamp = $this->getLatestOffset( $pageid );
458 $earliestTimestamp = $this->getEarliestOffset( $pageid );
459
460 /*
461 * When we're displaying previous revisions, we need to reverse
462 * the array, because it's queried in reverse order.
463 */
464 if ($direction == DIR_PREV)
465 $revisions = array_reverse($revisions);
466
467 /*
468 * lowts is the timestamp of the first revision on this page.
469 * hights is the timestamp of the last revision.
470 */
471
472 $lowts = $hights = 0;
473
474 if( count( $revisions ) ) {
475 $latestShown = $revisions[0]->rev_timestamp;
476 $earliestShown = $revisions[count($revisions) - 1]->rev_timestamp;
477 }
478
479 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
480 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
481 $firsttext = wfMsgHtml("histfirst");
482 $lasttext = wfMsgHtml("histlast");
483
484 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset={$latestShown}&limit={$limit}");
485 $nexturl = $wgTitle->escapeLocalURL("action=history&offset={$earliestShown}&limit={$limit}");
486
487 $urls = array();
488 foreach (array(20, 50, 100, 250, 500) as $num) {
489 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
490 "action=history&offset={$offset}&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
491 }
492
493 $bits = implode($urls, ' | ');
494
495 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
496 if( $latestShown < $latestTimestamp ) {
497 $prevtext = "<a href=\"$prevurl\">".wfMsgHtml("prevn", $limit)."</a>";
498 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
499 } else {
500 $prevtext = wfMsgHtml("prevn", $limit);
501 }
502
503 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
504 if( $earliestShown > $earliestTimestamp ) {
505 $nexttext = "<a href=\"$nexturl\">".wfMsgHtml("nextn", $limit)."</a>";
506 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
507 } else {
508 $nexttext = wfMsgHtml("nextn", $limit);
509 }
510
511 $firstlast = "($lasttext | $firsttext)";
512
513 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
514 }
515 }
516
517 ?>