merge ORACLE_WORK. sorry, this may break some parts of MySQL, i did not test extensi...
[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 $dbr = wfGetDB(DB_SLAVE);
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 $id = $this->mTitle->getArticleID();
82 if( $id == 0 ) {
83 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
84 wfProfileOut( $fname );
85 return;
86 }
87
88 /*
89 * Extract limit, the number of revisions to show, and
90 * offset, the timestamp to begin at, from the URL.
91 */
92 $limit = $wgRequest->getInt('limit', 50);
93 $offset = $wgRequest->getText('offset');
94
95 /* Offset must be an integral. */
96 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
97 $offset = 0;
98 # $offset = $dbr->timestamp($offset);
99 $dboffset = $dbr->timestamp($offset);
100 wfdebug("offset=[$offset] dboffset=[$dboffset]\n");
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($id, $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 $first = !$i && $offset == 0;
160 $next = isset( $revisions[$i + 1] ) ? $revisions[$i + 1 ] : null;
161 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $first);
162 $counter++;
163 }
164
165 /*
166 * End navbar.
167 */
168 $s .= $this->endHistoryList();
169 $s .= $navbar;
170
171 /*
172 * Article validation line.
173 */
174 if ($wgUseValidation)
175 $s .= '<p>' . Validation::getStatisticsLink( $this->mArticle ) . '</p>' ;
176
177 $wgOut->addHTML( $s );
178 wfProfileOut( $fname );
179 }
180
181 /** @todo document */
182 function beginHistoryList() {
183 global $wgTitle;
184 $this->lastdate = '';
185 $s = '<p>' . wfMsg( 'histlegend' ) . '</p>';
186 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
187 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
188 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
189 $s .= $this->submitButton();
190 $s .= '<ul id="pagehistory">';
191 return $s;
192 }
193
194 /** @todo document */
195 function endHistoryList() {
196 $last = wfMsg( 'last' );
197
198 $s = '</ul>';
199 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
200 $s .= '</form>';
201 return $s;
202 }
203
204 /** @todo document */
205 function submitButton( $bits = array() ) {
206 return ( $this->linesonpage > 0 )
207 ? wfElement( 'input', array_merge( $bits,
208 array(
209 'class' => 'historysubmit',
210 'type' => 'submit',
211 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
212 'title' => wfMsg( 'tooltip-compareselectedversions' ),
213 'value' => wfMsg( 'compareselectedversions' ),
214 ) ) )
215 : '';
216 }
217
218 /** @todo document */
219 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false ) {
220 global $wgLang, $wgContLang;
221
222 static $message;
223 if( !isset( $message ) ) {
224 foreach( explode( ' ', 'cur last selectolderversionfordiff selectnewerversionfordiff minoreditletter' ) as $msg ) {
225 $message[$msg] = wfMsg( $msg );
226 }
227 }
228
229 $link = $this->revLink( $row );
230
231 if ( 0 == $row->rev_user ) {
232 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
233 $ul = $this->mSkin->makeKnownLinkObj( $contribsPage,
234 htmlspecialchars( $row->rev_user_text ),
235 'target=' . urlencode( $row->rev_user_text ) );
236 } else {
237 $userPage =& Title::makeTitle( NS_USER, $row->rev_user_text );
238 $ul = $this->mSkin->makeLinkObj( $userPage , htmlspecialchars( $row->rev_user_text ) );
239 }
240
241 $s = '<li>';
242 if( $row->rev_deleted ) {
243 $s .= '<span class="deleted">';
244 }
245 $curlink = $this->curLink( $row, $latest );
246 $lastlink = $this->lastLink( $row, $next, $counter );
247 $arbitrary = $this->diffButtons( $row, $latest, $counter );
248 $s .= "({$curlink}) ({$lastlink}) $arbitrary {$link} <span class='user'>{$ul}</span>";
249
250 if( $row->rev_minor_edit ) {
251 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), $message['minoreditletter'] );
252 }
253
254 $s .= $this->mSkin->commentBlock( $row->rev_comment, $this->mTitle );
255 if ($this->getNotificationTimestamp() && ($row->rev_timestamp >= $this->getNotificationTimestamp())) {
256 $s .= wfMsg( 'updatedmarker' );
257 }
258 if( $row->rev_deleted ) {
259 $s .= "</span> " . htmlspecialchars( wfMsg( 'deletedrev' ) );
260 }
261 $s .= '</li>';
262
263 return $s;
264 }
265
266 /** @todo document */
267 function revLink( $row ) {
268 global $wgUser, $wgLang;
269 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true );
270 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
271 return $date;
272 } else {
273 return $this->mSkin->makeKnownLinkObj(
274 $this->mTitle,
275 $date,
276 'oldid='.$row->rev_id );
277 }
278 }
279
280 /** @todo document */
281 function curLink( $row, $latest ) {
282 global $wgUser;
283 $cur = htmlspecialchars( wfMsg( 'cur' ) );
284 if( $latest
285 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
286 return $cur;
287 } else {
288 return $this->mSkin->makeKnownLinkObj(
289 $this->mTitle,
290 $cur,
291 'diff=' . $this->getLatestID($this->mTitle->getArticleID())
292 . '&oldid=' . $row->rev_id );
293 }
294 }
295
296 /** @todo document */
297 function lastLink( $row, $next, $counter ) {
298 global $wgUser;
299 $last = htmlspecialchars( wfMsg( 'last' ) );
300 if( is_null( $next )
301 || ( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) ) {
302 return $last;
303 } else {
304 return $this->mSkin->makeKnownLinkObj(
305 $this->mTitle,
306 $last,
307 "diff={$row->rev_id}&oldid={$next->rev_id}",
308 '',
309 '',
310 ' tabindex="'.$counter.'"' );
311 }
312 }
313
314 /** @todo document */
315 function diffButtons( $row, $latest, $counter ) {
316 global $wgUser;
317 if( $this->linesonpage > 1) {
318 $radio = array(
319 'type' => 'radio',
320 'value' => $row->rev_id,
321 'title' => wfMsg( 'selectolderversionfordiff' )
322 );
323 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
324 $radio['disabled'] = 'disabled';
325 }
326
327 # XXX: move title texts to javascript
328 if ( $latest ) {
329 $first = wfElement( 'input', array_merge(
330 $radio,
331 array(
332 'style' => 'visibility:hidden',
333 'name' => 'oldid' ) ) );
334 $checkmark = array( 'checked' => 'checked' );
335 } else {
336 if( $counter == 2 ) {
337 $checkmark = array( 'checked' => 'checked' );
338 } else {
339 $checkmark = array();
340 }
341 $first = wfElement( 'input', array_merge(
342 $radio,
343 $checkmark,
344 array( 'name' => 'oldid' ) ) );
345 $checkmark = array();
346 }
347 $second = wfElement( 'input', array_merge(
348 $radio,
349 $checkmark,
350 array( 'name' => 'diff' ) ) );
351 return $first . $second;
352 } else {
353 return '';
354 }
355 }
356
357 /** @todo document */
358 function getLatestOffset($id) {
359 return $this->getExtremeOffset( $id, 'max' );
360 }
361
362 /** @todo document */
363 function getEarliestOffset($id) {
364 return $this->getExtremeOffset( $id, 'min' );
365 }
366
367 /** @todo document */
368 function getExtremeOffset( $id, $func ) {
369 $db =& wfGetDB(DB_SLAVE);
370 return $db->selectField( 'revision',
371 "$func(rev_timestamp)",
372 array( 'rev_page' => $id ),
373 'PageHistory::getExtremeOffset' );
374 }
375
376 /** @todo document */
377 function getLatestID( $id ) {
378 $db =& wfGetDB(DB_SLAVE);
379 return $db->selectField( 'revision',
380 "max(rev_id)",
381 array( 'rev_page' => $id ),
382 'PageHistory::getLatestID' );
383 }
384
385 /** @todo document */
386 function getLastOffsetForPaging( $id, $step = 50 ) {
387 $db =& wfGetDB(DB_SLAVE);
388 $revision = $db->tableName( 'revision' );
389 $sql = "SELECT rev_timestamp FROM $revision WHERE rev_page = $id " .
390 "ORDER BY rev_timestamp ASC";
391 $sql = $db->limitResult($sql, $step, 0);
392 $res = $db->query( $sql, "PageHistory::getLastOffsetForPaging" );
393 $n = $db->numRows( $res );
394
395 $last = null;
396 while( $obj = $db->fetchObject( $res ) ) {
397 $last = $obj->rev_timestamp;
398 }
399 $db->freeResult( $res );
400 return $last;
401 }
402
403 /** @todo document */
404 function getDirection() {
405 global $wgRequest;
406
407 if ($wgRequest->getText("dir") == "prev")
408 return DIR_PREV;
409 else
410 return DIR_NEXT;
411 }
412
413 /** @todo document */
414 function fetchRevisions($limit, $offset, $direction) {
415 global $wgUser, $wgShowUpdatedMarker;
416
417 /* Check one extra row to see whether we need to show 'next' and diff links */
418 $limitplus = $limit + 1;
419
420 $namespace = $this->mTitle->getNamespace();
421 $title = $this->mTitle->getText();
422 $uid = $wgUser->getID();
423 $db =& wfGetDB( DB_SLAVE );
424
425 $use_index = $db->useIndexClause( 'page_timestamp' );
426 $revision = $db->tableName( 'revision' );
427
428 $limits = $offsets = "";
429
430 if ($direction == DIR_PREV)
431 list($dirs, $oper) = array("ASC", ">=");
432 else /* $direction = DIR_NEXT */
433 list($dirs, $oper) = array("DESC", "<=");
434
435 if ($offset)
436 $offsets .= " AND rev_timestamp $oper '$offset' ";
437
438 $page_id = $this->mTitle->getArticleID();
439
440 $sql = "SELECT rev_id,rev_user," .
441 "rev_comment,rev_user_text,rev_timestamp,rev_minor_edit,rev_deleted ".
442 "FROM $revision $use_index " .
443 "WHERE rev_page=$page_id " .
444 $offsets .
445 "ORDER BY rev_timestamp $dirs ";
446 if ($limit)
447 $sql = $db->limitResult($sql, $limitplus, 0);
448 $res = $db->query($sql, "PageHistory::fetchRevisions");
449
450 $result = array();
451 while (($obj = $db->fetchObject($res)) != NULL)
452 $result[] = $obj;
453
454 return $result;
455 }
456
457 /** @todo document */
458 function getNotificationTimestamp() {
459 global $wgUser, $wgShowUpdatedMarker;
460
461 if ($this->mNotificationTimestamp !== NULL)
462 return $this->mNotificationTimestamp;
463
464 if ($wgUser->getID() == 0 || !$wgShowUpdatedMarker)
465 return $this->mNotificationTimestamp = false;
466
467 $db =& wfGetDB(DB_SLAVE);
468
469 $this->mNotificationTimestamp = $db->selectField(
470 'watchlist',
471 'wl_notificationtimestamp',
472 array( 'wl_namespace' => $this->mTitle->getNamespace(),
473 'wl_title' => $this->mTitle->getDBkey(),
474 'wl_user' => $wgUser->getID()
475 ),
476 "PageHistory::getNotficationTimestamp");
477
478 return $this->mNotificationTimestamp;
479 }
480
481 /** @todo document */
482 function makeNavbar($revisions, $offset, $limit, $direction) {
483 global $wgTitle, $wgLang;
484
485 $revisions = array_slice($revisions, 0, $limit);
486
487 $pageid = $this->mTitle->getArticleID();
488 $latestTimestamp = wfTimestamp(TS_MW, $this->getLatestOffset( $pageid ));
489 $earliestTimestamp = wfTimestamp(TS_MW, $this->getEarliestOffset( $pageid ));
490
491 /*
492 * When we're displaying previous revisions, we need to reverse
493 * the array, because it's queried in reverse order.
494 */
495 if ($direction == DIR_PREV)
496 $revisions = array_reverse($revisions);
497
498 /*
499 * lowts is the timestamp of the first revision on this page.
500 * hights is the timestamp of the last revision.
501 */
502
503 $lowts = $hights = 0;
504
505 if( count( $revisions ) ) {
506 $latestShown = wfTimestamp(TS_MW, $revisions[0]->rev_timestamp);
507 $earliestShown = wfTimestamp(TS_MW, $revisions[count($revisions) - 1]->rev_timestamp);
508 }
509
510 $firsturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}&go=first");
511 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
512 $firsttext = wfMsgHtml('histfirst');
513 $lasttext = wfMsgHtml('histlast');
514
515 $prevurl = $wgTitle->escapeLocalURL("action=history&dir=prev&offset=$latestShown&limit={$limit}");
516 $nexturl = $wgTitle->escapeLocalURL("action=history&offset=$earliestShown&limit={$limit}");
517
518 $urls = array();
519 foreach (array(20, 50, 100, 250, 500) as $num) {
520 $urls[] = "<a href=\"".$wgTitle->escapeLocalURL(
521 "action=history&offset=" . wfTimestamp(TS_MW, $offset) .
522 "&limit={$num}")."\">".$wgLang->formatNum($num)."</a>";
523 }
524
525 $bits = implode($urls, ' | ');
526
527 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
528 if( $latestShown < $latestTimestamp ) {
529 $prevtext = "<a href=\"$prevurl\">".wfMsgHtml("prevn", $limit)."</a>";
530 $lasttext = "<a href=\"$lasturl\">$lasttext</a>";
531 } else {
532 $prevtext = wfMsgHtml("prevn", $limit);
533 }
534
535 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
536 if( $earliestShown > $earliestTimestamp ) {
537 $nexttext = "<a href=\"$nexturl\">".wfMsgHtml("nextn", $limit)."</a>";
538 $firsttext = "<a href=\"$firsturl\">$firsttext</a>";
539 } else {
540 $nexttext = wfMsgHtml("nextn", $limit);
541 }
542
543 $firstlast = "($lasttext | $firsttext)";
544
545 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
546 }
547 }
548
549 ?>