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