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->getLocalURL( array(
164 'type' => 'revision',
165 'target' => $this->mTitle
->getPrefixedDbKey()
168 'id' => 'mw-history-revdeleteform',
169 'style' => 'visibility:hidden;float:right;'
172 $s .= Xml
::hidden( 'ids', '', array('id'=>'revdel-oldid') );
173 $s .= Xml
::submitButton( wfMsg( 'showhideselectedversions' ) );
174 $s .= Xml
::closeElement( 'form' );
176 $s .= Xml
::openElement( 'form', array( 'action' => $wgScript,
177 'id' => 'mw-history-compare' ) );
178 $s .= Xml
::hidden( 'title', $this->mTitle
->getPrefixedDbKey() );
179 if( $wgEnableHtmlDiff ) {
180 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
182 'name' => 'htmldiff',
183 'class' => 'historysubmit',
184 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
185 'title' => wfMsg( 'tooltip-compareselectedversions' ),
188 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
190 'class' => 'historysubmit',
191 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
192 'title' => wfMsg( 'tooltip-compareselectedversions' ),
196 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
198 'class' => 'historysubmit',
199 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
200 'title' => wfMsg( 'tooltip-compareselectedversions' ),
204 $s .= '<ul id="pagehistory">' . "\n";
209 * Creates end of history list with a submit button
211 * @return string HTML output
213 function endHistoryList() {
214 global $wgEnableHtmlDiff;
216 if( $wgEnableHtmlDiff ) {
217 $s .= $this->submitButton( wfMsg( 'visualcomparison'),
219 'name' => 'htmldiff',
220 'class' => 'historysubmit',
221 'accesskey' => wfMsg( 'accesskey-visualcomparison' ),
222 'title' => wfMsg( 'tooltip-compareselectedversions' ),
225 $s .= $this->submitButton( wfMsg( 'wikicodecomparison'),
227 'class' => 'historysubmit',
228 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
229 'title' => wfMsg( 'tooltip-compareselectedversions' ),
233 $s .= $this->submitButton( wfMsg( 'compareselectedversions'),
235 'class' => 'historysubmit',
236 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
237 'title' => wfMsg( 'tooltip-compareselectedversions' ),
246 * Creates a submit button
248 * @param array $attributes attributes
249 * @return string HTML output for the submit button
251 function submitButton($message, $attributes = array() ) {
252 # Disable submit button if history has 1 revision only
253 if( $this->linesonpage
> 1 ) {
254 return Xml
::submitButton( $message , $attributes );
261 * Returns a row from the history printout.
263 * @todo document some more, and maybe clean up the code (some params redundant?)
265 * @param Row $row The database row corresponding to the previous line.
266 * @param mixed $next The database row corresponding to the next line.
267 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
268 * @param $notificationtimestamp
269 * @param bool $latest Whether this row corresponds to the page's latest revision.
270 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
271 * @return string HTML output for the row
273 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false,
274 $latest = false, $firstInList = false )
276 global $wgUser, $wgLang;
277 $rev = new Revision( $row );
278 $rev->setTitle( $this->mTitle
);
280 $curlink = $this->curLink( $rev, $latest );
281 $lastlink = $this->lastLink( $rev, $next, $counter );
282 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
283 $link = $this->revLink( $rev );
286 $s = "($curlink) ($lastlink) $arbitrary";
288 if( $wgUser->isAllowed( 'deleterevision' ) ) {
289 // Hide JS by default for non-JS browsing
290 $hidden = array( 'style' => 'display:none' );
291 // If revision was hidden from sysops
292 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
293 $del = Xml
::check( 'deleterevisions', false,
294 $hidden +
array('disabled' => 'disabled') );
295 $del .= Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
296 '(' . $this->message
['rev-delundel'] . ')' );
297 // Otherwise, show the link...
299 $jsCall = 'updateShowHideForm('.$rev->getId().',this.checked)';
300 $del = Xml
::check( 'showhiderevisions', false,
301 $hidden +
array('onchange' => $jsCall) );
303 'type' => 'revision',
304 'target' => $this->mTitle
->getPrefixedDbkey(),
305 'ids' => $rev->getId() );
306 $del .= $this->mSkin
->revDeleteLink( $query,
307 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
313 $s .= " <span class='history-user'>" . $this->mSkin
->revUserTools( $rev, true ) . "</span>";
315 if( $rev->isMinor() ) {
316 $s .= ' ' . Xml
::element( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
319 if( !is_null( $size = $rev->getSize() ) && !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
320 $s .= ' ' . $this->mSkin
->formatRevisionSize( $size );
323 $s .= $this->mSkin
->revComment( $rev, false, true );
325 if( $notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp) ) {
326 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
331 if( !is_null( $next ) && is_object( $next ) ) {
332 if( $latest && $this->mTitle
->userCan( 'rollback' ) && $this->mTitle
->userCan( 'edit' ) ) {
333 $tools[] = '<span class="mw-rollback-link">'.
334 $this->mSkin
->buildRollbackLink( $rev ).'</span>';
337 if( $this->mTitle
->quickUserCan( 'edit' )
338 && !$rev->isDeleted( Revision
::DELETED_TEXT
)
339 && !$next->rev_deleted
& Revision
::DELETED_TEXT
)
341 # Create undo tooltip for the first (=latest) line only
342 $undoTooltip = $latest
343 ?
array( 'title' => wfMsg( 'tooltip-undo' ) )
345 $undolink = $this->mSkin
->link(
347 wfMsgHtml( 'editundo' ),
349 array( 'action' => 'edit', 'undoafter' => $next->rev_id
, 'undo' => $rev->getId() ),
350 array( 'known', 'noclasses' )
352 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
357 $s .= ' (' . $wgLang->pipeList( $tools ) . ')';
361 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'history' );
362 $classes = array_merge( $classes, $newClasses );
363 $s .= " $tagSummary";
365 wfRunHooks( 'PageHistoryLineEnding', array( $this, &$row , &$s ) );
367 $classes = implode( ' ', $classes );
369 return "<li class=\"$classes\">$s</li>\n";
373 * Create a link to view this revision of the page
374 * @param Revision $rev
377 function revLink( $rev ) {
379 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
380 $date = htmlspecialchars( $date );
381 if( !$rev->isDeleted( Revision
::DELETED_TEXT
) ) {
382 $link = $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $date, "oldid=" . $rev->getId() );
384 $link = "<span class=\"history-deleted\">$date</span>";
390 * Create a diff-to-current link for this revision for this page
391 * @param Revision $rev
392 * @param Bool $latest, this is the latest revision of the page?
395 function curLink( $rev, $latest ) {
396 $cur = $this->message
['cur'];
397 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
400 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $cur,
401 'diff=' . $this->mTitle
->getLatestRevID() . "&oldid=" . $rev->getId() );
406 * Create a diff-to-previous link for this revision for this page.
407 * @param Revision $prevRev, the previous revision
408 * @param mixed $next, the newer revision
409 * @param int $counter, what row on the history list this is
412 function lastLink( $prevRev, $next, $counter ) {
413 $last = $this->message
['last'];
414 # $next may either be a Row, null, or "unkown"
415 $nextRev = is_object($next) ?
new Revision( $next ) : $next;
416 if( is_null($next) ) {
417 # Probably no next row
419 } elseif( $next === 'unknown' ) {
420 # Next row probably exists but is unknown, use an oldid=prev link
421 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
422 "diff=" . $prevRev->getId() . "&oldid=prev" );
423 } elseif( !$prevRev->userCan(Revision
::DELETED_TEXT
) ||
!$nextRev->userCan(Revision
::DELETED_TEXT
) ) {
426 return $this->mSkin
->makeKnownLinkObj( $this->mTitle
, $last,
427 "diff=" . $prevRev->getId() . "&oldid={$next->rev_id}" );
432 * Create radio buttons for page history
434 * @param object $rev Revision
435 * @param bool $firstInList Is this version the first one?
436 * @param int $counter A counter of what row number we're at, counted from the top row = 1.
437 * @return string HTML output for the radio buttons
439 function diffButtons( $rev, $firstInList, $counter ) {
440 if( $this->linesonpage
> 1 ) {
441 $radio = array( 'type' => 'radio', 'value' => $rev->getId() );
442 /** @todo: move title texts to javascript */
444 $first = Xml
::element( 'input',
445 array_merge( $radio, array( 'style' => 'visibility:hidden', 'name' => 'oldid' ) )
447 $checkmark = array( 'checked' => 'checked' );
449 # Check visibility of old revisions
450 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
451 $radio['disabled'] = 'disabled';
452 $checkmark = array(); // We will check the next possible one
453 } else if( $counter == 2 ||
!$this->mOldIdChecked
) {
454 $checkmark = array( 'checked' => 'checked' );
455 $this->mOldIdChecked
= $rev->getId();
457 $checkmark = array();
459 $first = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'oldid' ) ) );
460 $checkmark = array();
462 $second = Xml
::element( 'input', array_merge( $radio, $checkmark, array( 'name' => 'diff' ) ) );
463 return $first . $second;
470 * Fetch an array of revisions, specified by a given limit, offset and
471 * direction. This is now only used by the feeds. It was previously
472 * used by the main UI but that's now handled by the pager.
474 function fetchRevisions($limit, $offset, $direction) {
475 $dbr = wfGetDB( DB_SLAVE
);
477 if( $direction == PageHistory
::DIR_PREV
)
478 list($dirs, $oper) = array("ASC", ">=");
479 else /* $direction == PageHistory::DIR_NEXT */
480 list($dirs, $oper) = array("DESC", "<=");
483 $offsets = array("rev_timestamp $oper '$offset'");
487 $page_id = $this->mTitle
->getArticleID();
489 return $dbr->select( 'revision',
490 Revision
::selectFields(),
491 array_merge(array("rev_page=$page_id"), $offsets),
493 array( 'ORDER BY' => "rev_timestamp $dirs",
494 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
499 * Output a subscription feed listing recent edits to this page.
500 * @param string $type
502 function feed( $type ) {
503 global $wgFeedClasses, $wgRequest, $wgFeedLimit;
504 if( !FeedUtils
::checkFeedOutput($type) ) {
508 $feed = new $wgFeedClasses[$type](
509 $this->mTitle
->getPrefixedText() . ' - ' .
510 wfMsgForContent( 'history-feed-title' ),
511 wfMsgForContent( 'history-feed-description' ),
512 $this->mTitle
->getFullUrl( 'action=history' ) );
514 // Get a limit on number of feed entries. Provide a sane default
515 // of 10 if none is defined (but limit to $wgFeedLimit max)
516 $limit = $wgRequest->getInt( 'limit', 10 );
517 if( $limit > $wgFeedLimit ||
$limit < 1 ) {
520 $items = $this->fetchRevisions($limit, 0, PageHistory
::DIR_NEXT
);
524 foreach( $items as $row ) {
525 $feed->outItem( $this->feedItem( $row ) );
528 $feed->outItem( $this->feedEmpty() );
533 function feedEmpty() {
536 wfMsgForContent( 'nohistory' ),
537 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
538 $this->mTitle
->getFullUrl(),
539 wfTimestamp( TS_MW
),
541 $this->mTitle
->getTalkPage()->getFullUrl() );
545 * Generate a FeedItem object from a given revision table row
546 * Borrows Recent Changes' feed generation functions for formatting;
547 * includes a diff to the previous revision (if any).
552 function feedItem( $row ) {
553 $rev = new Revision( $row );
554 $rev->setTitle( $this->mTitle
);
555 $text = FeedUtils
::formatDiffRow( $this->mTitle
,
556 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
558 $rev->getTimestamp(),
559 $rev->getComment() );
561 if( $rev->getComment() == '' ) {
563 $ts = $rev->getTimestamp();
564 $title = wfMsgForContent( 'history-feed-item-nocomment',
566 $wgContLang->timeanddate( $ts ),
567 $wgContLang->date( $ts ),
568 $wgContLang->time( $ts ) );
570 $title = $rev->getUserText() . wfMsgForContent( 'colon-separator' ) . FeedItem
::stripComment( $rev->getComment() );
576 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
577 $rev->getTimestamp(),
579 $this->mTitle
->getTalkPage()->getFullUrl() );
587 class PageHistoryPager
extends ReverseChronologicalPager
{
588 public $mLastRow = false, $mPageHistory, $mTitle;
590 function __construct( $pageHistory, $year='', $month='', $tagFilter = '' ) {
591 parent
::__construct();
592 $this->mPageHistory
= $pageHistory;
593 $this->mTitle
=& $this->mPageHistory
->mTitle
;
594 $this->tagFilter
= $tagFilter;
595 $this->getDateCond( $year, $month );
598 function getQueryInfo() {
600 'tables' => array('revision'),
601 'fields' => array_merge( Revision
::selectFields(), array('ts_tags') ),
602 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
603 'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
604 'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
606 ChangeTags
::modifyDisplayQuery( $queryInfo['tables'],
607 $queryInfo['fields'],
609 $queryInfo['join_conds'],
610 $queryInfo['options'],
612 wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
616 function getIndexField() {
617 return 'rev_timestamp';
620 function formatRow( $row ) {
621 if( $this->mLastRow
) {
622 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
623 $firstInList = $this->mCounter
== 1;
624 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
625 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );
629 $this->mLastRow
= $row;
633 function getStartBody() {
634 $this->mLastRow
= false;
639 function getEndBody() {
640 if( $this->mLastRow
) {
641 $latest = $this->mCounter
== 1 && $this->mIsFirst
;
642 $firstInList = $this->mCounter
== 1;
643 if( $this->mIsBackwards
) {
644 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
645 if( $this->mOffset
== '' ) {
651 # The next row is the past-the-end row
652 $next = $this->mPastTheEndRow
;
654 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
655 $this->mTitle
->getNotificationTimestamp(), $latest, $firstInList );