* Port tests from t/inc/
[mediawiki.git] / includes / specials / SpecialContributions.php
blob392f4332e77845cec468ee4db3684fab1a7ef0a5
1 <?php
2 /**
3 * Special:Contributions, show user contributions in a paged list
4 * @file
5 * @ingroup SpecialPage
6 */
8 class SpecialContributions extends SpecialPage {
10 public function __construct() {
11 parent::__construct( 'Contributions' );
14 public function execute( $par ) {
15 global $wgUser, $wgOut, $wgLang, $wgRequest;
17 $this->setHeaders();
18 $this->outputHeader();
20 $this->opts = array();
22 if( $par == 'newbies' ) {
23 $target = 'newbies';
24 $this->opts['contribs'] = 'newbie';
25 } elseif( isset( $par ) ) {
26 $target = $par;
27 } else {
28 $target = $wgRequest->getVal( 'target' );
31 // check for radiobox
32 if( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
33 $target = 'newbies';
34 $this->opts['contribs'] = 'newbie';
37 if( !strlen( $target ) ) {
38 $wgOut->addHTML( $this->getForm() );
39 return;
42 $this->opts['limit'] = $wgRequest->getInt( 'limit', $wgUser->getOption('rclimit') );
43 $this->opts['target'] = $target;
45 $nt = Title::makeTitleSafe( NS_USER, $target );
46 if( !$nt ) {
47 $wgOut->addHTML( $this->getForm() );
48 return;
50 $id = User::idFromName( $nt->getText() );
52 if( $target != 'newbies' ) {
53 $target = $nt->getText();
54 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
55 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
56 } else {
57 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
58 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
61 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
62 $this->opts['namespace'] = intval( $ns );
63 } else {
64 $this->opts['namespace'] = '';
67 $this->opts['tagfilter'] = (string) $wgRequest->getVal( 'tagfilter' );
69 // Allows reverts to have the bot flag in recent changes. It is just here to
70 // be passed in the form at the top of the page
71 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
72 $this->opts['bot'] = '1';
75 $skip = $wgRequest->getText( 'offset' ) || $wgRequest->getText( 'dir' ) == 'prev';
76 # Offset overrides year/month selection
77 if( $skip ) {
78 $this->opts['year'] = '';
79 $this->opts['month'] = '';
80 } else {
81 $this->opts['year'] = $wgRequest->getIntOrNull( 'year' );
82 $this->opts['month'] = $wgRequest->getIntOrNull( 'month' );
85 // Add RSS/atom links
86 $this->setSyndicated();
87 $feedType = $wgRequest->getVal( 'feed' );
88 if( $feedType ) {
89 return $this->feed( $feedType );
92 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
94 $wgOut->addHTML( $this->getForm() );
96 $pager = new ContribsPager( $target, $this->opts['namespace'], $this->opts['year'], $this->opts['month'] );
97 if( !$pager->getNumRows() ) {
98 $wgOut->addWikiMsg( 'nocontribs', $target );
99 } else {
100 # Show a message about slave lag, if applicable
101 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
102 $wgOut->showLagWarning( $lag );
104 $wgOut->addHTML(
105 '<p>' . $pager->getNavigationBar() . '</p>' .
106 $pager->getBody() .
107 '<p>' . $pager->getNavigationBar() . '</p>'
112 # Show the appropriate "footer" message - WHOIS tools, etc.
113 if( $target != 'newbies' ) {
114 $message = 'sp-contributions-footer';
115 if ( IP::isIPAddress( $target ) ) {
116 $message = 'sp-contributions-footer-anon';
117 } else {
118 $user = User::newFromName( $target );
119 if ( !$user || $user->isAnon() ) {
120 // No message for non-existing users
121 return;
125 $text = wfMsgNoTrans( $message, $target );
126 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
127 $wgOut->wrapWikiMsg(
128 "<div class='mw-contributions-footer'>\n$1\n</div>",
129 array( $message, $target ) );
135 protected function setSyndicated() {
136 global $wgOut;
137 $wgOut->setSyndicated( true );
138 $wgOut->setFeedAppendQuery( wfArrayToCGI( $this->opts ) );
142 * Generates the subheading with links
143 * @param Title $nt @see Title object for the target
144 * @param integer $id User ID for the target
145 * @return String: appropriately-escaped HTML to be output literally
146 * @todo Fixme: almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
148 protected function contributionsSub( $nt, $id ) {
149 global $wgSysopUserBans, $wgLang, $wgUser, $wgOut;
151 $sk = $wgUser->getSkin();
153 if ( $id === null ) {
154 $user = htmlspecialchars( $nt->getText() );
155 } else {
156 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
158 $userObj = User::newFromName( $nt->getText(), /* check for username validity not needed */ false );
159 $talk = $nt->getTalkPage();
160 if( $talk ) {
161 # Talk page link
162 $tools[] = $sk->link( $talk, wfMsgHtml( 'sp-contributions-talk' ) );
163 if( ( $id !== null && $wgSysopUserBans ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
164 if( $wgUser->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
165 if ( $userObj->isBlocked() ) {
166 $tools[] = $sk->linkKnown( # Change block link
167 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
168 wfMsgHtml( 'change-blocklink' )
170 $tools[] = $sk->linkKnown( # Unblock link
171 SpecialPage::getTitleFor( 'BlockList' ),
172 wfMsgHtml( 'unblocklink' ),
173 array(),
174 array(
175 'action' => 'unblock',
176 'ip' => $nt->getDBkey()
180 else { # User is not blocked
181 $tools[] = $sk->linkKnown( # Block link
182 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
183 wfMsgHtml( 'blocklink' )
187 # Block log link
188 $tools[] = $sk->linkKnown(
189 SpecialPage::getTitleFor( 'Log' ),
190 wfMsgHtml( 'sp-contributions-blocklog' ),
191 array(),
192 array(
193 'type' => 'block',
194 'page' => $nt->getPrefixedText()
198 # Other logs link
199 $tools[] = $sk->linkKnown(
200 SpecialPage::getTitleFor( 'Log' ),
201 wfMsgHtml( 'sp-contributions-logs' ),
202 array(),
203 array( 'user' => $nt->getText() )
206 # Add link to deleted user contributions for priviledged users
207 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
208 $tools[] = $sk->linkKnown(
209 SpecialPage::getTitleFor( 'DeletedContributions', $nt->getDBkey() ),
210 wfMsgHtml( 'sp-contributions-deleted' )
214 # Add a link to change user rights for privileged users
215 $userrightsPage = new UserrightsPage();
216 if( $id !== null && $userrightsPage->userCanChangeRights( User::newFromId( $id ) ) ) {
217 $tools[] = $sk->linkKnown(
218 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
219 wfMsgHtml( 'sp-contributions-userrights' )
223 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
225 $links = $wgLang->pipeList( $tools );
227 // Show a note if the user is blocked and display the last block log entry.
228 if ( $userObj->isBlocked() ) {
229 LogEventsList::showLogExtract(
230 $wgOut,
231 'block',
232 $nt->getPrefixedText(),
234 array(
235 'lim' => 1,
236 'showIfEmpty' => false,
237 'msgKey' => array(
238 'sp-contributions-blocked-notice',
239 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
241 'offset' => '' # don't use $wgRequest parameter offset
247 // Old message 'contribsub' had one parameter, but that doesn't work for
248 // languages that want to put the "for" bit right after $user but before
249 // $links. If 'contribsub' is around, use it for reverse compatibility,
250 // otherwise use 'contribsub2'.
251 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
252 return wfMsgHtml( 'contribsub2', $user, $links );
253 } else {
254 return wfMsgHtml( 'contribsub', "$user ($links)" );
259 * Generates the namespace selector form with hidden attributes.
260 * @param $this->opts Array: the options to be included.
262 protected function getForm() {
263 global $wgScript;
265 $this->opts['title'] = $this->getTitle()->getPrefixedText();
266 if( !isset( $this->opts['target'] ) ) {
267 $this->opts['target'] = '';
268 } else {
269 $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
272 if( !isset( $this->opts['namespace'] ) ) {
273 $this->opts['namespace'] = '';
276 if( !isset( $this->opts['contribs'] ) ) {
277 $this->opts['contribs'] = 'user';
280 if( !isset( $this->opts['year'] ) ) {
281 $this->opts['year'] = '';
284 if( !isset( $this->opts['month'] ) ) {
285 $this->opts['month'] = '';
288 if( $this->opts['contribs'] == 'newbie' ) {
289 $this->opts['target'] = '';
292 if( !isset( $this->opts['tagfilter'] ) ) {
293 $this->opts['tagfilter'] = '';
296 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
297 # Add hidden params for tracking
298 foreach ( $this->opts as $name => $value ) {
299 if( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
300 continue;
302 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
305 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
307 $f .= '<fieldset>' .
308 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
309 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
310 'contribs', 'newbie' , 'newbie', $this->opts['contribs'] == 'newbie' ? true : false ) . '<br />' .
311 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
312 'contribs' , 'user', 'user', $this->opts['contribs'] == 'user' ? true : false ) . ' ' .
313 Html::input( 'target', $this->opts['target'], 'text', array(
314 'size' => '20',
315 'required' => ''
316 ) + ( $this->opts['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
317 '<span style="white-space: nowrap">' .
318 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
319 Xml::namespaceSelector( $this->opts['namespace'], '' ) .
320 '</span>' .
321 ( $tagFilter ? Xml::tags( 'p', null, implode( '&nbsp;', $tagFilter ) ) : '' ) .
322 Xml::openElement( 'p' ) .
323 '<span style="white-space: nowrap">' .
324 Xml::dateMenu( $this->opts['year'], $this->opts['month'] ) .
325 '</span>' . ' ' .
326 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
327 Xml::closeElement( 'p' );
329 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
330 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
331 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
333 $f .= '</fieldset>' .
334 Xml::closeElement( 'form' );
335 return $f;
339 * Output a subscription feed listing recent edits to this page.
340 * @param string $type
342 protected function feed( $type ) {
343 global $wgRequest, $wgFeed, $wgFeedClasses, $wgFeedLimit;
345 if( !$wgFeed ) {
346 global $wgOut;
347 $wgOut->addWikiMsg( 'feed-unavailable' );
348 return;
351 if( !isset( $wgFeedClasses[$type] ) ) {
352 global $wgOut;
353 $wgOut->addWikiMsg( 'feed-invalid' );
354 return;
357 $feed = new $wgFeedClasses[$type](
358 $this->feedTitle(),
359 wfMsgExt( 'tagline', 'parsemag' ),
360 $this->getTitle()->getFullUrl() . "/" . urlencode($this->opts['target'])
363 // Already valid title
364 $nt = Title::makeTitleSafe( NS_USER, $this->opts['target'] );
365 $target = $this->opts['target'] == 'newbies' ? 'newbies' : $nt->getText();
367 $pager = new ContribsPager( $target, $this->opts['namespace'],
368 $this->opts['year'], $this->opts['month'], $this->opts['tagfilter'] );
370 $pager->mLimit = min( $this->opts['limit'], $wgFeedLimit );
372 $feed->outHeader();
373 if( $pager->getNumRows() > 0 ) {
374 while( $row = $pager->mResult->fetchObject() ) {
375 $feed->outItem( $this->feedItem( $row ) );
378 $feed->outFooter();
381 protected function feedTitle() {
382 global $wgContLanguageCode, $wgSitename;
383 $page = SpecialPage::getPage( 'Contributions' );
384 $desc = $page->getDescription();
385 return "$wgSitename - $desc [$wgContLanguageCode]";
388 protected function feedItem( $row ) {
389 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
390 if( $title ) {
391 $date = $row->rev_timestamp;
392 $comments = $title->getTalkPage()->getFullURL();
393 $revision = Revision::newFromTitle( $title, $row->rev_id );
395 return new FeedItem(
396 $title->getPrefixedText(),
397 $this->feedItemDesc( $revision ),
398 $title->getFullURL(),
399 $date,
400 $this->feedItemAuthor( $revision ),
401 $comments
403 } else {
404 return null;
408 protected function feedItemAuthor( $revision ) {
409 return $revision->getUserText();
412 protected function feedItemDesc( $revision ) {
413 if( $revision ) {
414 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
415 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
416 "</p>\n<hr />\n<div>" .
417 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
419 return '';
424 * Pager for Special:Contributions
425 * @ingroup SpecialPage Pager
427 class ContribsPager extends ReverseChronologicalPager {
428 public $mDefaultDirection = true;
429 var $messages, $target;
430 var $namespace = '', $mDb;
432 function __construct( $target, $namespace = false, $year = false, $month = false, $tagFilter = false ) {
433 parent::__construct();
435 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
437 foreach( $msgs as $msg ) {
438 $this->messages[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
441 $this->target = $target;
442 $this->namespace = $namespace;
443 $this->tagFilter = $tagFilter;
445 $this->getDateCond( $year, $month );
447 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
450 function getDefaultQuery() {
451 $query = parent::getDefaultQuery();
452 $query['target'] = $this->target;
453 return $query;
456 function getQueryInfo() {
457 global $wgUser;
458 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
460 $conds = array_merge( $userCond, $this->getNamespaceCond() );
461 // Paranoia: avoid brute force searches (bug 17342)
462 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
463 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::DELETED_USER) . ' = 0';
464 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
465 $conds[] = $this->mDb->bitAnd('rev_deleted',Revision::SUPPRESSED_USER) .
466 ' != ' . Revision::SUPPRESSED_USER;
468 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
470 $queryInfo = array(
471 'tables' => $tables,
472 'fields' => array(
473 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
474 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
475 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
477 'conds' => $conds,
478 'options' => array( 'USE INDEX' => array('revision' => $index) ),
479 'join_conds' => $join_cond
482 ChangeTags::modifyDisplayQuery(
483 $queryInfo['tables'],
484 $queryInfo['fields'],
485 $queryInfo['conds'],
486 $queryInfo['join_conds'],
487 $queryInfo['options'],
488 $this->tagFilter
491 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
492 return $queryInfo;
495 function getUserCond() {
496 $condition = array();
497 $join_conds = array();
498 if( $this->target == 'newbies' ) {
499 $tables = array( 'user_groups', 'page', 'revision' );
500 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
501 $condition[] = 'rev_user >' . (int)($max - $max / 100);
502 $condition[] = 'ug_group IS NULL';
503 $index = 'user_timestamp';
504 # FIXME: other groups may have 'bot' rights
505 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
506 } else {
507 $tables = array( 'page', 'revision' );
508 $condition['rev_user_text'] = $this->target;
509 $index = 'usertext_timestamp';
511 return array( $tables, $index, $condition, $join_conds );
514 function getNamespaceCond() {
515 if( $this->namespace !== '' ) {
516 return array( 'page_namespace' => (int)$this->namespace );
517 } else {
518 return array();
522 function getIndexField() {
523 return 'rev_timestamp';
526 function getStartBody() {
527 return "<ul>\n";
530 function getEndBody() {
531 return "</ul>\n";
535 * Generates each row in the contributions list.
537 * Contributions which are marked "top" are currently on top of the history.
538 * For these contributions, a [rollback] link is shown for users with roll-
539 * back privileges. The rollback link restores the most recent version that
540 * was not written by the target user.
542 * @todo This would probably look a lot nicer in a table.
544 function formatRow( $row ) {
545 global $wgUser, $wgLang, $wgContLang;
546 wfProfileIn( __METHOD__ );
548 $sk = $this->getSkin();
549 $rev = new Revision( $row );
550 $classes = array();
552 $page = Title::newFromRow( $row );
553 $page->resetArticleId( $row->rev_page ); // use process cache
554 $link = $sk->link(
555 $page,
556 htmlspecialchars( $page->getPrefixedText() ),
557 array(),
558 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
560 # Mark current revisions
561 $difftext = $topmarktext = '';
562 if( $row->rev_id == $row->page_latest ) {
563 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
564 # Add rollback link
565 if( !$row->page_is_new && $page->quickUserCan( 'rollback' )
566 && $page->quickUserCan( 'edit' ) )
568 $topmarktext .= ' '.$sk->generateRollback( $rev );
571 # Is there a visible previous revision?
572 if( $rev->userCan( Revision::DELETED_TEXT ) && $rev->getParentId() !== 0 ) {
573 $difftext = $sk->linkKnown(
574 $page,
575 $this->messages['diff'],
576 array(),
577 array(
578 'diff' => 'prev',
579 'oldid' => $row->rev_id
582 } else {
583 $difftext = $this->messages['diff'];
585 $histlink = $sk->linkKnown(
586 $page,
587 $this->messages['hist'],
588 array(),
589 array( 'action' => 'history' )
592 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
593 $date = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
594 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
595 $d = '<span class="history-deleted">' . $date . '</span>';
596 } else {
597 $d = $sk->linkKnown(
598 $page,
599 htmlspecialchars($date),
600 array(),
601 array( 'oldid' => intval( $row->rev_id ) )
605 if( $this->target == 'newbies' ) {
606 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
607 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) ) . ' ';
608 } else {
609 $userlink = '';
612 if( $rev->getParentId() === 0 ) {
613 $nflag = ChangesList::flag( 'newpage' );
614 } else {
615 $nflag = '';
618 if( $rev->isMinor() ) {
619 $mflag = ChangesList::flag( 'minor' );
620 } else {
621 $mflag = '';
624 // Don't show useless link to people who cannot hide revisions
625 $canHide = $wgUser->isAllowed( 'deleterevision' );
626 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
627 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
628 $del = $this->mSkin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
629 } else {
630 $query = array(
631 'type' => 'revision',
632 'target' => $page->getPrefixedDbkey(),
633 'ids' => $rev->getId()
635 $del = $this->mSkin->revDeleteLink( $query,
636 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
638 $del .= ' ';
639 } else {
640 $del = '';
643 $diffHistLinks = '(' . $difftext . $this->messages['pipe-separator'] . $histlink . ')';
644 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
646 # Denote if username is redacted for this edit
647 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
648 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
651 # Tags, if any.
652 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
653 $classes = array_merge( $classes, $newClasses );
654 $ret .= " $tagSummary";
656 // Let extensions add data
657 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
659 $classes = implode( ' ', $classes );
660 $ret = "<li class=\"$classes\">$ret</li>\n";
661 wfProfileOut( __METHOD__ );
662 return $ret;
666 * Get the Database object in use
668 * @return Database
670 public function getDatabase() {
671 return $this->mDb;