5 * Split off from Article.php and Skin.php, 2003-12-22
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
22 var $mArticle, $mTitle, $mSkin;
25 var $mLatestId = null;
27 private $mOldIdChecked = 0;
30 * Construct a new PageHistory.
32 * @param Article $article
35 function __construct( $article ) {
37 $this->mArticle
=& $article;
38 $this->mTitle
=& $article->mTitle
;
39 $this->mSkin
= $wgUser->getSkin();
40 $this->preCacheMessages();
43 function getArticle() {
44 return $this->mArticle
;
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') );
65 * Print the history page for an article.
70 global $wgOut, $wgRequest, $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' );
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__
);
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 );
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 ) . ' ' .
127 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
128 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
132 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
137 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
138 $this->linesonpage
= $pager->getNumRows();
140 $pager->getNavigationBar() .
141 $this->beginHistoryList() .
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 $wgUser, $wgScript, $wgEnableHtmlDiff;
157 $this->lastdate
= '';
158 $s = wfMsgExt( 'histlegend', array( 'parse') );
159 if( $wgUser->isAllowed('deleterevision') ) {
160 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
161 $s .= Xml
::openElement( 'form',
163 'action' => $revdel->getFullUrl(),
164 'method' => 'get', 'id' => 'mw-history-revdeleteform',
165 'style' => 'visibility:hidden;float:right;'
168 $s .= Xml
::hidden( 'target', $this->mTitle
->getPrefixedDbKey() );
169 $s .= Xml
::hidden( 'oldid', '', array('id'=>'revdel-oldid') );
170 $s .= Xml
::submitButton( wfMsg( 'showhideselectedversions' ) );
171 $s .= Xml
::closeElement( 'form' );
173 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript,
174 'id' => 'mw-history-compare' ) );
175 $s .= Xml
::hidden( 'title', $this->mTitle
->getPrefixedDbKey() );
176 if( $wgEnableHtmlDiff ) {
177 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
179 'name' => 'htmldiff',
180 'class' => 'historysubmit',
181 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
182 'title' => wfMsg( 'tooltip-compareselectedversions' ),
185 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
187 'class' => 'historysubmit',
188 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
189 'title' => wfMsg( 'tooltip-compareselectedversions' ),
193 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
195 'class' => 'historysubmit',
196 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
197 'title' => wfMsg( 'tooltip-compareselectedversions' ),
201 $s .= '<ul id="pagehistory">' . "\n";
206 * Creates end of history list with a submit button
208 * @return string HTML output
210 function endHistoryList() {
211 global $wgEnableHtmlDiff;
213 if( $wgEnableHtmlDiff ) {
214 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
216 'name' => 'htmldiff',
217 'class' => 'historysubmit',
218 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
219 'title' => wfMsg( 'tooltip-compareselectedversions' ),
222 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
224 'class' => 'historysubmit',
225 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
226 'title' => wfMsg( 'tooltip-compareselectedversions' ),
230 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
232 'class' => 'historysubmit',
233 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
234 'title' => wfMsg( 'tooltip-compareselectedversions' ),
243 * Creates a submit button
245 * @param array $attributes attributes
246 * @return string HTML output for the submit button
248 function submitButton($message, $attributes = array() ) {
249 # Disable submit button if history has 1 revision only
250 if( $this->linesonpage
> 1 ) {
251 return Xml
::submitButton( $message , $attributes );
258 * Returns a row from the history printout.
260 * @todo document some more, and maybe clean up the code (some params redundant?)
262 * @param Row $row The database row corresponding to the previous line.
263 * @param mixed $next The database row corresponding to the next line.
264 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
265 * @param $notificationtimestamp
266 * @param bool $latest Whether this row corresponds to the page's latest revision.
267 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
268 * @return string HTML output for the row
270 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
271 $latest = false, $firstInList = false )
273 global $wgUser, $wgLang;
274 $rev = new Revision( $row );
275 $rev->setTitle( $this->mTitle
);
277 $curlink = $this->curLink( $rev, $latest );
278 $lastlink = $this->lastLink( $rev, $next, $counter );
279 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
280 $link = $this->revLink( $rev );
283 $s = "($curlink) ($lastlink) $arbitrary";
285 if( $wgUser->isAllowed( 'deleterevision' ) ) {
286 // Hide JS by default for non-JS browsing
287 $hidden = array( 'style' => 'visibility:hidden' );
288 // If revision was hidden from sysops
289 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
290 $del = Xml
::check( 'deleterevisions', false,
291 $hidden +
array('disabled' => 'disabled') );
292 $del .= Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
293 '(' . $this->message
['rev-delundel'] . ')' );
294 // Otherwise, show the link...
296 $jsCall = 'updateShowHideForm('.$rev->getId().',this.checked)';
297 $del = Xml
::check( 'showhiderevisions', false,
298 $hidden +
array('onchange' => $jsCall) );
299 $query = array( 'target' => $this->mTitle
->getPrefixedDbkey(),
300 'oldid' => $rev->getId() );
301 $del .= $this->mSkin
->revDeleteLink( $query,
302 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
308 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
310 if( $rev->isMinor() ) {
311 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
314 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
315 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
318 $s .= $this->mSkin
->revComment( $rev, false, true );
320 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
321 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
323 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
324 $s .= ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
329 if( !is_null( $next ) && is_object( $next ) ) {
330 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
331 $tools[] = '<span class="mw-rollback-link">'.$this->mSkin
->buildRollbackLink( $rev ).'</span>';
334 if( $this->mTitle
->quickUserCan( 'edit' ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) &&
335 !$next->rev_deleted
& Revision
::DELETED_TEXT
)
337 # Create undo tooltip for the first (=latest) line only
338 $undoTooltip = $latest
339 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
341 $undolink = $this->mSkin
->link(
343 wfMsgHtml( 'editundo' ),
345 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
346 array( 'known', 'noclasses' )
348 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
353 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
357 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
358 $classes = array_merge( $classes, $newClasses );
359 $s .= " $tagSummary";
361 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
363 $classes = implode( ' ', $classes );
365 return "<li class=\"$classes\">$s</li>\n";
369 * Create a link to view this revision of the page
370 * @param Revision $rev
373 function revLink( $rev ) {
375 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
376 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
377 $link = $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $date, "oldid=" . $rev->getId() );
381 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
382 $link = "<span class=\"history-deleted\">$link</span>";
388 * Create a diff-to-current link for this revision for this page
389 * @param Revision $rev
390 * @param Bool $latest, this is the latest revision of the page?
393 function curLink( $rev, $latest ) {
394 $cur = $this->message
['cur'];
395 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
398 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
399 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
404 * Create a diff-to-previous link for this revision for this page.
405 * @param Revision $prevRev, the previous revision
406 * @param mixed $next, the newer revision
407 * @param int $counter, what row on the history list this is
410 function lastLink( $prevRev, $next, $counter ) {
411 $last = $this->message
['last'];
412 # $next may either be a Row, null, or "unkown"
413 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
414 if( is_null($next) ) {
415 # Probably no next row
417 } elseif( $next === 'unknown' ) {
418 # Next row probably exists but is unknown, use an oldid=prev link
419 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
420 "diff=" . $prevRev->getId() . "&oldid=prev" );
421 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
424 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
425 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
430 * Create radio buttons for page history
432 * @param object $rev Revision
433 * @param bool $firstInList Is this version the first one?
434 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
435 * @return string HTML output for the radio buttons
437 function diffButtons( $rev, $firstInList, $counter ) {
438 if( $this->linesonpage
> 1 ) {
439 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
440 /** @todo: move title texts to javascript */
442 $first = Xml
::element( 'input',
443 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
445 $checkmark = array( 'checked' => 'checked' );
447 # Check visibility of old revisions
448 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
449 $radio['disabled'] = 'disabled';
450 $checkmark = array(); // We will check the next possible one
451 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
452 $checkmark = array( 'checked' => 'checked' );
453 $this->mOldIdChecked
= $rev->getId();
455 $checkmark = array();
457 $first = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
458 $checkmark = array();
460 $second = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
461 return $first . $second;
468 * Fetch an array of revisions, specified by a given limit, offset and
469 * direction. This is now only used by the feeds. It was previously
470 * used by the main UI but that's now handled by the pager.
472 function fetchRevisions($limit, $offset, $direction) {
473 $dbr = wfGetDB( DB_SLAVE
);
475 if( $direction == PageHistory
::DIR_PREV
)
476 list($dirs, $oper) = array("ASC", ">=");
477 else /* $direction == PageHistory::DIR_NEXT */
478 list($dirs, $oper) = array("DESC", "<=");
481 $offsets = array("rev_timestamp $oper '$offset'");
485 $page_id = $this->mTitle
->getArticleID();
487 return $dbr->select( 'revision',
488 Revision
::selectFields(),
489 array_merge(array("rev_page=$page_id"), $offsets),
491 array( 'ORDER BY' => "rev_timestamp $dirs",
492 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
497 * Output a subscription feed listing recent edits to this page.
498 * @param string $type
500 function feed( $type ) {
501 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
502 if( !FeedUtils
::checkFeedOutput($type) ) {
506 $feed = new $wgFeedClasses[$type](
507 $this->mTitle
->getPrefixedText() . ' - ' .
508 wfMsgForContent( 'history-feed-title' ),
509 wfMsgForContent( 'history-feed-description' ),
510 $this->mTitle
->getFullUrl( 'action=history' ) );
512 // Get a limit on number of feed entries. Provide a sane default
513 // of 10 if none is defined (but limit to $wgFeedLimit max)
514 $limit = $wgRequest->getInt( 'limit', 10 );
515 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
518 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
522 foreach( $items as $row ) {
523 $feed->outItem( $this->feedItem( $row ) );
526 $feed->outItem( $this->feedEmpty() );
531 function feedEmpty() {
534 wfMsgForContent( 'nohistory' ),
535 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
536 $this->mTitle
->getFullUrl(),
537 wfTimestamp( TS_MW
),
539 $this->mTitle
->getTalkPage()->getFullUrl() );
543 * Generate a FeedItem object from a given revision table row
544 * Borrows Recent Changes' feed generation functions for formatting;
545 * includes a diff to the previous revision (if any).
550 function feedItem( $row ) {
551 $rev = new Revision( $row );
552 $rev->setTitle( $this->mTitle
);
553 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
554 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
556 $rev->getTimestamp(),
557 $rev->getComment() );
559 if( $rev->getComment() == '' ) {
561 $title = wfMsgForContent( 'history-feed-item-nocomment',
563 $wgContLang->timeanddate( $rev->getTimestamp() ) );
565 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
571 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
572 $rev->getTimestamp(),
574 $this->mTitle
->getTalkPage()->getFullUrl() );
582 class PageHistoryPager
extends ReverseChronologicalPager
{
583 public $mLastRow = false, $mPageHistory, $mTitle;
585 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
586 parent
::__construct();
587 $this->mPageHistory
= $pageHistory;
588 $this->mTitle
=& $this->mPageHistory
->mTitle
;
589 $this->tagFilter
= $tagFilter;
590 $this->getDateCond( $year, $month );
593 function getQueryInfo() {
595 'tables' => array('revision'),
596 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
597 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
598 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
599 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
601 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
602 $queryInfo['fields'],
604 $queryInfo['join_conds'],
605 $queryInfo['options'],
607 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
611 function getIndexField() {
612 return 'rev_timestamp';
615 function formatRow( $row ) {
616 if( $this->mLastRow
) {
617 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
618 $firstInList = $this->mCounter
== 1;
619 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
620 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
624 $this->mLastRow
= $row;
628 function getStartBody() {
629 $this->mLastRow
= false;
634 function getEndBody() {
635 if( $this->mLastRow
) {
636 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
637 $firstInList = $this->mCounter
== 1;
638 if( $this->mIsBackwards
) {
639 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
640 if( $this->mOffset
== '' ) {
646 # The next row is the past-the-end row
647 $next = $this->mPastTheEndRow
;
649 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
650 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );