Fix some doxygen warnings
[mediawiki.git] / includes / HistoryPage.php
blobc52e4eb550f7a06e967a96ce1594db514cd7033b
1 <?php
2 /**
3 * Page history
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @file
7 */
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.
14 * Construct it by passing in an Article, and call $h->history() to print the
15 * history.
18 class HistoryPage {
19 const DIR_PREV = 0;
20 const DIR_NEXT = 1;
22 var $article, $title, $skin;
24 /**
25 * Construct a new HistoryPage.
27 * @param $article Article
28 * @return nothing
30 function __construct( $article ) {
31 global $wgUser;
32 $this->article = $article;
33 $this->title = $article->getTitle();
34 $this->skin = $wgUser->getSkin();
35 $this->preCacheMessages();
38 function getArticle() {
39 return $this->article;
42 function getTitle() {
43 return $this->title;
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
50 function preCacheMessages() {
51 // Precache various messages
52 if( !isset( $this->message ) ) {
53 $msgs = array( 'cur', 'last', 'pipe-separator' );
54 foreach( $msgs as $msg ) {
55 $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities') );
60 /**
61 * Print the history page for an article.
63 * @return nothing
65 function history() {
66 global $wgOut, $wgRequest, $wgScript;
69 * Allow client caching.
71 if( $wgOut->checkLastModified( $this->article->getTouched() ) )
72 return; // Client cache fresh and headers sent, nothing more to do.
74 wfProfileIn( __METHOD__ );
77 * Setup page variables.
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' );
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' )
96 $wgOut->setSubtitle( $logLink );
98 $feedType = $wgRequest->getVal( 'feed' );
99 if( $feedType ) {
100 wfProfileOut( __METHOD__ );
101 return $this->feed( $feedType );
105 * Fail if article doesn't exist.
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(),
115 array( 'lim' => 10,
116 'conds' => array( "log_action != 'revision'" ),
117 'showIfEmpty' => false,
118 'msgKey' => array( 'moveddeleted-notice' )
121 wfProfileOut( __METHOD__ );
122 return;
126 * Add date selector to quickly get to a certain time
128 $year = $wgRequest->getInt( 'year' );
129 $month = $wgRequest->getInt( 'month' );
130 $tagFilter = $wgRequest->getVal( 'tagfilter' );
131 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
133 * Option to show only revisions that have been (partially) hidden via RevisionDelete
134 * Note that this can run a *long* time if there are many revisions to look at.
135 * We use "isBigDeletion" to determine if the history is too big to go through.
136 * Additionally, only users with 'deleterevision' right can filter for deleted edits.
138 if ( $this->title->userCan( 'deleterevision' ) && ( !$this->article->isBigDeletion() || $this->title->userCan( 'bigdelete' ) ) ) {
139 $conds = ( $wgRequest->getBool( 'deleted' ) ) ? array("rev_deleted != '0'") : array();
140 $checkDeleted = Xml::checkLabel( wfMsg( 'history-show-deleted' ), 'deleted', '', $wgRequest->getBool( 'deleted' ) ) . "\n";
142 else { # Don't filter and don't add the checkbox for filtering
143 $conds = array();
144 $checkDeleted = '';
147 $action = htmlspecialchars( $wgScript );
148 $wgOut->addHTML(
149 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
150 Xml::fieldset(
151 wfMsg( 'history-fieldset-title' ),
152 false,
153 array( 'id' => 'mw-history-search' )
155 Xml::hidden( 'title', $this->title->getPrefixedDBKey() ) . "\n" .
156 Xml::hidden( 'action', 'history' ) . "\n" .
157 Xml::dateMenu( $year, $month ) . '&nbsp;' .
158 ( $tagSelector ? ( implode( '&nbsp;', $tagSelector ) . '&nbsp;' ) : '' ) .
159 $checkDeleted .
160 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
161 '</fieldset></form>'
164 wfRunHooks( 'PageHistoryBeforeList', array( &$this->article ) );
167 * Do the list
169 $pager = new HistoryPager( $this, $year, $month, $tagFilter, $conds );
170 $wgOut->addHTML(
171 $pager->getNavigationBar() .
172 $pager->getBody() .
173 $pager->getNavigationBar()
176 wfProfileOut( __METHOD__ );
180 * Fetch an array of revisions, specified by a given limit, offset and
181 * direction. This is now only used by the feeds. It was previously
182 * used by the main UI but that's now handled by the pager.
184 * @param $limit Integer: the limit number of revisions to get
185 * @param $offset Integer
186 * @param $direction Integer: either HistoryPage::DIR_PREV or HistoryPage::DIR_NEXT
187 * @return ResultWrapper
189 function fetchRevisions( $limit, $offset, $direction ) {
190 $dbr = wfGetDB( DB_SLAVE );
192 if( $direction == HistoryPage::DIR_PREV )
193 list($dirs, $oper) = array("ASC", ">=");
194 else /* $direction == HistoryPage::DIR_NEXT */
195 list($dirs, $oper) = array("DESC", "<=");
197 if( $offset )
198 $offsets = array("rev_timestamp $oper '$offset'");
199 else
200 $offsets = array();
202 $page_id = $this->title->getArticleID();
204 return $dbr->select( 'revision',
205 Revision::selectFields(),
206 array_merge(array("rev_page=$page_id"), $offsets),
207 __METHOD__,
208 array( 'ORDER BY' => "rev_timestamp $dirs",
209 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
214 * Output a subscription feed listing recent edits to this page.
216 * @param $type String: feed type
218 function feed( $type ) {
219 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
220 if( !FeedUtils::checkFeedOutput($type) ) {
221 return;
224 $feed = new $wgFeedClasses[$type](
225 $this->title->getPrefixedText() . ' - ' .
226 wfMsgForContent( 'history-feed-title' ),
227 wfMsgForContent( 'history-feed-description' ),
228 $this->title->getFullUrl( 'action=history' )
231 // Get a limit on number of feed entries. Provide a sane default
232 // of 10 if none is defined (but limit to $wgFeedLimit max)
233 $limit = $wgRequest->getInt( 'limit', 10 );
234 if( $limit > $wgFeedLimit || $limit < 1 ) {
235 $limit = 10;
237 $items = $this->fetchRevisions($limit, 0, HistoryPage::DIR_NEXT);
239 $feed->outHeader();
240 if( $items ) {
241 foreach( $items as $row ) {
242 $feed->outItem( $this->feedItem( $row ) );
244 } else {
245 $feed->outItem( $this->feedEmpty() );
247 $feed->outFooter();
250 function feedEmpty() {
251 global $wgOut;
252 return new FeedItem(
253 wfMsgForContent( 'nohistory' ),
254 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
255 $this->title->getFullUrl(),
256 wfTimestamp( TS_MW ),
258 $this->title->getTalkPage()->getFullUrl()
263 * Generate a FeedItem object from a given revision table row
264 * Borrows Recent Changes' feed generation functions for formatting;
265 * includes a diff to the previous revision (if any).
267 * @param $row Object: database row
268 * @return FeedItem
270 function feedItem( $row ) {
271 $rev = new Revision( $row );
272 $rev->setTitle( $this->title );
273 $text = FeedUtils::formatDiffRow(
274 $this->title,
275 $this->title->getPreviousRevisionID( $rev->getId() ),
276 $rev->getId(),
277 $rev->getTimestamp(),
278 $rev->getComment()
280 if( $rev->getComment() == '' ) {
281 global $wgContLang;
282 $title = wfMsgForContent( 'history-feed-item-nocomment',
283 $rev->getUserText(),
284 $wgContLang->timeanddate( $rev->getTimestamp() ),
285 $wgContLang->date( $rev->getTimestamp() ),
286 $wgContLang->time( $rev->getTimestamp() )
288 } else {
289 $title = $rev->getUserText() .
290 wfMsgForContent( 'colon-separator' ) .
291 FeedItem::stripComment( $rev->getComment() );
293 return new FeedItem(
294 $title,
295 $text,
296 $this->title->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
297 $rev->getTimestamp(),
298 $rev->getUserText(),
299 $this->title->getTalkPage()->getFullUrl()
305 * @ingroup Pager
307 class HistoryPager extends ReverseChronologicalPager {
308 public $lastRow = false, $counter, $historyPage, $title, $buttons, $conds;
309 protected $oldIdChecked;
311 function __construct( $historyPage, $year='', $month='', $tagFilter = '', $conds = array() ) {
312 parent::__construct();
313 $this->historyPage = $historyPage;
314 $this->title = $this->historyPage->title;
315 $this->tagFilter = $tagFilter;
316 $this->getDateCond( $year, $month );
317 $this->conds = $conds;
320 // For hook compatibility...
321 function getArticle() {
322 return $this->historyPage->getArticle();
325 function getQueryInfo() {
326 $queryInfo = array(
327 'tables' => array('revision'),
328 'fields' => Revision::selectFields(),
329 'conds' => array_merge( array('rev_page' => $this->historyPage->title->getArticleID() ), $this->conds ),
330 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
331 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
333 ChangeTags::modifyDisplayQuery(
334 $queryInfo['tables'],
335 $queryInfo['fields'],
336 $queryInfo['conds'],
337 $queryInfo['join_conds'],
338 $queryInfo['options'],
339 $this->tagFilter
341 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
342 return $queryInfo;
345 function getIndexField() {
346 return 'rev_timestamp';
349 function formatRow( $row ) {
350 if( $this->lastRow ) {
351 $latest = ($this->counter == 1 && $this->mIsFirst);
352 $firstInList = $this->counter == 1;
353 $s = $this->historyLine( $this->lastRow, $row, $this->counter++,
354 $this->title->getNotificationTimestamp(), $latest, $firstInList );
355 } else {
356 $s = '';
358 $this->lastRow = $row;
359 return $s;
363 * Creates begin of history list with a submit button
365 * @return string HTML output
367 function getStartBody() {
368 global $wgScript, $wgUser, $wgOut, $wgContLang;
369 $this->lastRow = false;
370 $this->counter = 1;
371 $this->oldIdChecked = 0;
373 $wgOut->wrapWikiMsg( "<div class='mw-history-legend'>\n$1</div>", 'histlegend' );
374 $s = Xml::openElement( 'form', array( 'action' => $wgScript,
375 'id' => 'mw-history-compare' ) ) . "\n";
376 $s .= Xml::hidden( 'title', $this->title->getPrefixedDbKey() ) . "\n";
377 $s .= Xml::hidden( 'action', 'historysubmit' ) . "\n";
379 $this->buttons = '<div>';
380 if( $wgUser->isAllowed('deleterevision') ) {
381 $float = $wgContLang->alignEnd();
382 # Note bug #20966, <button> is non-standard in IE<8
383 $this->buttons .= Xml::element( 'button',
384 array(
385 'type' => 'submit',
386 'name' => 'revisiondelete',
387 'value' => '1',
388 'style' => "float: $float;",
389 'class' => 'mw-history-revisiondelete-button',
391 wfMsg( 'showhideselectedversions' )
392 ) . "\n";
394 $this->buttons .= $this->submitButton( wfMsg( 'compareselectedversions'),
395 array(
396 'class' => 'historysubmit',
397 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
398 'title' => wfMsg( 'tooltip-compareselectedversions' ),
400 ) . "\n";
401 $this->buttons .= '</div>';
402 $s .= $this->buttons . '<ul id="pagehistory">' . "\n";
403 return $s;
406 function getEndBody() {
407 if( $this->lastRow ) {
408 $latest = $this->counter == 1 && $this->mIsFirst;
409 $firstInList = $this->counter == 1;
410 if( $this->mIsBackwards ) {
411 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
412 if( $this->mOffset == '' ) {
413 $next = null;
414 } else {
415 $next = 'unknown';
417 } else {
418 # The next row is the past-the-end row
419 $next = $this->mPastTheEndRow;
421 $s = $this->historyLine( $this->lastRow, $next, $this->counter++,
422 $this->title->getNotificationTimestamp(), $latest, $firstInList );
423 } else {
424 $s = '';
426 $s .= "</ul>\n";
427 # Add second buttons only if there is more than one rev
428 if( $this->getNumRows() > 2 ) {
429 $s .= $this->buttons;
431 $s .= '</form>';
432 return $s;
436 * Creates a submit button
438 * @param $message String: text of the submit button, will be escaped
439 * @param $attributes Array: attributes
440 * @return String: HTML output for the submit button
442 function submitButton( $message, $attributes = array() ) {
443 # Disable submit button if history has 1 revision only
444 if( $this->getNumRows() > 1 ) {
445 return Xml::submitButton( $message , $attributes );
446 } else {
447 return '';
452 * Returns a row from the history printout.
454 * @todo document some more, and maybe clean up the code (some params redundant?)
456 * @param $row Object: the database row corresponding to the previous line.
457 * @param $next Mixed: the database row corresponding to the next line.
458 * @param $counter Integer: apparently a counter of what row number we're at, counted from the top row = 1.
459 * @param $notificationtimestamp
460 * @param $latest Boolean: whether this row corresponds to the page's latest revision.
461 * @param $firstInList Boolean: whether this row corresponds to the first displayed on this history page.
462 * @return String: HTML output for the row
464 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
465 $latest = false, $firstInList = false )
467 global $wgUser, $wgLang;
468 $rev = new Revision( $row );
469 $rev->setTitle( $this->title );
471 $curlink = $this->curLink( $rev, $latest );
472 $lastlink = $this->lastLink( $rev, $next, $counter );
473 $diffButtons = $this->diffButtons( $rev, $firstInList, $counter );
474 $histLinks = Html::rawElement(
475 'span',
476 array( 'class' => 'mw-history-histlinks' ),
477 '(' . $curlink . $this->historyPage->message['pipe-separator'] . $lastlink . ') '
479 $s = $histLinks . $diffButtons;
481 $link = $this->revLink( $rev );
482 $classes = array();
484 $del = '';
485 // User can delete revisions...
486 if( $wgUser->isAllowed( 'deleterevision' ) ) {
487 // If revision was hidden from sysops, disable the checkbox
488 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
489 $del = Xml::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
490 // Otherwise, enable the checkbox...
491 } else {
492 $del = Xml::check( 'showhiderevisions', false, array( 'name' => 'ids['.$rev->getId().']' ) );
494 // User can only view deleted revisions...
495 } else if( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) {
496 // If revision was hidden from sysops, disable the link
497 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
498 $cdel = $this->getSkin()->revDeleteLinkDisabled( false );
499 // Otherwise, show the link...
500 } else {
501 $query = array( 'type' => 'revision',
502 'target' => $this->title->getPrefixedDbkey(), 'ids' => $rev->getId() );
503 $del .= $this->getSkin()->revDeleteLink( $query,
504 $rev->isDeleted( Revision::DELETED_RESTRICTED ), false );
507 if( $del ) $s .= " $del ";
509 $s .= " $link";
510 $s .= " <span class='history-user'>" . $this->getSkin()->revUserTools( $rev, true ) . "</span>";
512 if( $rev->isMinor() ) {
513 $s .= ' ' . ChangesList::flag( 'minor' );
516 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
517 $s .= ' ' . $this->getSkin()->formatRevisionSize( $size );
520 $s .= $this->getSkin()->revComment( $rev, false, true );
522 if( $notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp) ) {
523 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
526 $tools = array();
528 # Rollback and undo links
529 if( !is_null( $next ) && is_object( $next ) ) {
530 if( $latest && $this->title->userCan( 'rollback' ) && $this->title->userCan( 'edit' ) ) {
531 $tools[] = '<span class="mw-rollback-link">'.
532 $this->getSkin()->buildRollbackLink( $rev ).'</span>';
535 if( $this->title->quickUserCan( 'edit' )
536 && !$rev->isDeleted( Revision::DELETED_TEXT )
537 && !$next->rev_deleted & Revision::DELETED_TEXT )
539 # Create undo tooltip for the first (=latest) line only
540 $undoTooltip = $latest
541 ? array( 'title' => wfMsg( 'tooltip-undo' ) )
542 : array();
543 $undolink = $this->getSkin()->link(
544 $this->title,
545 wfMsgHtml( 'editundo' ),
546 $undoTooltip,
547 array(
548 'action' => 'edit',
549 'undoafter' => $next->rev_id,
550 'undo' => $rev->getId()
552 array( 'known', 'noclasses' )
554 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
558 if( $tools ) {
559 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
562 # Tags
563 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
564 $classes = array_merge( $classes, $newClasses );
565 $s .= " $tagSummary";
567 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s, &$classes ) );
569 $attribs = array();
570 if ( $classes ) {
571 $attribs['class'] = implode( ' ', $classes );
574 return Xml::tags( 'li', $attribs, $s ) . "\n";
578 * Create a link to view this revision of the page
580 * @param $rev Revision
581 * @return String
583 function revLink( $rev ) {
584 global $wgLang;
585 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
586 $date = htmlspecialchars( $date );
587 if( !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
588 $link = $this->getSkin()->link(
589 $this->title,
590 $date,
591 array(),
592 array( 'oldid' => $rev->getId() ),
593 array( 'known', 'noclasses' )
595 } else {
596 $link = "<span class=\"history-deleted\">$date</span>";
598 return $link;
602 * Create a diff-to-current link for this revision for this page
604 * @param $rev Revision
605 * @param $latest Boolean: this is the latest revision of the page?
606 * @return String
608 function curLink( $rev, $latest ) {
609 $cur = $this->historyPage->message['cur'];
610 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
611 return $cur;
612 } else {
613 return $this->getSkin()->link(
614 $this->title,
615 $cur,
616 array(),
617 array(
618 'diff' => $this->title->getLatestRevID(),
619 'oldid' => $rev->getId()
621 array( 'known', 'noclasses' )
627 * Create a diff-to-previous link for this revision for this page.
629 * @param $prevRev Revision: the previous revision
630 * @param $next Mixed: the newer revision
631 * @param $counter Integer: what row on the history list this is
632 * @return String
634 function lastLink( $prevRev, $next, $counter ) {
635 $last = $this->historyPage->message['last'];
636 # $next may either be a Row, null, or "unkown"
637 $nextRev = is_object($next) ? new Revision( $next ) : $next;
638 if( is_null($next) ) {
639 # Probably no next row
640 return $last;
641 } elseif( $next === 'unknown' ) {
642 # Next row probably exists but is unknown, use an oldid=prev link
643 return $this->getSkin()->link(
644 $this->title,
645 $last,
646 array(),
647 array(
648 'diff' => $prevRev->getId(),
649 'oldid' => 'prev'
651 array( 'known', 'noclasses' )
653 } elseif( !$prevRev->userCan(Revision::DELETED_TEXT) || !$nextRev->userCan(Revision::DELETED_TEXT) ) {
654 return $last;
655 } else {
656 return $this->getSkin()->link(
657 $this->title,
658 $last,
659 array(),
660 array(
661 'diff' => $prevRev->getId(),
662 'oldid' => $next->rev_id
664 array( 'known', 'noclasses' )
670 * Create radio buttons for page history
672 * @param $rev Revision object
673 * @param $firstInList Boolean: is this version the first one?
674 * @param $counter Integer: a counter of what row number we're at, counted from the top row = 1.
675 * @return String: HTML output for the radio buttons
677 function diffButtons( $rev, $firstInList, $counter ) {
678 if( $this->getNumRows() > 1 ) {
679 $id = $rev->getId();
680 $radio = array( 'type' => 'radio', 'value' => $id );
681 /** @todo: move title texts to javascript */
682 if( $firstInList ) {
683 $first = Xml::element( 'input',
684 array_merge( $radio, array(
685 'style' => 'visibility:hidden',
686 'name' => 'oldid',
687 'id' => 'mw-oldid-null' ) )
689 $checkmark = array( 'checked' => 'checked' );
690 } else {
691 # Check visibility of old revisions
692 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
693 $radio['disabled'] = 'disabled';
694 $checkmark = array(); // We will check the next possible one
695 } else if( $counter == 2 || !$this->oldIdChecked ) {
696 $checkmark = array( 'checked' => 'checked' );
697 $this->oldIdChecked = $id;
698 } else {
699 $checkmark = array();
701 $first = Xml::element( 'input',
702 array_merge( $radio, $checkmark, array(
703 'name' => 'oldid',
704 'id' => "mw-oldid-$id" ) ) );
705 $checkmark = array();
707 $second = Xml::element( 'input',
708 array_merge( $radio, $checkmark, array(
709 'name' => 'diff',
710 'id' => "mw-diff-$id" ) ) );
711 return $first . $second;
712 } else {
713 return '';
719 * Backwards-compatibility aliases
721 class PageHistory extends HistoryPage {}
722 class PageHistoryPager extends HistoryPager {}