*Tweak rev_deleted message
[mediawiki.git] / includes / PageHistory.php
blob6842107dd4faaa50d305d66b9afd9e5fc58ef815
1 <?php
2 /**
3 * Page history
5 * Split off from Article.php and Skin.php, 2003-12-22
6 */
8 /**
9 * This class handles printing the history page for an article. In order to
10 * be efficient, it uses timestamps rather than offsets for paging, to avoid
11 * costly LIMIT,offset queries.
13 * Construct it by passing in an Article, and call $h->history() to print the
14 * history.
17 class PageHistory {
18 const DIR_PREV = 0;
19 const DIR_NEXT = 1;
21 var $mArticle, $mTitle, $mSkin;
22 var $lastdate;
23 var $linesonpage;
24 var $mNotificationTimestamp;
25 var $mLatestId = null;
27 /**
28 * Construct a new PageHistory.
30 * @param Article $article
31 * @returns nothing
33 function __construct($article) {
34 global $wgUser;
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38 $this->mNotificationTimestamp = NULL;
39 $this->mSkin = $wgUser->getSkin();
42 /**
43 * Print the history page for an article.
45 * @returns nothing
47 function history() {
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. */
56 return;
58 $fname = 'PageHistory::history';
59 wfProfileIn( $fname );
62 * Setup page variables.
64 $wgOut->setPageTitle( wfMsg( 'history-title', $this->mTitle->getPrefixedText() ) );
65 $wgOut->setPageTitleActionText( wfMsg( 'history_short' ) );
66 $wgOut->setArticleFlag( false );
67 $wgOut->setArticleRelated( true );
68 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 $wgOut->setSyndicated( true );
71 $logPage = SpecialPage::getTitleFor( 'Log' );
72 $logLink = $this->mSkin->makeKnownLinkObj( $logPage, wfMsgHtml( 'viewpagelogs' ), 'page=' . $this->mTitle->getPrefixedUrl() );
74 $subtitle = wfMsgHtml( 'revhistory' ) . '<br />' . $logLink;
75 $wgOut->setSubtitle( $subtitle );
77 $feedType = $wgRequest->getVal( 'feed' );
78 if( $feedType ) {
79 wfProfileOut( $fname );
80 return $this->feed( $feedType );
84 * Fail if article doesn't exist.
86 if( !$this->mTitle->exists() ) {
87 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
88 wfProfileOut( $fname );
89 return;
94 * "go=first" means to jump to the last (earliest) history page.
95 * This is deprecated, it no longer appears in the user interface
97 if ( $wgRequest->getText("go") == 'first' ) {
98 $limit = $wgRequest->getInt( 'limit', 50 );
99 $wgOut->redirect( $wgTitle->getLocalURL( "action=history&limit={$limit}&dir=prev" ) );
100 return;
103 wfRunHooks( 'PageHistoryBeforeList', array( &$this->mArticle ) );
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' ).' ['.wfMsg( 'accesskey-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, $wgLang;
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 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
203 // If revision was hidden from sysops
204 $del = wfMsgHtml( 'rev-delundel' );
205 } else {
206 $del = $this->mSkin->makeKnownLinkObj( $revdel,
207 wfMsg( 'rev-delundel' ),
208 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
209 '&oldid=' . urlencode( $rev->getId() ) );
211 $s .= " (<small>$del</small>) ";
214 $s .= " $link";
215 #getUser is safe, but this avoids making the invalid untargeted contribs links
216 if( $row->rev_deleted & Revision::DELETED_USER ) {
217 $user = '<span class="history-deleted">' . wfMsg('rev-deleted-user') . '</span>';
219 $s .= " <span class='history-user'>$user</span>";
221 if( $row->rev_minor_edit ) {
222 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsg( 'minoreditletter') );
225 if ( !is_null( $size = $rev->getSize() ) ) {
226 if ( $size == 0 )
227 $stxt = wfMsgHtml( 'historyempty' );
228 else
229 $stxt = wfMsgExt( 'historysize', array( 'parsemag' ), $wgLang->formatNum( $size ) );
230 $s .= " <span class=\"history-size\">$stxt</span>";
233 #getComment is safe, but this is better formatted
234 if( $rev->isDeleted( Revision::DELETED_COMMENT ) ) {
235 $s .= " <span class=\"history-deleted\"><span class=\"comment\">" .
236 wfMsgHtml( 'rev-deleted-comment' ) . "</span></span>";
237 } else {
238 $s .= $this->mSkin->revComment( $rev );
241 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
242 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
244 #add blurb about text having been deleted
245 if( $row->rev_deleted & Revision::DELETED_TEXT ) {
246 $s .= ' ' . wfMsgHtml( 'deletedrev' );
249 $tools = array();
251 if ( !is_null( $next ) && is_object( $next ) ) {
252 if( $wgUser->isAllowed( 'rollback' ) && $latest ) {
253 $tools[] = '<span class="mw-rollback-link">'
254 . $this->mSkin->buildRollbackLink( $rev )
255 . '</span>';
258 $undolink = $this->mSkin->makeKnownLinkObj(
259 $this->mTitle,
260 wfMsgHtml( 'editundo' ),
261 'action=edit&undoafter=' . $next->rev_id . '&undo=' . $rev->getId()
263 $tools[] = "<span class=\"mw-history-undo\">{$undolink}</span>";
266 if( $tools ) {
267 $s .= ' (' . implode( ' | ', $tools ) . ')';
270 wfRunHooks( 'PageHistoryLineEnding', array( &$row , &$s ) );
272 $s .= "</li>\n";
274 return $s;
277 /** @todo document */
278 function revLink( $rev ) {
279 global $wgLang;
280 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
281 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
282 $link = $this->mSkin->makeKnownLinkObj(
283 $this->mTitle, $date, "oldid=" . $rev->getId() );
284 } else {
285 $link = $date;
287 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
288 return '<span class="history-deleted">' . $link . '</span>';
290 return $link;
293 /** @todo document */
294 function curLink( $rev, $latest ) {
295 $cur = wfMsgExt( 'cur', array( 'escape') );
296 if( $latest || !$rev->userCan( Revision::DELETED_TEXT ) ) {
297 return $cur;
298 } else {
299 return $this->mSkin->makeKnownLinkObj(
300 $this->mTitle, $cur,
301 'diff=' . $this->getLatestID() .
302 "&oldid=" . $rev->getId() );
306 /** @todo document */
307 function lastLink( $rev, $next, $counter ) {
308 $last = wfMsgExt( 'last', array( 'escape' ) );
309 if ( is_null( $next ) ) {
310 # Probably no next row
311 return $last;
312 } elseif ( $next === 'unknown' ) {
313 # Next row probably exists but is unknown, use an oldid=prev link
314 return $this->mSkin->makeKnownLinkObj(
315 $this->mTitle,
316 $last,
317 "diff=" . $rev->getId() . "&oldid=prev" );
318 } elseif( !$rev->userCan( Revision::DELETED_TEXT ) ) {
319 return $last;
320 } else {
321 return $this->mSkin->makeKnownLinkObj(
322 $this->mTitle,
323 $last,
324 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
328 "tabindex={$counter}"*/ );
332 /** @todo document */
333 function diffButtons( $rev, $firstInList, $counter ) {
334 if( $this->linesonpage > 1) {
335 $radio = array(
336 'type' => 'radio',
337 'value' => $rev->getId(),
338 # do we really need to flood this on every item?
339 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
342 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
343 $radio['disabled'] = 'disabled';
346 /** @todo: move title texts to javascript */
347 if ( $firstInList ) {
348 $first = wfElement( 'input', array_merge(
349 $radio,
350 array(
351 'style' => 'visibility:hidden',
352 'name' => 'oldid' ) ) );
353 $checkmark = array( 'checked' => 'checked' );
354 } else {
355 if( $counter == 2 ) {
356 $checkmark = array( 'checked' => 'checked' );
357 } else {
358 $checkmark = array();
360 $first = wfElement( 'input', array_merge(
361 $radio,
362 $checkmark,
363 array( 'name' => 'oldid' ) ) );
364 $checkmark = array();
366 $second = wfElement( 'input', array_merge(
367 $radio,
368 $checkmark,
369 array( 'name' => 'diff' ) ) );
370 return $first . $second;
371 } else {
372 return '';
376 /** @todo document */
377 function getLatestId() {
378 if( is_null( $this->mLatestId ) ) {
379 $id = $this->mTitle->getArticleID();
380 $db = wfGetDB(DB_SLAVE);
381 $this->mLatestId = $db->selectField( 'page',
382 "page_latest",
383 array( 'page_id' => $id ),
384 'PageHistory::getLatestID' );
386 return $this->mLatestId;
390 * Fetch an array of revisions, specified by a given limit, offset and
391 * direction. This is now only used by the feeds. It was previously
392 * used by the main UI but that's now handled by the pager.
394 function fetchRevisions($limit, $offset, $direction) {
395 $fname = 'PageHistory::fetchRevisions';
397 $dbr = wfGetDB( DB_SLAVE );
399 if ($direction == PageHistory::DIR_PREV)
400 list($dirs, $oper) = array("ASC", ">=");
401 else /* $direction == PageHistory::DIR_NEXT */
402 list($dirs, $oper) = array("DESC", "<=");
404 if ($offset)
405 $offsets = array("rev_timestamp $oper '$offset'");
406 else
407 $offsets = array();
409 $page_id = $this->mTitle->getArticleID();
411 $res = $dbr->select(
412 'revision',
413 Revision::selectFields(),
414 array_merge(array("rev_page=$page_id"), $offsets),
415 $fname,
416 array('ORDER BY' => "rev_timestamp $dirs",
417 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
420 $result = array();
421 while (($obj = $dbr->fetchObject($res)) != NULL)
422 $result[] = $obj;
424 return $result;
427 /** @todo document */
428 function getNotificationTimestamp() {
429 global $wgUser, $wgShowUpdatedMarker;
430 $fname = 'PageHistory::getNotficationTimestamp';
432 if ($this->mNotificationTimestamp !== NULL)
433 return $this->mNotificationTimestamp;
435 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
436 return $this->mNotificationTimestamp = false;
438 $dbr = wfGetDB(DB_SLAVE);
440 $this->mNotificationTimestamp = $dbr->selectField(
441 'watchlist',
442 'wl_notificationtimestamp',
443 array( 'wl_namespace' => $this->mTitle->getNamespace(),
444 'wl_title' => $this->mTitle->getDBkey(),
445 'wl_user' => $wgUser->getID()
447 $fname);
449 // Don't use the special value reserved for telling whether the field is filled
450 if ( is_null( $this->mNotificationTimestamp ) ) {
451 $this->mNotificationTimestamp = false;
454 return $this->mNotificationTimestamp;
458 * Output a subscription feed listing recent edits to this page.
459 * @param string $type
461 function feed( $type ) {
462 require_once 'SpecialRecentchanges.php';
464 global $wgFeedClasses;
465 if( !isset( $wgFeedClasses[$type] ) ) {
466 global $wgOut;
467 $wgOut->addWikiText( wfMsg( 'feed-invalid' ) );
468 return;
471 $feed = new $wgFeedClasses[$type](
472 $this->mTitle->getPrefixedText() . ' - ' .
473 wfMsgForContent( 'history-feed-title' ),
474 wfMsgForContent( 'history-feed-description' ),
475 $this->mTitle->getFullUrl( 'action=history' ) );
477 $items = $this->fetchRevisions(10, 0, PageHistory::DIR_NEXT);
478 $feed->outHeader();
479 if( $items ) {
480 foreach( $items as $row ) {
481 $feed->outItem( $this->feedItem( $row ) );
483 } else {
484 $feed->outItem( $this->feedEmpty() );
486 $feed->outFooter();
489 function feedEmpty() {
490 global $wgOut;
491 return new FeedItem(
492 wfMsgForContent( 'nohistory' ),
493 $wgOut->parse( wfMsgForContent( 'history-feed-empty' ) ),
494 $this->mTitle->getFullUrl(),
495 wfTimestamp( TS_MW ),
497 $this->mTitle->getTalkPage()->getFullUrl() );
501 * Generate a FeedItem object from a given revision table row
502 * Borrows Recent Changes' feed generation functions for formatting;
503 * includes a diff to the previous revision (if any).
505 * @param $row
506 * @return FeedItem
508 function feedItem( $row ) {
509 $rev = new Revision( $row );
510 $rev->setTitle( $this->mTitle );
511 $text = rcFormatDiffRow( $this->mTitle,
512 $this->mTitle->getPreviousRevisionID( $rev->getId() ),
513 $rev->getId(),
514 $rev->getTimestamp(),
515 $rev->getComment() );
517 if( $rev->getComment() == '' ) {
518 global $wgContLang;
519 $title = wfMsgForContent( 'history-feed-item-nocomment',
520 $rev->getUserText(),
521 $wgContLang->timeanddate( $rev->getTimestamp() ) );
522 } else {
523 $title = $rev->getUserText() . ": " . $this->stripComment( $rev->getComment() );
526 return new FeedItem(
527 $title,
528 $text,
529 $this->mTitle->getFullUrl( 'diff=' . $rev->getId() . '&oldid=prev' ),
530 $rev->getTimestamp(),
531 $rev->getUserText(),
532 $this->mTitle->getTalkPage()->getFullUrl() );
536 * Quickie hack... strip out wikilinks to more legible form from the comment.
538 function stripComment( $text ) {
539 return preg_replace( '/\[\[([^]]*\|)?([^]]+)\]\]/', '\2', $text );
545 * @addtogroup Pager
547 class PageHistoryPager extends ReverseChronologicalPager {
548 public $mLastRow = false, $mPageHistory;
550 function __construct( $pageHistory ) {
551 parent::__construct();
552 $this->mPageHistory = $pageHistory;
555 function getQueryInfo() {
556 return array(
557 'tables' => 'revision',
558 'fields' => Revision::selectFields(),
559 'conds' => array('rev_page' => $this->mPageHistory->mTitle->getArticleID() ),
560 'options' => array( 'USE INDEX' => 'page_timestamp' )
564 function getIndexField() {
565 return 'rev_timestamp';
568 function formatRow( $row ) {
569 if ( $this->mLastRow ) {
570 $latest = $this->mCounter == 1 && $this->mOffset == '';
571 $firstInList = $this->mCounter == 1;
572 $s = $this->mPageHistory->historyLine( $this->mLastRow, $row, $this->mCounter++,
573 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
574 } else {
575 $s = '';
577 $this->mLastRow = $row;
578 return $s;
581 function getStartBody() {
582 $this->mLastRow = false;
583 $this->mCounter = 1;
584 return '';
587 function getEndBody() {
588 if ( $this->mLastRow ) {
589 $latest = $this->mCounter == 1 && $this->mOffset == 0;
590 $firstInList = $this->mCounter == 1;
591 if ( $this->mIsBackwards ) {
592 # Next row is unknown, but for UI reasons, probably exists if an offset has been specified
593 if ( $this->mOffset == '' ) {
594 $next = null;
595 } else {
596 $next = 'unknown';
598 } else {
599 # The next row is the past-the-end row
600 $next = $this->mPastTheEndRow;
602 $s = $this->mPageHistory->historyLine( $this->mLastRow, $next, $this->mCounter++,
603 $this->mPageHistory->getNotificationTimestamp(), $latest, $firstInList );
604 } else {
605 $s = '';
607 return $s;