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