* (bug 11993) Remove contentsub "revision history", it is useless because it is alrea...
[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 */
7
8 /**
9 * This class handles printing the history page for an article. In order to
10 * be efficient, it uses timestamps rather than offsets for paging, to avoid
11 * costly LIMIT,offset queries.
12 *
13 * Construct it by passing in an Article, and call $h->history() to print the
14 * history.
15 *
16 */
17 class PageHistory {
18 const DIR_PREV = 0;
19 const DIR_NEXT = 1;
20
21 var $mArticle, $mTitle, $mSkin;
22 var $lastdate;
23 var $linesonpage;
24 var $mNotificationTimestamp;
25 var $mLatestId = null;
26
27 /**
28 * Construct a new PageHistory.
29 *
30 * @param Article $article
31 * @returns nothing
32 */
33 function __construct($article) {
34 global $wgUser;
35
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38 $this->mNotificationTimestamp = NULL;
39 $this->mSkin = $wgUser->getSkin();
40 }
41
42 /**
43 * Print the history page for an article.
44 *
45 * @returns nothing
46 */
47 function history() {
48 global $wgOut, $wgRequest, $wgTitle;
49
50 /*
51 * Allow client caching.
52 */
53
54 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
55 /* Client cache fresh and headers sent, nothing more to do. */
56 return;
57
58 $fname = 'PageHistory::history';
59 wfProfileIn( $fname );
60
61 /*
62 * Setup page variables.
63 */
64 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle->getPrefixedText() ) );
65 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
66 $wgOut->setArticleFlag( false );
67 $wgOut->setArticleRelated( true );
68 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 $wgOut->setSyndicated( true );
70
71 $logPage = SpecialPage::getTitleFor( 'Log' );
72 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle->getPrefixedUrl() );
73 $wgOut->setSubtitle( $logLink );
74
75 $feedType = $wgRequest->getVal( 'feed' );
76 if( $feedType ) {
77 wfProfileOut( $fname );
78 return $this->feed( $feedType );
79 }
80
81 /*
82 * Fail if article doesn't exist.
83 */
84 if( !$this->mTitle->exists() ) {
85 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
86 wfProfileOut( $fname );
87 return;
88 }
89
90 /*
91 * "go=first" means to jump to the last (earliest) history page.
92 * This is deprecated, it no longer appears in the user interface
93 */
94 if ( $wgRequest->getText("go") == 'first' ) {
95 $limit = $wgRequest->getInt( 'limit', 50 );
96 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
97 return;
98 }
99
100 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle ) );
101
102 /**
103 * Do the list
104 */
105 $pager = new PageHistoryPager( $this );
106 $this->linesonpage = $pager->getNumRows();
107 $wgOut->addHTML(
108 $pager->getNavigationBar() .
109 $this->beginHistoryList() .
110 $pager->getBody() .
111 $this->endHistoryList() .
112 $pager->getNavigationBar()
113 );
114 wfProfileOut( $fname );
115 }
116
117 /** @todo document */
118 function beginHistoryList() {
119 global $wgTitle;
120 $this->lastdate = '';
121 $s = wfMsgExt( 'histlegend', array( 'parse') );
122 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
123 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
124
125 // The following line is SUPPOSED to have double-quotes around the
126 // $prefixedkey variable, because htmlspecialchars() doesn't escape
127 // single-quotes.
128 //
129 // On at least two occasions people have changed it to single-quotes,
130 // which creates invalid HTML and incorrect display of the resulting
131 // link.
132 //
133 // Please do not break this a third time. Thank you for your kind
134 // consideration and cooperation.
135 //
136 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
137
138 $s .= $this->submitButton();
139 $s .= '<ul id="pagehistory">' . "\n";
140 return $s;
141 }
142
143 /** @todo document */
144 function endHistoryList() {
145 $s = '</ul>';
146 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
147 $s .= '</form>';
148 return $s;
149 }
150
151 /** @todo document */
152 function submitButton( $bits = array() ) {
153 return ( $this->linesonpage > 0 )
154 ? wfElement( 'input', array_merge( $bits,
155 array(
156 'class' => 'historysubmit',
157 'type' => 'submit',
158 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
159 'title' => wfMsg( 'tooltip-compareselectedversions' ).' ['.wfMsg( 'accesskey-compareselectedversions' ).']',
160 'value' => wfMsg( 'compareselectedversions' ),
161 ) ) )
162 : '';
163 }
164
165 /**
166 * Returns a row from the history printout.
167 *
168 * @todo document some more, and maybe clean up the code (some params redundant?)
169 *
170 * @param object $row The database row corresponding to the line (or is it the previous line?).
171 * @param object $next The database row corresponding to the next line (or is it this one?).
172 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
173 * @param $notificationtimestamp
174 * @param bool $latest Whether this row corresponds to the page's latest revision.
175 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
176 * @return string HTML output for the row
177 */
178 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
179 global $wgUser, $wgLang;
180 $rev = new Revision( $row );
181 $rev->setTitle( $this->mTitle );
182
183 $s = '<li>';
184 $curlink = $this->curLink( $rev, $latest );
185 $lastlink = $this->lastLink( $rev, $next, $counter );
186 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
187 $link = $this->revLink( $rev );
188
189 $user = $this->mSkin->userLink( $rev->getUser(), $rev->getUserText() )
190 . $this->mSkin->userToolLinks( $rev->getUser(), $rev->getUserText() );
191
192 $s .= "($curlink) ($lastlink) $arbitrary";
193
194 if( $wgUser->isAllowed( 'deleterevision' ) ) {
195 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
196 if( $firstInList ) {
197 // We don't currently handle well changing the top revision's settings
198 $del = wfMsgHtml( 'rev-delundel' );
199 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
200 // If revision was hidden from sysops
201 $del = wfMsgHtml( 'rev-delundel' );
202 } else {
203 $del = $this->mSkin->makeKnownLinkObj( $revdel,
204 wfMsg( 'rev-delundel' ),
205 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
206 '&oldid=' . urlencode( $rev->getId() ) );
207 }
208 $s .= " (<small>$del</small>) ";
209 }
210
211 $s .= " $link";
212 #getUser is safe, but this avoids making the invalid untargeted contribs links
213 if( $row->rev_deleted & Revision::DELETED_USER ) {
214 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
215 }
216 $s .= " <span class='history-user'>$user</span>";
217
218 if( $row->rev_minor_edit ) {
219 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
220 }
221
222 if ( !is_null( $size = $rev->getSize() ) ) {
223 if ( $size == 0 )
224 $stxt = wfMsgHtml( 'historyempty' );
225 else
226 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
227 $s .= " <span class=\"history-size\">$stxt</span>";
228 }
229
230 #getComment is safe, but this is better formatted
231 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
232 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
233 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
234 } else {
235 $s .= $this->mSkin->revComment( $rev );
236 }
237
238 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
239 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
240 }
241 #add blurb about text having been deleted
242 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
243 $s .= ' ' . wfMsgHtml( 'deletedrev' );
244 }
245
246 $tools = array();
247
248 if ( !is_null( $next ) && is_object( $next ) ) {
249 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
250 $tools[] = '<span class="mw-rollback-link">'
251 . $this->mSkin->buildRollbackLink( $rev )
252 . '</span>';
253 }
254
255 $undolink = $this->mSkin->makeKnownLinkObj(
256 $this->mTitle,
257 wfMsgHtml( 'editundo' ),
258 'action=edit&undoafter=' . $next->rev_id . '&undo=' . $rev->getId()
259 );
260 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
261 }
262
263 if( $tools ) {
264 $s .= ' (' . implode( ' | ', $tools ) . ')';
265 }
266
267 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
268
269 $s .= "</li>\n";
270
271 return $s;
272 }
273
274 /** @todo document */
275 function revLink( $rev ) {
276 global $wgLang;
277 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
278 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
279 $link = $this->mSkin->makeKnownLinkObj(
280 $this->mTitle, $date, "oldid=" . $rev->getId() );
281 } else {
282 $link = $date;
283 }
284 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
285 return '<span class="history-deleted">' . $link . '</span>';
286 }
287 return $link;
288 }
289
290 /** @todo document */
291 function curLink( $rev, $latest ) {
292 $cur = wfMsgExt( 'cur', array( 'escape') );
293 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
294 return $cur;
295 } else {
296 return $this->mSkin->makeKnownLinkObj(
297 $this->mTitle, $cur,
298 'diff=' . $this->getLatestID() .
299 "&oldid=" . $rev->getId() );
300 }
301 }
302
303 /** @todo document */
304 function lastLink( $rev, $next, $counter ) {
305 $last = wfMsgExt( 'last', array( 'escape' ) );
306 if ( is_null( $next ) ) {
307 # Probably no next row
308 return $last;
309 } elseif ( $next === 'unknown' ) {
310 # Next row probably exists but is unknown, use an oldid=prev link
311 return $this->mSkin->makeKnownLinkObj(
312 $this->mTitle,
313 $last,
314 "diff=" . $rev->getId() . "&oldid=prev" );
315 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
316 return $last;
317 } else {
318 return $this->mSkin->makeKnownLinkObj(
319 $this->mTitle,
320 $last,
321 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
322 /*,
323 '',
324 '',
325 "tabindex={$counter}"*/ );
326 }
327 }
328
329 /** @todo document */
330 function diffButtons( $rev, $firstInList, $counter ) {
331 if( $this->linesonpage > 1) {
332 $radio = array(
333 'type' => 'radio',
334 'value' => $rev->getId(),
335 # do we really need to flood this on every item?
336 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
337 );
338
339 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
340 $radio['disabled'] = 'disabled';
341 }
342
343 /** @todo: move title texts to javascript */
344 if ( $firstInList ) {
345 $first = wfElement( 'input', array_merge(
346 $radio,
347 array(
348 'style' => 'visibility:hidden',
349 'name' => 'oldid' ) ) );
350 $checkmark = array( 'checked' => 'checked' );
351 } else {
352 if( $counter == 2 ) {
353 $checkmark = array( 'checked' => 'checked' );
354 } else {
355 $checkmark = array();
356 }
357 $first = wfElement( 'input', array_merge(
358 $radio,
359 $checkmark,
360 array( 'name' => 'oldid' ) ) );
361 $checkmark = array();
362 }
363 $second = wfElement( 'input', array_merge(
364 $radio,
365 $checkmark,
366 array( 'name' => 'diff' ) ) );
367 return $first . $second;
368 } else {
369 return '';
370 }
371 }
372
373 /** @todo document */
374 function getLatestId() {
375 if( is_null( $this->mLatestId ) ) {
376 $id = $this->mTitle->getArticleID();
377 $db = wfGetDB(DB_SLAVE);
378 $this->mLatestId = $db->selectField( 'page',
379 "page_latest",
380 array( 'page_id' => $id ),
381 'PageHistory::getLatestID' );
382 }
383 return $this->mLatestId;
384 }
385
386 /**
387 * Fetch an array of revisions, specified by a given limit, offset and
388 * direction. This is now only used by the feeds. It was previously
389 * used by the main UI but that's now handled by the pager.
390 */
391 function fetchRevisions($limit, $offset, $direction) {
392 $fname = 'PageHistory::fetchRevisions';
393
394 $dbr = wfGetDB( DB_SLAVE );
395
396 if ($direction == PageHistory::DIR_PREV)
397 list($dirs, $oper) = array("ASC", ">=");
398 else /* $direction == PageHistory::DIR_NEXT */
399 list($dirs, $oper) = array("DESC", "<=");
400
401 if ($offset)
402 $offsets = array("rev_timestamp $oper '$offset'");
403 else
404 $offsets = array();
405
406 $page_id = $this->mTitle->getArticleID();
407
408 $res = $dbr->select(
409 'revision',
410 Revision::selectFields(),
411 array_merge(array("rev_page=$page_id"), $offsets),
412 $fname,
413 array('ORDER BY' => "rev_timestamp $dirs",
414 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
415 );
416
417 $result = array();
418 while (($obj = $dbr->fetchObject($res)) != NULL)
419 $result[] = $obj;
420
421 return $result;
422 }
423
424 /** @todo document */
425 function getNotificationTimestamp() {
426 global $wgUser, $wgShowUpdatedMarker;
427 $fname = 'PageHistory::getNotficationTimestamp';
428
429 if ($this->mNotificationTimestamp !== NULL)
430 return $this->mNotificationTimestamp;
431
432 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
433 return $this->mNotificationTimestamp = false;
434
435 $dbr = wfGetDB(DB_SLAVE);
436
437 $this->mNotificationTimestamp = $dbr->selectField(
438 'watchlist',
439 'wl_notificationtimestamp',
440 array( 'wl_namespace' => $this->mTitle->getNamespace(),
441 'wl_title' => $this->mTitle->getDBkey(),
442 'wl_user' => $wgUser->getID()
443 ),
444 $fname);
445
446 // Don't use the special value reserved for telling whether the field is filled
447 if ( is_null( $this->mNotificationTimestamp ) ) {
448 $this->mNotificationTimestamp = false;
449 }
450
451 return $this->mNotificationTimestamp;
452 }
453
454 /**
455 * Output a subscription feed listing recent edits to this page.
456 * @param string $type
457 */
458 function feed( $type ) {
459 require_once 'SpecialRecentchanges.php';
460
461 global $wgFeedClasses;
462 if( !isset( $wgFeedClasses[$type] ) ) {
463 global $wgOut;
464 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
465 return;
466 }
467
468 $feed = new $wgFeedClasses[$type](
469 $this->mTitle->getPrefixedText() . ' - ' .
470 wfMsgForContent( 'history-feed-title' ),
471 wfMsgForContent( 'history-feed-description' ),
472 $this->mTitle->getFullUrl( 'action=history' ) );
473
474 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
475 $feed->outHeader();
476 if( $items ) {
477 foreach( $items as $row ) {
478 $feed->outItem( $this->feedItem( $row ) );
479 }
480 } else {
481 $feed->outItem( $this->feedEmpty() );
482 }
483 $feed->outFooter();
484 }
485
486 function feedEmpty() {
487 global $wgOut;
488 return new FeedItem(
489 wfMsgForContent( 'nohistory' ),
490 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
491 $this->mTitle->getFullUrl(),
492 wfTimestamp( TS_MW ),
493 '',
494 $this->mTitle->getTalkPage()->getFullUrl() );
495 }
496
497 /**
498 * Generate a FeedItem object from a given revision table row
499 * Borrows Recent Changes' feed generation functions for formatting;
500 * includes a diff to the previous revision (if any).
501 *
502 * @param $row
503 * @return FeedItem
504 */
505 function feedItem( $row ) {
506 $rev = new Revision( $row );
507 $rev->setTitle( $this->mTitle );
508 $text = rcFormatDiffRow( $this->mTitle,
509 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
510 $rev->getId(),
511 $rev->getTimestamp(),
512 $rev->getComment() );
513
514 if( $rev->getComment() == '' ) {
515 global $wgContLang;
516 $title = wfMsgForContent( 'history-feed-item-nocomment',
517 $rev->getUserText(),
518 $wgContLang->timeanddate( $rev->getTimestamp() ) );
519 } else {
520 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
521 }
522
523 return new FeedItem(
524 $title,
525 $text,
526 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
527 $rev->getTimestamp(),
528 $rev->getUserText(),
529 $this->mTitle->getTalkPage()->getFullUrl() );
530 }
531
532 /**
533 * Quickie hack... strip out wikilinks to more legible form from the comment.
534 */
535 function stripComment( $text ) {
536 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
537 }
538 }
539
540
541 /**
542 * @addtogroup Pager
543 */
544 class PageHistoryPager extends ReverseChronologicalPager {
545 public $mLastRow = false, $mPageHistory;
546
547 function __construct( $pageHistory ) {
548 parent::__construct();
549 $this->mPageHistory = $pageHistory;
550 }
551
552 function getQueryInfo() {
553 return array(
554 'tables' => 'revision',
555 'fields' => Revision::selectFields(),
556 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
557 'options' => array( 'USE INDEX' => 'page_timestamp' )
558 );
559 }
560
561 function getIndexField() {
562 return 'rev_timestamp';
563 }
564
565 function formatRow( $row ) {
566 if ( $this->mLastRow ) {
567 $latest = $this->mCounter == 1 && $this->mOffset == '';
568 $firstInList = $this->mCounter == 1;
569 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
570 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
571 } else {
572 $s = '';
573 }
574 $this->mLastRow = $row;
575 return $s;
576 }
577
578 function getStartBody() {
579 $this->mLastRow = false;
580 $this->mCounter = 1;
581 return '';
582 }
583
584 function getEndBody() {
585 if ( $this->mLastRow ) {
586 $latest = $this->mCounter == 1 && $this->mOffset == 0;
587 $firstInList = $this->mCounter == 1;
588 if ( $this->mIsBackwards ) {
589 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
590 if ( $this->mOffset == '' ) {
591 $next = null;
592 } else {
593 $next = 'unknown';
594 }
595 } else {
596 # The next row is the past-the-end row
597 $next = $this->mPastTheEndRow;
598 }
599 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
600 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
601 } else {
602 $s = '';
603 }
604 return $s;
605 }
606 }