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