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