* (bugs 5051, 5376) All tooltips and accesskeys moved out of Monobook.js and into...
[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 = Title::makeTitle( NS_SPECIAL, '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' ).' ['.wfMsg( 'accesskey-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;
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 = Title::makeTitle( NS_SPECIAL, '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 $s .= "</li>\n";
214
215 return $s;
216 }
217
218 /** @todo document */
219 function revLink( $rev ) {
220 global $wgLang;
221 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
222 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
223 $link = $this->mSkin->makeKnownLinkObj(
224 $this->mTitle, $date, "oldid=" . $rev->getId() );
225 } else {
226 $link = $date;
227 }
228 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
229 return '<span class="history-deleted">' . $link . '</span>';
230 }
231 return $link;
232 }
233
234 /** @todo document */
235 function curLink( $rev, $latest ) {
236 $cur = wfMsgExt( 'cur', array( 'escape') );
237 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
238 return $cur;
239 } else {
240 return $this->mSkin->makeKnownLinkObj(
241 $this->mTitle, $cur,
242 'diff=' . $this->getLatestID() .
243 "&oldid=" . $rev->getId() );
244 }
245 }
246
247 /** @todo document */
248 function lastLink( $rev, $next, $counter ) {
249 $last = wfMsgExt( 'last', array( 'escape' ) );
250 if ( is_null( $next ) ) {
251 # Probably no next row
252 return $last;
253 } elseif ( $next === 'unknown' ) {
254 # Next row probably exists but is unknown, use an oldid=prev link
255 return $this->mSkin->makeKnownLinkObj(
256 $this->mTitle,
257 $last,
258 "diff=" . $rev->getId() . "&oldid=prev" );
259 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
260 return $last;
261 } else {
262 return $this->mSkin->makeKnownLinkObj(
263 $this->mTitle,
264 $last,
265 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
266 /*,
267 '',
268 '',
269 "tabindex={$counter}"*/ );
270 }
271 }
272
273 /** @todo document */
274 function diffButtons( $rev, $firstInList, $counter ) {
275 if( $this->linesonpage > 1) {
276 $radio = array(
277 'type' => 'radio',
278 'value' => $rev->getId(),
279 # do we really need to flood this on every item?
280 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
281 );
282
283 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
284 $radio['disabled'] = 'disabled';
285 }
286
287 /** @todo: move title texts to javascript */
288 if ( $firstInList ) {
289 $first = wfElement( 'input', array_merge(
290 $radio,
291 array(
292 'style' => 'visibility:hidden',
293 'name' => 'oldid' ) ) );
294 $checkmark = array( 'checked' => 'checked' );
295 } else {
296 if( $counter == 2 ) {
297 $checkmark = array( 'checked' => 'checked' );
298 } else {
299 $checkmark = array();
300 }
301 $first = wfElement( 'input', array_merge(
302 $radio,
303 $checkmark,
304 array( 'name' => 'oldid' ) ) );
305 $checkmark = array();
306 }
307 $second = wfElement( 'input', array_merge(
308 $radio,
309 $checkmark,
310 array( 'name' => 'diff' ) ) );
311 return $first . $second;
312 } else {
313 return '';
314 }
315 }
316
317 /** @todo document */
318 function getLatestId() {
319 if( is_null( $this->mLatestId ) ) {
320 $id = $this->mTitle->getArticleID();
321 $db =& wfGetDB(DB_SLAVE);
322 $this->mLatestId = $db->selectField( 'page',
323 "page_latest",
324 array( 'page_id' => $id ),
325 'PageHistory::getLatestID' );
326 }
327 return $this->mLatestId;
328 }
329
330 /**
331 * Fetch an array of revisions, specified by a given limit, offset and
332 * direction. This is now only used by the feeds. It was previously
333 * used by the main UI but that's now handled by the pager.
334 */
335 function fetchRevisions($limit, $offset, $direction) {
336 $fname = 'PageHistory::fetchRevisions';
337
338 $dbr =& wfGetDB( DB_SLAVE );
339
340 if ($direction == PageHistory::DIR_PREV)
341 list($dirs, $oper) = array("ASC", ">=");
342 else /* $direction == PageHistory::DIR_NEXT */
343 list($dirs, $oper) = array("DESC", "<=");
344
345 if ($offset)
346 $offsets = array("rev_timestamp $oper '$offset'");
347 else
348 $offsets = array();
349
350 $page_id = $this->mTitle->getArticleID();
351
352 $res = $dbr->select(
353 'revision',
354 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
355 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
356 array_merge(array("rev_page=$page_id"), $offsets),
357 $fname,
358 array('ORDER BY' => "rev_timestamp $dirs",
359 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
360 );
361
362 $result = array();
363 while (($obj = $dbr->fetchObject($res)) != NULL)
364 $result[] = $obj;
365
366 return $result;
367 }
368
369 /** @todo document */
370 function getNotificationTimestamp() {
371 global $wgUser, $wgShowUpdatedMarker;
372 $fname = 'PageHistory::getNotficationTimestamp';
373
374 if ($this->mNotificationTimestamp !== NULL)
375 return $this->mNotificationTimestamp;
376
377 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
378 return $this->mNotificationTimestamp = false;
379
380 $dbr =& wfGetDB(DB_SLAVE);
381
382 $this->mNotificationTimestamp = $dbr->selectField(
383 'watchlist',
384 'wl_notificationtimestamp',
385 array( 'wl_namespace' => $this->mTitle->getNamespace(),
386 'wl_title' => $this->mTitle->getDBkey(),
387 'wl_user' => $wgUser->getID()
388 ),
389 $fname);
390
391 // Don't use the special value reserved for telling whether the field is filled
392 if ( is_null( $this->mNotificationTimestamp ) ) {
393 $this->mNotificationTimestamp = false;
394 }
395
396 return $this->mNotificationTimestamp;
397 }
398
399 /**
400 * Output a subscription feed listing recent edits to this page.
401 * @param string $type
402 */
403 function feed( $type ) {
404 require_once 'SpecialRecentchanges.php';
405
406 global $wgFeedClasses;
407 if( !isset( $wgFeedClasses[$type] ) ) {
408 global $wgOut;
409 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
410 return;
411 }
412
413 $feed = new $wgFeedClasses[$type](
414 $this->mTitle->getPrefixedText() . ' - ' .
415 wfMsgForContent( 'history-feed-title' ),
416 wfMsgForContent( 'history-feed-description' ),
417 $this->mTitle->getFullUrl( 'action=history' ) );
418
419 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
420 $feed->outHeader();
421 if( $items ) {
422 foreach( $items as $row ) {
423 $feed->outItem( $this->feedItem( $row ) );
424 }
425 } else {
426 $feed->outItem( $this->feedEmpty() );
427 }
428 $feed->outFooter();
429 }
430
431 function feedEmpty() {
432 global $wgOut;
433 return new FeedItem(
434 wfMsgForContent( 'nohistory' ),
435 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
436 $this->mTitle->getFullUrl(),
437 wfTimestamp( TS_MW ),
438 '',
439 $this->mTitle->getTalkPage()->getFullUrl() );
440 }
441
442 /**
443 * Generate a FeedItem object from a given revision table row
444 * Borrows Recent Changes' feed generation functions for formatting;
445 * includes a diff to the previous revision (if any).
446 *
447 * @param $row
448 * @return FeedItem
449 */
450 function feedItem( $row ) {
451 $rev = new Revision( $row );
452 $rev->setTitle( $this->mTitle );
453 $text = rcFormatDiffRow( $this->mTitle,
454 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
455 $rev->getId(),
456 $rev->getTimestamp(),
457 $rev->getComment() );
458
459 if( $rev->getComment() == '' ) {
460 global $wgContLang;
461 $title = wfMsgForContent( 'history-feed-item-nocomment',
462 $rev->getUserText(),
463 $wgContLang->timeanddate( $rev->getTimestamp() ) );
464 } else {
465 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
466 }
467
468 return new FeedItem(
469 $title,
470 $text,
471 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
472 $rev->getTimestamp(),
473 $rev->getUserText(),
474 $this->mTitle->getTalkPage()->getFullUrl() );
475 }
476
477 /**
478 * Quickie hack... strip out wikilinks to more legible form from the comment.
479 */
480 function stripComment( $text ) {
481 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
482 }
483 }
484
485
486 class PageHistoryPager extends ReverseChronologicalPager {
487 public $mLastRow = false, $mPageHistory;
488
489 function __construct( $pageHistory ) {
490 parent::__construct();
491 $this->mPageHistory = $pageHistory;
492 }
493
494 function getQueryInfo() {
495 return array(
496 'tables' => 'revision',
497 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
498 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
499 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
500 'options' => array( 'USE INDEX' => 'page_timestamp' )
501 );
502 }
503
504 function getIndexField() {
505 return 'rev_timestamp';
506 }
507
508 function formatRow( $row ) {
509 if ( $this->mLastRow ) {
510 $latest = $this->mCounter == 1 && $this->mOffset == '';
511 $firstInList = $this->mCounter == 1;
512 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
513 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
514 } else {
515 $s = '';
516 }
517 $this->mLastRow = $row;
518 return $s;
519 }
520
521 function getStartBody() {
522 $this->mLastRow = false;
523 $this->mCounter = 1;
524 return '';
525 }
526
527 function getEndBody() {
528 if ( $this->mLastRow ) {
529 $latest = $this->mCounter == 1 && $this->mOffset == 0;
530 $firstInList = $this->mCounter == 1;
531 if ( $this->mIsBackwards ) {
532 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
533 if ( $this->mOffset == '' ) {
534 $next = null;
535 } else {
536 $next = 'unknown';
537 }
538 } else {
539 # The next row is the past-the-end row
540 $next = $this->mPastTheEndRow;
541 }
542 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
543 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
544 } else {
545 $s = '';
546 }
547 return $s;
548 }
549 }
550
551 ?>