(bug 20966) Use the name of the <button> element to determine which button was clicke...
[lhc/web/wiklou.git] / includes / HistoryPage.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @file
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 */
18 class HistoryPage {
19 const DIR_PREV = 0;
20 const DIR_NEXT = 1;
21
22 var $article, $title, $skin;
23
24 /**
25 * Construct a new HistoryPage.
26 *
27 * @param Article $article
28 * @returns nothing
29 */
30 function __construct( $article ) {
31 global $wgUser;
32 $this->article = $article;
33 $this->title = $article->getTitle();
34 $this->skin = $wgUser->getSkin();
35 $this->preCacheMessages();
36 }
37
38 function getArticle() {
39 return $this->article;
40 }
41
42 function getTitle() {
43 return $this->title;
44 }
45
46 /**
47 * As we use the same small set of messages in various methods and that
48 * they are called often, we call them once and save them in $this->message
49 */
50 function preCacheMessages() {
51 // Precache various messages
52 if( !isset( $this->message ) ) {
53 $msgs = array( 'cur', 'last', 'rev-delundel' );
54 foreach( $msgs as $msg ) {
55 $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
56 }
57 }
58 }
59
60 /**
61 * Print the history page for an article.
62 *
63 * @returns nothing
64 */
65 function history() {
66 global $wgOut, $wgRequest, $wgScript;
67
68 /*
69 * Allow client caching.
70 */
71 if( $wgOut->checkLastModified( $this->article->getTouched() ) )
72 return; // Client cache fresh and headers sent, nothing more to do.
73
74 wfProfileIn( __METHOD__ );
75
76 /*
77 * Setup page variables.
78 */
79 $wgOut->setPageTitle( wfMsg( 'history-title', $this->title->getPrefixedText() ) );
80 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
81 $wgOut->setArticleFlag( false );
82 $wgOut->setArticleRelated( true );
83 $wgOut->setRobotPolicy( 'noindex,nofollow' );
84 $wgOut->setSyndicated( true );
85 $wgOut->setFeedAppendQuery( 'action=history' );
86 $wgOut->addScriptFile( 'history.js' );
87
88 $logPage = SpecialPage::getTitleFor( 'Log' );
89 $logLink = $this->skin->link(
90 $logPage,
91 wfMsgHtml( 'viewpagelogs' ),
92 array(),
93 array( 'page' => $this->title->getPrefixedText() ),
94 array( 'known', 'noclasses' )
95 );
96 $wgOut->setSubtitle( $logLink );
97
98 $feedType = $wgRequest->getVal( 'feed' );
99 if( $feedType ) {
100 wfProfileOut( __METHOD__ );
101 return $this->feed( $feedType );
102 }
103
104 /*
105 * Fail if article doesn't exist.
106 */
107 if( !$this->title->exists() ) {
108 $wgOut->addWikiMsg( 'nohistory' );
109 # show deletion/move log if there is an entry
110 LogEventsList::showLogExtract(
111 $wgOut,
112 array( 'delete', 'move' ),
113 $this->title->getPrefixedText(),
114 '',
115 array( 'lim' => 10,
116 'conds' => array( "log_action != 'revision'" ),
117 'showIfEmpty' => false,
118 'msgKey' => array( 'moveddeleted-notice' )
119 )
120 );
121 wfProfileOut( __METHOD__ );
122 return;
123 }
124
125 /**
126 * Add date selector to quickly get to a certain time
127 */
128 $year = $wgRequest->getInt( 'year' );
129 $month = $wgRequest->getInt( 'month' );
130 $tagFilter = $wgRequest->getVal( 'tagfilter' );
131 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
132
133 $action = htmlspecialchars( $wgScript );
134 $wgOut->addHTML(
135 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
136 Xml::fieldset(
137 wfMsg( 'history-fieldset-title' ),
138 false,
139 array( 'id' => 'mw-history-search' )
140 ) .
141 Xml::hidden( 'title', $this->title->getPrefixedDBKey() ) . "\n" .
142 Xml::hidden( 'action', 'history' ) . "\n" .
143 xml::dateMenu( $year, $month ) . '&nbsp;' .
144 ( $tagSelector ? ( implode( '&nbsp;', $tagSelector ) . '&nbsp;' ) : '' ) .
145 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
146 '</fieldset></form>'
147 );
148
149 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article ) );
150
151 /**
152 * Do the list
153 */
154 $pager = new HistoryPager( $this, $year, $month, $tagFilter );
155 $wgOut->addHTML(
156 $pager->getNavigationBar() .
157 $pager->getBody() .
158 $pager->getNavigationBar()
159 );
160
161 wfProfileOut( __METHOD__ );
162 }
163
164 /**
165 * Fetch an array of revisions, specified by a given limit, offset and
166 * direction. This is now only used by the feeds. It was previously
167 * used by the main UI but that's now handled by the pager.
168 */
169 function fetchRevisions($limit, $offset, $direction) {
170 $dbr = wfGetDB( DB_SLAVE );
171
172 if( $direction == HistoryPage::DIR_PREV )
173 list($dirs, $oper) = array("ASC", ">=");
174 else /* $direction == HistoryPage::DIR_NEXT */
175 list($dirs, $oper) = array("DESC", "<=");
176
177 if( $offset )
178 $offsets = array("rev_timestamp $oper '$offset'");
179 else
180 $offsets = array();
181
182 $page_id = $this->title->getArticleID();
183
184 return $dbr->select( 'revision',
185 Revision::selectFields(),
186 array_merge(array("rev_page=$page_id"), $offsets),
187 __METHOD__,
188 array( 'ORDER BY' => "rev_timestamp $dirs",
189 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
190 );
191 }
192
193 /**
194 * Output a subscription feed listing recent edits to this page.
195 * @param string $type
196 */
197 function feed( $type ) {
198 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
199 if( !FeedUtils::checkFeedOutput($type) ) {
200 return;
201 }
202
203 $feed = new $wgFeedClasses[$type](
204 $this->title->getPrefixedText() . ' - ' .
205 wfMsgForContent( 'history-feed-title' ),
206 wfMsgForContent( 'history-feed-description' ),
207 $this->title->getFullUrl( 'action=history' )
208 );
209
210 // Get a limit on number of feed entries. Provide a sane default
211 // of 10 if none is defined (but limit to $wgFeedLimit max)
212 $limit = $wgRequest->getInt( 'limit', 10 );
213 if( $limit > $wgFeedLimit || $limit < 1 ) {
214 $limit = 10;
215 }
216 $items = $this->fetchRevisions($limit, 0, HistoryPage::DIR_NEXT);
217
218 $feed->outHeader();
219 if( $items ) {
220 foreach( $items as $row ) {
221 $feed->outItem( $this->feedItem( $row ) );
222 }
223 } else {
224 $feed->outItem( $this->feedEmpty() );
225 }
226 $feed->outFooter();
227 }
228
229 function feedEmpty() {
230 global $wgOut;
231 return new FeedItem(
232 wfMsgForContent( 'nohistory' ),
233 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
234 $this->title->getFullUrl(),
235 wfTimestamp( TS_MW ),
236 '',
237 $this->title->getTalkPage()->getFullUrl()
238 );
239 }
240
241 /**
242 * Generate a FeedItem object from a given revision table row
243 * Borrows Recent Changes' feed generation functions for formatting;
244 * includes a diff to the previous revision (if any).
245 *
246 * @param $row
247 * @return FeedItem
248 */
249 function feedItem( $row ) {
250 $rev = new Revision( $row );
251 $rev->setTitle( $this->title );
252 $text = FeedUtils::formatDiffRow(
253 $this->title,
254 $this->title->getPreviousRevisionID( $rev->getId() ),
255 $rev->getId(),
256 $rev->getTimestamp(),
257 $rev->getComment()
258 );
259 if( $rev->getComment() == '' ) {
260 global $wgContLang;
261 $title = wfMsgForContent( 'history-feed-item-nocomment',
262 $rev->getUserText(),
263 $wgContLang->timeanddate( $rev->getTimestamp() ) );
264 } else {
265 $title = $rev->getUserText() .
266 wfMsgForContent( 'colon-separator' ) .
267 FeedItem::stripComment( $rev->getComment() );
268 }
269 return new FeedItem(
270 $title,
271 $text,
272 $this->title->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
273 $rev->getTimestamp(),
274 $rev->getUserText(),
275 $this->title->getTalkPage()->getFullUrl()
276 );
277 }
278 }
279
280 /**
281 * @ingroup Pager
282 */
283 class HistoryPager extends ReverseChronologicalPager {
284 public $lastRow = false, $counter, $historyPage, $title, $buttons;
285 protected $oldIdChecked;
286
287 function __construct( $historyPage, $year='', $month='', $tagFilter = '' ) {
288 parent::__construct();
289 $this->historyPage = $historyPage;
290 $this->title = $this->historyPage->title;
291 $this->tagFilter = $tagFilter;
292 $this->getDateCond( $year, $month );
293 }
294
295 // For hook compatibility...
296 function getArticle() {
297 return $this->historyPage->getArticle();
298 }
299
300 function getQueryInfo() {
301 $queryInfo = array(
302 'tables' => array('revision'),
303 'fields' => array_merge( Revision::selectFields(), array('ts_tags') ),
304 'conds' => array('rev_page' => $this->historyPage->title->getArticleID() ),
305 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
306 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
307 );
308 ChangeTags::modifyDisplayQuery(
309 $queryInfo['tables'],
310 $queryInfo['fields'],
311 $queryInfo['conds'],
312 $queryInfo['join_conds'],
313 $queryInfo['options'],
314 $this->tagFilter
315 );
316 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
317 return $queryInfo;
318 }
319
320 function getIndexField() {
321 return 'rev_timestamp';
322 }
323
324 function formatRow( $row ) {
325 if( $this->lastRow ) {
326 $latest = ($this->counter == 1 && $this->mIsFirst);
327 $firstInList = $this->counter == 1;
328 $s = $this->historyLine( $this->lastRow, $row, $this->counter++,
329 $this->title->getNotificationTimestamp(), $latest, $firstInList );
330 } else {
331 $s = '';
332 }
333 $this->lastRow = $row;
334 return $s;
335 }
336
337 /**
338 * Creates begin of history list with a submit button
339 *
340 * @return string HTML output
341 */
342 function getStartBody() {
343 global $wgScript, $wgEnableHtmlDiff, $wgUser, $wgOut, $wgContLang;
344 $this->lastRow = false;
345 $this->counter = 1;
346 $this->oldIdChecked = 0;
347
348 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
349 $s = Xml::openElement( 'form', array( 'action' => $wgScript,
350 'id' => 'mw-history-compare' ) ) . "\n";
351 $s .= Xml::hidden( 'title', $this->title->getPrefixedDbKey() ) . "\n";
352 $s .= Xml::hidden( 'action', 'historysubmit' ) . "\n";
353
354 $this->buttons = '<div>';
355 if( $wgUser->isAllowed('deletedhistory') ) {
356 $float = $wgContLang->isRTL() ? 'left' : 'right';
357 # Note bug #20966, <button> is non-standard in IE<8
358 $this->buttons .= Xml::element( 'button',
359 array(
360 'type' => 'submit',
361 'name' => 'revisiondelete',
362 'value' => '1',
363 'style' => "float: $float;",
364 'class' => 'mw-history-revisiondelete-button',
365 ),
366 wfMsg( 'showhideselectedversions' )
367 ) . "\n";
368 }
369 if( $wgEnableHtmlDiff ) {
370 $this->buttons .= Xml::element( 'button',
371 array(
372 'type' => 'submit',
373 'name' => 'htmldiff',
374 'value' => '1',
375 'class' => 'historysubmit',
376 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
377 'title' => wfMsg( 'tooltip-compareselectedversions' ),
378 ),
379 wfMsg( 'visualcomparison')
380 ) . "\n";
381 $this->buttons .= $this->submitButton( wfMsg( 'wikicodecomparison'),
382 array(
383 'class' => 'historysubmit',
384 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
385 'title' => wfMsg( 'tooltip-compareselectedversions' ),
386 )
387 ) . "\n";
388 } else {
389 $this->buttons .= $this->submitButton( wfMsg( 'compareselectedversions'),
390 array(
391 'class' => 'historysubmit',
392 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
393 'title' => wfMsg( 'tooltip-compareselectedversions' ),
394 )
395 ) . "\n";
396 }
397 $this->buttons .= '</div>';
398 $s .= $this->buttons . '<ul id="pagehistory">' . "\n";
399 return $s;
400 }
401
402 function getEndBody() {
403 if( $this->lastRow ) {
404 $latest = $this->counter == 1 && $this->mIsFirst;
405 $firstInList = $this->counter == 1;
406 if( $this->mIsBackwards ) {
407 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
408 if( $this->mOffset == '' ) {
409 $next = null;
410 } else {
411 $next = 'unknown';
412 }
413 } else {
414 # The next row is the past-the-end row
415 $next = $this->mPastTheEndRow;
416 }
417 $s = $this->historyLine( $this->lastRow, $next, $this->counter++,
418 $this->title->getNotificationTimestamp(), $latest, $firstInList );
419 } else {
420 $s = '';
421 }
422 $s .= "</ul>\n";
423 $s .= $this->buttons;
424 $s .= '</form>';
425 return $s;
426 }
427
428 /**
429 * Creates a submit button
430 *
431 * @param array $attributes attributes
432 * @return string HTML output for the submit button
433 */
434 function submitButton($message, $attributes = array() ) {
435 # Disable submit button if history has 1 revision only
436 if( $this->getNumRows() > 1 ) {
437 return Xml::submitButton( $message , $attributes );
438 } else {
439 return '';
440 }
441 }
442
443 /**
444 * Returns a row from the history printout.
445 *
446 * @todo document some more, and maybe clean up the code (some params redundant?)
447 *
448 * @param Row $row The database row corresponding to the previous line.
449 * @param mixed $next The database row corresponding to the next line.
450 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
451 * @param $notificationtimestamp
452 * @param bool $latest Whether this row corresponds to the page's latest revision.
453 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
454 * @return string HTML output for the row
455 */
456 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
457 $latest = false, $firstInList = false )
458 {
459 global $wgUser, $wgLang;
460 $rev = new Revision( $row );
461 $rev->setTitle( $this->title );
462
463 $curlink = $this->curLink( $rev, $latest );
464 $lastlink = $this->lastLink( $rev, $next, $counter );
465 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
466 $link = $this->revLink( $rev );
467 $classes = array();
468
469 $s = "($curlink) ($lastlink) $diffButtons";
470
471 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
472 // Don't show useless link to people who cannot hide revisions
473 if( !$rev->getVisibility() && !$wgUser->isAllowed( 'deleterevision' ) ) {
474 $del = Xml::check( 'deleterevisions', false, array('class' => 'mw-revdelundel-hidden') );
475 // If revision was hidden from sysops
476 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
477 $del = Xml::check( 'deleterevisions', false, array('disabled' => 'disabled') );
478 // Otherwise, show the link...
479 } else {
480 $id = $rev->getId();
481 $del = Xml::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
482 }
483 $s .= " $del ";
484 }
485
486 $s .= " $link";
487 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
488
489 if( $rev->isMinor() ) {
490 $s .= ' ' . ChangesList::flag( 'minor' );
491 }
492
493 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
494 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
495 }
496
497 $s .= $this->getSkin()->revComment( $rev, false, true );
498
499 if( $notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp) ) {
500 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
501 }
502
503 $tools = array();
504
505 # Rollback and undo links
506 if( !is_null( $next ) && is_object( $next ) ) {
507 if( $latest && $this->title->userCan( 'rollback' ) && $this->title->userCan( 'edit' ) ) {
508 $tools[] = '<span class="mw-rollback-link">'.
509 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
510 }
511
512 if( $this->title->quickUserCan( 'edit' )
513 && !$rev->isDeleted( Revision::DELETED_TEXT )
514 && !$next->rev_deleted & Revision::DELETED_TEXT )
515 {
516 # Create undo tooltip for the first (=latest) line only
517 $undoTooltip = $latest
518 ? array( 'title' => wfMsg( 'tooltip-undo' ) )
519 : array();
520 $undolink = $this->getSkin()->link(
521 $this->title,
522 wfMsgHtml( 'editundo' ),
523 $undoTooltip,
524 array(
525 'action' => 'edit',
526 'undoafter' => $next->rev_id,
527 'undo' => $rev->getId()
528 ),
529 array( 'known', 'noclasses' )
530 );
531 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
532 }
533 }
534
535 if( $tools ) {
536 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
537 }
538
539 # Tags
540 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
541 $classes = array_merge( $classes, $newClasses );
542 $s .= " $tagSummary";
543
544 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
545
546 $attribs = array();
547 if ( $classes ) {
548 $attribs['class'] = implode( ' ', $classes );
549 }
550
551 return Xml::tags( 'li', $attribs, $s ) . "\n";
552 }
553
554 /**
555 * Create a link to view this revision of the page
556 * @param Revision $rev
557 * @returns string
558 */
559 function revLink( $rev ) {
560 global $wgLang;
561 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
562 $date = htmlspecialchars( $date );
563 if( !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
564 $link = $this->getSkin()->link(
565 $this->title,
566 $date,
567 array(),
568 array( 'oldid' => $rev->getId() ),
569 array( 'known', 'noclasses' )
570 );
571 } else {
572 $link = "<span class=\"history-deleted\">$date</span>";
573 }
574 return $link;
575 }
576
577 /**
578 * Create a diff-to-current link for this revision for this page
579 * @param Revision $rev
580 * @param Bool $latest, this is the latest revision of the page?
581 * @returns string
582 */
583 function curLink( $rev, $latest ) {
584 $cur = $this->historyPage->message['cur'];
585 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
586 return $cur;
587 } else {
588 return $this->getSkin()->link(
589 $this->title,
590 $cur,
591 array(),
592 array(
593 'diff' => $this->title->getLatestRevID(),
594 'oldid' => $rev->getId()
595 ),
596 array( 'known', 'noclasses' )
597 );
598 }
599 }
600
601 /**
602 * Create a diff-to-previous link for this revision for this page.
603 * @param Revision $prevRev, the previous revision
604 * @param mixed $next, the newer revision
605 * @param int $counter, what row on the history list this is
606 * @returns string
607 */
608 function lastLink( $prevRev, $next, $counter ) {
609 $last = $this->historyPage->message['last'];
610 # $next may either be a Row, null, or "unkown"
611 $nextRev = is_object($next) ? new Revision( $next ) : $next;
612 if( is_null($next) ) {
613 # Probably no next row
614 return $last;
615 } elseif( $next === 'unknown' ) {
616 # Next row probably exists but is unknown, use an oldid=prev link
617 return $this->getSkin()->link(
618 $this->title,
619 $last,
620 array(),
621 array(
622 'diff' => $prevRev->getId(),
623 'oldid' => 'prev'
624 ),
625 array( 'known', 'noclasses' )
626 );
627 } elseif( !$prevRev->userCan(Revision::DELETED_TEXT) || !$nextRev->userCan(Revision::DELETED_TEXT) ) {
628 return $last;
629 } else {
630 return $this->getSkin()->link(
631 $this->title,
632 $last,
633 array(),
634 array(
635 'diff' => $prevRev->getId(),
636 'oldid' => $next->rev_id
637 ),
638 array( 'known', 'noclasses' )
639 );
640 }
641 }
642
643 /**
644 * Create radio buttons for page history
645 *
646 * @param object $rev Revision
647 * @param bool $firstInList Is this version the first one?
648 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
649 * @return string HTML output for the radio buttons
650 */
651 function diffButtons( $rev, $firstInList, $counter ) {
652 if( $this->getNumRows() > 1 ) {
653 $id = $rev->getId();
654 $radio = array( 'type' => 'radio', 'value' => $id );
655 /** @todo: move title texts to javascript */
656 if( $firstInList ) {
657 $first = Xml::element( 'input',
658 array_merge( $radio, array(
659 'style' => 'visibility:hidden',
660 'name' => 'oldid',
661 'id' => 'mw-oldid-null' ) )
662 );
663 $checkmark = array( 'checked' => 'checked' );
664 } else {
665 # Check visibility of old revisions
666 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
667 $radio['disabled'] = 'disabled';
668 $checkmark = array(); // We will check the next possible one
669 } else if( $counter == 2 || !$this->oldIdChecked ) {
670 $checkmark = array( 'checked' => 'checked' );
671 $this->oldIdChecked = $id;
672 } else {
673 $checkmark = array();
674 }
675 $first = Xml::element( 'input',
676 array_merge( $radio, $checkmark, array(
677 'name' => 'oldid',
678 'id' => "mw-oldid-$id" ) ) );
679 $checkmark = array();
680 }
681 $second = Xml::element( 'input',
682 array_merge( $radio, $checkmark, array(
683 'name' => 'diff',
684 'id' => "mw-diff-$id" ) ) );
685 return $first . $second;
686 } else {
687 return '';
688 }
689 }
690 }
691
692 /**
693 * Backwards-compatibility aliases
694 */
695 class PageHistory extends HistoryPage {}
696 class PageHistoryPager extends HistoryPager {}