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
->link(
95 wfMsgHtml( 'viewpagelogs' ),
97 array( 'page' => $this->mTitle
->getPrefixedUrl() ),
98 array( 'known', 'noclasses' )
100 $wgOut->setSubtitle( $logLink );
102 $feedType = $wgRequest->getVal( 'feed' );
104 wfProfileOut( __METHOD__
);
105 return $this->feed( $feedType );
109 * Fail if article doesn't exist.
111 if( !$this->mTitle
->exists() ) {
112 $wgOut->addWikiMsg( 'nohistory' );
113 wfProfileOut( __METHOD__
);
118 * Add date selector to quickly get to a certain time
120 $year = $wgRequest->getInt( 'year' );
121 $month = $wgRequest->getInt( 'month' );
122 $tagFilter = $wgRequest->getVal( 'tagfilter' );
123 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
125 $action = htmlspecialchars( $wgScript );
127 "<form action=\"$action\" method=\"get\" id=\"mw-history-searchform\">" .
128 Xml
::fieldset( wfMsg( 'history-fieldset-title' ), false, array( 'id' => 'mw-history-search' ) ) .
129 Xml
::hidden( 'title', $this->mTitle
->getPrefixedDBKey() ) . "\n" .
130 Xml
::hidden( 'action', 'history' ) . "\n" .
131 xml
::dateMenu( $year, $month ) . ' ' .
132 ( $tagSelector ?
( implode( ' ', $tagSelector ) . ' ' ) : '' ) .
133 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
137 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle
) );
142 $pager = new PageHistoryPager( $this, $year, $month, $tagFilter );
143 $this->linesonpage
= $pager->getNumRows();
145 $pager->getNavigationBar() .
146 $this->beginHistoryList() .
148 $this->endHistoryList() .
149 $pager->getNavigationBar()
152 wfProfileOut( __METHOD__
);
156 * Creates begin of history list with a submit button
158 * @return string HTML output
160 function beginHistoryList() {
161 global $wgUser, $wgScript, $wgEnableHtmlDiff;
162 $this->lastdate
= '';
163 $s = wfMsgExt( 'histlegend', array( 'parse') );
164 if( $this->linesonpage
> 1 && $wgUser->isAllowed('deleterevision') ) {
165 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
166 $s .= Xml
::openElement( 'form',
168 'action' => $revdel->getLocalURL( array(
169 'type' => 'revision',
170 'target' => $this->mTitle
->getPrefixedDbKey()
173 'id' => 'mw-history-revdeleteform',
174 'style' => 'visibility:hidden;float:right;'
177 $s .= Xml
::hidden( 'ids', '', array('id'=>'revdel-oldid') );
178 $s .= Xml
::submitButton( wfMsg( 'showhideselectedversions' ) );
179 $s .= Xml
::closeElement( 'form' );
181 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript,
182 'id' => 'mw-history-compare' ) );
183 $s .= Xml
::hidden( 'title', $this->mTitle
->getPrefixedDbKey() );
184 if( $wgEnableHtmlDiff ) {
185 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
187 'name' => 'htmldiff',
188 'class' => 'historysubmit',
189 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
190 'title' => wfMsg( 'tooltip-compareselectedversions' ),
193 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
195 'class' => 'historysubmit',
196 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
197 'title' => wfMsg( 'tooltip-compareselectedversions' ),
201 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
203 'class' => 'historysubmit',
204 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
205 'title' => wfMsg( 'tooltip-compareselectedversions' ),
209 $s .= '<ul id="pagehistory">' . "\n";
214 * Creates end of history list with a submit button
216 * @return string HTML output
218 function endHistoryList() {
219 global $wgEnableHtmlDiff;
221 if( $wgEnableHtmlDiff ) {
222 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
224 'name' => 'htmldiff',
225 'class' => 'historysubmit',
226 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
227 'title' => wfMsg( 'tooltip-compareselectedversions' ),
230 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
232 'class' => 'historysubmit',
233 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
234 'title' => wfMsg( 'tooltip-compareselectedversions' ),
238 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
240 'class' => 'historysubmit',
241 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
242 'title' => wfMsg( 'tooltip-compareselectedversions' ),
251 * Creates a submit button
253 * @param array $attributes attributes
254 * @return string HTML output for the submit button
256 function submitButton($message, $attributes = array() ) {
257 # Disable submit button if history has 1 revision only
258 if( $this->linesonpage
> 1 ) {
259 return Xml
::submitButton( $message , $attributes );
266 * Returns a row from the history printout.
268 * @todo document some more, and maybe clean up the code (some params redundant?)
270 * @param Row $row The database row corresponding to the previous line.
271 * @param mixed $next The database row corresponding to the next line.
272 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
273 * @param $notificationtimestamp
274 * @param bool $latest Whether this row corresponds to the page's latest revision.
275 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
276 * @return string HTML output for the row
278 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
279 $latest = false, $firstInList = false )
281 global $wgUser, $wgLang;
282 $rev = new Revision( $row );
283 $rev->setTitle( $this->mTitle
);
285 $curlink = $this->curLink( $rev, $latest );
286 $lastlink = $this->lastLink( $rev, $next, $counter );
287 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
288 $link = $this->revLink( $rev );
291 $s = "($curlink) ($lastlink) $arbitrary";
293 if( $wgUser->isAllowed( 'deleterevision' ) ) {
294 // Hide JS by default for non-JS browsing
295 $hidden = array( 'style' => 'display:none' );
296 // If revision was hidden from sysops
297 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
298 $del = Xml
::check( 'deleterevisions', false,
299 $hidden +
array('disabled' => 'disabled') );
300 $del .= Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
301 '(' . $this->message
['rev-delundel'] . ')' );
302 // Otherwise, show the link...
304 $jsCall = 'updateShowHideForm('.$rev->getId().',this.checked)';
305 $del = Xml
::check( 'showhiderevisions', false,
306 $hidden +
array('onchange' => $jsCall) );
308 'type' => 'revision',
309 'target' => $this->mTitle
->getPrefixedDbkey(),
310 'ids' => $rev->getId() );
311 $del .= $this->mSkin
->revDeleteLink( $query,
312 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
318 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
320 if( $rev->isMinor() ) {
321 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
324 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
325 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
328 $s .= $this->mSkin
->revComment( $rev, false, true );
330 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
331 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
336 if( !is_null( $next ) && is_object( $next ) ) {
337 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
338 $tools[] = '<span class="mw-rollback-link">'.
339 $this->mSkin
->buildRollbackLink( $rev ).'</span>';
342 if( $this->mTitle
->quickUserCan( 'edit' )
343 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
344 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
346 # Create undo tooltip for the first (=latest) line only
347 $undoTooltip = $latest
348 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
350 $undolink = $this->mSkin
->link(
352 wfMsgHtml( 'editundo' ),
354 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
355 array( 'known', 'noclasses' )
357 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
362 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
366 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
367 $classes = array_merge( $classes, $newClasses );
368 $s .= " $tagSummary";
370 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
372 $classes = implode( ' ', $classes );
374 return "<li class=\"$classes\">$s</li>\n";
378 * Create a link to view this revision of the page
379 * @param Revision $rev
382 function revLink( $rev ) {
384 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
385 $date = htmlspecialchars( $date );
386 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
387 $link = $this->mSkin
->link(
391 array( 'oldid' => $rev->getId() ),
392 array( 'known', 'noclasses' )
395 $link = "<span class=\"history-deleted\">$date</span>";
401 * Create a diff-to-current link for this revision for this page
402 * @param Revision $rev
403 * @param Bool $latest, this is the latest revision of the page?
406 function curLink( $rev, $latest ) {
407 $cur = $this->message
['cur'];
408 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
411 return $this->mSkin
->link(
416 'diff' => $this->mTitle
->getLatestRevID(),
417 'oldid' => $rev->getId()
419 array( 'known', 'noclasses' )
425 * Create a diff-to-previous link for this revision for this page.
426 * @param Revision $prevRev, the previous revision
427 * @param mixed $next, the newer revision
428 * @param int $counter, what row on the history list this is
431 function lastLink( $prevRev, $next, $counter ) {
432 $last = $this->message
['last'];
433 # $next may either be a Row, null, or "unkown"
434 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
435 if( is_null($next) ) {
436 # Probably no next row
438 } elseif( $next === 'unknown' ) {
439 # Next row probably exists but is unknown, use an oldid=prev link
440 return $this->mSkin
->link(
445 'diff' => $prevRev->getId(),
448 array( 'known', 'noclasses' )
450 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
453 return $this->mSkin
->link(
458 'diff' => $prevRev->getId(),
459 'oldid' => $next->rev_id
461 array( 'known', 'noclasses' )
467 * Create radio buttons for page history
469 * @param object $rev Revision
470 * @param bool $firstInList Is this version the first one?
471 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
472 * @return string HTML output for the radio buttons
474 function diffButtons( $rev, $firstInList, $counter ) {
475 if( $this->linesonpage
> 1 ) {
476 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
477 /** @todo: move title texts to javascript */
479 $first = Xml
::element( 'input',
480 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
482 $checkmark = array( 'checked' => 'checked' );
484 # Check visibility of old revisions
485 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
486 $radio['disabled'] = 'disabled';
487 $checkmark = array(); // We will check the next possible one
488 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
489 $checkmark = array( 'checked' => 'checked' );
490 $this->mOldIdChecked
= $rev->getId();
492 $checkmark = array();
494 $first = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
495 $checkmark = array();
497 $second = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
498 return $first . $second;
505 * Fetch an array of revisions, specified by a given limit, offset and
506 * direction. This is now only used by the feeds. It was previously
507 * used by the main UI but that's now handled by the pager.
509 function fetchRevisions($limit, $offset, $direction) {
510 $dbr = wfGetDB( DB_SLAVE
);
512 if( $direction == PageHistory
::DIR_PREV
)
513 list($dirs, $oper) = array("ASC", ">=");
514 else /* $direction == PageHistory::DIR_NEXT */
515 list($dirs, $oper) = array("DESC", "<=");
518 $offsets = array("rev_timestamp $oper '$offset'");
522 $page_id = $this->mTitle
->getArticleID();
524 return $dbr->select( 'revision',
525 Revision
::selectFields(),
526 array_merge(array("rev_page=$page_id"), $offsets),
528 array( 'ORDER BY' => "rev_timestamp $dirs",
529 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
534 * Output a subscription feed listing recent edits to this page.
535 * @param string $type
537 function feed( $type ) {
538 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
539 if( !FeedUtils
::checkFeedOutput($type) ) {
543 $feed = new $wgFeedClasses[$type](
544 $this->mTitle
->getPrefixedText() . ' - ' .
545 wfMsgForContent( 'history-feed-title' ),
546 wfMsgForContent( 'history-feed-description' ),
547 $this->mTitle
->getFullUrl( array( 'action' => 'history' ) )
550 // Get a limit on number of feed entries. Provide a sane default
551 // of 10 if none is defined (but limit to $wgFeedLimit max)
552 $limit = $wgRequest->getInt( 'limit', 10 );
553 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
556 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
560 foreach( $items as $row ) {
561 $feed->outItem( $this->feedItem( $row ) );
564 $feed->outItem( $this->feedEmpty() );
569 function feedEmpty() {
572 wfMsgForContent( 'nohistory' ),
573 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
574 $this->mTitle
->getFullUrl(),
575 wfTimestamp( TS_MW
),
577 $this->mTitle
->getTalkPage()->getFullUrl() );
581 * Generate a FeedItem object from a given revision table row
582 * Borrows Recent Changes' feed generation functions for formatting;
583 * includes a diff to the previous revision (if any).
588 function feedItem( $row ) {
589 $rev = new Revision( $row );
590 $rev->setTitle( $this->mTitle
);
591 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
592 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
594 $rev->getTimestamp(),
595 $rev->getComment() );
597 if( $rev->getComment() == '' ) {
599 $ts = $rev->getTimestamp();
600 $title = wfMsgForContent( 'history-feed-item-nocomment',
602 $wgContLang->timeanddate( $ts ),
603 $wgContLang->date( $ts ),
604 $wgContLang->time( $ts ) );
606 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
612 $this->mTitle
->getFullUrl( array(
613 'diff' => $rev->getId(),
616 $rev->getTimestamp(),
618 $this->mTitle
->getTalkPage()->getFullUrl() );
625 class PageHistoryPager
extends ReverseChronologicalPager
{
626 public $mLastRow = false, $mPageHistory, $mTitle;
628 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
629 parent
::__construct();
630 $this->mPageHistory
= $pageHistory;
631 $this->mTitle
=& $this->mPageHistory
->mTitle
;
632 $this->tagFilter
= $tagFilter;
633 $this->getDateCond( $year, $month );
636 function getQueryInfo() {
638 'tables' => array('revision'),
639 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
640 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
641 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
642 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
644 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
645 $queryInfo['fields'],
647 $queryInfo['join_conds'],
648 $queryInfo['options'],
650 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
654 function getIndexField() {
655 return 'rev_timestamp';
658 function formatRow( $row ) {
659 if( $this->mLastRow
) {
660 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
661 $firstInList = $this->mCounter
== 1;
662 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
663 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
667 $this->mLastRow
= $row;
671 function getStartBody() {
672 $this->mLastRow
= false;
677 function getEndBody() {
678 if( $this->mLastRow
) {
679 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
680 $firstInList = $this->mCounter
== 1;
681 if( $this->mIsBackwards
) {
682 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
683 if( $this->mOffset
== '' ) {
689 # The next row is the past-the-end row
690 $next = $this->mPastTheEndRow
;
692 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
693 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );