* s~\t+$~~
[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 define('DIR_PREV', 0);
10 define('DIR_NEXT', 1);
11
12 /**
13 * This class handles printing the history page for an article. In order to
14 * be efficient, it uses timestamps rather than offsets for paging, to avoid
15 * costly LIMIT,offset queries.
16 *
17 * Construct it by passing in an Article, and call $h->history() to print the
18 * history.
19 *
20 * @package MediaWiki
21 */
22
23 class PageHistory {
24 var $mArticle, $mTitle, $mSkin;
25 var $lastdate;
26 var $linesonpage;
27 var $mNotificationTimestamp;
28 var $mLatestId = null;
29
30 /**
31 * Construct a new PageHistory.
32 *
33 * @param Article $article
34 * @returns nothing
35 */
36 function PageHistory($article) {
37 global $wgUser;
38
39 $this->mArticle =& $article;
40 $this->mTitle =& $article->mTitle;
41 $this->mNotificationTimestamp = NULL;
42 $this->mSkin = $wgUser->getSkin();
43
44 $this->defaultLimit = 50;
45 }
46
47 /**
48 * Print the history page for an article.
49 *
50 * @returns nothing
51 */
52 function history() {
53 global $wgUser, $wgOut, $wgRequest, $wgTitle;
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 if( !$this->mTitle->exists() ) {
79 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
80 wfProfileOut( $fname );
81 return;
82 }
83
84 $dbr =& wfGetDB(DB_SLAVE);
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', $this->defaultLimit);
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 # $offset = $dbr->timestamp($offset);
97 $dboffset = $offset === 0 ? 0 : $dbr->timestamp($offset);
98 /*
99 * "go=last" means to jump to the last history page.
100 */
101 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
102 switch ($gowhere) {
103 case "first":
104 if (($lastid = $this->getLastOffsetForPaging($this->mTitle->getArticleID(), $limit)) === NULL)
105 break;
106 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
107 wfTimestamp(TS_MW, $lastid));
108 break;
109 default:
110 $gourl = NULL;
111 }
112
113 if (!is_null($gourl)) {
114 $wgOut->redirect($gourl);
115 return;
116 }
117 }
118
119 /*
120 * Fetch revisions.
121 *
122 * If the user clicked "previous", we retrieve the revisions backwards,
123 * then reverse them. This is to avoid needing to know the timestamp of
124 * previous revisions when generating the URL.
125 */
126 $direction = $this->getDirection();
127 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
128 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
129
130 /*
131 * We fetch one more revision than needed to get the timestamp of the
132 * one after this page (and to know if it exists).
133 *
134 * linesonpage stores the actual number of lines.
135 */
136 if (count($revisions) < $limit + 1)
137 $this->linesonpage = count($revisions);
138 else
139 $this->linesonpage = count($revisions) - 1;
140
141 /* Un-reverse revisions */
142 if ($direction == DIR_PREV)
143 $revisions = array_reverse($revisions);
144
145 /*
146 * Print the top navbar.
147 */
148 $s = $navbar;
149 $s .= $this->beginHistoryList();
150 $counter = 1;
151
152 /*
153 * Print each revision, excluding the one-past-the-end, if any.
154 */
155 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
156 $latest = !$i && $offset == 0;
157 $firstInList = !$i;
158 $next = isset( $revisions[$i + 1] ) ? $revisions[$i + 1 ] : null;
159 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
160 $counter++;
161 }
162
163 /*
164 * End navbar.
165 */
166 $s .= $this->endHistoryList();
167 $s .= $navbar;
168
169 $wgOut->addHTML( $s );
170 wfProfileOut( $fname );
171 }
172
173 /** @todo document */
174 function beginHistoryList() {
175 global $wgTitle;
176 $this->lastdate = '';
177 $s = wfMsgWikiHtml( 'histlegend' );
178 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
179 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
180
181 // The following line is SUPPOSED to have double-quotes around the
182 // $prefixedkey variable, because htmlspecialchars() doesn't escape
183 // single-quotes.
184 //
185 // On at least two occasions people have changed it to single-quotes,
186 // which creates invalid HTML and incorrect display of the resulting
187 // link.
188 //
189 // Please do not break this a third time. Thank you for your kind
190 // consideration and cooperation.
191 //
192 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
193
194 $s .= $this->submitButton();
195 $s .= '<ul id="pagehistory">' . "\n";
196 return $s;
197 }
198
199 /** @todo document */
200 function endHistoryList() {
201 $last = wfMsg( 'last' );
202
203 $s = '</ul>';
204 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
205 $s .= '</form>';
206 return $s;
207 }
208
209 /** @todo document */
210 function submitButton( $bits = array() ) {
211 return ( $this->linesonpage > 0 )
212 ? wfElement( 'input', array_merge( $bits,
213 array(
214 'class' => 'historysubmit',
215 'type' => 'submit',
216 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
217 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
218 'value' => wfMsgHtml( 'compareselectedversions' ),
219 ) ) )
220 : '';
221 }
222
223 /** @todo document */
224 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
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 /* This feature is not yet used according to schema */
238 if( $row->rev_deleted ) {
239 $s .= '<span class="history-deleted">';
240 }
241 $curlink = $this->curLink( $row, $latest );
242 $lastlink = $this->lastLink( $row, $next, $counter );
243 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
244 $link = $this->revLink( $row );
245
246 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
247
248 if( $row->rev_minor_edit ) {
249 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
250 }
251
252 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
253 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
254 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
255 }
256 if( $row->rev_deleted ) {
257 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
258 }
259 $s .= "</li>\n";
260
261 return $s;
262 }
263
264 /** @todo document */
265 function revLink( $row ) {
266 global $wgUser, $wgLang;
267 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true );
268 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
269 return $date;
270 } else {
271 return $this->mSkin->makeKnownLinkObj(
272 $this->mTitle, $date, "oldid={$row->rev_id}" );
273 }
274 }
275
276 /** @todo document */
277 function curLink( $row, $latest ) {
278 global $wgUser;
279 $cur = wfMsgHtml( 'cur' );
280 if( $latest
281 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
282 return $cur;
283 } else {
284 return $this->mSkin->makeKnownLinkObj(
285 $this->mTitle, $cur,
286 'diff=' . $this->getLatestID() .
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 if( $row->rev_timestamp == $this->getEarliestOffset() ) {
297 return $last;
298 } else {
299 // Cut off by paging; there are more behind us...
300 return $this->mSkin->makeKnownLinkObj(
301 $this->mTitle,
302 $last,
303 "diff={$row->rev_id}&oldid=prev" );
304 }
305 } elseif( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
306 return $last;
307 } else {
308 return $this->mSkin->makeKnownLinkObj(
309 $this->mTitle,
310 $last,
311 "diff={$row->rev_id}&oldid={$next->rev_id}"
312 /*,
313 '',
314 '',
315 "tabindex={$counter}"*/ );
316 }
317 }
318
319 /** @todo document */
320 function diffButtons( $row, $firstInList, $counter ) {
321 global $wgUser;
322 if( $this->linesonpage > 1) {
323 $radio = array(
324 'type' => 'radio',
325 'value' => $row->rev_id,
326 # do we really need to flood this on every item?
327 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
328 );
329
330 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
331 $radio['disabled'] = 'disabled';
332 }
333
334 /** @todo: move title texts to javascript */
335 if ( $firstInList ) {
336 $first = wfElement( 'input', array_merge(
337 $radio,
338 array(
339 'style' => 'visibility:hidden',
340 'name' => 'oldid' ) ) );
341 $checkmark = array( 'checked' => 'checked' );
342 } else {
343 if( $counter == 2 ) {
344 $checkmark = array( 'checked' => 'checked' );
345 } else {
346 $checkmark = array();
347 }
348 $first = wfElement( 'input', array_merge(
349 $radio,
350 $checkmark,
351 array( 'name' => 'oldid' ) ) );
352 $checkmark = array();
353 }
354 $second = wfElement( 'input', array_merge(
355 $radio,
356 $checkmark,
357 array( 'name' => 'diff' ) ) );
358 return $first . $second;
359 } else {
360 return '';
361 }
362 }
363
364 /** @todo document */
365 function getLatestOffset( $id = null ) {
366 if ( $id === null) $id = $this->mTitle->getArticleID();
367 return $this->getExtremeOffset( $id, 'max' );
368 }
369
370 /** @todo document */
371 function getEarliestOffset( $id = null ) {
372 if ( $id === null) $id = $this->mTitle->getArticleID();
373 return $this->getExtremeOffset( $id, 'min' );
374 }
375
376 /** @todo document */
377 function getExtremeOffset( $id, $func ) {
378 $db =& wfGetDB(DB_SLAVE);
379 return $db->selectField( 'revision',
380 "$func(rev_timestamp)",
381 array( 'rev_page' => $id ),
382 'PageHistory::getExtremeOffset' );
383 }
384
385 /** @todo document */
386 function getLatestId() {
387 if( is_null( $this->mLatestId ) ) {
388 $id = $this->mTitle->getArticleID();
389 $db =& wfGetDB(DB_SLAVE);
390 $this->mLatestId = $db->selectField( 'revision',
391 "max(rev_id)",
392 array( 'rev_page' => $id ),
393 'PageHistory::getLatestID' );
394 }
395 return $this->mLatestId;
396 }
397
398 /** @todo document */
399 function getLastOffsetForPaging( $id, $step ) {
400 $fname = 'PageHistory::getLastOffsetForPaging';
401
402 $dbr =& wfGetDB(DB_SLAVE);
403 $res = $dbr->select(
404 'revision',
405 'rev_timestamp',
406 "rev_page=$id",
407 $fname,
408 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
409
410 $n = $dbr->numRows( $res );
411 $last = null;
412 while( $obj = $dbr->fetchObject( $res ) ) {
413 $last = $obj->rev_timestamp;
414 }
415 $dbr->freeResult( $res );
416 return $last;
417 }
418
419 /**
420 * @return returns the direction of browsing watchlist
421 */
422 function getDirection() {
423 global $wgRequest;
424 if ($wgRequest->getText("dir") == "prev")
425 return DIR_PREV;
426 else
427 return DIR_NEXT;
428 }
429
430 /** @todo document */
431 function fetchRevisions($limit, $offset, $direction) {
432 global $wgUser, $wgShowUpdatedMarker;
433 $fname = 'PageHistory::fetchRevisions';
434
435 $dbr =& wfGetDB( DB_SLAVE );
436
437 if ($direction == DIR_PREV)
438 list($dirs, $oper) = array("ASC", ">=");
439 else /* $direction == DIR_NEXT */
440 list($dirs, $oper) = array("DESC", "<=");
441
442 if ($offset)
443 $offsets = array("rev_timestamp $oper '$offset'");
444 else
445 $offsets = array();
446
447 $page_id = $this->mTitle->getArticleID();
448
449 $res = $dbr->select(
450 'revision',
451 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
452 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
453 array_merge(array("rev_page=$page_id"), $offsets),
454 $fname,
455 array('ORDER BY' => "rev_timestamp $dirs",
456 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
457 );
458
459 $result = array();
460 while (($obj = $dbr->fetchObject($res)) != NULL)
461 $result[] = $obj;
462
463 return $result;
464 }
465
466 /** @todo document */
467 function getNotificationTimestamp() {
468 global $wgUser, $wgShowUpdatedMarker;
469 $fname = 'PageHistory::getNotficationTimestamp';
470
471 if ($this->mNotificationTimestamp !== NULL)
472 return $this->mNotificationTimestamp;
473
474 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
475 return $this->mNotificationTimestamp = false;
476
477 $dbr =& wfGetDB(DB_SLAVE);
478
479 $this->mNotificationTimestamp = $dbr->selectField(
480 'watchlist',
481 'wl_notificationtimestamp',
482 array( 'wl_namespace' => $this->mTitle->getNamespace(),
483 'wl_title' => $this->mTitle->getDBkey(),
484 'wl_user' => $wgUser->getID()
485 ),
486 $fname);
487
488 return $this->mNotificationTimestamp;
489 }
490
491 /** @todo document */
492 function makeNavbar($revisions, $offset, $limit, $direction) {
493 global $wgTitle, $wgLang;
494
495 $revisions = array_slice($revisions, 0, $limit);
496
497 $latestTimestamp = wfTimestamp(TS_MW, $this->getLatestOffset());
498 $earliestTimestamp = wfTimestamp(TS_MW, $this->getEarliestOffset());
499
500 /*
501 * When we're displaying previous revisions, we need to reverse
502 * the array, because it's queried in reverse order.
503 */
504 if ($direction == DIR_PREV)
505 $revisions = array_reverse($revisions);
506
507 /*
508 * lowts is the timestamp of the first revision on this page.
509 * hights is the timestamp of the last revision.
510 */
511
512 $lowts = $hights = 0;
513
514 if( count( $revisions ) ) {
515 $latestShown = wfTimestamp(TS_MW, $revisions[0]->rev_timestamp);
516 $earliestShown = wfTimestamp(TS_MW, $revisions[count($revisions) - 1]->rev_timestamp);
517 }
518
519 /* Don't announce the limit everywhere if it's the default */
520 $usefulLimit = $limit == $this->defaultLimit ? '' : $limit;
521
522 $urls = array();
523 foreach (array(20, 50, 100, 250, 500) as $num) {
524 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
525 array('offset' => $offset == 0 ? '' : wfTimestamp(TS_MW, $offset), 'limit' => $num, ) );
526 }
527
528 $bits = implode($urls, ' | ');
529
530 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
531 if( $latestShown < $latestTimestamp ) {
532 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
533 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
534 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
535 array( 'limit' => $usefulLimit ) );
536 } else {
537 $prevtext = wfMsgHtml("prevn", $limit);
538 $lasttext = wfMsgHtml('histlast');
539 }
540
541 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
542 if( $earliestShown > $earliestTimestamp ) {
543 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
544 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
545 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
546 array( 'go' => 'first', 'limit' => $usefulLimit ) );
547 } else {
548 $nexttext = wfMsgHtml("nextn", $limit);
549 $firsttext = wfMsgHtml('histfirst');
550 }
551
552 $firstlast = "($lasttext | $firsttext)";
553
554 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
555 }
556
557 function MakeLink($text, $query = NULL) {
558 if ( $query === null ) return $text;
559 return $this->mSkin->makeKnownLinkObj(
560 $this->mTitle, $text,
561 wfArrayToCGI( $query, array( 'action' => 'history' )));
562 }
563
564
565 }
566
567 ?>