Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / includes / PageHistory.php
blobcbef1f4ca9398081874b1e82340b3d26d9918719
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 PageHistory {
19 const DIR_PREV = 0;
20 const DIR_NEXT = 1;
22 var $mArticle, $mTitle, $mSkin;
23 var $lastdate;
24 var $linesonpage;
25 var $mLatestId = null;
27 private $mOldIdChecked = 0;
29 /**
30 * Construct a new PageHistory.
32 * @param Article $article
33 * @returns nothing
35 function __construct( $article ) {
36 global $wgUser;
37 $this->mArticle =& $article;
38 $this->mTitle =& $article->mTitle;
39 $this->mSkin = $wgUser->getSkin();
40 $this->preCacheMessages();
43 function getArticle() {
44 return $this->mArticle;
47 function getTitle() {
48 return $this->mTitle;
51 /**
52 * As we use the same small set of messages in various methods and that
53 * they are called often, we call them once and save them in $this->message
55 function preCacheMessages() {
56 // Precache various messages
57 if( !isset( $this->message ) ) {
58 foreach( explode(' ', 'cur last rev-delundel' ) as $msg ) {
59 $this->message[$msg] = wfMsgExt( $msg, array( 'escape') );
64 /**
65 * Print the history page for an article.
67 * @returns nothing
69 function history() {
70 global $wgOut, $wgRequest, $wgTitle, $wgScript;
73 * Allow client caching.
75 if( $wgOut->checkLastModified( $this->mArticle->getTouched() ) )
76 return; // Client cache fresh and headers sent, nothing more to do.
78 wfProfileIn( __METHOD__ );
81 * Setup page variables.
83 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle->getPrefixedText() ) );
84 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
85 $wgOut->setArticleFlag( false );
86 $wgOut->setArticleRelated( true );
87 $wgOut->setRobotPolicy( 'noindex,nofollow' );
88 $wgOut->setSyndicated( true );
89 $wgOut->setFeedAppendQuery( 'action=history' );
90 $wgOut->addScriptFile( 'history.js' );
92 $logPage = SpecialPage::getTitleFor( 'Log' );
93 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ),
94 'page=' . $this->mTitle->getPrefixedUrl() );
95 $wgOut->setSubtitle( $logLink );
97 $feedType = $wgRequest->getVal( 'feed' );
98 if( $feedType ) {
99 wfProfileOut( __METHOD__ );
100 return $this->feed( $feedType );
104 * Fail if article doesn't exist.
106 if( !$this->mTitle->exists() ) {
107 $wgOut->addWikiMsg( 'nohistory' );
108 wfProfileOut( __METHOD__ );
109 return;
113 * Add date selector to quickly get to a certain time
115 $year = $wgRequest->getInt( 'year' );
116 $month = $wgRequest->getInt( 'month' );
117 $tagFilter = $wgRequest->getVal( 'tagfilter' );
118 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
120 $action = htmlspecialchars( $wgScript );
121 $wgOut->addHTML(
122 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
123 Xml::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
124 Xml::hidden( 'title', $this->mTitle->getPrefixedDBKey() ) . "\n" .
125 Xml::hidden( 'action', 'history' ) . "\n" .
126 xml::dateMenu( $year, $month ) . '&nbsp;' .
127 ( $tagSelector ? ( implode( '&nbsp;', $tagSelector ) . '&nbsp;' ) : '' ) .
128 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
129 '</fieldset></form>'
132 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle ) );
135 * Do the list
137 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
138 $this->linesonpage = $pager->getNumRows();
139 $wgOut->addHTML(
140 $pager->getNavigationBar() .
141 $this->beginHistoryList() .
142 $pager->getBody() .
143 $this->endHistoryList() .
144 $pager->getNavigationBar()
147 wfProfileOut( __METHOD__ );
151 * Creates begin of history list with a submit button
153 * @return string HTML output
155 function beginHistoryList() {
156 global $wgTitle, $wgScript, $wgEnableHtmlDiff;
157 $this->lastdate = '';
158 $s = wfMsgExt( 'histlegend', array( 'parse') );
159 $s .= Xml::openElement( 'form', array( 'action' => $wgScript, 'id' => 'mw-history-compare' ) );
160 $s .= Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() );
161 if( $wgEnableHtmlDiff ) {
162 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
163 array(
164 'name' => 'htmldiff',
165 'class' => 'historysubmit',
166 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
167 'title' => wfMsg( 'tooltip-compareselectedversions' ),
170 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
171 array(
172 'class' => 'historysubmit',
173 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
174 'title' => wfMsg( 'tooltip-compareselectedversions' ),
177 } else {
178 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
179 array(
180 'class' => 'historysubmit',
181 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
182 'title' => wfMsg( 'tooltip-compareselectedversions' ),
186 $s .= '<ul id="pagehistory">' . "\n";
187 return $s;
191 * Creates end of history list with a submit button
193 * @return string HTML output
195 function endHistoryList() {
196 global $wgEnableHtmlDiff;
197 $s = '</ul>';
198 if( $wgEnableHtmlDiff ) {
199 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
200 array(
201 'name' => 'htmldiff',
202 'class' => 'historysubmit',
203 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
204 'title' => wfMsg( 'tooltip-compareselectedversions' ),
207 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
208 array(
209 'class' => 'historysubmit',
210 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
211 'title' => wfMsg( 'tooltip-compareselectedversions' ),
214 } else {
215 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
216 array(
217 'class' => 'historysubmit',
218 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
219 'title' => wfMsg( 'tooltip-compareselectedversions' ),
223 $s .= '</form>';
224 return $s;
228 * Creates a submit button
230 * @param array $attributes attributes
231 * @return string HTML output for the submit button
233 function submitButton($message, $attributes = array() ) {
234 # Disable submit button if history has 1 revision only
235 if( $this->linesonpage > 1 ) {
236 return Xml::submitButton( $message , $attributes );
237 } else {
238 return '';
243 * Returns a row from the history printout.
245 * @todo document some more, and maybe clean up the code (some params redundant?)
247 * @param Row $row The database row corresponding to the previous line.
248 * @param mixed $next The database row corresponding to the next line.
249 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
250 * @param $notificationtimestamp
251 * @param bool $latest Whether this row corresponds to the page's latest revision.
252 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
253 * @return string HTML output for the row
255 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
256 global $wgUser, $wgLang;
257 $rev = new Revision( $row );
258 $rev->setTitle( $this->mTitle );
260 $curlink = $this->curLink( $rev, $latest );
261 $lastlink = $this->lastLink( $rev, $next, $counter );
262 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
263 $link = $this->revLink( $rev );
264 $classes = array();
266 $s = "($curlink) ($lastlink) $arbitrary";
268 if( $wgUser->isAllowed( 'deleterevision' ) ) {
269 if( $firstInList ) {
270 // We don't currently handle well changing the top revision's settings
271 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message['rev-delundel'].')' );
272 } else if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
273 // If revision was hidden from sysops
274 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.$this->message['rev-delundel'].')' );
275 } else {
276 $query = array( 'target' => $this->mTitle->getPrefixedDbkey(),
277 'oldid' => $rev->getId()
279 $del = $this->mSkin->revDeleteLink( $query, $rev->isDeleted( Revision::DELETED_RESTRICTED ) );
281 $s .= " $del ";
284 $s .= " $link";
285 $s .= " <span class='history-user'>" . $this->mSkin->revUserTools( $rev, true ) . "</span>";
287 if( $rev->isMinor() ) {
288 $s .= ' ' . Xml::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
291 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
292 $s .= ' ' . $this->mSkin->formatRevisionSize( $size );
295 $s .= $this->mSkin->revComment( $rev, false, true );
297 if( $notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp) ) {
298 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
300 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
301 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
304 $tools = array();
306 if( !is_null( $next ) && is_object( $next ) ) {
307 if( $latest && $this->mTitle->userCan( 'rollback' ) && $this->mTitle->userCan( 'edit' ) ) {
308 $tools[] = '<span class="mw-rollback-link">'.$this->mSkin->buildRollbackLink( $rev ).'</span>';
311 if( $this->mTitle->quickUserCan( 'edit' ) && !$rev->isDeleted( Revision::DELETED_TEXT ) &&
312 !$next->rev_deleted & Revision::DELETED_TEXT )
314 # Create undo tooltip for the first (=latest) line only
315 $undoTooltip = $latest
316 ? array( 'title' => wfMsg( 'tooltip-undo' ) )
317 : array();
318 $undolink = $this->mSkin->link(
319 $this->mTitle,
320 wfMsgHtml( 'editundo' ),
321 $undoTooltip,
322 array( 'action' => 'edit', 'undoafter' => $next->rev_id, 'undo' => $rev->getId() ),
323 array( 'known', 'noclasses' )
325 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
329 if( $tools ) {
330 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
333 # Tags
334 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'history' );
335 $classes = array_merge( $classes, $newClasses );
336 $s .= " $tagSummary";
338 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
340 $classes = implode( ' ', $classes );
342 return "<li class=\"$classes\">$s</li>\n";
346 * Create a link to view this revision of the page
347 * @param Revision $rev
348 * @returns string
350 function revLink( $rev ) {
351 global $wgLang;
352 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
353 if( !$rev->isDeleted( Revision::DELETED_TEXT ) ) {
354 $link = $this->mSkin->makeKnownLinkObj( $this->mTitle, $date, "oldid=" . $rev->getId() );
355 } else {
356 $link = '<span class="history-deleted">' . $date . '</span>';
358 return $link;
362 * Create a diff-to-current link for this revision for this page
363 * @param Revision $rev
364 * @param Bool $latest, this is the latest revision of the page?
365 * @returns string
367 function curLink( $rev, $latest ) {
368 $cur = $this->message['cur'];
369 if( $latest || $rev->isDeleted( Revision::DELETED_TEXT ) ) {
370 return $cur;
371 } else {
372 return $this->mSkin->makeKnownLinkObj( $this->mTitle, $cur,
373 'diff=' . $this->mTitle->getLatestRevID() . "&oldid=" . $rev->getId() );
378 * Create a diff-to-previous link for this revision for this page.
379 * @param Revision $prevRev, the previous revision
380 * @param mixed $next, the newer revision
381 * @param int $counter, what row on the history list this is
382 * @returns string
384 function lastLink( $prevRev, $next, $counter ) {
385 $last = $this->message['last'];
386 # $next may either be a Row, null, or "unkown"
387 $nextRev = is_object($next) ? new Revision( $next ) : $next;
388 if( is_null($next) ) {
389 # Probably no next row
390 return $last;
391 } elseif( $next === 'unknown' ) {
392 # Next row probably exists but is unknown, use an oldid=prev link
393 return $this->mSkin->makeKnownLinkObj( $this->mTitle, $last,
394 "diff=" . $prevRev->getId() . "&oldid=prev" );
395 } elseif( $prevRev->isDeleted(Revision::DELETED_TEXT) || $nextRev->isDeleted(Revision::DELETED_TEXT) ) {
396 return $last;
397 } else {
398 return $this->mSkin->makeKnownLinkObj( $this->mTitle, $last,
399 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
404 * Create radio buttons for page history
406 * @param object $rev Revision
407 * @param bool $firstInList Is this version the first one?
408 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
409 * @return string HTML output for the radio buttons
411 function diffButtons( $rev, $firstInList, $counter ) {
412 if( $this->linesonpage > 1 ) {
413 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
414 /** @todo: move title texts to javascript */
415 if( $firstInList ) {
416 $first = Xml::element( 'input',
417 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
419 $checkmark = array( 'checked' => 'checked' );
420 } else {
421 # Check visibility of old revisions
422 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
423 $radio['disabled'] = 'disabled';
424 $checkmark = array(); // We will check the next possible one
425 } else if( $counter == 2 || !$this->mOldIdChecked ) {
426 $checkmark = array( 'checked' => 'checked' );
427 $this->mOldIdChecked = $rev->getId();
428 } else {
429 $checkmark = array();
431 $first = Xml::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
432 $checkmark = array();
434 $second = Xml::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
435 return $first . $second;
436 } else {
437 return '';
442 * Fetch an array of revisions, specified by a given limit, offset and
443 * direction. This is now only used by the feeds. It was previously
444 * used by the main UI but that's now handled by the pager.
446 function fetchRevisions($limit, $offset, $direction) {
447 $dbr = wfGetDB( DB_SLAVE );
449 if( $direction == PageHistory::DIR_PREV )
450 list($dirs, $oper) = array("ASC", ">=");
451 else /* $direction == PageHistory::DIR_NEXT */
452 list($dirs, $oper) = array("DESC", "<=");
454 if( $offset )
455 $offsets = array("rev_timestamp $oper '$offset'");
456 else
457 $offsets = array();
459 $page_id = $this->mTitle->getArticleID();
461 return $dbr->select( 'revision',
462 Revision::selectFields(),
463 array_merge(array("rev_page=$page_id"), $offsets),
464 __METHOD__,
465 array( 'ORDER BY' => "rev_timestamp $dirs",
466 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
471 * Output a subscription feed listing recent edits to this page.
472 * @param string $type
474 function feed( $type ) {
475 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
476 if( !FeedUtils::checkFeedOutput($type) ) {
477 return;
480 $feed = new $wgFeedClasses[$type](
481 $this->mTitle->getPrefixedText() . ' - ' .
482 wfMsgForContent( 'history-feed-title' ),
483 wfMsgForContent( 'history-feed-description' ),
484 $this->mTitle->getFullUrl( 'action=history' ) );
486 // Get a limit on number of feed entries. Provide a sane default
487 // of 10 if none is defined (but limit to $wgFeedLimit max)
488 $limit = $wgRequest->getInt( 'limit', 10 );
489 if( $limit > $wgFeedLimit || $limit < 1 ) {
490 $limit = 10;
492 $items = $this->fetchRevisions($limit, 0, PageHistory::DIR_NEXT);
494 $feed->outHeader();
495 if( $items ) {
496 foreach( $items as $row ) {
497 $feed->outItem( $this->feedItem( $row ) );
499 } else {
500 $feed->outItem( $this->feedEmpty() );
502 $feed->outFooter();
505 function feedEmpty() {
506 global $wgOut;
507 return new FeedItem(
508 wfMsgForContent( 'nohistory' ),
509 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
510 $this->mTitle->getFullUrl(),
511 wfTimestamp( TS_MW ),
513 $this->mTitle->getTalkPage()->getFullUrl() );
517 * Generate a FeedItem object from a given revision table row
518 * Borrows Recent Changes' feed generation functions for formatting;
519 * includes a diff to the previous revision (if any).
521 * @param $row
522 * @return FeedItem
524 function feedItem( $row ) {
525 $rev = new Revision( $row );
526 $rev->setTitle( $this->mTitle );
527 $text = FeedUtils::formatDiffRow( $this->mTitle,
528 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
529 $rev->getId(),
530 $rev->getTimestamp(),
531 $rev->getComment() );
533 if( $rev->getComment() == '' ) {
534 global $wgContLang;
535 $title = wfMsgForContent( 'history-feed-item-nocomment',
536 $rev->getUserText(),
537 $wgContLang->timeanddate( $rev->getTimestamp() ) );
538 } else {
539 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem::stripComment( $rev->getComment() );
542 return new FeedItem(
543 $title,
544 $text,
545 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
546 $rev->getTimestamp(),
547 $rev->getUserText(),
548 $this->mTitle->getTalkPage()->getFullUrl() );
554 * @ingroup Pager
556 class PageHistoryPager extends ReverseChronologicalPager {
557 public $mLastRow = false, $mPageHistory, $mTitle;
559 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
560 parent::__construct();
561 $this->mPageHistory = $pageHistory;
562 $this->mTitle =& $this->mPageHistory->mTitle;
563 $this->tagFilter = $tagFilter;
564 $this->getDateCond( $year, $month );
567 function getQueryInfo() {
568 $queryInfo = array(
569 'tables' => array('revision'),
570 'fields' => array_merge( Revision::selectFields(), array('ts_tags') ),
571 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
572 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
573 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
575 ChangeTags::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter );
576 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
577 return $queryInfo;
580 function getIndexField() {
581 return 'rev_timestamp';
584 function formatRow( $row ) {
585 if( $this->mLastRow ) {
586 $latest = $this->mCounter == 1 && $this->mIsFirst;
587 $firstInList = $this->mCounter == 1;
588 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
589 $this->mTitle->getNotificationTimestamp(), $latest, $firstInList );
590 } else {
591 $s = '';
593 $this->mLastRow = $row;
594 return $s;
597 function getStartBody() {
598 $this->mLastRow = false;
599 $this->mCounter = 1;
600 return '';
603 function getEndBody() {
604 if( $this->mLastRow ) {
605 $latest = $this->mCounter == 1 && $this->mIsFirst;
606 $firstInList = $this->mCounter == 1;
607 if( $this->mIsBackwards ) {
608 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
609 if( $this->mOffset == '' ) {
610 $next = null;
611 } else {
612 $next = 'unknown';
614 } else {
615 # The next row is the past-the-end row
616 $next = $this->mPastTheEndRow;
618 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
619 $this->mTitle->getNotificationTimestamp(), $latest, $firstInList );
620 } else {
621 $s = '';
623 return $s;