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' );
72 $this->opts
['showSizeDiff'] = $wgRequest->getBool( 'showSizeDiff' );
74 $nt = Title
::makeTitleSafe( NS_USER
, $target );
76 $wgOut->addHTML( $this->getForm() );
79 $id = User
::idFromName( $nt->getText() );
81 if( $target != 'newbies' ) {
82 $target = $nt->getText();
83 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
84 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsgExt( 'contributions-title', array( 'parsemag' ),$target ) ) );
85 $user = User
::newFromName( $target, false );
86 if ( is_object($user) ) {
87 $wgUser->getSkin()->setRelevantUser( $user );
90 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
91 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
94 if( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
95 $this->opts
['namespace'] = intval( $ns );
97 $this->opts
['namespace'] = '';
100 $this->opts
['tagFilter'] = (string) $wgRequest->getVal( 'tagFilter' );
102 // Allows reverts to have the bot flag in recent changes. It is just here to
103 // be passed in the form at the top of the page
104 if( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
105 $this->opts
['bot'] = '1';
108 $skip = $wgRequest->getText( 'offset' ) ||
$wgRequest->getText( 'dir' ) == 'prev';
109 # Offset overrides year/month selection
111 $this->opts
['year'] = '';
112 $this->opts
['month'] = '';
114 $this->opts
['year'] = $wgRequest->getIntOrNull( 'year' );
115 $this->opts
['month'] = $wgRequest->getIntOrNull( 'month' );
118 // Add RSS/atom links
119 global $wgFeedClasses;
120 $apiParams = array( 'action' => 'feedcontributions', 'user' => $wgUser->getName() );
121 $feedTemplate = wfScript( 'api' ) . '?';
123 foreach( $wgFeedClasses as $format => $class ) {
124 $theseParams = $apiParams +
array( 'feedformat' => $format );
125 $url = $feedTemplate . wfArrayToCGI( $theseParams );
126 $wgOut->addFeedLink( $format, $url );
129 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
131 $wgOut->addHTML( $this->getForm() );
133 $pager = new ContribsPager( array(
135 'namespace' => $this->opts
['namespace'],
136 'year' => $this->opts
['year'],
137 'month' => $this->opts
['month'],
138 'deletedOnly' => $this->opts
['deletedOnly'],
139 'topOnly' => $this->opts
['topOnly'],
140 'showSizeDiff' => $this->opts
['showSizeDiff'],
142 if( !$pager->getNumRows() ) {
143 $wgOut->addWikiMsg( 'nocontribs', $target );
145 # Show a message about slave lag, if applicable
146 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
147 $wgOut->showLagWarning( $lag );
150 '<p>' . $pager->getNavigationBar() . '</p>' .
152 '<p>' . $pager->getNavigationBar() . '</p>'
155 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
157 # Show the appropriate "footer" message - WHOIS tools, etc.
158 if( $target != 'newbies' ) {
159 $message = 'sp-contributions-footer';
160 if ( IP
::isIPAddress( $target ) ) {
161 $message = 'sp-contributions-footer-anon';
163 $user = User
::newFromName( $target );
164 if ( !$user ||
$user->isAnon() ) {
165 // No message for non-existing users
170 if( !wfMessage( $message, $target )->isDisabled() ) {
172 "<div class='mw-contributions-footer'>\n$1\n</div>",
173 array( $message, $target ) );
180 * Generates the subheading with links
181 * @param $nt Title object for the target
182 * @param $id Integer: User ID for the target
183 * @return String: appropriately-escaped HTML to be output literally
184 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
186 protected function contributionsSub( $nt, $id ) {
187 global $wgLang, $wgUser, $wgOut;
189 $sk = $wgUser->getSkin();
191 if ( $id === null ) {
192 $user = htmlspecialchars( $nt->getText() );
194 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
196 $userObj = User
::newFromName( $nt->getText(), /* check for username validity not needed */ false );
197 $talk = $nt->getTalkPage();
199 $tools = self
::getUserLinks( $nt, $talk, $userObj, $wgUser );
200 $links = $wgLang->pipeList( $tools );
202 // Show a note if the user is blocked and display the last block log entry.
203 if ( $userObj->isBlocked() ) {
204 LogEventsList
::showLogExtract(
207 $nt->getPrefixedText(),
211 'showIfEmpty' => false,
214 'sp-contributions-blocked-notice-anon' :
215 'sp-contributions-blocked-notice',
216 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
218 'offset' => '' # don't use $wgRequest parameter offset
224 // Old message 'contribsub' had one parameter, but that doesn't work for
225 // languages that want to put the "for" bit right after $user but before
226 // $links. If 'contribsub' is around, use it for reverse compatibility,
227 // otherwise use 'contribsub2'.
228 if( wfEmptyMsg( 'contribsub' ) ) {
229 return wfMsgHtml( 'contribsub2', $user, $links );
231 return wfMsgHtml( 'contribsub', "$user ($links)" );
236 * Links to different places.
237 * @param $userpage Title: Target user page
238 * @param $talkpage Title: Talk page
239 * @param $target User: Target user object
240 * @param $subject User: The viewing user ($wgUser is still checked in some cases, like userrights page!!)
242 public static function getUserLinks( Title
$userpage, Title
$talkpage, User
$target, User
$subject ) {
244 $sk = $subject->getSkin();
245 $id = $target->getId();
246 $username = $target->getName();
248 $tools[] = $sk->link( $talkpage, wfMsgHtml( 'sp-contributions-talk' ) );
250 if( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
251 if( $subject->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
252 if ( $target->isBlocked() ) {
253 $tools[] = $sk->linkKnown( # Change block link
254 SpecialPage
::getTitleFor( 'Block', $username ),
255 wfMsgHtml( 'change-blocklink' )
257 $tools[] = $sk->linkKnown( # Unblock link
258 SpecialPage
::getTitleFor( 'Unblock', $username ),
259 wfMsgHtml( 'unblocklink' )
261 } else { # User is not blocked
262 $tools[] = $sk->linkKnown( # Block link
263 SpecialPage
::getTitleFor( 'Block', $username ),
264 wfMsgHtml( 'blocklink' )
269 $tools[] = $sk->linkKnown(
270 SpecialPage
::getTitleFor( 'Log' ),
271 wfMsgHtml( 'sp-contributions-blocklog' ),
275 'page' => $userpage->getPrefixedText()
280 $tools[] = $sk->linkKnown(
281 SpecialPage
::getTitleFor( 'Listfiles', $username ),
282 wfMsgHtml( 'sp-contributions-uploads' )
286 $tools[] = $sk->linkKnown(
287 SpecialPage
::getTitleFor( 'Log' ),
288 wfMsgHtml( 'sp-contributions-logs' ),
290 array( 'user' => $username )
293 # Add link to deleted user contributions for priviledged users
294 if( $subject->isAllowed( 'deletedhistory' ) ) {
295 $tools[] = $sk->linkKnown(
296 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
297 wfMsgHtml( 'sp-contributions-deleted' )
301 # Add a link to change user rights for privileged users
302 $userrightsPage = new UserrightsPage();
303 if( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
304 $tools[] = $sk->linkKnown(
305 SpecialPage
::getTitleFor( 'Userrights', $username ),
306 wfMsgHtml( 'sp-contributions-userrights' )
310 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
315 * Generates the namespace selector form with hidden attributes.
316 * @return String: HTML fragment
318 protected function getForm() {
319 global $wgScript, $wgMiserMode;
321 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
322 if( !isset( $this->opts
['target'] ) ) {
323 $this->opts
['target'] = '';
325 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
328 if( !isset( $this->opts
['namespace'] ) ) {
329 $this->opts
['namespace'] = '';
332 if( !isset( $this->opts
['contribs'] ) ) {
333 $this->opts
['contribs'] = 'user';
336 if( !isset( $this->opts
['year'] ) ) {
337 $this->opts
['year'] = '';
340 if( !isset( $this->opts
['month'] ) ) {
341 $this->opts
['month'] = '';
344 if( $this->opts
['contribs'] == 'newbie' ) {
345 $this->opts
['target'] = '';
348 if( !isset( $this->opts
['tagFilter'] ) ) {
349 $this->opts
['tagFilter'] = '';
352 if( !isset( $this->opts
['topOnly'] ) ) {
353 $this->opts
['topOnly'] = false;
356 if( !isset( $this->opts
['showSizeDiff'] ) ) {
357 $this->opts
['showSizeDiff'] = !$wgMiserMode;
360 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
362 # Add hidden params for tracking except for parameters in $skipParameters
363 $skipParameters = array( 'namespace', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly', 'showSizeDiff' );
364 foreach ( $this->opts
as $name => $value ) {
365 if( in_array( $name, $skipParameters ) ) {
368 $f .= "\t" . Html
::hidden( $name, $value ) . "\n";
371 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagFilter'] );
375 if ( !$wgMiserMode ) {
376 $fNS = Html
::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
377 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
378 Xml
::namespaceSelector( $this->opts
['namespace'], '' )
380 $fShowDiff = Xml
::checkLabel( wfMsg( 'sp-contributions-showsizediff' ), 'showSizeDiff', 'mw-show-size-diff', $this->opts
['showSizeDiff'] );
383 $f .= Xml
::fieldset( wfMsg( 'sp-contributions-search' ) ) .
384 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
385 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ) . '<br />' .
386 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
387 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ) . ' ' .
388 Html
::input( 'target', $this->opts
['target'], 'text', array(
391 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
393 Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
394 'deletedOnly', 'mw-show-deleted-only', $this->opts
['deletedOnly'] ) . '<br />' .
395 Xml
::tags( 'p', null, Xml
::checkLabel( wfMsg( 'sp-contributions-toponly' ),
396 'topOnly', 'mw-show-top-only', $this->opts
['topOnly'] ) ) .
398 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
399 Html
::rawElement( 'p', array( 'style' => 'white-space: nowrap' ),
400 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) . ' ' .
401 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) )
403 $explain = wfMessage( 'sp-contributions-explain' );
404 if ( $explain->exists() ) {
405 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
407 $f .= Xml
::closeElement('fieldset' ) .
408 Xml
::closeElement( 'form' );
414 * Pager for Special:Contributions
415 * @ingroup SpecialPage Pager
417 class ContribsPager
extends ReverseChronologicalPager
{
418 public $mDefaultDirection = true;
419 var $messages, $target;
420 var $namespace = '', $mDb;
421 var $preventClickjacking = false;
423 function __construct( $options ) {
424 parent
::__construct();
426 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
428 foreach( $msgs as $msg ) {
429 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
432 $this->target
= isset( $options['target'] ) ?
$options['target'] : '';
433 $this->namespace = isset( $options['namespace'] ) ?
$options['namespace'] : '';
434 $this->tagFilter
= isset( $options['tagFilter'] ) ?
$options['tagFilter'] : false;
436 $this->deletedOnly
= !empty( $options['deletedOnly'] );
437 $this->topOnly
= !empty( $options['topOnly'] );
438 $this->showSizeDiff
= !empty( $options['showSizeDiff'] );
440 $year = isset( $options['year'] ) ?
$options['year'] : false;
441 $month = isset( $options['month'] ) ?
$options['month'] : false;
442 $this->getDateCond( $year, $month );
444 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
447 function getDefaultQuery() {
448 $query = parent
::getDefaultQuery();
449 $query['target'] = $this->target
;
453 function getQueryInfo() {
454 global $wgUser, $wgMiserMode;
455 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
457 $conds = array_merge( $userCond, $this->getNamespaceCond() );
458 // Paranoia: avoid brute force searches (bug 17342)
459 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
460 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
461 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
462 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
463 ' != ' . Revision
::SUPPRESSED_USER
;
465 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
468 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
469 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
470 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted',
473 if ( $this->showSizeDiff
&& !$wgMiserMode ) {
474 $fields = array_merge( $fields, array( 'rc_old_len', 'rc_new_len' ) );
475 array_unshift( $tables, 'recentchanges' );
476 $join_cond['recentchanges'] = array( 'INNER JOIN', "rev_id = rc_this_oldid" );
483 'options' => array( 'USE INDEX' => array('revision' => $index) ),
484 'join_conds' => $join_cond
486 ChangeTags
::modifyDisplayQuery(
487 $queryInfo['tables'],
488 $queryInfo['fields'],
490 $queryInfo['join_conds'],
491 $queryInfo['options'],
495 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
499 function getUserCond() {
500 $condition = array();
501 $join_conds = array();
503 if( $this->target
== 'newbies' ) {
504 $tables = array( 'user_groups', 'page', 'revision' );
505 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
506 $condition[] = 'rev_user >' . (int)($max - $max / 100);
507 $condition[] = 'ug_group IS NULL';
508 $index = 'user_timestamp';
509 # @todo FIXME: Other groups may have 'bot' rights
510 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
512 $tables = array( 'page', 'revision' );
513 $condition['rev_user_text'] = $this->target
;
514 $index = 'usertext_timestamp';
516 if( $this->deletedOnly
) {
517 $condition[] = "rev_deleted != '0'";
519 if( $this->topOnly
) {
520 $condition[] = "rev_id = page_latest";
522 return array( $tables, $index, $condition, $join_conds );
525 function getNamespaceCond() {
527 if( $this->namespace !== '' && !$wgMiserMode ) {
528 return array( 'page_namespace' => (int)$this->namespace );
534 function getIndexField() {
535 return 'rev_timestamp';
538 function getStartBody() {
542 function getEndBody() {
547 * Generates each row in the contributions list.
549 * Contributions which are marked "top" are currently on top of the history.
550 * For these contributions, a [rollback] link is shown for users with roll-
551 * back privileges. The rollback link restores the most recent version that
552 * was not written by the target user.
554 * @todo This would probably look a lot nicer in a table.
556 function formatRow( $row ) {
557 global $wgUser, $wgLang, $wgContLang;
558 wfProfileIn( __METHOD__
);
560 $sk = $this->getSkin();
561 $rev = new Revision( $row );
564 $page = Title
::newFromRow( $row );
567 htmlspecialchars( $page->getPrefixedText() ),
569 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
571 # Mark current revisions
573 if( $row->rev_id
== $row->page_latest
) {
574 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
576 if( !$row->page_is_new
&& $page->quickUserCan( 'rollback' )
577 && $page->quickUserCan( 'edit' ) )
579 $this->preventClickjacking();
580 $topmarktext .= ' '.$sk->generateRollback( $rev );
583 # Is there a visible previous revision?
584 if( $rev->userCan( Revision
::DELETED_TEXT
) && $rev->getParentId() !== 0 ) {
585 $difftext = $sk->linkKnown(
587 $this->messages
['diff'],
591 'oldid' => $row->rev_id
595 $difftext = $this->messages
['diff'];
597 $histlink = $sk->linkKnown(
599 $this->messages
['hist'],
601 array( 'action' => 'history' )
604 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
605 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
606 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
609 htmlspecialchars($date),
611 array( 'oldid' => intval( $row->rev_id
) )
614 $d = htmlspecialchars( $date );
616 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
617 $d = '<span class="history-deleted">' . $d . '</span>';
620 if( $this->target
== 'newbies' ) {
621 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
622 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) ) . ' ';
627 if( $rev->getParentId() === 0 ) {
628 $nflag = ChangesList
::flag( 'newpage' );
633 if( $rev->isMinor() ) {
634 $mflag = ChangesList
::flag( 'minor' );
639 // Don't show useless link to people who cannot hide revisions
640 $canHide = $wgUser->isAllowed( 'deleterevision' );
641 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
642 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
643 $del = $this->mSkin
->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
646 'type' => 'revision',
647 'target' => $page->getPrefixedDbkey(),
648 'ids' => $rev->getId()
650 $del = $this->mSkin
->revDeleteLink( $query,
651 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
658 $diffHistLinks = '(' . $difftext . $this->messages
['pipe-separator'] . $histlink . ')';
661 $diffOut = ( $this->showSizeDiff
) ?
' . . '.ChangesList
::showCharacterDifference( $row->rc_old_len
, $row->rc_new_len
) : ' . . '.Linker
::formatRevisionSize( $row->rev_len
);
663 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$diffOut}{$userlink} {$comment} {$topmarktext}";
665 # Denote if username is redacted for this edit
666 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
667 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
671 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
672 $classes = array_merge( $classes, $newClasses );
673 $ret .= " $tagSummary";
675 // Let extensions add data
676 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
678 $classes = implode( ' ', $classes );
679 $ret = "<li class=\"$classes\">$ret</li>\n";
680 wfProfileOut( __METHOD__
);
685 * Get the Database object in use
687 * @return DatabaseBase
689 public function getDatabase() {
694 * Overwrite Pager function and return a helpful comment
696 function getSqlComment() {
697 if ( $this->namespace ||
$this->deletedOnly
) {
698 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
700 return 'contributions page unfiltered';
704 protected function preventClickjacking() {
705 $this->preventClickjacking
= true;
708 public function getPreventClickjacking() {
709 return $this->preventClickjacking
;