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( $this->linesonpage
> 1 && $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' => 'display:none' );
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->isDeleted( Revision
::DELETED_TEXT
) ) {
377 $link = $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $date, "oldid=" . $rev->getId() );
379 $link = "<span class=\"history-deleted\">$date</span>";
385 * Create a diff-to-current link for this revision for this page
386 * @param Revision $rev
387 * @param Bool $latest, this is the latest revision of the page?
390 function curLink( $rev, $latest ) {
391 $cur = $this->message
['cur'];
392 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
395 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
396 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
401 * Create a diff-to-previous link for this revision for this page.
402 * @param Revision $prevRev, the previous revision
403 * @param mixed $next, the newer revision
404 * @param int $counter, what row on the history list this is
407 function lastLink( $prevRev, $next, $counter ) {
408 $last = $this->message
['last'];
409 # $next may either be a Row, null, or "unkown"
410 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
411 if( is_null($next) ) {
412 # Probably no next row
414 } elseif( $next === 'unknown' ) {
415 # Next row probably exists but is unknown, use an oldid=prev link
416 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
417 "diff=" . $prevRev->getId() . "&oldid=prev" );
418 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
421 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
422 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
427 * Create radio buttons for page history
429 * @param object $rev Revision
430 * @param bool $firstInList Is this version the first one?
431 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
432 * @return string HTML output for the radio buttons
434 function diffButtons( $rev, $firstInList, $counter ) {
435 if( $this->linesonpage
> 1 ) {
436 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
437 /** @todo: move title texts to javascript */
439 $first = Xml
::element( 'input',
440 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
442 $checkmark = array( 'checked' => 'checked' );
444 # Check visibility of old revisions
445 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
446 $radio['disabled'] = 'disabled';
447 $checkmark = array(); // We will check the next possible one
448 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
449 $checkmark = array( 'checked' => 'checked' );
450 $this->mOldIdChecked
= $rev->getId();
452 $checkmark = array();
454 $first = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
455 $checkmark = array();
457 $second = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
458 return $first . $second;
465 * Fetch an array of revisions, specified by a given limit, offset and
466 * direction. This is now only used by the feeds. It was previously
467 * used by the main UI but that's now handled by the pager.
469 function fetchRevisions($limit, $offset, $direction) {
470 $dbr = wfGetDB( DB_SLAVE
);
472 if( $direction == PageHistory
::DIR_PREV
)
473 list($dirs, $oper) = array("ASC", ">=");
474 else /* $direction == PageHistory::DIR_NEXT */
475 list($dirs, $oper) = array("DESC", "<=");
478 $offsets = array("rev_timestamp $oper '$offset'");
482 $page_id = $this->mTitle
->getArticleID();
484 return $dbr->select( 'revision',
485 Revision
::selectFields(),
486 array_merge(array("rev_page=$page_id"), $offsets),
488 array( 'ORDER BY' => "rev_timestamp $dirs",
489 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
494 * Output a subscription feed listing recent edits to this page.
495 * @param string $type
497 function feed( $type ) {
498 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
499 if( !FeedUtils
::checkFeedOutput($type) ) {
503 $feed = new $wgFeedClasses[$type](
504 $this->mTitle
->getPrefixedText() . ' - ' .
505 wfMsgForContent( 'history-feed-title' ),
506 wfMsgForContent( 'history-feed-description' ),
507 $this->mTitle
->getFullUrl( 'action=history' ) );
509 // Get a limit on number of feed entries. Provide a sane default
510 // of 10 if none is defined (but limit to $wgFeedLimit max)
511 $limit = $wgRequest->getInt( 'limit', 10 );
512 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
515 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
519 foreach( $items as $row ) {
520 $feed->outItem( $this->feedItem( $row ) );
523 $feed->outItem( $this->feedEmpty() );
528 function feedEmpty() {
531 wfMsgForContent( 'nohistory' ),
532 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
533 $this->mTitle
->getFullUrl(),
534 wfTimestamp( TS_MW
),
536 $this->mTitle
->getTalkPage()->getFullUrl() );
540 * Generate a FeedItem object from a given revision table row
541 * Borrows Recent Changes' feed generation functions for formatting;
542 * includes a diff to the previous revision (if any).
547 function feedItem( $row ) {
548 $rev = new Revision( $row );
549 $rev->setTitle( $this->mTitle
);
550 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
551 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
553 $rev->getTimestamp(),
554 $rev->getComment() );
556 if( $rev->getComment() == '' ) {
558 $title = wfMsgForContent( 'history-feed-item-nocomment',
560 $wgContLang->timeanddate( $rev->getTimestamp() ) );
562 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
568 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
569 $rev->getTimestamp(),
571 $this->mTitle
->getTalkPage()->getFullUrl() );
579 class PageHistoryPager
extends ReverseChronologicalPager
{
580 public $mLastRow = false, $mPageHistory, $mTitle;
582 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
583 parent
::__construct();
584 $this->mPageHistory
= $pageHistory;
585 $this->mTitle
=& $this->mPageHistory
->mTitle
;
586 $this->tagFilter
= $tagFilter;
587 $this->getDateCond( $year, $month );
590 function getQueryInfo() {
592 'tables' => array('revision'),
593 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
594 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
595 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
596 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
598 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
599 $queryInfo['fields'],
601 $queryInfo['join_conds'],
602 $queryInfo['options'],
604 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
608 function getIndexField() {
609 return 'rev_timestamp';
612 function formatRow( $row ) {
613 if( $this->mLastRow
) {
614 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
615 $firstInList = $this->mCounter
== 1;
616 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
617 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
621 $this->mLastRow
= $row;
625 function getStartBody() {
626 $this->mLastRow
= false;
631 function getEndBody() {
632 if( $this->mLastRow
) {
633 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
634 $firstInList = $this->mCounter
== 1;
635 if( $this->mIsBackwards
) {
636 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
637 if( $this->mOffset
== '' ) {
643 # The next row is the past-the-end row
644 $next = $this->mPastTheEndRow
;
646 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
647 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );