removed some debugging statements, I think they were all added by kate in the oracle...
[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 $this->defaultLimit = 50;
47 }
48
49 /**
50 * Print the history page for an article.
51 *
52 * @returns nothing
53 */
54 function history() {
55 global $wgUser, $wgOut, $wgLang, $wgShowUpdatedMarker, $wgRequest,
56 $wgTitle, $wgUseValidation;
57
58 /*
59 * Allow client caching.
60 */
61
62 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
63 /* Client cache fresh and headers sent, nothing more to do. */
64 return;
65
66 $fname = 'PageHistory::history';
67 wfProfileIn( $fname );
68
69 /*
70 * Setup page variables.
71 */
72 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
73 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
74 $wgOut->setArticleFlag( false );
75 $wgOut->setArticleRelated( true );
76 $wgOut->setRobotpolicy( 'noindex,nofollow' );
77
78 /*
79 * Fail if article doesn't exist.
80 */
81 if( !$this->mTitle->exists() ) {
82 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
83 wfProfileOut( $fname );
84 return;
85 }
86
87 $dbr =& wfGetDB(DB_SLAVE);
88
89 /*
90 * Extract limit, the number of revisions to show, and
91 * offset, the timestamp to begin at, from the URL.
92 */
93 $limit = $wgRequest->getInt('limit', $this->defaultLimit);
94 $offset = $wgRequest->getText('offset');
95
96 /* Offset must be an integral. */
97 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
98 $offset = 0;
99 # $offset = $dbr->timestamp($offset);
100 $dboffset = $offset === 0 ? 0 : $dbr->timestamp($offset);
101 /*
102 * "go=last" means to jump to the last history page.
103 */
104 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
105 switch ($gowhere) {
106 case "first":
107 if (($lastid = $this->getLastOffsetForPaging($this->mTitle->getArticleID(), $limit)) === NULL)
108 break;
109 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
110 wfTimestamp(TS_MW, $lastid));
111 break;
112 default:
113 $gourl = NULL;
114 }
115
116 if (!is_null($gourl)) {
117 $wgOut->redirect($gourl);
118 return;
119 }
120 }
121
122 /*
123 * Fetch revisions.
124 *
125 * If the user clicked "previous", we retrieve the revisions backwards,
126 * then reverse them. This is to avoid needing to know the timestamp of
127 * previous revisions when generating the URL.
128 */
129 $direction = $this->getDirection();
130 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
131 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
132
133 /*
134 * We fetch one more revision than needed to get the timestamp of the
135 * one after this page (and to know if it exists).
136 *
137 * linesonpage stores the actual number of lines.
138 */
139 if (count($revisions) < $limit + 1)
140 $this->linesonpage = count($revisions);
141 else
142 $this->linesonpage = count($revisions) - 1;
143
144 /* Un-reverse revisions */
145 if ($direction == DIR_PREV)
146 $revisions = array_reverse($revisions);
147
148 /*
149 * Print the top navbar.
150 */
151 $s = $navbar;
152 $s .= $this->beginHistoryList();
153 $counter = 1;
154
155 /*
156 * Print each revision, excluding the one-past-the-end, if any.
157 */
158 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
159 $latest = !$i && $offset == 0;
160 $firstInList = !$i;
161 $next = isset( $revisions[$i + 1] ) ? $revisions[$i + 1 ] : null;
162 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
163 $counter++;
164 }
165
166 /*
167 * End navbar.
168 */
169 $s .= $this->endHistoryList();
170 $s .= $navbar;
171
172 /*
173 * Article validation line.
174 */
175 if ($wgUseValidation)
176 $s .= '<p>' . Validation::getStatisticsLink( $this->mArticle ) . '</p>' ;
177
178 $wgOut->addHTML( $s );
179 wfProfileOut( $fname );
180 }
181
182 /** @todo document */
183 function beginHistoryList() {
184 global $wgTitle;
185 $this->lastdate = '';
186 $s = wfMsgWikiHtml( 'histlegend' );
187 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
188 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
189 $s .= "<input type='hidden' name='title' value='{$prefixedkey}' />\n";
190 $s .= $this->submitButton();
191 $s .= '<ul id="pagehistory">' . "\n";
192 return $s;
193 }
194
195 /** @todo document */
196 function endHistoryList() {
197 $last = wfMsg( 'last' );
198
199 $s = '</ul>';
200 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
201 $s .= '</form>';
202 return $s;
203 }
204
205 /** @todo document */
206 function submitButton( $bits = array() ) {
207 return ( $this->linesonpage > 0 )
208 ? wfElement( 'input', array_merge( $bits,
209 array(
210 'class' => 'historysubmit',
211 'type' => 'submit',
212 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
213 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
214 'value' => wfMsgHtml( 'compareselectedversions' ),
215 ) ) )
216 : '';
217 }
218
219 /** @todo document */
220 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
221 global $wgLang, $wgContLang;
222
223 if ( 0 == $row->rev_user ) {
224 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
225 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
226 htmlspecialchars( $row->rev_user_text ),
227 'target=' . urlencode( $row->rev_user_text ) );
228 } else {
229 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
230 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
231 }
232
233 $s = '<li>';
234 /* This feature is not yet used according to schema */
235 if( $row->rev_deleted ) {
236 $s .= '<span class="history-deleted">';
237 }
238 $curlink = $this->curLink( $row, $latest );
239 $lastlink = $this->lastLink( $row, $next, $counter );
240 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
241 $link = $this->revLink( $row );
242
243 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
244
245 if( $row->rev_minor_edit ) {
246 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
247 }
248
249 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
250 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
251 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
252 }
253 if( $row->rev_deleted ) {
254 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
255 }
256 $s .= "</li>\n";
257
258 return $s;
259 }
260
261 /** @todo document */
262 function revLink( $row ) {
263 global $wgUser, $wgLang;
264 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $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, $date, "oldid={$row->rev_id}" );
270 }
271 }
272
273 /** @todo document */
274 function curLink( $row, $latest ) {
275 global $wgUser;
276 $cur = wfMsgHtml( 'cur' );
277 if( $latest
278 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
279 return $cur;
280 } else {
281 return $this->mSkin->makeKnownLinkObj(
282 $this->mTitle, $cur,
283 'diff=' . $this->getLatestID() .
284 "&oldid={$row->rev_id}" );
285 }
286 }
287
288 /** @todo document */
289 function lastLink( $row, $next, $counter ) {
290 global $wgUser;
291 $last = htmlspecialchars( wfMsg( 'last' ) );
292 if( is_null( $next )
293 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
294 return $last;
295 } else {
296 return $this->mSkin->makeKnownLinkObj(
297 $this->mTitle,
298 $last,
299 "diff={$row->rev_id}&oldid={$next->rev_id}"
300 /*,
301 '',
302 '',
303 "tabindex={$counter}"*/ );
304 }
305 }
306
307 /** @todo document */
308 function diffButtons( $row, $firstInList, $counter ) {
309 global $wgUser;
310 if( $this->linesonpage > 1) {
311 $radio = array(
312 'type' => 'radio',
313 'value' => $row->rev_id,
314 # do we really need to flood this on every item?
315 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
316 );
317
318 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
319 $radio['disabled'] = 'disabled';
320 }
321
322 /** @todo: move title texts to javascript */
323 if ( $firstInList ) {
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 = null ) {
354 if ( $id === null) $id = $this->mTitle->getArticleID();
355 return $this->getExtremeOffset( $id, 'max' );
356 }
357
358 /** @todo document */
359 function getEarliestOffset( $id = null ) {
360 if ( $id === null) $id = $this->mTitle->getArticleID();
361 return $this->getExtremeOffset( $id, 'min' );
362 }
363
364 /** @todo document */
365 function getExtremeOffset( $id, $func ) {
366 $db =& wfGetDB(DB_SLAVE);
367 return $db->selectField( 'revision',
368 "$func(rev_timestamp)",
369 array( 'rev_page' => $id ),
370 'PageHistory::getExtremeOffset' );
371 }
372
373 /** @todo document */
374 function getLatestID( $id = null ) {
375 if ( $id === null) $id = $this->mTitle->getArticleID();
376 $db =& wfGetDB(DB_SLAVE);
377 return $db->selectField( 'revision',
378 "max(rev_id)",
379 array( 'rev_page' => $id ),
380 'PageHistory::getLatestID' );
381 }
382
383 /** @todo document */
384 function getLastOffsetForPaging( $id, $step ) {
385 $fname = 'PageHistory::getLastOffsetForPaging';
386
387 $dbr =& wfGetDB(DB_SLAVE);
388 $res = $dbr->select(
389 'revision',
390 'rev_timestamp',
391 "rev_page=$id",
392 $fname,
393 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
394
395 $n = $dbr->numRows( $res );
396 $last = null;
397 while( $obj = $dbr->fetchObject( $res ) ) {
398 $last = $obj->rev_timestamp;
399 }
400 $dbr->freeResult( $res );
401 return $last;
402 }
403
404 /**
405 * @return returns the direction of browsing watchlist
406 */
407 function getDirection() {
408 global $wgRequest;
409 if ($wgRequest->getText("dir") == "prev")
410 return DIR_PREV;
411 else
412 return DIR_NEXT;
413 }
414
415 /** @todo document */
416 function fetchRevisions($limit, $offset, $direction) {
417 global $wgUser, $wgShowUpdatedMarker;
418 $fname = 'PageHistory::fetchRevisions';
419
420 $dbr =& wfGetDB( DB_SLAVE );
421
422 if ($direction == DIR_PREV)
423 list($dirs, $oper) = array("ASC", ">=");
424 else /* $direction == DIR_NEXT */
425 list($dirs, $oper) = array("DESC", "<=");
426
427 if ($offset)
428 $offsets = array("rev_timestamp $oper '$offset'");
429 else
430 $offsets = array();
431
432 $page_id = $this->mTitle->getArticleID();
433
434 $res = $dbr->select(
435 'revision',
436 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
437 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
438 array_merge(array("rev_page=$page_id"), $offsets),
439 $fname,
440 array('ORDER BY' => "rev_timestamp $dirs",
441 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
442 );
443
444 $result = array();
445 while (($obj = $dbr->fetchObject($res)) != NULL)
446 $result[] = $obj;
447
448 return $result;
449 }
450
451 /** @todo document */
452 function getNotificationTimestamp() {
453 global $wgUser, $wgShowUpdatedMarker;
454 $fname = 'PageHistory::getNotficationTimestamp';
455
456 if ($this->mNotificationTimestamp !== NULL)
457 return $this->mNotificationTimestamp;
458
459 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
460 return $this->mNotificationTimestamp = false;
461
462 $dbr =& wfGetDB(DB_SLAVE);
463
464 $this->mNotificationTimestamp = $dbr->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 $fname);
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 $latestTimestamp = wfTimestamp(TS_MW, $this->getLatestOffset());
483 $earliestTimestamp = wfTimestamp(TS_MW, $this->getEarliestOffset());
484
485 /*
486 * When we're displaying previous revisions, we need to reverse
487 * the array, because it's queried in reverse order.
488 */
489 if ($direction == DIR_PREV)
490 $revisions = array_reverse($revisions);
491
492 /*
493 * lowts is the timestamp of the first revision on this page.
494 * hights is the timestamp of the last revision.
495 */
496
497 $lowts = $hights = 0;
498
499 if( count( $revisions ) ) {
500 $latestShown = wfTimestamp(TS_MW, $revisions[0]->rev_timestamp);
501 $earliestShown = wfTimestamp(TS_MW, $revisions[count($revisions) - 1]->rev_timestamp);
502 }
503
504 /* Don't announce the limit everywhere if it's the default */
505 $usefulLimit = $limit == $this->defaultLimit ? '' : $limit;
506
507 $urls = array();
508 foreach (array(20, 50, 100, 250, 500) as $num) {
509 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
510 array('offset' => $offset == 0 ? '' : wfTimestamp(TS_MW, $offset), 'limit' => $num, ) );
511 }
512
513 $bits = implode($urls, ' | ');
514
515 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
516 if( $latestShown < $latestTimestamp ) {
517 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
518 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
519 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
520 array( 'limit' => $usefulLimit ) );
521 } else {
522 $prevtext = wfMsgHtml("prevn", $limit);
523 $lasttext = wfMsgHtml('histlast');
524 }
525
526 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
527 if( $earliestShown > $earliestTimestamp ) {
528 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
529 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
530 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
531 array( 'go' => 'first', 'limit' => $usefulLimit ) );
532 } else {
533 $nexttext = wfMsgHtml("nextn", $limit);
534 $firsttext = wfMsgHtml('histfirst');
535 }
536
537 $firstlast = "($lasttext | $firsttext)";
538
539 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
540 }
541
542 function MakeLink($text, $query = NULL) {
543 if ( $query === null ) return $text;
544 return $this->mSkin->makeKnownLinkObj(
545 $this->mTitle, $text,
546 wfArrayToCGI( $query, array( 'action' => 'history' )));
547 }
548
549
550 }
551
552 ?>