Add recentchanges recent changes for pg upddates.
[mediawiki.git] / includes / PageHistory.php
blobaea0f0ed224e8ed11d642cb701f356e8de4ec23b
1 <?php
2 /**
3 * Page history
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @package MediaWiki
7 */
9 /**
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
15 * history.
17 * @package MediaWiki
20 class PageHistory {
21 const DIR_PREV = 0;
22 const DIR_NEXT = 1;
24 var $mArticle, $mTitle, $mSkin;
25 var $lastdate;
26 var $linesonpage;
27 var $mNotificationTimestamp;
28 var $mLatestId = null;
30 /**
31 * Construct a new PageHistory.
33 * @param Article $article
34 * @returns nothing
36 function PageHistory($article) {
37 global $wgUser;
39 $this->mArticle =& $article;
40 $this->mTitle =& $article->mTitle;
41 $this->mNotificationTimestamp = NULL;
42 $this->mSkin = $wgUser->getSkin();
45 /**
46 * Print the history page for an article.
48 * @returns nothing
50 function history() {
51 global $wgOut, $wgRequest, $wgTitle;
54 * Allow client caching.
57 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
58 /* Client cache fresh and headers sent, nothing more to do. */
59 return;
61 $fname = 'PageHistory::history';
62 wfProfileIn( $fname );
65 * Setup page variables.
67 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
68 $wgOut->setArticleFlag( false );
69 $wgOut->setArticleRelated( true );
70 $wgOut->setRobotpolicy( 'noindex,nofollow' );
71 $wgOut->setSyndicated( true );
73 $logPage = SpecialPage::getTitleFor( 'Log' );
74 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle->getPrefixedUrl() );
76 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
77 $wgOut->setSubtitle( $subtitle );
79 $feedType = $wgRequest->getVal( 'feed' );
80 if( $feedType ) {
81 wfProfileOut( $fname );
82 return $this->feed( $feedType );
86 * Fail if article doesn't exist.
88 if( !$this->mTitle->exists() ) {
89 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
90 wfProfileOut( $fname );
91 return;
96 * "go=first" means to jump to the last (earliest) history page.
97 * This is deprecated, it no longer appears in the user interface
99 if ( $wgRequest->getText("go") == 'first' ) {
100 $limit = $wgRequest->getInt( 'limit', 50 );
101 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
102 return;
105 /**
106 * Do the list
108 $pager = new PageHistoryPager( $this );
109 $this->linesonpage = $pager->getNumRows();
110 $wgOut->addHTML(
111 $pager->getNavigationBar() .
112 $this->beginHistoryList() .
113 $pager->getBody() .
114 $this->endHistoryList() .
115 $pager->getNavigationBar()
117 wfProfileOut( $fname );
120 /** @todo document */
121 function beginHistoryList() {
122 global $wgTitle;
123 $this->lastdate = '';
124 $s = wfMsgExt( 'histlegend', array( 'parse') );
125 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
126 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
128 // The following line is SUPPOSED to have double-quotes around the
129 // $prefixedkey variable, because htmlspecialchars() doesn't escape
130 // single-quotes.
132 // On at least two occasions people have changed it to single-quotes,
133 // which creates invalid HTML and incorrect display of the resulting
134 // link.
136 // Please do not break this a third time. Thank you for your kind
137 // consideration and cooperation.
139 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
141 $s .= $this->submitButton();
142 $s .= '<ul id="pagehistory">' . "\n";
143 return $s;
146 /** @todo document */
147 function endHistoryList() {
148 $s = '</ul>';
149 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
150 $s .= '</form>';
151 return $s;
154 /** @todo document */
155 function submitButton( $bits = array() ) {
156 return ( $this->linesonpage > 0 )
157 ? wfElement( 'input', array_merge( $bits,
158 array(
159 'class' => 'historysubmit',
160 'type' => 'submit',
161 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
162 'title' => wfMsg( 'tooltip-compareselectedversions' ),
163 'value' => wfMsg( 'compareselectedversions' ),
164 ) ) )
165 : '';
169 * Returns a row from the history printout.
171 * @todo document some more, and maybe clean up the code (some params redundant?)
173 * @param object $row The database row corresponding to the line (or is it the previous line?).
174 * @param object $next The database row corresponding to the next line (or is it this one?).
175 * @param int $counter Apparently a counter of what row number we're at, counted from the top row = 1.
176 * @param $notificationtimestamp
177 * @param bool $latest Whether this row corresponds to the page's latest revision.
178 * @param bool $firstInList Whether this row corresponds to the first displayed on this history page.
179 * @return string HTML output for the row
181 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
182 global $wgUser;
183 $rev = new Revision( $row );
184 $rev->setTitle( $this->mTitle );
186 $s = '<li>';
187 $curlink = $this->curLink( $rev, $latest );
188 $lastlink = $this->lastLink( $rev, $next, $counter );
189 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
190 $link = $this->revLink( $rev );
192 $user = $this->mSkin->userLink( $rev->getUser(), $rev->getUserText() )
193 . $this->mSkin->userToolLinks( $rev->getUser(), $rev->getUserText() );
195 $s .= "($curlink) ($lastlink) $arbitrary";
197 if( $wgUser->isAllowed( 'deleterevision' ) ) {
198 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
199 if( $firstInList ) {
200 // We don't currently handle well changing the top revision's settings
201 $del = wfMsgHtml( 'rev-delundel' );
202 } else {
203 $del = $this->mSkin->makeKnownLinkObj( $revdel,
204 wfMsg( 'rev-delundel' ),
205 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
206 '&oldid=' . urlencode( $rev->getId() ) );
208 $s .= "(<small>$del</small>) ";
211 $s .= " $link <span class='history-user'>$user</span>";
213 if( $row->rev_minor_edit ) {
214 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
217 $s .= $this->mSkin->revComment( $rev );
218 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
219 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
221 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
222 $s .= ' ' . wfMsgHtml( 'deletedrev' );
224 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
225 $s .= ' '.$this->mSkin->generateRollback( $rev );
227 $s .= "</li>\n";
229 return $s;
232 /** @todo document */
233 function revLink( $rev ) {
234 global $wgLang;
235 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
236 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
237 $link = $this->mSkin->makeKnownLinkObj(
238 $this->mTitle, $date, "oldid=" . $rev->getId() );
239 } else {
240 $link = $date;
242 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
243 return '<span class="history-deleted">' . $link . '</span>';
245 return $link;
248 /** @todo document */
249 function curLink( $rev, $latest ) {
250 $cur = wfMsgExt( 'cur', array( 'escape') );
251 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
252 return $cur;
253 } else {
254 return $this->mSkin->makeKnownLinkObj(
255 $this->mTitle, $cur,
256 'diff=' . $this->getLatestID() .
257 "&oldid=" . $rev->getId() );
261 /** @todo document */
262 function lastLink( $rev, $next, $counter ) {
263 $last = wfMsgExt( 'last', array( 'escape' ) );
264 if ( is_null( $next ) ) {
265 # Probably no next row
266 return $last;
267 } elseif ( $next === 'unknown' ) {
268 # Next row probably exists but is unknown, use an oldid=prev link
269 return $this->mSkin->makeKnownLinkObj(
270 $this->mTitle,
271 $last,
272 "diff=" . $rev->getId() . "&oldid=prev" );
273 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
274 return $last;
275 } else {
276 return $this->mSkin->makeKnownLinkObj(
277 $this->mTitle,
278 $last,
279 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
283 "tabindex={$counter}"*/ );
287 /** @todo document */
288 function diffButtons( $rev, $firstInList, $counter ) {
289 if( $this->linesonpage > 1) {
290 $radio = array(
291 'type' => 'radio',
292 'value' => $rev->getId(),
293 # do we really need to flood this on every item?
294 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
297 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
298 $radio['disabled'] = 'disabled';
301 /** @todo: move title texts to javascript */
302 if ( $firstInList ) {
303 $first = wfElement( 'input', array_merge(
304 $radio,
305 array(
306 'style' => 'visibility:hidden',
307 'name' => 'oldid' ) ) );
308 $checkmark = array( 'checked' => 'checked' );
309 } else {
310 if( $counter == 2 ) {
311 $checkmark = array( 'checked' => 'checked' );
312 } else {
313 $checkmark = array();
315 $first = wfElement( 'input', array_merge(
316 $radio,
317 $checkmark,
318 array( 'name' => 'oldid' ) ) );
319 $checkmark = array();
321 $second = wfElement( 'input', array_merge(
322 $radio,
323 $checkmark,
324 array( 'name' => 'diff' ) ) );
325 return $first . $second;
326 } else {
327 return '';
331 /** @todo document */
332 function getLatestId() {
333 if( is_null( $this->mLatestId ) ) {
334 $id = $this->mTitle->getArticleID();
335 $db =& wfGetDB(DB_SLAVE);
336 $this->mLatestId = $db->selectField( 'page',
337 "page_latest",
338 array( 'page_id' => $id ),
339 'PageHistory::getLatestID' );
341 return $this->mLatestId;
345 * Fetch an array of revisions, specified by a given limit, offset and
346 * direction. This is now only used by the feeds. It was previously
347 * used by the main UI but that's now handled by the pager.
349 function fetchRevisions($limit, $offset, $direction) {
350 $fname = 'PageHistory::fetchRevisions';
352 $dbr =& wfGetDB( DB_SLAVE );
354 if ($direction == PageHistory::DIR_PREV)
355 list($dirs, $oper) = array("ASC", ">=");
356 else /* $direction == PageHistory::DIR_NEXT */
357 list($dirs, $oper) = array("DESC", "<=");
359 if ($offset)
360 $offsets = array("rev_timestamp $oper '$offset'");
361 else
362 $offsets = array();
364 $page_id = $this->mTitle->getArticleID();
366 $res = $dbr->select(
367 'revision',
368 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
369 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
370 array_merge(array("rev_page=$page_id"), $offsets),
371 $fname,
372 array('ORDER BY' => "rev_timestamp $dirs",
373 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
376 $result = array();
377 while (($obj = $dbr->fetchObject($res)) != NULL)
378 $result[] = $obj;
380 return $result;
383 /** @todo document */
384 function getNotificationTimestamp() {
385 global $wgUser, $wgShowUpdatedMarker;
386 $fname = 'PageHistory::getNotficationTimestamp';
388 if ($this->mNotificationTimestamp !== NULL)
389 return $this->mNotificationTimestamp;
391 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
392 return $this->mNotificationTimestamp = false;
394 $dbr =& wfGetDB(DB_SLAVE);
396 $this->mNotificationTimestamp = $dbr->selectField(
397 'watchlist',
398 'wl_notificationtimestamp',
399 array( 'wl_namespace' => $this->mTitle->getNamespace(),
400 'wl_title' => $this->mTitle->getDBkey(),
401 'wl_user' => $wgUser->getID()
403 $fname);
405 // Don't use the special value reserved for telling whether the field is filled
406 if ( is_null( $this->mNotificationTimestamp ) ) {
407 $this->mNotificationTimestamp = false;
410 return $this->mNotificationTimestamp;
414 * Output a subscription feed listing recent edits to this page.
415 * @param string $type
417 function feed( $type ) {
418 require_once 'SpecialRecentchanges.php';
420 global $wgFeedClasses;
421 if( !isset( $wgFeedClasses[$type] ) ) {
422 global $wgOut;
423 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
424 return;
427 $feed = new $wgFeedClasses[$type](
428 $this->mTitle->getPrefixedText() . ' - ' .
429 wfMsgForContent( 'history-feed-title' ),
430 wfMsgForContent( 'history-feed-description' ),
431 $this->mTitle->getFullUrl( 'action=history' ) );
433 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
434 $feed->outHeader();
435 if( $items ) {
436 foreach( $items as $row ) {
437 $feed->outItem( $this->feedItem( $row ) );
439 } else {
440 $feed->outItem( $this->feedEmpty() );
442 $feed->outFooter();
445 function feedEmpty() {
446 global $wgOut;
447 return new FeedItem(
448 wfMsgForContent( 'nohistory' ),
449 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
450 $this->mTitle->getFullUrl(),
451 wfTimestamp( TS_MW ),
453 $this->mTitle->getTalkPage()->getFullUrl() );
457 * Generate a FeedItem object from a given revision table row
458 * Borrows Recent Changes' feed generation functions for formatting;
459 * includes a diff to the previous revision (if any).
461 * @param $row
462 * @return FeedItem
464 function feedItem( $row ) {
465 $rev = new Revision( $row );
466 $rev->setTitle( $this->mTitle );
467 $text = rcFormatDiffRow( $this->mTitle,
468 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
469 $rev->getId(),
470 $rev->getTimestamp(),
471 $rev->getComment() );
473 if( $rev->getComment() == '' ) {
474 global $wgContLang;
475 $title = wfMsgForContent( 'history-feed-item-nocomment',
476 $rev->getUserText(),
477 $wgContLang->timeanddate( $rev->getTimestamp() ) );
478 } else {
479 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
482 return new FeedItem(
483 $title,
484 $text,
485 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
486 $rev->getTimestamp(),
487 $rev->getUserText(),
488 $this->mTitle->getTalkPage()->getFullUrl() );
492 * Quickie hack... strip out wikilinks to more legible form from the comment.
494 function stripComment( $text ) {
495 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
500 class PageHistoryPager extends ReverseChronologicalPager {
501 public $mLastRow = false, $mPageHistory;
503 function __construct( $pageHistory ) {
504 parent::__construct();
505 $this->mPageHistory = $pageHistory;
508 function getQueryInfo() {
509 return array(
510 'tables' => 'revision',
511 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
512 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
513 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
514 'options' => array( 'USE INDEX' => 'page_timestamp' )
518 function getIndexField() {
519 return 'rev_timestamp';
522 function formatRow( $row ) {
523 if ( $this->mLastRow ) {
524 $latest = $this->mCounter == 1 && $this->mOffset == '';
525 $firstInList = $this->mCounter == 1;
526 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
527 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
528 } else {
529 $s = '';
531 $this->mLastRow = $row;
532 return $s;
535 function getStartBody() {
536 $this->mLastRow = false;
537 $this->mCounter = 1;
538 return '';
541 function getEndBody() {
542 if ( $this->mLastRow ) {
543 $latest = $this->mCounter == 1 && $this->mOffset == 0;
544 $firstInList = $this->mCounter == 1;
545 if ( $this->mIsBackwards ) {
546 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
547 if ( $this->mOffset == '' ) {
548 $next = null;
549 } else {
550 $next = 'unknown';
552 } else {
553 # The next row is the past-the-end row
554 $next = $this->mPastTheEndRow;
556 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
557 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
558 } else {
559 $s = '';
561 return $s;