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