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
24 var $mArticle, $mTitle;
27 var $mNotificationTimestamp;
28 var $mLatestId = null;
31 * Construct a new PageHistory.
33 * @param Article $article
36 function PageHistory($article) {
37 $this->mArticle
=& $article;
38 $this->mTitle
=& $article->mTitle
;
39 $this->mNotificationTimestamp
= NULL;
43 * Print the history page for an article.
48 global $wgOut, $wgRequest, $wgTitle;
51 * Allow client caching.
54 if( $wgOut->checkLastModified( $this->mArticle
->getTimestamp() ) )
55 /* Client cache fresh and headers sent, nothing more to do. */
58 $fname = 'PageHistory::history';
59 wfProfileIn( $fname );
62 * Setup page variables.
64 $wgOut->setPageTitle( $this->mTitle
->getPrefixedText() );
65 $wgOut->setArticleFlag( false );
66 $wgOut->setArticleRelated( true );
67 $wgOut->setRobotpolicy( 'noindex,nofollow' );
68 $wgOut->setSyndicated( true );
70 $logPage = SpecialPage
::getTitleFor( 'Log' );
71 $logLink = Linker
::makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle
->getPrefixedUrl() );
73 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
74 $wgOut->setSubtitle( $subtitle );
76 $feedType = $wgRequest->getVal( 'feed' );
78 wfProfileOut( $fname );
79 return $this->feed( $feedType );
83 * Fail if article doesn't exist.
85 if( !$this->mTitle
->exists() ) {
86 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
87 wfProfileOut( $fname );
93 * "go=first" means to jump to the last (earliest) history page.
94 * This is deprecated, it no longer appears in the user interface
96 if ( $wgRequest->getText("go") == 'first' ) {
97 $limit = $wgRequest->getInt( 'limit', 50 );
98 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
105 $pager = new PageHistoryPager( $this );
106 $navbar = $pager->getNavigationBar();
107 $this->linesonpage
= $pager->getNumRows();
109 $pager->getNavigationBar() .
110 $this->beginHistoryList() .
112 $this->endHistoryList() .
113 $pager->getNavigationBar()
115 wfProfileOut( $fname );
118 /** @todo document */
119 function beginHistoryList() {
121 $this->lastdate
= '';
122 $s = wfMsgExt( 'histlegend', array( 'parse') );
123 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
124 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
126 // The following line is SUPPOSED to have double-quotes around the
127 // $prefixedkey variable, because htmlspecialchars() doesn't escape
130 // On at least two occasions people have changed it to single-quotes,
131 // which creates invalid HTML and incorrect display of the resulting
134 // Please do not break this a third time. Thank you for your kind
135 // consideration and cooperation.
137 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
139 $s .= $this->submitButton();
140 $s .= '<ul id="pagehistory">' . "\n";
144 /** @todo document */
145 function endHistoryList() {
147 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
152 /** @todo document */
153 function submitButton( $bits = array() ) {
154 return ( $this->linesonpage
> 0 )
155 ?
wfElement( 'input', array_merge( $bits,
157 'class' => 'historysubmit',
159 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
160 'title' => wfMsg( 'tooltip-compareselectedversions' ),
161 'value' => wfMsg( 'compareselectedversions' ),
166 /** @todo document */
167 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
169 $rev = new Revision( $row );
170 $rev->setTitle( $this->mTitle
);
173 $curlink = $this->curLink( $rev, $latest );
174 $lastlink = $this->lastLink( $rev, $next, $counter );
175 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
176 $link = $this->revLink( $rev );
178 $user = Linker
::userLink( $rev->getUser(), $rev->getUserText() )
179 . Linker
::userToolLinks( $rev->getUser(), $rev->getUserText() );
181 $s .= "($curlink) ($lastlink) $arbitrary";
183 if( $wgUser->isAllowed( 'deleterevision' ) ) {
184 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
186 // We don't currently handle well changing the top revision's settings
187 $del = wfMsgHtml( 'rev-delundel' );
189 $del = Linker
::makeKnownLinkObj( $revdel,
190 wfMsg( 'rev-delundel' ),
191 'target=' . urlencode( $this->mTitle
->getPrefixedDbkey() ) .
192 '&oldid=' . urlencode( $rev->getId() ) );
194 $s .= "(<small>$del</small>) ";
197 $s .= " $link <span class='history-user'>$user</span>";
199 if( $row->rev_minor_edit
) {
200 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
203 $s .= Linker
::revComment( $rev );
204 if ($notificationtimestamp && ($row->rev_timestamp
>= $notificationtimestamp)) {
205 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
207 if( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
208 $s .= ' ' . wfMsgHtml( 'deletedrev' );
215 /** @todo document */
216 function revLink( $rev ) {
218 $date = $wgLang->timeanddate( wfTimestamp(TS_MW
, $rev->getTimestamp()), true );
219 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
220 $link = Linker
::makeKnownLinkObj(
221 $this->mTitle
, $date, "oldid=" . $rev->getId() );
225 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
226 return '<span class="history-deleted">' . $link . '</span>';
231 /** @todo document */
232 function curLink( $rev, $latest ) {
233 $cur = wfMsgExt( 'cur', array( 'escape') );
234 if( $latest ||
!$rev->userCan( Revision
::DELETED_TEXT
) ) {
237 return Linker
::makeKnownLinkObj(
239 'diff=' . $this->getLatestID() .
240 "&oldid=" . $rev->getId() );
244 /** @todo document */
245 function lastLink( $rev, $next, $counter ) {
246 $last = wfMsgExt( 'last', array( 'escape' ) );
247 if ( is_null( $next ) ) {
248 # Probably no next row
250 } elseif ( $next === 'unknown' ) {
251 # Next row probably exists but is unknown, use an oldid=prev link
252 return Linker
::makeKnownLinkObj(
255 "diff=" . $rev->getId() . "&oldid=prev" );
256 } elseif( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
259 return Linker
::makeKnownLinkObj(
262 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
266 "tabindex={$counter}"*/ );
270 /** @todo document */
271 function diffButtons( $rev, $firstInList, $counter ) {
272 if( $this->linesonpage
> 1) {
275 'value' => $rev->getId(),
276 # do we really need to flood this on every item?
277 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
280 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
281 $radio['disabled'] = 'disabled';
284 /** @todo: move title texts to javascript */
285 if ( $firstInList ) {
286 $first = wfElement( 'input', array_merge(
289 'style' => 'visibility:hidden',
290 'name' => 'oldid' ) ) );
291 $checkmark = array( 'checked' => 'checked' );
293 if( $counter == 2 ) {
294 $checkmark = array( 'checked' => 'checked' );
296 $checkmark = array();
298 $first = wfElement( 'input', array_merge(
301 array( 'name' => 'oldid' ) ) );
302 $checkmark = array();
304 $second = wfElement( 'input', array_merge(
307 array( 'name' => 'diff' ) ) );
308 return $first . $second;
314 /** @todo document */
315 function getLatestId() {
316 if( is_null( $this->mLatestId
) ) {
317 $id = $this->mTitle
->getArticleID();
318 $db =& wfGetDB(DB_SLAVE
);
319 $this->mLatestId
= $db->selectField( 'page',
321 array( 'page_id' => $id ),
322 'PageHistory::getLatestID' );
324 return $this->mLatestId
;
328 * Fetch an array of revisions, specified by a given limit, offset and
329 * direction. This is now only used by the feeds. It was previously
330 * used by the main UI but that's now handled by the pager.
332 function fetchRevisions($limit, $offset, $direction) {
333 $fname = 'PageHistory::fetchRevisions';
335 $dbr =& wfGetDB( DB_SLAVE
);
337 if ($direction == PageHistory
::DIR_PREV
)
338 list($dirs, $oper) = array("ASC", ">=");
339 else /* $direction == PageHistory::DIR_NEXT */
340 list($dirs, $oper) = array("DESC", "<=");
343 $offsets = array("rev_timestamp $oper '$offset'");
347 $page_id = $this->mTitle
->getArticleID();
351 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
352 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
353 array_merge(array("rev_page=$page_id"), $offsets),
355 array('ORDER BY' => "rev_timestamp $dirs",
356 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
360 while (($obj = $dbr->fetchObject($res)) != NULL)
366 /** @todo document */
367 function getNotificationTimestamp() {
368 global $wgUser, $wgShowUpdatedMarker;
369 $fname = 'PageHistory::getNotficationTimestamp';
371 if ($this->mNotificationTimestamp
!== NULL)
372 return $this->mNotificationTimestamp
;
374 if ($wgUser->isAnon() ||
!$wgShowUpdatedMarker)
375 return $this->mNotificationTimestamp
= false;
377 $dbr =& wfGetDB(DB_SLAVE
);
379 $this->mNotificationTimestamp
= $dbr->selectField(
381 'wl_notificationtimestamp',
382 array( 'wl_namespace' => $this->mTitle
->getNamespace(),
383 'wl_title' => $this->mTitle
->getDBkey(),
384 'wl_user' => $wgUser->getID()
388 // Don't use the special value reserved for telling whether the field is filled
389 if ( is_null( $this->mNotificationTimestamp
) ) {
390 $this->mNotificationTimestamp
= false;
393 return $this->mNotificationTimestamp
;
397 * Output a subscription feed listing recent edits to this page.
398 * @param string $type
400 function feed( $type ) {
401 require_once 'SpecialRecentchanges.php';
403 global $wgFeedClasses;
404 if( !isset( $wgFeedClasses[$type] ) ) {
406 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
410 $feed = new $wgFeedClasses[$type](
411 $this->mTitle
->getPrefixedText() . ' - ' .
412 wfMsgForContent( 'history-feed-title' ),
413 wfMsgForContent( 'history-feed-description' ),
414 $this->mTitle
->getFullUrl( 'action=history' ) );
416 $items = $this->fetchRevisions(10, 0, PageHistory
::DIR_NEXT
);
419 foreach( $items as $row ) {
420 $feed->outItem( $this->feedItem( $row ) );
423 $feed->outItem( $this->feedEmpty() );
428 function feedEmpty() {
431 wfMsgForContent( 'nohistory' ),
432 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
433 $this->mTitle
->getFullUrl(),
434 wfTimestamp( TS_MW
),
436 $this->mTitle
->getTalkPage()->getFullUrl() );
440 * Generate a FeedItem object from a given revision table row
441 * Borrows Recent Changes' feed generation functions for formatting;
442 * includes a diff to the previous revision (if any).
447 function feedItem( $row ) {
448 $rev = new Revision( $row );
449 $rev->setTitle( $this->mTitle
);
450 $text = rcFormatDiffRow( $this->mTitle
,
451 $this->mTitle
->getPreviousRevisionID( $rev->getId() ),
453 $rev->getTimestamp(),
454 $rev->getComment() );
456 if( $rev->getComment() == '' ) {
458 $title = wfMsgForContent( 'history-feed-item-nocomment',
460 $wgContLang->timeanddate( $rev->getTimestamp() ) );
462 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
468 $this->mTitle
->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
469 $rev->getTimestamp(),
471 $this->mTitle
->getTalkPage()->getFullUrl() );
475 * Quickie hack... strip out wikilinks to more legible form from the comment.
477 function stripComment( $text ) {
478 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
483 class PageHistoryPager
extends ReverseChronologicalPager
{
484 public $mLastRow = false, $mPageHistory;
486 function __construct( $pageHistory ) {
487 parent
::__construct();
488 $this->mPageHistory
= $pageHistory;
491 function getQueryInfo() {
493 'tables' => 'revision',
494 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
495 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
496 'conds' => array('rev_page' => $this->mPageHistory
->mTitle
->getArticleID() ),
497 'options' => array( 'USE INDEX' => 'page_timestamp' )
501 function getIndexField() {
502 return 'rev_timestamp';
505 function formatRow( $row ) {
506 if ( $this->mLastRow
) {
507 $latest = $this->mCounter
== 1 && $this->mOffset
== '';
508 $firstInList = $this->mCounter
== 1;
509 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $row, $this->mCounter++
,
510 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );
514 $this->mLastRow
= $row;
518 function getStartBody() {
519 $this->mLastRow
= false;
524 function getEndBody() {
525 if ( $this->mLastRow
) {
526 $latest = $this->mCounter
== 1 && $this->mOffset
== 0;
527 $firstInList = $this->mCounter
== 1;
528 if ( $this->mIsBackwards
) {
529 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
530 if ( $this->mOffset
== '' ) {
536 # The next row is the past-the-end row
537 $next = $this->mPastTheEndRow
;
539 $s = $this->mPageHistory
->historyLine( $this->mLastRow
, $next, $this->mCounter++
,
540 $this->mPageHistory
->getNotificationTimestamp(), $latest, $firstInList );