3 * Implements Special:Contributions
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Special:Contributions, show user contributions in a paged list
27 * @ingroup SpecialPage
30 class SpecialContributions
extends SpecialPage
{
34 public function __construct() {
35 parent
::__construct( 'Contributions' );
38 public function execute( $par ) {
39 global $wgUser, $wgOut, $wgRequest;
42 $this->outputHeader();
43 $wgOut->addModuleStyles( 'mediawiki.special' );
45 $this->opts
= array();
47 if( $par == 'newbies' ) {
49 $this->opts
['contribs'] = 'newbie';
50 } elseif( isset( $par ) ) {
53 $target = $wgRequest->getVal( 'target' );
57 if( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
59 $this->opts
['contribs'] = 'newbie';
62 $this->opts
['deletedOnly'] = $wgRequest->getBool( 'deletedOnly' );
64 if( !strlen( $target ) ) {
65 $wgOut->addHTML( $this->getForm() );
69 $this->opts
['limit'] = $wgRequest->getInt( 'limit', $wgUser->getOption('rclimit') );
70 $this->opts
['target'] = $target;
71 $this->opts
['topOnly'] = $wgRequest->getBool( 'topOnly' );
73 $nt = Title
::makeTitleSafe( NS_USER
, $target );
75 $wgOut->addHTML( $this->getForm() );
78 $id = User
::idFromName( $nt->getText() );
80 if( $target != 'newbies' ) {
81 $target = $nt->getText();
82 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
83 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
84 $user = User
::newFromName( $target, false );
85 if ( is_object( $user ) ) {
86 $this->getSkin()->setRelevantUser( $user );
89 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
90 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
93 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
94 $this->opts
['namespace'] = intval( $ns );
96 $this->opts
['namespace'] = '';
99 $this->opts
['tagFilter'] = (string) $wgRequest->getVal( 'tagFilter' );
101 // Allows reverts to have the bot flag in recent changes. It is just here to
102 // be passed in the form at the top of the page
103 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
104 $this->opts
['bot'] = '1';
107 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
108 # Offset overrides year/month selection
110 $this->opts
['year'] = '';
111 $this->opts
['month'] = '';
113 $this->opts
['year'] = $wgRequest->getIntOrNull( 'year' );
114 $this->opts
['month'] = $wgRequest->getIntOrNull( 'month' );
117 $feedType = $wgRequest->getVal( 'feed' );
119 // Maintain some level of backwards compatability
120 // If people request feeds using the old parameters, redirect to API
122 'action' => 'feedcontributions',
123 'feedformat' => $feedType,
126 if ( $this->opts
['topOnly'] ) {
127 $apiParams['toponly'] = true;
129 if ( $this->opts
['showSizeDiff'] ) {
130 $apiParams['showsizediff'] = true;
132 if ( $this->opts
['deletedOnly'] ) {
133 $apiParams['deletedonly'] = true;
135 if ( $this->opts
['tagFilter'] !== '' ) {
136 $apiParams['tagfilter'] = $this->opts
['tagFilter'];
138 if ( $this->opts
['namespace'] !== '' ) {
139 $apiParams['namespace'] = $this->opts
['namespace'];
141 if ( $this->opts
['year'] !== null ) {
142 $apiParams['year'] = $this->opts
['year'];
144 if ( $this->opts
['month'] !== null ) {
145 $apiParams['month'] = $this->opts
['month'];
148 $url = wfScript( 'api' ) . '?' . wfArrayToCGI( $apiParams );
150 $wgOut->redirect( $url, '301' );
154 // Add RSS/atom links
155 $this->addFeedLinks( array( 'action' => 'feedcontributions', 'user' => $target ) );
157 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
159 $wgOut->addHTML( $this->getForm() );
161 $pager = new ContribsPager( array(
163 'namespace' => $this->opts
['namespace'],
164 'year' => $this->opts
['year'],
165 'month' => $this->opts
['month'],
166 'deletedOnly' => $this->opts
['deletedOnly'],
167 'topOnly' => $this->opts
['topOnly'],
169 if( !$pager->getNumRows() ) {
170 $wgOut->addWikiMsg( 'nocontribs', $target );
172 # Show a message about slave lag, if applicable
173 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
175 $wgOut->showLagWarning( $lag );
178 '<p>' . $pager->getNavigationBar() . '</p>' .
180 '<p>' . $pager->getNavigationBar() . '</p>'
183 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
185 # Show the appropriate "footer" message - WHOIS tools, etc.
186 if( $target != 'newbies' ) {
187 $message = 'sp-contributions-footer';
188 if ( IP
::isIPAddress( $target ) ) {
189 $message = 'sp-contributions-footer-anon';
191 $user = User
::newFromName( $target );
192 if ( !$user ||
$user->isAnon() ) {
193 // No message for non-existing users
198 if( !wfMessage( $message, $target )->isDisabled() ) {
200 "<div class='mw-contributions-footer'>\n$1\n</div>",
201 array( $message, $target ) );
208 * Generates the subheading with links
209 * @param $nt Title object for the target
210 * @param $id Integer: User ID for the target
211 * @return String: appropriately-escaped HTML to be output literally
212 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
214 protected function contributionsSub( $nt, $id ) {
215 global $wgLang, $wgUser, $wgOut;
217 $sk = $this->getSkin();
219 if ( $id === null ) {
220 $user = htmlspecialchars( $nt->getText() );
222 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
224 $userObj = User
::newFromName( $nt->getText(), /* check for username validity not needed */ false );
225 $talk = $nt->getTalkPage();
227 $tools = self
::getUserLinks( $nt, $talk, $userObj, $wgUser );
228 $links = $wgLang->pipeList( $tools );
230 // Show a note if the user is blocked and display the last block log entry.
231 if ( $userObj->isBlocked() ) {
232 LogEventsList
::showLogExtract(
235 $nt->getPrefixedText(),
239 'showIfEmpty' => false,
242 'sp-contributions-blocked-notice-anon' :
243 'sp-contributions-blocked-notice',
244 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
246 'offset' => '' # don't use $wgRequest parameter offset
252 // Old message 'contribsub' had one parameter, but that doesn't work for
253 // languages that want to put the "for" bit right after $user but before
254 // $links. If 'contribsub' is around, use it for reverse compatibility,
255 // otherwise use 'contribsub2'.
256 if( wfEmptyMsg( 'contribsub' ) ) {
257 return wfMsgHtml( 'contribsub2', $user, $links );
259 return wfMsgHtml( 'contribsub', "$user ($links)" );
264 * Links to different places.
265 * @param $userpage Title: Target user page
266 * @param $talkpage Title: Talk page
267 * @param $target User: Target user object
268 * @param $subject User: The viewing user ($wgUser is still checked in some cases, like userrights page!!)
270 public static function getUserLinks( Title
$userpage, Title
$talkpage, User
$target, User
$subject ) {
272 $sk = $subject->getSkin();
273 $id = $target->getId();
274 $username = $target->getName();
276 $tools[] = $sk->link( $talkpage, wfMsgHtml( 'sp-contributions-talk' ) );
278 if( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
279 if( $subject->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
280 if ( $target->isBlocked() ) {
281 $tools[] = $sk->linkKnown( # Change block link
282 SpecialPage
::getTitleFor( 'Block', $username ),
283 wfMsgHtml( 'change-blocklink' )
285 $tools[] = $sk->linkKnown( # Unblock link
286 SpecialPage
::getTitleFor( 'Unblock', $username ),
287 wfMsgHtml( 'unblocklink' )
289 } else { # User is not blocked
290 $tools[] = $sk->linkKnown( # Block link
291 SpecialPage
::getTitleFor( 'Block', $username ),
292 wfMsgHtml( 'blocklink' )
297 $tools[] = $sk->linkKnown(
298 SpecialPage
::getTitleFor( 'Log', 'block' ),
299 wfMsgHtml( 'sp-contributions-blocklog' ),
302 'page' => $userpage->getPrefixedText()
307 $tools[] = $sk->linkKnown(
308 SpecialPage
::getTitleFor( 'Listfiles', $username ),
309 wfMsgHtml( 'sp-contributions-uploads' )
313 $tools[] = $sk->linkKnown(
314 SpecialPage
::getTitleFor( 'Log', $username ),
315 wfMsgHtml( 'sp-contributions-logs' )
318 # Add link to deleted user contributions for priviledged users
319 if( $subject->isAllowed( 'deletedhistory' ) && !$subject->isBlocked() ) {
320 $tools[] = $sk->linkKnown(
321 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
322 wfMsgHtml( 'sp-contributions-deleted' )
326 # Add a link to change user rights for privileged users
327 $userrightsPage = new UserrightsPage();
328 if( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
329 $tools[] = $sk->linkKnown(
330 SpecialPage
::getTitleFor( 'Userrights', $username ),
331 wfMsgHtml( 'sp-contributions-userrights' )
335 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
340 * Generates the namespace selector form with hidden attributes.
341 * @return String: HTML fragment
343 protected function getForm() {
344 global $wgScript, $wgMiserMode;
346 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
347 if( !isset( $this->opts
['target'] ) ) {
348 $this->opts
['target'] = '';
350 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
353 if( !isset( $this->opts
['namespace'] ) ) {
354 $this->opts
['namespace'] = '';
357 if( !isset( $this->opts
['contribs'] ) ) {
358 $this->opts
['contribs'] = 'user';
361 if( !isset( $this->opts
['year'] ) ) {
362 $this->opts
['year'] = '';
365 if( !isset( $this->opts
['month'] ) ) {
366 $this->opts
['month'] = '';
369 if( $this->opts
['contribs'] == 'newbie' ) {
370 $this->opts
['target'] = '';
373 if( !isset( $this->opts
['tagFilter'] ) ) {
374 $this->opts
['tagFilter'] = '';
377 if( !isset( $this->opts
['topOnly'] ) ) {
378 $this->opts
['topOnly'] = false;
381 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
383 # Add hidden params for tracking except for parameters in $skipParameters
384 $skipParameters = array( 'namespace', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly' );
385 foreach ( $this->opts
as $name => $value ) {
386 if( in_array( $name, $skipParameters ) ) {
389 $f .= "\t" . Html
::hidden( $name, $value ) . "\n";
392 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagFilter'] );
394 $fNS = ( $wgMiserMode ) ?
'' :
395 Html
::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
396 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
397 Xml
::namespaceSelector( $this->opts
['namespace'], '' )
400 $f .= Xml
::fieldset( wfMsg( 'sp-contributions-search' ) ) .
401 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
402 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ) . '<br />' .
403 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
404 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ) . ' ' .
405 Html
::input( 'target', $this->opts
['target'], 'text', array(
408 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
410 Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
411 'deletedOnly', 'mw-show-deleted-only', $this->opts
['deletedOnly'] ) . '<br />' .
412 Xml
::tags( 'p', null, Xml
::checkLabel( wfMsg( 'sp-contributions-toponly' ),
413 'topOnly', 'mw-show-top-only', $this->opts
['topOnly'] ) ) .
414 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
415 Html
::rawElement( 'p', array( 'style' => 'white-space: nowrap' ),
416 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) . ' ' .
417 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) )
419 $explain = wfMessage( 'sp-contributions-explain' );
420 if ( $explain->exists() ) {
421 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
423 $f .= Xml
::closeElement('fieldset' ) .
424 Xml
::closeElement( 'form' );
430 * Pager for Special:Contributions
431 * @ingroup SpecialPage Pager
433 class ContribsPager
extends ReverseChronologicalPager
{
434 public $mDefaultDirection = true;
435 var $messages, $target;
436 var $namespace = '', $mDb;
437 var $preventClickjacking = false;
439 function __construct( $options ) {
440 parent
::__construct();
442 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
444 foreach( $msgs as $msg ) {
445 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
448 $this->target
= isset( $options['target'] ) ?
$options['target'] : '';
449 $this->namespace = isset( $options['namespace'] ) ?
$options['namespace'] : '';
450 $this->tagFilter
= isset( $options['tagFilter'] ) ?
$options['tagFilter'] : false;
452 $this->deletedOnly
= !empty( $options['deletedOnly'] );
453 $this->topOnly
= !empty( $options['topOnly'] );
455 $year = isset( $options['year'] ) ?
$options['year'] : false;
456 $month = isset( $options['month'] ) ?
$options['month'] : false;
457 $this->getDateCond( $year, $month );
459 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
462 function getDefaultQuery() {
463 $query = parent
::getDefaultQuery();
464 $query['target'] = $this->target
;
468 function getTitle() {
469 return SpecialPage
::getTitleFor( 'Contributions' );
472 function getQueryInfo() {
474 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
476 $conds = array_merge( $userCond, $this->getNamespaceCond() );
477 // Paranoia: avoid brute force searches (bug 17342)
478 if( !$wgUser->isAllowed( 'deletedhistory' ) ||
$wgUser->isBlocked() ) {
479 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
480 } elseif( !$wgUser->isAllowed( 'suppressrevision' ) ) {
481 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
482 ' != ' . Revision
::SUPPRESSED_USER
;
484 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
489 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
490 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
491 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted'
494 'options' => array( 'USE INDEX' => array('revision' => $index) ),
495 'join_conds' => $join_cond
498 ChangeTags
::modifyDisplayQuery(
499 $queryInfo['tables'],
500 $queryInfo['fields'],
502 $queryInfo['join_conds'],
503 $queryInfo['options'],
507 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
511 function getUserCond() {
512 $condition = array();
513 $join_conds = array();
514 if( $this->target
== 'newbies' ) {
515 $tables = array( 'user_groups', 'page', 'revision' );
516 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
517 $condition[] = 'rev_user >' . (int)($max - $max / 100);
518 $condition[] = 'ug_group IS NULL';
519 $index = 'user_timestamp';
520 # @todo FIXME: Other groups may have 'bot' rights
521 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
523 $tables = array( 'page', 'revision' );
524 $condition['rev_user_text'] = $this->target
;
525 $index = 'usertext_timestamp';
527 if( $this->deletedOnly
) {
528 $condition[] = "rev_deleted != '0'";
530 if( $this->topOnly
) {
531 $condition[] = "rev_id = page_latest";
533 return array( $tables, $index, $condition, $join_conds );
536 function getNamespaceCond() {
538 if( $this->namespace !== '' && !$wgMiserMode ) {
539 return array( 'page_namespace' => (int)$this->namespace );
545 function getIndexField() {
546 return 'rev_timestamp';
549 function getStartBody() {
553 function getEndBody() {
558 * Generates each row in the contributions list.
560 * Contributions which are marked "top" are currently on top of the history.
561 * For these contributions, a [rollback] link is shown for users with roll-
562 * back privileges. The rollback link restores the most recent version that
563 * was not written by the target user.
565 * @todo This would probably look a lot nicer in a table.
567 function formatRow( $row ) {
568 global $wgUser, $wgLang;
569 wfProfileIn( __METHOD__
);
571 $sk = $this->getSkin();
572 $rev = new Revision( $row );
575 $page = Title
::newFromRow( $row );
578 htmlspecialchars( $page->getPrefixedText() ),
580 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
582 # Mark current revisions
584 if( $row->rev_id
== $row->page_latest
) {
585 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
587 if( !$row->page_is_new
&& $page->quickUserCan( 'rollback' )
588 && $page->quickUserCan( 'edit' ) )
590 $this->preventClickjacking();
591 $topmarktext .= ' '.$sk->generateRollback( $rev );
594 # Is there a visible previous revision?
595 if( $rev->userCan( Revision
::DELETED_TEXT
) && $rev->getParentId() !== 0 ) {
596 $difftext = $sk->linkKnown(
598 $this->messages
['diff'],
602 'oldid' => $row->rev_id
606 $difftext = $this->messages
['diff'];
608 $histlink = $sk->linkKnown(
610 $this->messages
['hist'],
612 array( 'action' => 'history' )
615 $comment = $wgLang->getDirMark() . $sk->revComment( $rev, false, true );
616 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
617 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
620 htmlspecialchars($date),
622 array( 'oldid' => intval( $row->rev_id
) )
625 $d = htmlspecialchars( $date );
627 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
628 $d = '<span class="history-deleted">' . $d . '</span>';
631 if( $this->target
== 'newbies' ) {
632 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
633 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) ) . ' ';
638 if( $rev->getParentId() === 0 ) {
639 $nflag = ChangesList
::flag( 'newpage' );
644 if( $rev->isMinor() ) {
645 $mflag = ChangesList
::flag( 'minor' );
650 // Don't show useless link to people who cannot hide revisions
651 $canHide = $wgUser->isAllowed( 'deleterevision' );
652 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
653 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
654 $del = $this->mSkin
->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
657 'type' => 'revision',
658 'target' => $page->getPrefixedDbkey(),
659 'ids' => $rev->getId()
661 $del = $this->mSkin
->revDeleteLink( $query,
662 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
669 $diffHistLinks = '(' . $difftext . $this->messages
['pipe-separator'] . $histlink . ')';
670 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
672 # Denote if username is redacted for this edit
673 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
674 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
678 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
679 $classes = array_merge( $classes, $newClasses );
680 $ret .= " $tagSummary";
682 // Let extensions add data
683 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
685 $classes = implode( ' ', $classes );
686 $ret = "<li class=\"$classes\">$ret</li>\n";
687 wfProfileOut( __METHOD__
);
692 * Get the Database object in use
694 * @return DatabaseBase
696 public function getDatabase() {
701 * Overwrite Pager function and return a helpful comment
703 function getSqlComment() {
704 if ( $this->namespace ||
$this->deletedOnly
) {
705 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
707 return 'contributions page unfiltered';
711 protected function preventClickjacking() {
712 $this->preventClickjacking
= true;
715 public function getPreventClickjacking() {
716 return $this->preventClickjacking
;