useless call
[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 var $mLatestId = null;
32
33 /**
34 * Construct a new PageHistory.
35 *
36 * @param Article $article
37 * @returns nothing
38 */
39 function PageHistory($article) {
40 global $wgUser;
41
42 $this->mArticle =& $article;
43 $this->mTitle =& $article->mTitle;
44 $this->mNotificationTimestamp = NULL;
45 $this->mSkin = $wgUser->getSkin();
46
47 $this->defaultLimit = 50;
48 }
49
50 /**
51 * Print the history page for an article.
52 *
53 * @returns nothing
54 */
55 function history() {
56 global $wgUser, $wgOut, $wgRequest, $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
190 // The following line is SUPPOSED to have double-quotes around the
191 // $prefixedkey variable, because htmlspecialchars() doesn't escape
192 // single-quotes.
193 //
194 // On at least two occasions people have changed it to single-quotes,
195 // which creates invalid HTML and incorrect display of the resulting
196 // link.
197 //
198 // Please do not break this a third time. Thank you for your kind
199 // consideration and cooperation.
200 //
201 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
202
203 $s .= $this->submitButton();
204 $s .= '<ul id="pagehistory">' . "\n";
205 return $s;
206 }
207
208 /** @todo document */
209 function endHistoryList() {
210 $last = wfMsg( 'last' );
211
212 $s = '</ul>';
213 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
214 $s .= '</form>';
215 return $s;
216 }
217
218 /** @todo document */
219 function submitButton( $bits = array() ) {
220 return ( $this->linesonpage > 0 )
221 ? wfElement( 'input', array_merge( $bits,
222 array(
223 'class' => 'historysubmit',
224 'type' => 'submit',
225 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
226 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
227 'value' => wfMsgHtml( 'compareselectedversions' ),
228 ) ) )
229 : '';
230 }
231
232 /** @todo document */
233 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
234
235 if ( 0 == $row->rev_user ) {
236 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
237 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
238 htmlspecialchars( $row->rev_user_text ),
239 'target=' . urlencode( $row->rev_user_text ) );
240 } else {
241 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
242 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
243 }
244
245 $s = '<li>';
246 /* This feature is not yet used according to schema */
247 if( $row->rev_deleted ) {
248 $s .= '<span class="history-deleted">';
249 }
250 $curlink = $this->curLink( $row, $latest );
251 $lastlink = $this->lastLink( $row, $next, $counter );
252 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
253 $link = $this->revLink( $row );
254
255 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
256
257 if( $row->rev_minor_edit ) {
258 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
259 }
260
261 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
262 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
263 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
264 }
265 if( $row->rev_deleted ) {
266 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
267 }
268 $s .= "</li>\n";
269
270 return $s;
271 }
272
273 /** @todo document */
274 function revLink( $row ) {
275 global $wgUser, $wgLang;
276 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true );
277 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
278 return $date;
279 } else {
280 return $this->mSkin->makeKnownLinkObj(
281 $this->mTitle, $date, "oldid={$row->rev_id}" );
282 }
283 }
284
285 /** @todo document */
286 function curLink( $row, $latest ) {
287 global $wgUser;
288 $cur = wfMsgHtml( 'cur' );
289 if( $latest
290 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
291 return $cur;
292 } else {
293 return $this->mSkin->makeKnownLinkObj(
294 $this->mTitle, $cur,
295 'diff=' . $this->getLatestID() .
296 "&oldid={$row->rev_id}" );
297 }
298 }
299
300 /** @todo document */
301 function lastLink( $row, $next, $counter ) {
302 global $wgUser;
303 $last = htmlspecialchars( wfMsg( 'last' ) );
304 if( is_null( $next ) ) {
305 if( $row->rev_timestamp == $this->getEarliestOffset() ) {
306 return $last;
307 } else {
308 // Cut off by paging; there are more behind us...
309 return $this->mSkin->makeKnownLinkObj(
310 $this->mTitle,
311 $last,
312 "diff={$row->rev_id}&oldid=prev" );
313 }
314 } elseif( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
315 return $last;
316 } else {
317 return $this->mSkin->makeKnownLinkObj(
318 $this->mTitle,
319 $last,
320 "diff={$row->rev_id}&oldid={$next->rev_id}"
321 /*,
322 '',
323 '',
324 "tabindex={$counter}"*/ );
325 }
326 }
327
328 /** @todo document */
329 function diffButtons( $row, $firstInList, $counter ) {
330 global $wgUser;
331 if( $this->linesonpage > 1) {
332 $radio = array(
333 'type' => 'radio',
334 'value' => $row->rev_id,
335 # do we really need to flood this on every item?
336 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
337 );
338
339 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
340 $radio['disabled'] = 'disabled';
341 }
342
343 /** @todo: move title texts to javascript */
344 if ( $firstInList ) {
345 $first = wfElement( 'input', array_merge(
346 $radio,
347 array(
348 'style' => 'visibility:hidden',
349 'name' => 'oldid' ) ) );
350 $checkmark = array( 'checked' => 'checked' );
351 } else {
352 if( $counter == 2 ) {
353 $checkmark = array( 'checked' => 'checked' );
354 } else {
355 $checkmark = array();
356 }
357 $first = wfElement( 'input', array_merge(
358 $radio,
359 $checkmark,
360 array( 'name' => 'oldid' ) ) );
361 $checkmark = array();
362 }
363 $second = wfElement( 'input', array_merge(
364 $radio,
365 $checkmark,
366 array( 'name' => 'diff' ) ) );
367 return $first . $second;
368 } else {
369 return '';
370 }
371 }
372
373 /** @todo document */
374 function getLatestOffset( $id = null ) {
375 if ( $id === null) $id = $this->mTitle->getArticleID();
376 return $this->getExtremeOffset( $id, 'max' );
377 }
378
379 /** @todo document */
380 function getEarliestOffset( $id = null ) {
381 if ( $id === null) $id = $this->mTitle->getArticleID();
382 return $this->getExtremeOffset( $id, 'min' );
383 }
384
385 /** @todo document */
386 function getExtremeOffset( $id, $func ) {
387 $db =& wfGetDB(DB_SLAVE);
388 return $db->selectField( 'revision',
389 "$func(rev_timestamp)",
390 array( 'rev_page' => $id ),
391 'PageHistory::getExtremeOffset' );
392 }
393
394 /** @todo document */
395 function getLatestId() {
396 if( is_null( $this->mLatestId ) ) {
397 $id = $this->mTitle->getArticleID();
398 $db =& wfGetDB(DB_SLAVE);
399 $this->mLatestId = $db->selectField( 'revision',
400 "max(rev_id)",
401 array( 'rev_page' => $id ),
402 'PageHistory::getLatestID' );
403 }
404 return $this->mLatestId;
405 }
406
407 /** @todo document */
408 function getLastOffsetForPaging( $id, $step ) {
409 $fname = 'PageHistory::getLastOffsetForPaging';
410
411 $dbr =& wfGetDB(DB_SLAVE);
412 $res = $dbr->select(
413 'revision',
414 'rev_timestamp',
415 "rev_page=$id",
416 $fname,
417 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
418
419 $n = $dbr->numRows( $res );
420 $last = null;
421 while( $obj = $dbr->fetchObject( $res ) ) {
422 $last = $obj->rev_timestamp;
423 }
424 $dbr->freeResult( $res );
425 return $last;
426 }
427
428 /**
429 * @return returns the direction of browsing watchlist
430 */
431 function getDirection() {
432 global $wgRequest;
433 if ($wgRequest->getText("dir") == "prev")
434 return DIR_PREV;
435 else
436 return DIR_NEXT;
437 }
438
439 /** @todo document */
440 function fetchRevisions($limit, $offset, $direction) {
441 global $wgUser, $wgShowUpdatedMarker;
442 $fname = 'PageHistory::fetchRevisions';
443
444 $dbr =& wfGetDB( DB_SLAVE );
445
446 if ($direction == DIR_PREV)
447 list($dirs, $oper) = array("ASC", ">=");
448 else /* $direction == DIR_NEXT */
449 list($dirs, $oper) = array("DESC", "<=");
450
451 if ($offset)
452 $offsets = array("rev_timestamp $oper '$offset'");
453 else
454 $offsets = array();
455
456 $page_id = $this->mTitle->getArticleID();
457
458 $res = $dbr->select(
459 'revision',
460 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
461 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
462 array_merge(array("rev_page=$page_id"), $offsets),
463 $fname,
464 array('ORDER BY' => "rev_timestamp $dirs",
465 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
466 );
467
468 $result = array();
469 while (($obj = $dbr->fetchObject($res)) != NULL)
470 $result[] = $obj;
471
472 return $result;
473 }
474
475 /** @todo document */
476 function getNotificationTimestamp() {
477 global $wgUser, $wgShowUpdatedMarker;
478 $fname = 'PageHistory::getNotficationTimestamp';
479
480 if ($this->mNotificationTimestamp !== NULL)
481 return $this->mNotificationTimestamp;
482
483 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
484 return $this->mNotificationTimestamp = false;
485
486 $dbr =& wfGetDB(DB_SLAVE);
487
488 $this->mNotificationTimestamp = $dbr->selectField(
489 'watchlist',
490 'wl_notificationtimestamp',
491 array( 'wl_namespace' => $this->mTitle->getNamespace(),
492 'wl_title' => $this->mTitle->getDBkey(),
493 'wl_user' => $wgUser->getID()
494 ),
495 $fname);
496
497 return $this->mNotificationTimestamp;
498 }
499
500 /** @todo document */
501 function makeNavbar($revisions, $offset, $limit, $direction) {
502 global $wgTitle, $wgLang;
503
504 $revisions = array_slice($revisions, 0, $limit);
505
506 $latestTimestamp = wfTimestamp(TS_MW, $this->getLatestOffset());
507 $earliestTimestamp = wfTimestamp(TS_MW, $this->getEarliestOffset());
508
509 /*
510 * When we're displaying previous revisions, we need to reverse
511 * the array, because it's queried in reverse order.
512 */
513 if ($direction == DIR_PREV)
514 $revisions = array_reverse($revisions);
515
516 /*
517 * lowts is the timestamp of the first revision on this page.
518 * hights is the timestamp of the last revision.
519 */
520
521 $lowts = $hights = 0;
522
523 if( count( $revisions ) ) {
524 $latestShown = wfTimestamp(TS_MW, $revisions[0]->rev_timestamp);
525 $earliestShown = wfTimestamp(TS_MW, $revisions[count($revisions) - 1]->rev_timestamp);
526 }
527
528 /* Don't announce the limit everywhere if it's the default */
529 $usefulLimit = $limit == $this->defaultLimit ? '' : $limit;
530
531 $urls = array();
532 foreach (array(20, 50, 100, 250, 500) as $num) {
533 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
534 array('offset' => $offset == 0 ? '' : wfTimestamp(TS_MW, $offset), 'limit' => $num, ) );
535 }
536
537 $bits = implode($urls, ' | ');
538
539 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
540 if( $latestShown < $latestTimestamp ) {
541 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
542 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
543 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
544 array( 'limit' => $usefulLimit ) );
545 } else {
546 $prevtext = wfMsgHtml("prevn", $limit);
547 $lasttext = wfMsgHtml('histlast');
548 }
549
550 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
551 if( $earliestShown > $earliestTimestamp ) {
552 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
553 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
554 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
555 array( 'go' => 'first', 'limit' => $usefulLimit ) );
556 } else {
557 $nexttext = wfMsgHtml("nextn", $limit);
558 $firsttext = wfMsgHtml('histfirst');
559 }
560
561 $firstlast = "($lasttext | $firsttext)";
562
563 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
564 }
565
566 function MakeLink($text, $query = NULL) {
567 if ( $query === null ) return $text;
568 return $this->mSkin->makeKnownLinkObj(
569 $this->mTitle, $text,
570 wfArrayToCGI( $query, array( 'action' => 'history' )));
571 }
572
573
574 }
575
576 ?>