* Bug 20944: Align revision delete button left for RTL wikis.
[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
353 $this->buttons = '<div>';
354 if( $wgUser->isAllowed('deletedhistory') ) {
355 $float = $wgContLang->isRTL() ? 'left' : 'right';
356 $this->buttons .= Xml::element( 'button',
357 array(
358 'type' => 'submit',
359 'name' => 'action',
360 'value' => 'revisiondelete',
361 'style' => "float: $float;",
362 'class' => 'mw-history-revisiondelete-button',
363 ),
364 wfMsg( 'showhideselectedversions' )
365 ) . "\n";
366 }
367 if( $wgEnableHtmlDiff ) {
368 $this->buttons .= Xml::element( 'button',
369 array(
370 'type' => 'submit',
371 'name' => 'htmldiff',
372 'value' => '1',
373 'class' => 'historysubmit',
374 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
375 'title' => wfMsg( 'tooltip-compareselectedversions' ),
376 ),
377 wfMsg( 'visualcomparison')
378 ) . "\n";
379 $this->buttons .= $this->submitButton( wfMsg( 'wikicodecomparison'),
380 array(
381 'class' => 'historysubmit',
382 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
383 'title' => wfMsg( 'tooltip-compareselectedversions' ),
384 )
385 ) . "\n";
386 } else {
387 $this->buttons .= $this->submitButton( wfMsg( 'compareselectedversions'),
388 array(
389 'class' => 'historysubmit',
390 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
391 'title' => wfMsg( 'tooltip-compareselectedversions' ),
392 )
393 ) . "\n";
394 }
395 $this->buttons .= '</div>';
396 $s .= $this->buttons . '<ul id="pagehistory">' . "\n";
397 return $s;
398 }
399
400 function getEndBody() {
401 if( $this->lastRow ) {
402 $latest = $this->counter == 1 && $this->mIsFirst;
403 $firstInList = $this->counter == 1;
404 if( $this->mIsBackwards ) {
405 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
406 if( $this->mOffset == '' ) {
407 $next = null;
408 } else {
409 $next = 'unknown';
410 }
411 } else {
412 # The next row is the past-the-end row
413 $next = $this->mPastTheEndRow;
414 }
415 $s = $this->historyLine( $this->lastRow, $next, $this->counter++,
416 $this->title->getNotificationTimestamp(), $latest, $firstInList );
417 } else {
418 $s = '';
419 }
420 $s .= "</ul>\n";
421 $s .= $this->buttons;
422 $s .= '</form>';
423 return $s;
424 }
425
426 /**
427 * Creates a submit button
428 *
429 * @param array $attributes attributes
430 * @return string HTML output for the submit button
431 */
432 function submitButton($message, $attributes = array() ) {
433 # Disable submit button if history has 1 revision only
434 if( $this->getNumRows() > 1 ) {
435 return Xml::submitButton( $message , $attributes );
436 } else {
437 return '';
438 }
439 }
440
441 /**
442 * Returns a row from the history printout.
443 *
444 * @todo document some more, and maybe clean up the code (some params redundant?)
445 *
446 * @param Row $row The database row corresponding to the previous line.
447 * @param mixed $next The database row corresponding to the next line.
448 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
449 * @param $notificationtimestamp
450 * @param bool $latest Whether this row corresponds to the page's latest revision.
451 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
452 * @return string HTML output for the row
453 */
454 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
455 $latest = false, $firstInList = false )
456 {
457 global $wgUser, $wgLang;
458 $rev = new Revision( $row );
459 $rev->setTitle( $this->title );
460
461 $curlink = $this->curLink( $rev, $latest );
462 $lastlink = $this->lastLink( $rev, $next, $counter );
463 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
464 $link = $this->revLink( $rev );
465 $classes = array();
466
467 $s = "($curlink) ($lastlink) $diffButtons";
468
469 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
470 // Don't show useless link to people who cannot hide revisions
471 if( !$rev->getVisibility() && !$wgUser->isAllowed( 'deleterevision' ) ) {
472 $del = Xml::check( 'deleterevisions', false, array('class' => 'mw-revdelundel-hidden') );
473 // If revision was hidden from sysops
474 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
475 $del = Xml::check( 'deleterevisions', false, array('disabled' => 'disabled') );
476 // Otherwise, show the link...
477 } else {
478 $id = $rev->getId();
479 $del = Xml::check( 'showhiderevisions', false, array( 'name' => "ids[$id]" ) );
480 }
481 $s .= " $del ";
482 }
483
484 $s .= " $link";
485 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
486
487 if( $rev->isMinor() ) {
488 $s .= ' ' . ChangesList::flag( 'minor' );
489 }
490
491 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
492 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
493 }
494
495 $s .= $this->getSkin()->revComment( $rev, false, true );
496
497 if( $notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp) ) {
498 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
499 }
500
501 $tools = array();
502
503 # Rollback and undo links
504 if( !is_null( $next ) && is_object( $next ) ) {
505 if( $latest && $this->title->userCan( 'rollback' ) && $this->title->userCan( 'edit' ) ) {
506 $tools[] = '<span class="mw-rollback-link">'.
507 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
508 }
509
510 if( $this->title->quickUserCan( 'edit' )
511 && !$rev->isDeleted( Revision::DELETED_TEXT )
512 && !$next->rev_deleted & Revision::DELETED_TEXT )
513 {
514 # Create undo tooltip for the first (=latest) line only
515 $undoTooltip = $latest
516 ? array( 'title' => wfMsg( 'tooltip-undo' ) )
517 : array();
518 $undolink = $this->getSkin()->link(
519 $this->title,
520 wfMsgHtml( 'editundo' ),
521 $undoTooltip,
522 array(
523 'action' => 'edit',
524 'undoafter' => $next->rev_id,
525 'undo' => $rev->getId()
526 ),
527 array( 'known', 'noclasses' )
528 );
529 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
530 }
531 }
532
533 if( $tools ) {
534 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
535 }
536
537 # Tags
538 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
539 $classes = array_merge( $classes, $newClasses );
540 $s .= " $tagSummary";
541
542 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
543
544 $attribs = array();
545 if ( $classes ) {
546 $attribs['class'] = implode( ' ', $classes );
547 }
548
549 return Xml::tags( 'li', $attribs, $s ) . "\n";
550 }
551
552 /**
553 * Create a link to view this revision of the page
554 * @param Revision $rev
555 * @returns string
556 */
557 function revLink( $rev ) {
558 global $wgLang;
559 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
560 $date = htmlspecialchars( $date );
561 if( !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
562 $link = $this->getSkin()->link(
563 $this->title,
564 $date,
565 array(),
566 array( 'oldid' => $rev->getId() ),
567 array( 'known', 'noclasses' )
568 );
569 } else {
570 $link = "<span class=\"history-deleted\">$date</span>";
571 }
572 return $link;
573 }
574
575 /**
576 * Create a diff-to-current link for this revision for this page
577 * @param Revision $rev
578 * @param Bool $latest, this is the latest revision of the page?
579 * @returns string
580 */
581 function curLink( $rev, $latest ) {
582 $cur = $this->historyPage->message['cur'];
583 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
584 return $cur;
585 } else {
586 return $this->getSkin()->link(
587 $this->title,
588 $cur,
589 array(),
590 array(
591 'diff' => $this->title->getLatestRevID(),
592 'oldid' => $rev->getId()
593 ),
594 array( 'known', 'noclasses' )
595 );
596 }
597 }
598
599 /**
600 * Create a diff-to-previous link for this revision for this page.
601 * @param Revision $prevRev, the previous revision
602 * @param mixed $next, the newer revision
603 * @param int $counter, what row on the history list this is
604 * @returns string
605 */
606 function lastLink( $prevRev, $next, $counter ) {
607 $last = $this->historyPage->message['last'];
608 # $next may either be a Row, null, or "unkown"
609 $nextRev = is_object($next) ? new Revision( $next ) : $next;
610 if( is_null($next) ) {
611 # Probably no next row
612 return $last;
613 } elseif( $next === 'unknown' ) {
614 # Next row probably exists but is unknown, use an oldid=prev link
615 return $this->getSkin()->link(
616 $this->title,
617 $last,
618 array(),
619 array(
620 'diff' => $prevRev->getId(),
621 'oldid' => 'prev'
622 ),
623 array( 'known', 'noclasses' )
624 );
625 } elseif( !$prevRev->userCan(Revision::DELETED_TEXT) || !$nextRev->userCan(Revision::DELETED_TEXT) ) {
626 return $last;
627 } else {
628 return $this->getSkin()->link(
629 $this->title,
630 $last,
631 array(),
632 array(
633 'diff' => $prevRev->getId(),
634 'oldid' => $next->rev_id
635 ),
636 array( 'known', 'noclasses' )
637 );
638 }
639 }
640
641 /**
642 * Create radio buttons for page history
643 *
644 * @param object $rev Revision
645 * @param bool $firstInList Is this version the first one?
646 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
647 * @return string HTML output for the radio buttons
648 */
649 function diffButtons( $rev, $firstInList, $counter ) {
650 if( $this->getNumRows() > 1 ) {
651 $id = $rev->getId();
652 $radio = array( 'type' => 'radio', 'value' => $id );
653 /** @todo: move title texts to javascript */
654 if( $firstInList ) {
655 $first = Xml::element( 'input',
656 array_merge( $radio, array(
657 'style' => 'visibility:hidden',
658 'name' => 'oldid',
659 'id' => 'mw-oldid-null' ) )
660 );
661 $checkmark = array( 'checked' => 'checked' );
662 } else {
663 # Check visibility of old revisions
664 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
665 $radio['disabled'] = 'disabled';
666 $checkmark = array(); // We will check the next possible one
667 } else if( $counter == 2 || !$this->oldIdChecked ) {
668 $checkmark = array( 'checked' => 'checked' );
669 $this->oldIdChecked = $id;
670 } else {
671 $checkmark = array();
672 }
673 $first = Xml::element( 'input',
674 array_merge( $radio, $checkmark, array(
675 'name' => 'oldid',
676 'id' => "mw-oldid-$id" ) ) );
677 $checkmark = array();
678 }
679 $second = Xml::element( 'input',
680 array_merge( $radio, $checkmark, array(
681 'name' => 'diff',
682 'id' => "mw-diff-$id" ) ) );
683 return $first . $second;
684 } else {
685 return '';
686 }
687 }
688 }
689
690 /**
691 * Backwards-compatibility aliases
692 */
693 class PageHistory extends HistoryPage {}
694 class PageHistoryPager extends HistoryPager {}