Cleanup, html-safety and output
[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 wfdebug("offset=[$offset] dboffset=[$dboffset]\n");
102 /*
103 * "go=last" means to jump to the last history page.
104 */
105 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
106 switch ($gowhere) {
107 case "first":
108 if (($lastid = $this->getLastOffsetForPaging($this->mTitle->getArticleID(), $limit)) === NULL)
109 break;
110 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
111 wfTimestamp(TS_MW, $lastid));
112 break;
113 default:
114 $gourl = NULL;
115 }
116
117 if (!is_null($gourl)) {
118 $wgOut->redirect($gourl);
119 return;
120 }
121 }
122
123 /*
124 * Fetch revisions.
125 *
126 * If the user clicked "previous", we retrieve the revisions backwards,
127 * then reverse them. This is to avoid needing to know the timestamp of
128 * previous revisions when generating the URL.
129 */
130 $direction = $this->getDirection();
131 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
132 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
133
134 /*
135 * We fetch one more revision than needed to get the timestamp of the
136 * one after this page (and to know if it exists).
137 *
138 * linesonpage stores the actual number of lines.
139 */
140 if (count($revisions) < $limit + 1)
141 $this->linesonpage = count($revisions);
142 else
143 $this->linesonpage = count($revisions) - 1;
144
145 /* Un-reverse revisions */
146 if ($direction == DIR_PREV)
147 $revisions = array_reverse($revisions);
148
149 /*
150 * Print the top navbar.
151 */
152 $s = $navbar;
153 $s .= $this->beginHistoryList();
154 $counter = 1;
155
156 /*
157 * Print each revision, excluding the one-past-the-end, if any.
158 */
159 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
160 $latest = !$i && $offset == 0;
161 $firstInList = !$i;
162 $next = isset( $revisions[$i + 1] ) ? $revisions[$i + 1 ] : null;
163 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
164 $counter++;
165 }
166
167 /*
168 * End navbar.
169 */
170 $s .= $this->endHistoryList();
171 $s .= $navbar;
172
173 /*
174 * Article validation line.
175 */
176 if ($wgUseValidation)
177 $s .= '<p>' . Validation::getStatisticsLink( $this->mArticle ) . '</p>' ;
178
179 $wgOut->addHTML( $s );
180 wfProfileOut( $fname );
181 }
182
183 /** @todo document */
184 function beginHistoryList() {
185 global $wgTitle;
186 $this->lastdate = '';
187 $s = wfMsgWikiHtml( 'histlegend' );
188 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
189 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
190 $s .= "<input type='hidden' name='title' value='{$prefixedkey}' />\n";
191 $s .= $this->submitButton();
192 $s .= '<ul id="pagehistory">' . "\n";
193 return $s;
194 }
195
196 /** @todo document */
197 function endHistoryList() {
198 $last = wfMsg( 'last' );
199
200 $s = '</ul>';
201 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
202 $s .= '</form>';
203 return $s;
204 }
205
206 /** @todo document */
207 function submitButton( $bits = array() ) {
208 return ( $this->linesonpage > 0 )
209 ? wfElement( 'input', array_merge( $bits,
210 array(
211 'class' => 'historysubmit',
212 'type' => 'submit',
213 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
214 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
215 'value' => wfMsgHtml( 'compareselectedversions' ),
216 ) ) )
217 : '';
218 }
219
220 /** @todo document */
221 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
222 global $wgLang, $wgContLang;
223
224 if ( 0 == $row->rev_user ) {
225 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
226 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
227 htmlspecialchars( $row->rev_user_text ),
228 'target=' . urlencode( $row->rev_user_text ) );
229 } else {
230 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
231 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
232 }
233
234 $s = '<li>';
235 /* This feature is not yet used according to schema */
236 if( $row->rev_deleted ) {
237 $s .= '<span class="history-deleted">';
238 }
239 $curlink = $this->curLink( $row, $latest );
240 $lastlink = $this->lastLink( $row, $next, $counter );
241 $arbitrary = $this->diffButtons( $row, $firstInList, $counter );
242 $link = $this->revLink( $row );
243
244 $s .= "($curlink) ($lastlink) $arbitrary $link <span class='history-user'>$ul</span>";
245
246 if( $row->rev_minor_edit ) {
247 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
248 }
249
250 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
251 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
252 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
253 }
254 if( $row->rev_deleted ) {
255 $s .= '</span> ' . wfMsgHtml( 'deletedrev' );
256 }
257 $s .= "</li>\n";
258
259 return $s;
260 }
261
262 /** @todo document */
263 function revLink( $row ) {
264 global $wgUser, $wgLang;
265 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true );
266 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
267 return $date;
268 } else {
269 return $this->mSkin->makeKnownLinkObj(
270 $this->mTitle, $date, "oldid={$row->rev_id}" );
271 }
272 }
273
274 /** @todo document */
275 function curLink( $row, $latest ) {
276 global $wgUser;
277 $cur = wfMsgHtml( 'cur' );
278 if( $latest
279 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
280 return $cur;
281 } else {
282 return $this->mSkin->makeKnownLinkObj(
283 $this->mTitle, $cur,
284 'diff=' . $this->getLatestID() .
285 "&oldid={$row->rev_id}" );
286 }
287 }
288
289 /** @todo document */
290 function lastLink( $row, $next, $counter ) {
291 global $wgUser;
292 $last = htmlspecialchars( wfMsg( 'last' ) );
293 if( is_null( $next )
294 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
295 return $last;
296 } else {
297 return $this->mSkin->makeKnownLinkObj(
298 $this->mTitle,
299 $last,
300 "diff={$row->rev_id}&oldid={$next->rev_id}"
301 /*,
302 '',
303 '',
304 "tabindex={$counter}"*/ );
305 }
306 }
307
308 /** @todo document */
309 function diffButtons( $row, $firstInList, $counter ) {
310 global $wgUser;
311 if( $this->linesonpage > 1) {
312 $radio = array(
313 'type' => 'radio',
314 'value' => $row->rev_id,
315 # do we really need to flood this on every item?
316 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
317 );
318
319 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
320 $radio['disabled'] = 'disabled';
321 }
322
323 /** @todo: move title texts to javascript */
324 if ( $firstInList ) {
325 $first = wfElement( 'input', array_merge(
326 $radio,
327 array(
328 'style' => 'visibility:hidden',
329 'name' => 'oldid' ) ) );
330 $checkmark = array( 'checked' => 'checked' );
331 } else {
332 if( $counter == 2 ) {
333 $checkmark = array( 'checked' => 'checked' );
334 } else {
335 $checkmark = array();
336 }
337 $first = wfElement( 'input', array_merge(
338 $radio,
339 $checkmark,
340 array( 'name' => 'oldid' ) ) );
341 $checkmark = array();
342 }
343 $second = wfElement( 'input', array_merge(
344 $radio,
345 $checkmark,
346 array( 'name' => 'diff' ) ) );
347 return $first . $second;
348 } else {
349 return '';
350 }
351 }
352
353 /** @todo document */
354 function getLatestOffset( $id = null ) {
355 if ( $id === null) $id = $this->mTitle->getArticleID();
356 return $this->getExtremeOffset( $id, 'max' );
357 }
358
359 /** @todo document */
360 function getEarliestOffset( $id = null ) {
361 if ( $id === null) $id = $this->mTitle->getArticleID();
362 return $this->getExtremeOffset( $id, 'min' );
363 }
364
365 /** @todo document */
366 function getExtremeOffset( $id, $func ) {
367 $db =& wfGetDB(DB_SLAVE);
368 return $db->selectField( 'revision',
369 "$func(rev_timestamp)",
370 array( 'rev_page' => $id ),
371 'PageHistory::getExtremeOffset' );
372 }
373
374 /** @todo document */
375 function getLatestID( $id = null ) {
376 if ( $id === null) $id = $this->mTitle->getArticleID();
377 $db =& wfGetDB(DB_SLAVE);
378 return $db->selectField( 'revision',
379 "max(rev_id)",
380 array( 'rev_page' => $id ),
381 'PageHistory::getLatestID' );
382 }
383
384 /** @todo document */
385 function getLastOffsetForPaging( $id, $step ) {
386 $fname = 'PageHistory::getLastOffsetForPaging';
387
388 $dbr =& wfGetDB(DB_SLAVE);
389 $res = $dbr->select(
390 'revision',
391 'rev_timestamp',
392 "rev_page=$id",
393 $fname,
394 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
395
396 $n = $dbr->numRows( $res );
397 $last = null;
398 while( $obj = $db->fetchObject( $res ) ) {
399 $last = $obj->rev_timestamp;
400 }
401 $db->freeResult( $res );
402 return $last;
403 }
404
405 /**
406 * @return returns the direction of browsing watchlist
407 */
408 function getDirection() {
409 global $wgRequest;
410 if ($wgRequest->getText("dir") == "prev")
411 return DIR_PREV;
412 else
413 return DIR_NEXT;
414 }
415
416 /** @todo document */
417 function fetchRevisions($limit, $offset, $direction) {
418 global $wgUser, $wgShowUpdatedMarker;
419 $fname = 'PageHistory::fetchRevisions';
420
421 $dbr =& wfGetDB( DB_SLAVE );
422
423 if ($direction == DIR_PREV)
424 list($dirs, $oper) = array("ASC", ">=");
425 else /* $direction == DIR_NEXT */
426 list($dirs, $oper) = array("DESC", "<=");
427
428 if ($offset)
429 $offsets = array("rev_timestamp $oper '$offset'");
430 else
431 $offsets = array();
432
433 $page_id = $this->mTitle->getArticleID();
434
435 $res = $dbr->select(
436 'revision',
437 array('rev_id', 'rev_user', 'rev_comment', 'rev_user_text',
438 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
439 array_merge(array("rev_page=$page_id"), $offsets),
440 $fname,
441 array('ORDER BY' => "rev_timestamp $dirs",
442 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
443 );
444
445 $result = array();
446 while (($obj = $dbr->fetchObject($res)) != NULL)
447 $result[] = $obj;
448
449 return $result;
450 }
451
452 /** @todo document */
453 function getNotificationTimestamp() {
454 global $wgUser, $wgShowUpdatedMarker;
455 $fname = 'PageHistory::getNotficationTimestamp';
456
457 if ($this->mNotificationTimestamp !== NULL)
458 return $this->mNotificationTimestamp;
459
460 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
461 return $this->mNotificationTimestamp = false;
462
463 $dbr =& wfGetDB(DB_SLAVE);
464
465 $this->mNotificationTimestamp = $dbr->selectField(
466 'watchlist',
467 'wl_notificationtimestamp',
468 array( 'wl_namespace' => $this->mTitle->getNamespace(),
469 'wl_title' => $this->mTitle->getDBkey(),
470 'wl_user' => $wgUser->getID()
471 ),
472 $fname);
473
474 return $this->mNotificationTimestamp;
475 }
476
477 /** @todo document */
478 function makeNavbar($revisions, $offset, $limit, $direction) {
479 global $wgTitle, $wgLang;
480
481 $revisions = array_slice($revisions, 0, $limit);
482
483 $latestTimestamp = wfTimestamp(TS_MW, $this->getLatestOffset());
484 $earliestTimestamp = wfTimestamp(TS_MW, $this->getEarliestOffset());
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 = wfTimestamp(TS_MW, $revisions[0]->rev_timestamp);
502 $earliestShown = wfTimestamp(TS_MW, $revisions[count($revisions) - 1]->rev_timestamp);
503 }
504
505 /* Don't announce the limit everywhere if it's the default */
506 $usefulLimit = $limit == $this->defaultLimit ? '' : $limit;
507
508 $urls = array();
509 foreach (array(20, 50, 100, 250, 500) as $num) {
510 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
511 array('offset' => $offset == 0 ? '' : wfTimestamp(TS_MW, $offset), 'limit' => $num, ) );
512 }
513
514 $bits = implode($urls, ' | ');
515
516 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
517 if( $latestShown < $latestTimestamp ) {
518 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
519 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
520 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
521 array( 'limit' => $usefulLimit ) );
522 } else {
523 $prevtext = wfMsgHtml("prevn", $limit);
524 $lasttext = wfMsgHtml('histlast');
525 }
526
527 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
528 if( $earliestShown > $earliestTimestamp ) {
529 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
530 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
531 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
532 array( 'go' => 'first', 'limit' => $usefulLimit ) );
533 } else {
534 $nexttext = wfMsgHtml("nextn", $limit);
535 $firsttext = wfMsgHtml('histfirst');
536 }
537
538 $firstlast = "($lasttext | $firsttext)";
539
540 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
541 }
542
543 function MakeLink($text, $query = NULL) {
544 if ( $query === null ) return $text;
545 return $this->mSkin->makeKnownLinkObj(
546 $this->mTitle, $text,
547 wfArrayToCGI( $query, array( 'action' => 'history' )));
548 }
549
550
551 }
552
553 ?>