Introduced hook function for requesting that extension messages be loaded
[mediawiki.git] / includes / PageHistory.php
blobd7f426fc4d6efb4ec282d7f916a6046eb420ca2f
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 = Title::makeTitle( NS_SPECIAL, '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 $navbar = $pager->getNavigationBar();
110 $this->linesonpage = $pager->getNumRows();
111 $wgOut->addHTML(
112 $pager->getNavigationBar() .
113 $this->beginHistoryList() .
114 $pager->getBody() .
115 $this->endHistoryList() .
116 $pager->getNavigationBar()
118 wfProfileOut( $fname );
121 /** @todo document */
122 function beginHistoryList() {
123 global $wgTitle;
124 $this->lastdate = '';
125 $s = wfMsgExt( 'histlegend', array( 'parse') );
126 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
127 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
129 // The following line is SUPPOSED to have double-quotes around the
130 // $prefixedkey variable, because htmlspecialchars() doesn't escape
131 // single-quotes.
133 // On at least two occasions people have changed it to single-quotes,
134 // which creates invalid HTML and incorrect display of the resulting
135 // link.
137 // Please do not break this a third time. Thank you for your kind
138 // consideration and cooperation.
140 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
142 $s .= $this->submitButton();
143 $s .= '<ul id="pagehistory">' . "\n";
144 return $s;
147 /** @todo document */
148 function endHistoryList() {
149 $s = '</ul>';
150 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
151 $s .= '</form>';
152 return $s;
155 /** @todo document */
156 function submitButton( $bits = array() ) {
157 return ( $this->linesonpage > 0 )
158 ? wfElement( 'input', array_merge( $bits,
159 array(
160 'class' => 'historysubmit',
161 'type' => 'submit',
162 'accesskey' => wfMsg( 'accesskey-compareselectedversions' ),
163 'title' => wfMsg( 'tooltip-compareselectedversions' ),
164 'value' => wfMsg( 'compareselectedversions' ),
165 ) ) )
166 : '';
169 /** @todo document */
170 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
171 global $wgUser;
172 $rev = new Revision( $row );
173 $rev->setTitle( $this->mTitle );
175 $s = '<li>';
176 $curlink = $this->curLink( $rev, $latest );
177 $lastlink = $this->lastLink( $rev, $next, $counter );
178 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
179 $link = $this->revLink( $rev );
181 $user = $this->mSkin->userLink( $rev->getUser(), $rev->getUserText() )
182 . $this->mSkin->userToolLinks( $rev->getUser(), $rev->getUserText() );
184 $s .= "($curlink) ($lastlink) $arbitrary";
186 if( $wgUser->isAllowed( 'deleterevision' ) ) {
187 $revdel = Title::makeTitle( NS_SPECIAL, 'Revisiondelete' );
188 if( $firstInList ) {
189 // We don't currently handle well changing the top revision's settings
190 $del = wfMsgHtml( 'rev-delundel' );
191 } else {
192 $del = $this->mSkin->makeKnownLinkObj( $revdel,
193 wfMsg( 'rev-delundel' ),
194 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
195 '&oldid=' . urlencode( $rev->getId() ) );
197 $s .= "(<small>$del</small>) ";
200 $s .= " $link <span class='history-user'>$user</span>";
202 if( $row->rev_minor_edit ) {
203 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
206 $s .= $this->mSkin->revComment( $rev );
207 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
208 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
210 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
211 $s .= ' ' . wfMsgHtml( 'deletedrev' );
213 $s .= "</li>\n";
215 return $s;
218 /** @todo document */
219 function revLink( $rev ) {
220 global $wgLang;
221 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
222 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
223 $link = $this->mSkin->makeKnownLinkObj(
224 $this->mTitle, $date, "oldid=" . $rev->getId() );
225 } else {
226 $link = $date;
228 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
229 return '<span class="history-deleted">' . $link . '</span>';
231 return $link;
234 /** @todo document */
235 function curLink( $rev, $latest ) {
236 $cur = wfMsgExt( 'cur', array( 'escape') );
237 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
238 return $cur;
239 } else {
240 return $this->mSkin->makeKnownLinkObj(
241 $this->mTitle, $cur,
242 'diff=' . $this->getLatestID() .
243 "&oldid=" . $rev->getId() );
247 /** @todo document */
248 function lastLink( $rev, $next, $counter ) {
249 $last = wfMsgExt( 'last', array( 'escape' ) );
250 if ( is_null( $next ) ) {
251 # Probably no next row
252 return $last;
253 } elseif ( $next === 'unknown' ) {
254 # Next row probably exists but is unknown, use an oldid=prev link
255 return $this->mSkin->makeKnownLinkObj(
256 $this->mTitle,
257 $last,
258 "diff=" . $rev->getId() . "&oldid=prev" );
259 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
260 return $last;
261 } else {
262 return $this->mSkin->makeKnownLinkObj(
263 $this->mTitle,
264 $last,
265 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
269 "tabindex={$counter}"*/ );
273 /** @todo document */
274 function diffButtons( $rev, $firstInList, $counter ) {
275 if( $this->linesonpage > 1) {
276 $radio = array(
277 'type' => 'radio',
278 'value' => $rev->getId(),
279 # do we really need to flood this on every item?
280 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
283 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
284 $radio['disabled'] = 'disabled';
287 /** @todo: move title texts to javascript */
288 if ( $firstInList ) {
289 $first = wfElement( 'input', array_merge(
290 $radio,
291 array(
292 'style' => 'visibility:hidden',
293 'name' => 'oldid' ) ) );
294 $checkmark = array( 'checked' => 'checked' );
295 } else {
296 if( $counter == 2 ) {
297 $checkmark = array( 'checked' => 'checked' );
298 } else {
299 $checkmark = array();
301 $first = wfElement( 'input', array_merge(
302 $radio,
303 $checkmark,
304 array( 'name' => 'oldid' ) ) );
305 $checkmark = array();
307 $second = wfElement( 'input', array_merge(
308 $radio,
309 $checkmark,
310 array( 'name' => 'diff' ) ) );
311 return $first . $second;
312 } else {
313 return '';
317 /** @todo document */
318 function getLatestId() {
319 if( is_null( $this->mLatestId ) ) {
320 $id = $this->mTitle->getArticleID();
321 $db =& wfGetDB(DB_SLAVE);
322 $this->mLatestId = $db->selectField( 'page',
323 "page_latest",
324 array( 'page_id' => $id ),
325 'PageHistory::getLatestID' );
327 return $this->mLatestId;
331 * Fetch an array of revisions, specified by a given limit, offset and
332 * direction. This is now only used by the feeds. It was previously
333 * used by the main UI but that's now handled by the pager.
335 function fetchRevisions($limit, $offset, $direction) {
336 $fname = 'PageHistory::fetchRevisions';
338 $dbr =& wfGetDB( DB_SLAVE );
340 if ($direction == PageHistory::DIR_PREV)
341 list($dirs, $oper) = array("ASC", ">=");
342 else /* $direction == PageHistory::DIR_NEXT */
343 list($dirs, $oper) = array("DESC", "<=");
345 if ($offset)
346 $offsets = array("rev_timestamp $oper '$offset'");
347 else
348 $offsets = array();
350 $page_id = $this->mTitle->getArticleID();
352 $res = $dbr->select(
353 'revision',
354 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
355 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
356 array_merge(array("rev_page=$page_id"), $offsets),
357 $fname,
358 array('ORDER BY' => "rev_timestamp $dirs",
359 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
362 $result = array();
363 while (($obj = $dbr->fetchObject($res)) != NULL)
364 $result[] = $obj;
366 return $result;
369 /** @todo document */
370 function getNotificationTimestamp() {
371 global $wgUser, $wgShowUpdatedMarker;
372 $fname = 'PageHistory::getNotficationTimestamp';
374 if ($this->mNotificationTimestamp !== NULL)
375 return $this->mNotificationTimestamp;
377 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
378 return $this->mNotificationTimestamp = false;
380 $dbr =& wfGetDB(DB_SLAVE);
382 $this->mNotificationTimestamp = $dbr->selectField(
383 'watchlist',
384 'wl_notificationtimestamp',
385 array( 'wl_namespace' => $this->mTitle->getNamespace(),
386 'wl_title' => $this->mTitle->getDBkey(),
387 'wl_user' => $wgUser->getID()
389 $fname);
391 // Don't use the special value reserved for telling whether the field is filled
392 if ( is_null( $this->mNotificationTimestamp ) ) {
393 $this->mNotificationTimestamp = false;
396 return $this->mNotificationTimestamp;
400 * Output a subscription feed listing recent edits to this page.
401 * @param string $type
403 function feed( $type ) {
404 require_once 'SpecialRecentchanges.php';
406 global $wgFeedClasses;
407 if( !isset( $wgFeedClasses[$type] ) ) {
408 global $wgOut;
409 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
410 return;
413 $feed = new $wgFeedClasses[$type](
414 $this->mTitle->getPrefixedText() . ' - ' .
415 wfMsgForContent( 'history-feed-title' ),
416 wfMsgForContent( 'history-feed-description' ),
417 $this->mTitle->getFullUrl( 'action=history' ) );
419 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
420 $feed->outHeader();
421 if( $items ) {
422 foreach( $items as $row ) {
423 $feed->outItem( $this->feedItem( $row ) );
425 } else {
426 $feed->outItem( $this->feedEmpty() );
428 $feed->outFooter();
431 function feedEmpty() {
432 global $wgOut;
433 return new FeedItem(
434 wfMsgForContent( 'nohistory' ),
435 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
436 $this->mTitle->getFullUrl(),
437 wfTimestamp( TS_MW ),
439 $this->mTitle->getTalkPage()->getFullUrl() );
443 * Generate a FeedItem object from a given revision table row
444 * Borrows Recent Changes' feed generation functions for formatting;
445 * includes a diff to the previous revision (if any).
447 * @param $row
448 * @return FeedItem
450 function feedItem( $row ) {
451 $rev = new Revision( $row );
452 $rev->setTitle( $this->mTitle );
453 $text = rcFormatDiffRow( $this->mTitle,
454 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
455 $rev->getId(),
456 $rev->getTimestamp(),
457 $rev->getComment() );
459 if( $rev->getComment() == '' ) {
460 global $wgContLang;
461 $title = wfMsgForContent( 'history-feed-item-nocomment',
462 $rev->getUserText(),
463 $wgContLang->timeanddate( $rev->getTimestamp() ) );
464 } else {
465 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
468 return new FeedItem(
469 $title,
470 $text,
471 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
472 $rev->getTimestamp(),
473 $rev->getUserText(),
474 $this->mTitle->getTalkPage()->getFullUrl() );
478 * Quickie hack... strip out wikilinks to more legible form from the comment.
480 function stripComment( $text ) {
481 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
486 class PageHistoryPager extends ReverseChronologicalPager {
487 public $mLastRow = false, $mPageHistory;
489 function __construct( $pageHistory ) {
490 parent::__construct();
491 $this->mPageHistory = $pageHistory;
494 function getQueryInfo() {
495 return array(
496 'tables' => 'revision',
497 'fields' => array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
498 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
499 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
500 'options' => array( 'USE INDEX' => 'page_timestamp' )
504 function getIndexField() {
505 return 'rev_timestamp';
508 function formatRow( $row ) {
509 if ( $this->mLastRow ) {
510 $latest = $this->mCounter == 1 && $this->mOffset == '';
511 $firstInList = $this->mCounter == 1;
512 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
513 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
514 } else {
515 $s = '';
517 $this->mLastRow = $row;
518 return $s;
521 function getStartBody() {
522 $this->mLastRow = false;
523 $this->mCounter = 1;
524 return '';
527 function getEndBody() {
528 if ( $this->mLastRow ) {
529 $latest = $this->mCounter == 1 && $this->mOffset == 0;
530 $firstInList = $this->mCounter == 1;
531 if ( $this->mIsBackwards ) {
532 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
533 if ( $this->mOffset == '' ) {
534 $next = null;
535 } else {
536 $next = 'unknown';
538 } else {
539 # The next row is the past-the-end row
540 $next = $this->mPastTheEndRow;
542 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
543 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
544 } else {
545 $s = '';
547 return $s;