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