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