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