Big oops - merged to wrong branch.
[lhc/web/wiklou.git] / includes / actions / HistoryAction.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 /**
26 * This class handles printing the history page for an article. In order to
27 * be efficient, it uses timestamps rather than offsets for paging, to avoid
28 * costly LIMIT,offset queries.
29 *
30 * Construct it by passing in an Article, and call $h->history() to print the
31 * history.
32 *
33 */
34 class HistoryAction extends FormlessAction {
35 const DIR_PREV = 0;
36 const DIR_NEXT = 1;
37
38 public function getName() {
39 return 'history';
40 }
41
42 public function requiresWrite() {
43 return false;
44 }
45
46 public function requiresUnblock() {
47 return false;
48 }
49
50 protected function getPageTitle() {
51 return $this->msg( 'history-title', $this->getTitle()->getPrefixedText() )->text();
52 }
53
54 protected function getDescription() {
55 // Creation of a subtitle link pointing to [[Special:Log]]
56 return Linker::linkKnown(
57 SpecialPage::getTitleFor( 'Log' ),
58 $this->msg( 'viewpagelogs' )->escaped(),
59 array(),
60 array( 'page' => $this->getTitle()->getPrefixedText() )
61 );
62 }
63
64 /**
65 * Get the Article object we are working on.
66 * @return Page
67 */
68 public function getArticle() {
69 return $this->page;
70 }
71
72 /**
73 * As we use the same small set of messages in various methods and that
74 * they are called often, we call them once and save them in $this->message
75 */
76 private function preCacheMessages() {
77 // Precache various messages
78 if ( !isset( $this->message ) ) {
79 $msgs = array( 'cur', 'last', 'pipe-separator' );
80 foreach ( $msgs as $msg ) {
81 $this->message[$msg] = $this->msg( $msg )->escaped();
82 }
83 }
84 }
85
86 /**
87 * Print the history page for an article.
88 */
89 function onView() {
90 global $wgScript, $wgUseFileCache;
91
92 $out = $this->getOutput();
93 $request = $this->getRequest();
94
95 /**
96 * Allow client caching.
97 */
98 if ( $out->checkLastModified( $this->page->getTouched() ) ) {
99 return; // Client cache fresh and headers sent, nothing more to do.
100 }
101
102 wfProfileIn( __METHOD__ );
103
104 $this->preCacheMessages();
105
106 # Fill in the file cache if not set already
107 if ( $wgUseFileCache && HTMLFileCache::useFileCache( $this->getContext() ) ) {
108 $cache = HTMLFileCache::newFromTitle( $this->getTitle(), 'history' );
109 if ( !$cache->isCacheGood( /* Assume up to date */ ) ) {
110 ob_start( array( &$cache, 'saveToFileCache' ) );
111 }
112 }
113
114 // Setup page variables.
115 $out->setFeedAppendQuery( 'action=history' );
116 $out->addModules( array( 'mediawiki.legacy.history', 'mediawiki.action.history' ) );
117
118 // Handle atom/RSS feeds.
119 $feedType = $request->getVal( 'feed' );
120 if ( $feedType ) {
121 $this->feed( $feedType );
122 wfProfileOut( __METHOD__ );
123 return;
124 }
125
126 // Fail nicely if article doesn't exist.
127 if ( !$this->page->exists() ) {
128 $out->addWikiMsg( 'nohistory' );
129 # show deletion/move log if there is an entry
130 LogEventsList::showLogExtract(
131 $out,
132 array( 'delete', 'move' ),
133 $this->getTitle(),
134 '',
135 array( 'lim' => 10,
136 'conds' => array( "log_action != 'revision'" ),
137 'showIfEmpty' => false,
138 'msgKey' => array( 'moveddeleted-notice' )
139 )
140 );
141 wfProfileOut( __METHOD__ );
142 return;
143 }
144
145 /**
146 * Add date selector to quickly get to a certain time
147 */
148 $year = $request->getInt( 'year' );
149 $month = $request->getInt( 'month' );
150 $tagFilter = $request->getVal( 'tagfilter' );
151 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
152
153 /**
154 * Option to show only revisions that have been (partially) hidden via RevisionDelete
155 */
156 if ( $request->getBool( 'deleted' ) ) {
157 $conds = array( "rev_deleted != '0'" );
158 } else {
159 $conds = array();
160 }
161 $checkDeleted = Xml::checkLabel( $this->msg( 'history-show-deleted' )->text(),
162 'deleted', 'mw-show-deleted-only', $request->getBool( 'deleted' ) ) . "\n";
163
164 // Add the general form
165 $action = htmlspecialchars( $wgScript );
166 $out->addHTML(
167 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
168 Xml::fieldset(
169 $this->msg( 'history-fieldset-title' )->text(),
170 false,
171 array( 'id' => 'mw-history-search' )
172 ) .
173 Html::hidden( 'title', $this->getTitle()->getPrefixedDBKey() ) . "\n" .
174 Html::hidden( 'action', 'history' ) . "\n" .
175 Xml::dateMenu( $year, $month ) . '&#160;' .
176 ( $tagSelector ? ( implode( '&#160;', $tagSelector ) . '&#160;' ) : '' ) .
177 $checkDeleted .
178 Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
179 '</fieldset></form>'
180 );
181
182 wfRunHooks( 'PageHistoryBeforeList', array( &$this->page ) );
183
184 // Create and output the list.
185 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
186 $out->addHTML(
187 $pager->getNavigationBar() .
188 $pager->getBody() .
189 $pager->getNavigationBar()
190 );
191 $out->preventClickjacking( $pager->getPreventClickjacking() );
192
193 wfProfileOut( __METHOD__ );
194 }
195
196 /**
197 * Fetch an array of revisions, specified by a given limit, offset and
198 * direction. This is now only used by the feeds. It was previously
199 * used by the main UI but that's now handled by the pager.
200 *
201 * @param $limit Integer: the limit number of revisions to get
202 * @param $offset Integer
203 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
204 * @return ResultWrapper
205 */
206 function fetchRevisions( $limit, $offset, $direction ) {
207 $dbr = wfGetDB( DB_SLAVE );
208
209 if ( $direction == HistoryPage::DIR_PREV ) {
210 list( $dirs, $oper ) = array( "ASC", ">=" );
211 } else { /* $direction == HistoryPage::DIR_NEXT */
212 list( $dirs, $oper ) = array( "DESC", "<=" );
213 }
214
215 if ( $offset ) {
216 $offsets = array( "rev_timestamp $oper '$offset'" );
217 } else {
218 $offsets = array();
219 }
220
221 $page_id = $this->page->getId();
222
223 return $dbr->select( 'revision',
224 Revision::selectFields(),
225 array_merge( array( "rev_page=$page_id" ), $offsets ),
226 __METHOD__,
227 array( 'ORDER BY' => "rev_timestamp $dirs",
228 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
229 );
230 }
231
232 /**
233 * Output a subscription feed listing recent edits to this page.
234 *
235 * @param $type String: feed type
236 */
237 function feed( $type ) {
238 global $wgFeedClasses, $wgFeedLimit;
239 if ( !FeedUtils::checkFeedOutput( $type ) ) {
240 return;
241 }
242 $request = $this->getRequest();
243
244 $feed = new $wgFeedClasses[$type](
245 $this->getTitle()->getPrefixedText() . ' - ' .
246 wfMsgForContent( 'history-feed-title' ),
247 wfMsgForContent( 'history-feed-description' ),
248 $this->getTitle()->getFullUrl( 'action=history' )
249 );
250
251 // Get a limit on number of feed entries. Provide a sane default
252 // of 10 if none is defined (but limit to $wgFeedLimit max)
253 $limit = $request->getInt( 'limit', 10 );
254 if ( $limit > $wgFeedLimit || $limit < 1 ) {
255 $limit = 10;
256 }
257 $items = $this->fetchRevisions( $limit, 0, HistoryPage::DIR_NEXT );
258
259 // Generate feed elements enclosed between header and footer.
260 $feed->outHeader();
261 if ( $items->numRows() ) {
262 foreach ( $items as $row ) {
263 $feed->outItem( $this->feedItem( $row ) );
264 }
265 } else {
266 $feed->outItem( $this->feedEmpty() );
267 }
268 $feed->outFooter();
269 }
270
271 function feedEmpty() {
272 return new FeedItem(
273 wfMsgForContent( 'nohistory' ),
274 $this->getOutput()->parse( wfMsgForContent( 'history-feed-empty' ) ),
275 $this->getTitle()->getFullUrl(),
276 wfTimestamp( TS_MW ),
277 '',
278 $this->getTitle()->getTalkPage()->getFullUrl()
279 );
280 }
281
282 /**
283 * Generate a FeedItem object from a given revision table row
284 * Borrows Recent Changes' feed generation functions for formatting;
285 * includes a diff to the previous revision (if any).
286 *
287 * @param $row Object: database row
288 * @return FeedItem
289 */
290 function feedItem( $row ) {
291 $rev = new Revision( $row );
292 $rev->setTitle( $this->getTitle() );
293 $text = FeedUtils::formatDiffRow(
294 $this->getTitle(),
295 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
296 $rev->getId(),
297 $rev->getTimestamp(),
298 $rev->getComment()
299 );
300 if ( $rev->getComment() == '' ) {
301 global $wgContLang;
302 $title = wfMsgForContent( 'history-feed-item-nocomment',
303 $rev->getUserText(),
304 $wgContLang->timeanddate( $rev->getTimestamp() ),
305 $wgContLang->date( $rev->getTimestamp() ),
306 $wgContLang->time( $rev->getTimestamp() )
307 );
308 } else {
309 $title = $rev->getUserText() .
310 wfMsgForContent( 'colon-separator' ) .
311 FeedItem::stripComment( $rev->getComment() );
312 }
313 return new FeedItem(
314 $title,
315 $text,
316 $this->getTitle()->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
317 $rev->getTimestamp(),
318 $rev->getUserText(),
319 $this->getTitle()->getTalkPage()->getFullUrl()
320 );
321 }
322 }
323
324 /**
325 * @ingroup Pager
326 */
327 class HistoryPager extends ReverseChronologicalPager {
328 public $lastRow = false, $counter, $historyPage, $buttons, $conds;
329 protected $oldIdChecked;
330 protected $preventClickjacking = false;
331 /**
332 * @var array
333 */
334 protected $parentLens;
335
336 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
337 parent::__construct( $historyPage->getContext() );
338 $this->historyPage = $historyPage;
339 $this->tagFilter = $tagFilter;
340 $this->getDateCond( $year, $month );
341 $this->conds = $conds;
342 }
343
344 // For hook compatibility...
345 function getArticle() {
346 return $this->historyPage->getArticle();
347 }
348
349 function getSqlComment() {
350 if ( $this->conds ) {
351 return 'history page filtered'; // potentially slow, see CR r58153
352 } else {
353 return 'history page unfiltered';
354 }
355 }
356
357 function getQueryInfo() {
358 $queryInfo = array(
359 'tables' => array( 'revision', 'user' ),
360 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
361 'conds' => array_merge(
362 array( 'rev_page' => $this->getWikiPage()->getId() ),
363 $this->conds ),
364 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
365 'join_conds' => array(
366 'user' => Revision::userJoinCond(),
367 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
368 );
369 ChangeTags::modifyDisplayQuery(
370 $queryInfo['tables'],
371 $queryInfo['fields'],
372 $queryInfo['conds'],
373 $queryInfo['join_conds'],
374 $queryInfo['options'],
375 $this->tagFilter
376 );
377 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
378 return $queryInfo;
379 }
380
381 function getIndexField() {
382 return 'rev_timestamp';
383 }
384
385 function formatRow( $row ) {
386 if ( $this->lastRow ) {
387 $latest = ( $this->counter == 1 && $this->mIsFirst );
388 $firstInList = $this->counter == 1;
389 $this->counter++;
390 $s = $this->historyLine( $this->lastRow, $row,
391 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
392 } else {
393 $s = '';
394 }
395 $this->lastRow = $row;
396 return $s;
397 }
398
399 function doBatchLookups() {
400 # Do a link batch query
401 $this->mResult->seek( 0 );
402 $batch = new LinkBatch();
403 $revIds = array();
404 foreach ( $this->mResult as $row ) {
405 if( $row->rev_parent_id ) {
406 $revIds[] = $row->rev_parent_id;
407 }
408 if( !is_null( $row->user_name ) ) {
409 $batch->add( NS_USER, $row->user_name );
410 $batch->add( NS_USER_TALK, $row->user_name );
411 } else { # for anons or usernames of imported revisions
412 $batch->add( NS_USER, $row->rev_user_text );
413 $batch->add( NS_USER_TALK, $row->rev_user_text );
414 }
415 }
416 $this->parentLens = $this->getParentLengths( $revIds );
417 $batch->execute();
418 $this->mResult->seek( 0 );
419 }
420
421 /**
422 * Do a batched query to get the parent revision lengths
423 * @param $revIds array
424 * @return array
425 * @TODO: stolen from Contributions, refactor
426 */
427 private function getParentLengths( array $revIds ) {
428 $revLens = array();
429 if ( !$revIds ) {
430 return $revLens; // empty
431 }
432 wfProfileIn( __METHOD__ );
433 $res = $this->mDb->select( 'revision',
434 array( 'rev_id', 'rev_len' ),
435 array( 'rev_id' => $revIds ),
436 __METHOD__ );
437 foreach ( $res as $row ) {
438 $revLens[$row->rev_id] = $row->rev_len;
439 }
440 wfProfileOut( __METHOD__ );
441 return $revLens;
442 }
443
444 /**
445 * Creates begin of history list with a submit button
446 *
447 * @return string HTML output
448 */
449 function getStartBody() {
450 global $wgScript;
451 $this->lastRow = false;
452 $this->counter = 1;
453 $this->oldIdChecked = 0;
454
455 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
456 $s = Html::openElement( 'form', array( 'action' => $wgScript,
457 'id' => 'mw-history-compare' ) ) . "\n";
458 $s .= Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . "\n";
459 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
460
461 // Button container stored in $this->buttons for re-use in getEndBody()
462 $this->buttons = '<div>';
463 $this->buttons .= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
464 array( 'class' => 'historysubmit mw-history-compareselectedversions-button' )
465 + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
466 ) . "\n";
467
468 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
469 $this->buttons .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
470 }
471 $this->buttons .= '</div>';
472
473 $s .= $this->buttons;
474 $s .= '<ul id="pagehistory">' . "\n";
475 return $s;
476 }
477
478 private function getRevisionButton( $name, $msg ) {
479 $this->preventClickjacking();
480 # Note bug #20966, <button> is non-standard in IE<8
481 $element = Html::element( 'button',
482 array(
483 'type' => 'submit',
484 'name' => $name,
485 'value' => '1',
486 'class' => "historysubmit mw-history-$name-button",
487 ),
488 $this->msg( $msg )->text()
489 ) . "\n";
490 return $element;
491 }
492
493 function getEndBody() {
494 if ( $this->lastRow ) {
495 $latest = $this->counter == 1 && $this->mIsFirst;
496 $firstInList = $this->counter == 1;
497 if ( $this->mIsBackwards ) {
498 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
499 if ( $this->mOffset == '' ) {
500 $next = null;
501 } else {
502 $next = 'unknown';
503 }
504 } else {
505 # The next row is the past-the-end row
506 $next = $this->mPastTheEndRow;
507 }
508 $this->counter++;
509 $s = $this->historyLine( $this->lastRow, $next,
510 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
511 } else {
512 $s = '';
513 }
514 $s .= "</ul>\n";
515 # Add second buttons only if there is more than one rev
516 if ( $this->getNumRows() > 2 ) {
517 $s .= $this->buttons;
518 }
519 $s .= '</form>';
520 return $s;
521 }
522
523 /**
524 * Creates a submit button
525 *
526 * @param $message String: text of the submit button, will be escaped
527 * @param $attributes Array: attributes
528 * @return String: HTML output for the submit button
529 */
530 function submitButton( $message, $attributes = array() ) {
531 # Disable submit button if history has 1 revision only
532 if ( $this->getNumRows() > 1 ) {
533 return Xml::submitButton( $message , $attributes );
534 } else {
535 return '';
536 }
537 }
538
539 /**
540 * Returns a row from the history printout.
541 *
542 * @todo document some more, and maybe clean up the code (some params redundant?)
543 *
544 * @param $row Object: the database row corresponding to the previous line.
545 * @param $next Mixed: the database row corresponding to the next line. (chronologically previous)
546 * @param $notificationtimestamp
547 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
548 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
549 * @return String: HTML output for the row
550 */
551 function historyLine( $row, $next, $notificationtimestamp = false,
552 $latest = false, $firstInList = false )
553 {
554 $rev = new Revision( $row );
555 $rev->setTitle( $this->getTitle() );
556
557 if ( is_object( $next ) ) {
558 $prevRev = new Revision( $next );
559 $prevRev->setTitle( $this->getTitle() );
560 } else {
561 $prevRev = null;
562 }
563
564 $curlink = $this->curLink( $rev, $latest );
565 $lastlink = $this->lastLink( $rev, $next );
566 $diffButtons = $this->diffButtons( $rev, $firstInList );
567 $histLinks = Html::rawElement(
568 'span',
569 array( 'class' => 'mw-history-histlinks' ),
570 $this->msg( 'parentheses' )->rawParams( $curlink . $this->historyPage->message['pipe-separator'] . $lastlink )->escaped()
571 );
572 $s = $histLinks . $diffButtons;
573
574 $link = $this->revLink( $rev );
575 $classes = array();
576
577 $del = '';
578 $user = $this->getUser();
579 // Show checkboxes for each revision
580 if ( $user->isAllowed( 'deleterevision' ) ) {
581 $this->preventClickjacking();
582 // If revision was hidden from sysops, disable the checkbox
583 if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
584 $del = Xml::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
585 // Otherwise, enable the checkbox...
586 } else {
587 $del = Xml::check( 'showhiderevisions', false,
588 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
589 }
590 // User can only view deleted revisions...
591 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
592 // If revision was hidden from sysops, disable the link
593 if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
594 $cdel = Linker::revDeleteLinkDisabled( false );
595 // Otherwise, show the link...
596 } else {
597 $query = array( 'type' => 'revision',
598 'target' => $this->getTitle()->getPrefixedDbkey(), 'ids' => $rev->getId() );
599 $del .= Linker::revDeleteLink( $query,
600 $rev->isDeleted( Revision::DELETED_RESTRICTED ), false );
601 }
602 }
603 if ( $del ) {
604 $s .= " $del ";
605 }
606
607 $lang = $this->getLanguage();
608 $dirmark = $lang->getDirMark();
609
610 $s .= " $link";
611 $s .= $dirmark;
612 $s .= " <span class='history-user'>" .
613 Linker::revUserTools( $rev, true ) . "</span>";
614 $s .= $dirmark;
615
616 if ( $rev->isMinor() ) {
617 $s .= ' ' . ChangesList::flag( 'minor' );
618 }
619
620 # Size is always public data
621 $prevSize = isset( $this->parentLens[$row->rev_parent_id] )
622 ? $this->parentLens[$row->rev_parent_id]
623 : 0;
624 $sDiff = ChangesList::showCharacterDifference( $prevSize, $rev->getSize() );
625 $fSize = Linker::formatRevisionSize($rev->getSize());
626 $s .= " . . $fSize $sDiff";
627
628 # Text following the character difference is added just before running hooks
629 $s2 = Linker::revComment( $rev, false, true );
630
631 if ( $notificationtimestamp && ( $row->rev_timestamp >= $notificationtimestamp ) ) {
632 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
633 }
634
635 $tools = array();
636
637 # Rollback and undo links
638 if ( $prevRev &&
639 !count( $this->getTitle()->getUserPermissionsErrors( 'edit', $this->getUser() ) ) )
640 {
641 if ( $latest && !count( $this->getTitle()->getUserPermissionsErrors( 'rollback', $this->getUser() ) ) ) {
642 $this->preventClickjacking();
643 $tools[] = '<span class="mw-rollback-link">' .
644 Linker::buildRollbackLink( $rev ) . '</span>';
645 }
646
647 if ( !$rev->isDeleted( Revision::DELETED_TEXT )
648 && !$prevRev->isDeleted( Revision::DELETED_TEXT ) )
649 {
650 # Create undo tooltip for the first (=latest) line only
651 $undoTooltip = $latest
652 ? array( 'title' => $this->msg( 'tooltip-undo' )->text() )
653 : array();
654 $undolink = Linker::linkKnown(
655 $this->getTitle(),
656 $this->msg( 'editundo' )->escaped(),
657 $undoTooltip,
658 array(
659 'action' => 'edit',
660 'undoafter' => $prevRev->getId(),
661 'undo' => $rev->getId()
662 )
663 );
664 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
665 }
666 }
667
668 if ( $tools ) {
669 $s2 .= ' '. $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped();
670 }
671
672 # Tags
673 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
674 $classes = array_merge( $classes, $newClasses );
675 if ( $tagSummary !== '' ) {
676 $s2 .= " $tagSummary";
677 }
678
679 # Include separator between character difference and following text
680 if ( $s2 !== '' ) {
681 $s .= " . . $s2";
682 }
683
684 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
685
686 $attribs = array();
687 if ( $classes ) {
688 $attribs['class'] = implode( ' ', $classes );
689 }
690
691 return Xml::tags( 'li', $attribs, $s ) . "\n";
692 }
693
694 /**
695 * Create a link to view this revision of the page
696 *
697 * @param $rev Revision
698 * @return String
699 */
700 function revLink( $rev ) {
701 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
702 $date = htmlspecialchars( $date );
703 if ( $rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
704 $link = Linker::linkKnown(
705 $this->getTitle(),
706 $date,
707 array(),
708 array( 'oldid' => $rev->getId() )
709 );
710 } else {
711 $link = $date;
712 }
713 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
714 $link = "<span class=\"history-deleted\">$link</span>";
715 }
716 return $link;
717 }
718
719 /**
720 * Create a diff-to-current link for this revision for this page
721 *
722 * @param $rev Revision
723 * @param $latest Boolean: this is the latest revision of the page?
724 * @return String
725 */
726 function curLink( $rev, $latest ) {
727 $cur = $this->historyPage->message['cur'];
728 if ( $latest || !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
729 return $cur;
730 } else {
731 return Linker::linkKnown(
732 $this->getTitle(),
733 $cur,
734 array(),
735 array(
736 'diff' => $this->getWikiPage()->getLatest(),
737 'oldid' => $rev->getId()
738 )
739 );
740 }
741 }
742
743 /**
744 * Create a diff-to-previous link for this revision for this page.
745 *
746 * @param $prevRev Revision: the previous revision
747 * @param $next Mixed: the newer revision
748 * @return String
749 */
750 function lastLink( $prevRev, $next ) {
751 $last = $this->historyPage->message['last'];
752 # $next may either be a Row, null, or "unkown"
753 $nextRev = is_object( $next ) ? new Revision( $next ) : $next;
754 if ( is_null( $next ) ) {
755 # Probably no next row
756 return $last;
757 } elseif ( $next === 'unknown' ) {
758 # Next row probably exists but is unknown, use an oldid=prev link
759 return Linker::linkKnown(
760 $this->getTitle(),
761 $last,
762 array(),
763 array(
764 'diff' => $prevRev->getId(),
765 'oldid' => 'prev'
766 )
767 );
768 } elseif ( !$prevRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
769 || !$nextRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) )
770 {
771 return $last;
772 } else {
773 return Linker::linkKnown(
774 $this->getTitle(),
775 $last,
776 array(),
777 array(
778 'diff' => $prevRev->getId(),
779 'oldid' => $next->rev_id
780 )
781 );
782 }
783 }
784
785 /**
786 * Create radio buttons for page history
787 *
788 * @param $rev Revision object
789 * @param $firstInList Boolean: is this version the first one?
790 *
791 * @return String: HTML output for the radio buttons
792 */
793 function diffButtons( $rev, $firstInList ) {
794 if ( $this->getNumRows() > 1 ) {
795 $id = $rev->getId();
796 $radio = array( 'type' => 'radio', 'value' => $id );
797 /** @todo: move title texts to javascript */
798 if ( $firstInList ) {
799 $first = Xml::element( 'input',
800 array_merge( $radio, array(
801 'style' => 'visibility:hidden',
802 'name' => 'oldid',
803 'id' => 'mw-oldid-null' ) )
804 );
805 $checkmark = array( 'checked' => 'checked' );
806 } else {
807 # Check visibility of old revisions
808 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
809 $radio['disabled'] = 'disabled';
810 $checkmark = array(); // We will check the next possible one
811 } elseif ( !$this->oldIdChecked ) {
812 $checkmark = array( 'checked' => 'checked' );
813 $this->oldIdChecked = $id;
814 } else {
815 $checkmark = array();
816 }
817 $first = Xml::element( 'input',
818 array_merge( $radio, $checkmark, array(
819 'name' => 'oldid',
820 'id' => "mw-oldid-$id" ) ) );
821 $checkmark = array();
822 }
823 $second = Xml::element( 'input',
824 array_merge( $radio, $checkmark, array(
825 'name' => 'diff',
826 'id' => "mw-diff-$id" ) ) );
827 return $first . $second;
828 } else {
829 return '';
830 }
831 }
832
833 /**
834 * This is called if a write operation is possible from the generated HTML
835 */
836 function preventClickjacking( $enable = true ) {
837 $this->preventClickjacking = $enable;
838 }
839
840 /**
841 * Get the "prevent clickjacking" flag
842 * @return bool
843 */
844 function getPreventClickjacking() {
845 return $this->preventClickjacking;
846 }
847 }
848
849 /**
850 * Backwards-compatibility alias
851 */
852 class HistoryPage extends HistoryAction {
853 public function __construct( Page $article ) { # Just to make it public
854 parent::__construct( $article );
855 }
856
857 public function history() {
858 $this->onView();
859 }
860 }