f-prot isn't so open now, so might aswell not advertise it in DefaultSetttings under...
[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 // Fail if article doesn't exist.
208 if( !$this->getTitle()->exists() ) {
209 return new FakeResultWrapper( array() );
210 }
211
212 $dbr = wfGetDB( DB_SLAVE );
213
214 if ( $direction == HistoryPage::DIR_PREV ) {
215 list( $dirs, $oper ) = array( "ASC", ">=" );
216 } else { /* $direction == HistoryPage::DIR_NEXT */
217 list( $dirs, $oper ) = array( "DESC", "<=" );
218 }
219
220 if ( $offset ) {
221 $offsets = array( "rev_timestamp $oper '$offset'" );
222 } else {
223 $offsets = array();
224 }
225
226 $page_id = $this->page->getId();
227
228 return $dbr->select( 'revision',
229 Revision::selectFields(),
230 array_merge( array( "rev_page=$page_id" ), $offsets ),
231 __METHOD__,
232 array( 'ORDER BY' => "rev_timestamp $dirs",
233 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit )
234 );
235 }
236
237 /**
238 * Output a subscription feed listing recent edits to this page.
239 *
240 * @param $type String: feed type
241 */
242 function feed( $type ) {
243 global $wgFeedClasses, $wgFeedLimit;
244 if ( !FeedUtils::checkFeedOutput( $type ) ) {
245 return;
246 }
247 $request = $this->getRequest();
248
249 $feed = new $wgFeedClasses[$type](
250 $this->getTitle()->getPrefixedText() . ' - ' .
251 wfMsgForContent( 'history-feed-title' ),
252 wfMsgForContent( 'history-feed-description' ),
253 $this->getTitle()->getFullUrl( 'action=history' )
254 );
255
256 // Get a limit on number of feed entries. Provide a sane default
257 // of 10 if none is defined (but limit to $wgFeedLimit max)
258 $limit = $request->getInt( 'limit', 10 );
259 if ( $limit > $wgFeedLimit || $limit < 1 ) {
260 $limit = 10;
261 }
262 $items = $this->fetchRevisions( $limit, 0, HistoryPage::DIR_NEXT );
263
264 // Generate feed elements enclosed between header and footer.
265 $feed->outHeader();
266 if ( $items->numRows() ) {
267 foreach ( $items as $row ) {
268 $feed->outItem( $this->feedItem( $row ) );
269 }
270 } else {
271 $feed->outItem( $this->feedEmpty() );
272 }
273 $feed->outFooter();
274 }
275
276 function feedEmpty() {
277 return new FeedItem(
278 wfMsgForContent( 'nohistory' ),
279 $this->getOutput()->parse( wfMsgForContent( 'history-feed-empty' ) ),
280 $this->getTitle()->getFullUrl(),
281 wfTimestamp( TS_MW ),
282 '',
283 $this->getTitle()->getTalkPage()->getFullUrl()
284 );
285 }
286
287 /**
288 * Generate a FeedItem object from a given revision table row
289 * Borrows Recent Changes' feed generation functions for formatting;
290 * includes a diff to the previous revision (if any).
291 *
292 * @param $row Object: database row
293 * @return FeedItem
294 */
295 function feedItem( $row ) {
296 $rev = new Revision( $row );
297 $rev->setTitle( $this->getTitle() );
298 $text = FeedUtils::formatDiffRow(
299 $this->getTitle(),
300 $this->getTitle()->getPreviousRevisionID( $rev->getId() ),
301 $rev->getId(),
302 $rev->getTimestamp(),
303 $rev->getComment()
304 );
305 if ( $rev->getComment() == '' ) {
306 global $wgContLang;
307 $title = wfMsgForContent( 'history-feed-item-nocomment',
308 $rev->getUserText(),
309 $wgContLang->timeanddate( $rev->getTimestamp() ),
310 $wgContLang->date( $rev->getTimestamp() ),
311 $wgContLang->time( $rev->getTimestamp() )
312 );
313 } else {
314 $title = $rev->getUserText() .
315 wfMsgForContent( 'colon-separator' ) .
316 FeedItem::stripComment( $rev->getComment() );
317 }
318 return new FeedItem(
319 $title,
320 $text,
321 $this->getTitle()->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
322 $rev->getTimestamp(),
323 $rev->getUserText(),
324 $this->getTitle()->getTalkPage()->getFullUrl()
325 );
326 }
327 }
328
329 /**
330 * @ingroup Pager
331 */
332 class HistoryPager extends ReverseChronologicalPager {
333 public $lastRow = false, $counter, $historyPage, $buttons, $conds;
334 protected $oldIdChecked;
335 protected $preventClickjacking = false;
336 /**
337 * @var array
338 */
339 protected $parentLens;
340
341 function __construct( $historyPage, $year = '', $month = '', $tagFilter = '', $conds = array() ) {
342 parent::__construct( $historyPage->getContext() );
343 $this->historyPage = $historyPage;
344 $this->tagFilter = $tagFilter;
345 $this->getDateCond( $year, $month );
346 $this->conds = $conds;
347 }
348
349 // For hook compatibility...
350 function getArticle() {
351 return $this->historyPage->getArticle();
352 }
353
354 function getSqlComment() {
355 if ( $this->conds ) {
356 return 'history page filtered'; // potentially slow, see CR r58153
357 } else {
358 return 'history page unfiltered';
359 }
360 }
361
362 function getQueryInfo() {
363 $queryInfo = array(
364 'tables' => array( 'revision', 'user' ),
365 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields() ),
366 'conds' => array_merge(
367 array( 'rev_page' => $this->getWikiPage()->getId() ),
368 $this->conds ),
369 'options' => array( 'USE INDEX' => array( 'revision' => 'page_timestamp' ) ),
370 'join_conds' => array(
371 'user' => Revision::userJoinCond(),
372 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
373 );
374 ChangeTags::modifyDisplayQuery(
375 $queryInfo['tables'],
376 $queryInfo['fields'],
377 $queryInfo['conds'],
378 $queryInfo['join_conds'],
379 $queryInfo['options'],
380 $this->tagFilter
381 );
382 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
383 return $queryInfo;
384 }
385
386 function getIndexField() {
387 return 'rev_timestamp';
388 }
389
390 function formatRow( $row ) {
391 if ( $this->lastRow ) {
392 $latest = ( $this->counter == 1 && $this->mIsFirst );
393 $firstInList = $this->counter == 1;
394 $this->counter++;
395 $s = $this->historyLine( $this->lastRow, $row,
396 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
397 } else {
398 $s = '';
399 }
400 $this->lastRow = $row;
401 return $s;
402 }
403
404 function doBatchLookups() {
405 # Do a link batch query
406 $this->mResult->seek( 0 );
407 $batch = new LinkBatch();
408 $revIds = array();
409 foreach ( $this->mResult as $row ) {
410 if( $row->rev_parent_id ) {
411 $revIds[] = $row->rev_parent_id;
412 }
413 if( !is_null( $row->user_name ) ) {
414 $batch->add( NS_USER, $row->user_name );
415 $batch->add( NS_USER_TALK, $row->user_name );
416 } else { # for anons or usernames of imported revisions
417 $batch->add( NS_USER, $row->rev_user_text );
418 $batch->add( NS_USER_TALK, $row->rev_user_text );
419 }
420 }
421 $this->parentLens = $this->getParentLengths( $revIds );
422 $batch->execute();
423 $this->mResult->seek( 0 );
424 }
425
426 /**
427 * Do a batched query to get the parent revision lengths
428 * @param $revIds array
429 * @return array
430 * @TODO: stolen from Contributions, refactor
431 */
432 private function getParentLengths( array $revIds ) {
433 $revLens = array();
434 if ( !$revIds ) {
435 return $revLens; // empty
436 }
437 wfProfileIn( __METHOD__ );
438 $res = $this->mDb->select( 'revision',
439 array( 'rev_id', 'rev_len' ),
440 array( 'rev_id' => $revIds ),
441 __METHOD__ );
442 foreach ( $res as $row ) {
443 $revLens[$row->rev_id] = $row->rev_len;
444 }
445 wfProfileOut( __METHOD__ );
446 return $revLens;
447 }
448
449 /**
450 * Creates begin of history list with a submit button
451 *
452 * @return string HTML output
453 */
454 function getStartBody() {
455 global $wgScript;
456 $this->lastRow = false;
457 $this->counter = 1;
458 $this->oldIdChecked = 0;
459
460 $this->getOutput()->wrapWikiMsg( "<div class='mw-history-legend'>\n$1\n</div>", 'histlegend' );
461 $s = Html::openElement( 'form', array( 'action' => $wgScript,
462 'id' => 'mw-history-compare' ) ) . "\n";
463 $s .= Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) . "\n";
464 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
465
466 // Button container stored in $this->buttons for re-use in getEndBody()
467 $this->buttons = '<div>';
468 $this->buttons .= $this->submitButton( $this->msg( 'compareselectedversions' )->text(),
469 array( 'class' => 'historysubmit mw-history-compareselectedversions-button' )
470 + Linker::tooltipAndAccesskeyAttribs( 'compareselectedversions' )
471 ) . "\n";
472
473 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
474 $this->buttons .= $this->getRevisionButton( 'revisiondelete', 'showhideselectedversions' );
475 }
476 $this->buttons .= '</div>';
477
478 $s .= $this->buttons;
479 $s .= '<ul id="pagehistory">' . "\n";
480 return $s;
481 }
482
483 private function getRevisionButton( $name, $msg ) {
484 $this->preventClickjacking();
485 # Note bug #20966, <button> is non-standard in IE<8
486 $element = Html::element( 'button',
487 array(
488 'type' => 'submit',
489 'name' => $name,
490 'value' => '1',
491 'class' => "historysubmit mw-history-$name-button",
492 ),
493 $this->msg( $msg )->text()
494 ) . "\n";
495 return $element;
496 }
497
498 function getEndBody() {
499 if ( $this->lastRow ) {
500 $latest = $this->counter == 1 && $this->mIsFirst;
501 $firstInList = $this->counter == 1;
502 if ( $this->mIsBackwards ) {
503 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
504 if ( $this->mOffset == '' ) {
505 $next = null;
506 } else {
507 $next = 'unknown';
508 }
509 } else {
510 # The next row is the past-the-end row
511 $next = $this->mPastTheEndRow;
512 }
513 $this->counter++;
514 $s = $this->historyLine( $this->lastRow, $next,
515 $this->getTitle()->getNotificationTimestamp( $this->getUser() ), $latest, $firstInList );
516 } else {
517 $s = '';
518 }
519 $s .= "</ul>\n";
520 # Add second buttons only if there is more than one rev
521 if ( $this->getNumRows() > 2 ) {
522 $s .= $this->buttons;
523 }
524 $s .= '</form>';
525 return $s;
526 }
527
528 /**
529 * Creates a submit button
530 *
531 * @param $message String: text of the submit button, will be escaped
532 * @param $attributes Array: attributes
533 * @return String: HTML output for the submit button
534 */
535 function submitButton( $message, $attributes = array() ) {
536 # Disable submit button if history has 1 revision only
537 if ( $this->getNumRows() > 1 ) {
538 return Xml::submitButton( $message , $attributes );
539 } else {
540 return '';
541 }
542 }
543
544 /**
545 * Returns a row from the history printout.
546 *
547 * @todo document some more, and maybe clean up the code (some params redundant?)
548 *
549 * @param $row Object: the database row corresponding to the previous line.
550 * @param $next Mixed: the database row corresponding to the next line. (chronologically previous)
551 * @param $notificationtimestamp
552 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
553 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
554 * @return String: HTML output for the row
555 */
556 function historyLine( $row, $next, $notificationtimestamp = false,
557 $latest = false, $firstInList = false )
558 {
559 $rev = new Revision( $row );
560 $rev->setTitle( $this->getTitle() );
561
562 if ( is_object( $next ) ) {
563 $prevRev = new Revision( $next );
564 $prevRev->setTitle( $this->getTitle() );
565 } else {
566 $prevRev = null;
567 }
568
569 $curlink = $this->curLink( $rev, $latest );
570 $lastlink = $this->lastLink( $rev, $next );
571 $diffButtons = $this->diffButtons( $rev, $firstInList );
572 $histLinks = Html::rawElement(
573 'span',
574 array( 'class' => 'mw-history-histlinks' ),
575 $this->msg( 'parentheses' )->rawParams( $curlink . $this->historyPage->message['pipe-separator'] . $lastlink )->escaped()
576 );
577 $s = $histLinks . $diffButtons;
578
579 $link = $this->revLink( $rev );
580 $classes = array();
581
582 $del = '';
583 $user = $this->getUser();
584 // Show checkboxes for each revision
585 if ( $user->isAllowed( 'deleterevision' ) ) {
586 $this->preventClickjacking();
587 // If revision was hidden from sysops, disable the checkbox
588 if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
589 $del = Xml::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
590 // Otherwise, enable the checkbox...
591 } else {
592 $del = Xml::check( 'showhiderevisions', false,
593 array( 'name' => 'ids[' . $rev->getId() . ']' ) );
594 }
595 // User can only view deleted revisions...
596 } elseif ( $rev->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) {
597 // If revision was hidden from sysops, disable the link
598 if ( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
599 $cdel = Linker::revDeleteLinkDisabled( false );
600 // Otherwise, show the link...
601 } else {
602 $query = array( 'type' => 'revision',
603 'target' => $this->getTitle()->getPrefixedDbkey(), 'ids' => $rev->getId() );
604 $del .= Linker::revDeleteLink( $query,
605 $rev->isDeleted( Revision::DELETED_RESTRICTED ), false );
606 }
607 }
608 if ( $del ) {
609 $s .= " $del ";
610 }
611
612 $lang = $this->getLanguage();
613 $dirmark = $lang->getDirMark();
614
615 $s .= " $link";
616 $s .= $dirmark;
617 $s .= " <span class='history-user'>" .
618 Linker::revUserTools( $rev, true ) . "</span>";
619 $s .= $dirmark;
620
621 if ( $rev->isMinor() ) {
622 $s .= ' ' . ChangesList::flag( 'minor' );
623 }
624
625 # Size is always public data
626 $prevSize = isset( $this->parentLens[$row->rev_parent_id] )
627 ? $this->parentLens[$row->rev_parent_id]
628 : 0;
629 $sDiff = ChangesList::showCharacterDifference( $prevSize, $rev->getSize() );
630 $fSize = Linker::formatRevisionSize($rev->getSize());
631 $s .= " . . $fSize $sDiff";
632
633 # Text following the character difference is added just before running hooks
634 $s2 = Linker::revComment( $rev, false, true );
635
636 if ( $notificationtimestamp && ( $row->rev_timestamp >= $notificationtimestamp ) ) {
637 $s2 .= ' <span class="updatedmarker">' . $this->msg( 'updatedmarker' )->escaped() . '</span>';
638 $classes[] = 'mw-history-line-updated';
639 }
640
641 $tools = array();
642
643 # Rollback and undo links
644 if ( $prevRev &&
645 !count( $this->getTitle()->getUserPermissionsErrors( 'edit', $this->getUser() ) ) )
646 {
647 if ( $latest && !count( $this->getTitle()->getUserPermissionsErrors( 'rollback', $this->getUser() ) ) ) {
648 $this->preventClickjacking();
649 $tools[] = '<span class="mw-rollback-link">' .
650 Linker::buildRollbackLink( $rev ) . '</span>';
651 }
652
653 if ( !$rev->isDeleted( Revision::DELETED_TEXT )
654 && !$prevRev->isDeleted( Revision::DELETED_TEXT ) )
655 {
656 # Create undo tooltip for the first (=latest) line only
657 $undoTooltip = $latest
658 ? array( 'title' => $this->msg( 'tooltip-undo' )->text() )
659 : array();
660 $undolink = Linker::linkKnown(
661 $this->getTitle(),
662 $this->msg( 'editundo' )->escaped(),
663 $undoTooltip,
664 array(
665 'action' => 'edit',
666 'undoafter' => $prevRev->getId(),
667 'undo' => $rev->getId()
668 )
669 );
670 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
671 }
672 }
673
674 if ( $tools ) {
675 $s2 .= ' '. $this->msg( 'parentheses' )->rawParams( $lang->pipeList( $tools ) )->escaped();
676 }
677
678 # Tags
679 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
680 $classes = array_merge( $classes, $newClasses );
681 if ( $tagSummary !== '' ) {
682 $s2 .= " $tagSummary";
683 }
684
685 # Include separator between character difference and following text
686 if ( $s2 !== '' ) {
687 $s .= " . . $s2";
688 }
689
690 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
691
692 $attribs = array();
693 if ( $classes ) {
694 $attribs['class'] = implode( ' ', $classes );
695 }
696
697 return Xml::tags( 'li', $attribs, $s ) . "\n";
698 }
699
700 /**
701 * Create a link to view this revision of the page
702 *
703 * @param $rev Revision
704 * @return String
705 */
706 function revLink( $rev ) {
707 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $this->getUser() );
708 $date = htmlspecialchars( $date );
709 if ( $rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
710 $link = Linker::linkKnown(
711 $this->getTitle(),
712 $date,
713 array(),
714 array( 'oldid' => $rev->getId() )
715 );
716 } else {
717 $link = $date;
718 }
719 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
720 $link = "<span class=\"history-deleted\">$link</span>";
721 }
722 return $link;
723 }
724
725 /**
726 * Create a diff-to-current link for this revision for this page
727 *
728 * @param $rev Revision
729 * @param $latest Boolean: this is the latest revision of the page?
730 * @return String
731 */
732 function curLink( $rev, $latest ) {
733 $cur = $this->historyPage->message['cur'];
734 if ( $latest || !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
735 return $cur;
736 } else {
737 return Linker::linkKnown(
738 $this->getTitle(),
739 $cur,
740 array(),
741 array(
742 'diff' => $this->getWikiPage()->getLatest(),
743 'oldid' => $rev->getId()
744 )
745 );
746 }
747 }
748
749 /**
750 * Create a diff-to-previous link for this revision for this page.
751 *
752 * @param $prevRev Revision: the previous revision
753 * @param $next Mixed: the newer revision
754 * @return String
755 */
756 function lastLink( $prevRev, $next ) {
757 $last = $this->historyPage->message['last'];
758 # $next may either be a Row, null, or "unkown"
759 $nextRev = is_object( $next ) ? new Revision( $next ) : $next;
760 if ( is_null( $next ) ) {
761 # Probably no next row
762 return $last;
763 } elseif ( $next === 'unknown' ) {
764 # Next row probably exists but is unknown, use an oldid=prev link
765 return Linker::linkKnown(
766 $this->getTitle(),
767 $last,
768 array(),
769 array(
770 'diff' => $prevRev->getId(),
771 'oldid' => 'prev'
772 )
773 );
774 } elseif ( !$prevRev->userCan( Revision::DELETED_TEXT, $this->getUser() )
775 || !$nextRev->userCan( Revision::DELETED_TEXT, $this->getUser() ) )
776 {
777 return $last;
778 } else {
779 return Linker::linkKnown(
780 $this->getTitle(),
781 $last,
782 array(),
783 array(
784 'diff' => $prevRev->getId(),
785 'oldid' => $next->rev_id
786 )
787 );
788 }
789 }
790
791 /**
792 * Create radio buttons for page history
793 *
794 * @param $rev Revision object
795 * @param $firstInList Boolean: is this version the first one?
796 *
797 * @return String: HTML output for the radio buttons
798 */
799 function diffButtons( $rev, $firstInList ) {
800 if ( $this->getNumRows() > 1 ) {
801 $id = $rev->getId();
802 $radio = array( 'type' => 'radio', 'value' => $id );
803 /** @todo: move title texts to javascript */
804 if ( $firstInList ) {
805 $first = Xml::element( 'input',
806 array_merge( $radio, array(
807 'style' => 'visibility:hidden',
808 'name' => 'oldid',
809 'id' => 'mw-oldid-null' ) )
810 );
811 $checkmark = array( 'checked' => 'checked' );
812 } else {
813 # Check visibility of old revisions
814 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
815 $radio['disabled'] = 'disabled';
816 $checkmark = array(); // We will check the next possible one
817 } elseif ( !$this->oldIdChecked ) {
818 $checkmark = array( 'checked' => 'checked' );
819 $this->oldIdChecked = $id;
820 } else {
821 $checkmark = array();
822 }
823 $first = Xml::element( 'input',
824 array_merge( $radio, $checkmark, array(
825 'name' => 'oldid',
826 'id' => "mw-oldid-$id" ) ) );
827 $checkmark = array();
828 }
829 $second = Xml::element( 'input',
830 array_merge( $radio, $checkmark, array(
831 'name' => 'diff',
832 'id' => "mw-diff-$id" ) ) );
833 return $first . $second;
834 } else {
835 return '';
836 }
837 }
838
839 /**
840 * This is called if a write operation is possible from the generated HTML
841 */
842 function preventClickjacking( $enable = true ) {
843 $this->preventClickjacking = $enable;
844 }
845
846 /**
847 * Get the "prevent clickjacking" flag
848 * @return bool
849 */
850 function getPreventClickjacking() {
851 return $this->preventClickjacking;
852 }
853 }
854
855 /**
856 * Backwards-compatibility alias
857 */
858 class HistoryPage extends HistoryAction {
859 public function __construct( Page $article ) { # Just to make it public
860 parent::__construct( $article );
861 }
862
863 public function history() {
864 $this->onView();
865 }
866 }