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 $this->addFeedLinks( array( 'action' => 'feedcontributions', 'user' => $wgUser->getName() ) );
121 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
123 $wgOut->addHTML( $this->getForm() );
125 $pager = new ContribsPager( array(
127 'namespace' => $this->opts
['namespace'],
128 'year' => $this->opts
['year'],
129 'month' => $this->opts
['month'],
130 'deletedOnly' => $this->opts
['deletedOnly'],
131 'topOnly' => $this->opts
['topOnly'],
132 'showSizeDiff' => $this->opts
['showSizeDiff'],
134 if( !$pager->getNumRows() ) {
135 $wgOut->addWikiMsg( 'nocontribs', $target );
137 # Show a message about slave lag, if applicable
138 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
139 $wgOut->showLagWarning( $lag );
142 '<p>' . $pager->getNavigationBar() . '</p>' .
144 '<p>' . $pager->getNavigationBar() . '</p>'
147 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
149 # Show the appropriate "footer" message - WHOIS tools, etc.
150 if( $target != 'newbies' ) {
151 $message = 'sp-contributions-footer';
152 if ( IP
::isIPAddress( $target ) ) {
153 $message = 'sp-contributions-footer-anon';
155 $user = User
::newFromName( $target );
156 if ( !$user ||
$user->isAnon() ) {
157 // No message for non-existing users
162 if( !wfMessage( $message, $target )->isDisabled() ) {
164 "<div class='mw-contributions-footer'>\n$1\n</div>",
165 array( $message, $target ) );
172 * Generates the subheading with links
173 * @param $nt Title object for the target
174 * @param $id Integer: User ID for the target
175 * @return String: appropriately-escaped HTML to be output literally
176 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
178 protected function contributionsSub( $nt, $id ) {
179 global $wgLang, $wgUser, $wgOut;
181 $sk = $wgUser->getSkin();
183 if ( $id === null ) {
184 $user = htmlspecialchars( $nt->getText() );
186 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
188 $userObj = User
::newFromName( $nt->getText(), /* check for username validity not needed */ false );
189 $talk = $nt->getTalkPage();
191 $tools = self
::getUserLinks( $nt, $talk, $userObj, $wgUser );
192 $links = $wgLang->pipeList( $tools );
194 // Show a note if the user is blocked and display the last block log entry.
195 if ( $userObj->isBlocked() ) {
196 LogEventsList
::showLogExtract(
199 $nt->getPrefixedText(),
203 'showIfEmpty' => false,
206 'sp-contributions-blocked-notice-anon' :
207 'sp-contributions-blocked-notice',
208 $nt->getText() # Support GENDER in 'sp-contributions-blocked-notice'
210 'offset' => '' # don't use $wgRequest parameter offset
216 // Old message 'contribsub' had one parameter, but that doesn't work for
217 // languages that want to put the "for" bit right after $user but before
218 // $links. If 'contribsub' is around, use it for reverse compatibility,
219 // otherwise use 'contribsub2'.
220 if( wfEmptyMsg( 'contribsub' ) ) {
221 return wfMsgHtml( 'contribsub2', $user, $links );
223 return wfMsgHtml( 'contribsub', "$user ($links)" );
228 * Links to different places.
229 * @param $userpage Title: Target user page
230 * @param $talkpage Title: Talk page
231 * @param $target User: Target user object
232 * @param $subject User: The viewing user ($wgUser is still checked in some cases, like userrights page!!)
234 public static function getUserLinks( Title
$userpage, Title
$talkpage, User
$target, User
$subject ) {
236 $sk = $subject->getSkin();
237 $id = $target->getId();
238 $username = $target->getName();
240 $tools[] = $sk->link( $talkpage, wfMsgHtml( 'sp-contributions-talk' ) );
242 if( ( $id !== null ) ||
( $id === null && IP
::isIPAddress( $username ) ) ) {
243 if( $subject->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
244 if ( $target->isBlocked() ) {
245 $tools[] = $sk->linkKnown( # Change block link
246 SpecialPage
::getTitleFor( 'Block', $username ),
247 wfMsgHtml( 'change-blocklink' )
249 $tools[] = $sk->linkKnown( # Unblock link
250 SpecialPage
::getTitleFor( 'Unblock', $username ),
251 wfMsgHtml( 'unblocklink' )
253 } else { # User is not blocked
254 $tools[] = $sk->linkKnown( # Block link
255 SpecialPage
::getTitleFor( 'Block', $username ),
256 wfMsgHtml( 'blocklink' )
261 $tools[] = $sk->linkKnown(
262 SpecialPage
::getTitleFor( 'Log' ),
263 wfMsgHtml( 'sp-contributions-blocklog' ),
267 'page' => $userpage->getPrefixedText()
272 $tools[] = $sk->linkKnown(
273 SpecialPage
::getTitleFor( 'Listfiles', $username ),
274 wfMsgHtml( 'sp-contributions-uploads' )
278 $tools[] = $sk->linkKnown(
279 SpecialPage
::getTitleFor( 'Log' ),
280 wfMsgHtml( 'sp-contributions-logs' ),
282 array( 'user' => $username )
285 # Add link to deleted user contributions for priviledged users
286 if( $subject->isAllowed( 'deletedhistory' ) ) {
287 $tools[] = $sk->linkKnown(
288 SpecialPage
::getTitleFor( 'DeletedContributions', $username ),
289 wfMsgHtml( 'sp-contributions-deleted' )
293 # Add a link to change user rights for privileged users
294 $userrightsPage = new UserrightsPage();
295 if( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
296 $tools[] = $sk->linkKnown(
297 SpecialPage
::getTitleFor( 'Userrights', $username ),
298 wfMsgHtml( 'sp-contributions-userrights' )
302 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
307 * Generates the namespace selector form with hidden attributes.
308 * @return String: HTML fragment
310 protected function getForm() {
311 global $wgScript, $wgMiserMode;
313 $this->opts
['title'] = $this->getTitle()->getPrefixedText();
314 if( !isset( $this->opts
['target'] ) ) {
315 $this->opts
['target'] = '';
317 $this->opts
['target'] = str_replace( '_' , ' ' , $this->opts
['target'] );
320 if( !isset( $this->opts
['namespace'] ) ) {
321 $this->opts
['namespace'] = '';
324 if( !isset( $this->opts
['contribs'] ) ) {
325 $this->opts
['contribs'] = 'user';
328 if( !isset( $this->opts
['year'] ) ) {
329 $this->opts
['year'] = '';
332 if( !isset( $this->opts
['month'] ) ) {
333 $this->opts
['month'] = '';
336 if( $this->opts
['contribs'] == 'newbie' ) {
337 $this->opts
['target'] = '';
340 if( !isset( $this->opts
['tagFilter'] ) ) {
341 $this->opts
['tagFilter'] = '';
344 if( !isset( $this->opts
['topOnly'] ) ) {
345 $this->opts
['topOnly'] = false;
348 if( !isset( $this->opts
['showSizeDiff'] ) ) {
349 $this->opts
['showSizeDiff'] = !$wgMiserMode;
352 $f = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
354 # Add hidden params for tracking except for parameters in $skipParameters
355 $skipParameters = array( 'namespace', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly', 'showSizeDiff' );
356 foreach ( $this->opts
as $name => $value ) {
357 if( in_array( $name, $skipParameters ) ) {
360 $f .= "\t" . Html
::hidden( $name, $value ) . "\n";
363 $tagFilter = ChangeTags
::buildTagFilterSelector( $this->opts
['tagFilter'] );
367 if ( !$wgMiserMode ) {
368 $fNS = Html
::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
369 Xml
::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
370 Xml
::namespaceSelector( $this->opts
['namespace'], '' )
372 $fShowDiff = Xml
::checkLabel( wfMsg( 'sp-contributions-showsizediff' ), 'showSizeDiff', 'mw-show-size-diff', $this->opts
['showSizeDiff'] );
375 $f .= Xml
::fieldset( wfMsg( 'sp-contributions-search' ) ) .
376 Xml
::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
377 'contribs', 'newbie' , 'newbie', $this->opts
['contribs'] == 'newbie' ) . '<br />' .
378 Xml
::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
379 'contribs' , 'user', 'user', $this->opts
['contribs'] == 'user' ) . ' ' .
380 Html
::input( 'target', $this->opts
['target'], 'text', array(
383 ) +
( $this->opts
['target'] ?
array() : array( 'autofocus' ) ) ) . ' '.
385 Xml
::checkLabel( wfMsg( 'history-show-deleted' ),
386 'deletedOnly', 'mw-show-deleted-only', $this->opts
['deletedOnly'] ) . '<br />' .
387 Xml
::tags( 'p', null, Xml
::checkLabel( wfMsg( 'sp-contributions-toponly' ),
388 'topOnly', 'mw-show-top-only', $this->opts
['topOnly'] ) ) .
390 ( $tagFilter ? Xml
::tags( 'p', null, implode( ' ', $tagFilter ) ) : '' ) .
391 Html
::rawElement( 'p', array( 'style' => 'white-space: nowrap' ),
392 Xml
::dateMenu( $this->opts
['year'], $this->opts
['month'] ) . ' ' .
393 Xml
::submitButton( wfMsg( 'sp-contributions-submit' ) )
395 $explain = wfMessage( 'sp-contributions-explain' );
396 if ( $explain->exists() ) {
397 $f .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
399 $f .= Xml
::closeElement('fieldset' ) .
400 Xml
::closeElement( 'form' );
406 * Pager for Special:Contributions
407 * @ingroup SpecialPage Pager
409 class ContribsPager
extends ReverseChronologicalPager
{
410 public $mDefaultDirection = true;
411 var $messages, $target;
412 var $namespace = '', $mDb;
413 var $preventClickjacking = false;
415 function __construct( $options ) {
416 parent
::__construct();
418 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
420 foreach( $msgs as $msg ) {
421 $this->messages
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
424 $this->target
= isset( $options['target'] ) ?
$options['target'] : '';
425 $this->namespace = isset( $options['namespace'] ) ?
$options['namespace'] : '';
426 $this->tagFilter
= isset( $options['tagFilter'] ) ?
$options['tagFilter'] : false;
428 $this->deletedOnly
= !empty( $options['deletedOnly'] );
429 $this->topOnly
= !empty( $options['topOnly'] );
430 $this->showSizeDiff
= !empty( $options['showSizeDiff'] );
432 $year = isset( $options['year'] ) ?
$options['year'] : false;
433 $month = isset( $options['month'] ) ?
$options['month'] : false;
434 $this->getDateCond( $year, $month );
436 $this->mDb
= wfGetDB( DB_SLAVE
, 'contributions' );
439 function getDefaultQuery() {
440 $query = parent
::getDefaultQuery();
441 $query['target'] = $this->target
;
445 function getQueryInfo() {
446 global $wgUser, $wgMiserMode;
447 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
449 $conds = array_merge( $userCond, $this->getNamespaceCond() );
450 // Paranoia: avoid brute force searches (bug 17342)
451 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
452 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0';
453 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
454 $conds[] = $this->mDb
->bitAnd('rev_deleted',Revision
::SUPPRESSED_USER
) .
455 ' != ' . Revision
::SUPPRESSED_USER
;
457 $join_cond['page'] = array( 'INNER JOIN', 'page_id=rev_page' );
460 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect',
461 'page_len','rev_id', 'rev_page', 'rev_text_id', 'rev_timestamp', 'rev_comment',
462 'rev_minor_edit', 'rev_user', 'rev_user_text', 'rev_parent_id', 'rev_deleted',
465 if ( $this->showSizeDiff
&& !$wgMiserMode ) {
466 $fields = array_merge( $fields, array( 'rc_old_len', 'rc_new_len' ) );
467 array_unshift( $tables, 'recentchanges' );
468 $join_cond['recentchanges'] = array( 'INNER JOIN', "rev_id = rc_this_oldid" );
475 'options' => array( 'USE INDEX' => array('revision' => $index) ),
476 'join_conds' => $join_cond
478 ChangeTags
::modifyDisplayQuery(
479 $queryInfo['tables'],
480 $queryInfo['fields'],
482 $queryInfo['join_conds'],
483 $queryInfo['options'],
487 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
491 function getUserCond() {
492 $condition = array();
493 $join_conds = array();
495 if( $this->target
== 'newbies' ) {
496 $tables = array( 'user_groups', 'page', 'revision' );
497 $max = $this->mDb
->selectField( 'user', 'max(user_id)', false, __METHOD__
);
498 $condition[] = 'rev_user >' . (int)($max - $max / 100);
499 $condition[] = 'ug_group IS NULL';
500 $index = 'user_timestamp';
501 # @todo FIXME: Other groups may have 'bot' rights
502 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
504 $tables = array( 'page', 'revision' );
505 $condition['rev_user_text'] = $this->target
;
506 $index = 'usertext_timestamp';
508 if( $this->deletedOnly
) {
509 $condition[] = "rev_deleted != '0'";
511 if( $this->topOnly
) {
512 $condition[] = "rev_id = page_latest";
514 return array( $tables, $index, $condition, $join_conds );
517 function getNamespaceCond() {
519 if( $this->namespace !== '' && !$wgMiserMode ) {
520 return array( 'page_namespace' => (int)$this->namespace );
526 function getIndexField() {
527 return 'rev_timestamp';
530 function getStartBody() {
534 function getEndBody() {
539 * Generates each row in the contributions list.
541 * Contributions which are marked "top" are currently on top of the history.
542 * For these contributions, a [rollback] link is shown for users with roll-
543 * back privileges. The rollback link restores the most recent version that
544 * was not written by the target user.
546 * @todo This would probably look a lot nicer in a table.
548 function formatRow( $row ) {
549 global $wgUser, $wgLang, $wgContLang;
550 wfProfileIn( __METHOD__
);
552 $sk = $this->getSkin();
553 $rev = new Revision( $row );
556 $page = Title
::newFromRow( $row );
559 htmlspecialchars( $page->getPrefixedText() ),
561 $page->isRedirect() ?
array( 'redirect' => 'no' ) : array()
563 # Mark current revisions
565 if( $row->rev_id
== $row->page_latest
) {
566 $topmarktext .= '<span class="mw-uctop">' . $this->messages
['uctop'] . '</span>';
568 if( !$row->page_is_new
&& $page->quickUserCan( 'rollback' )
569 && $page->quickUserCan( 'edit' ) )
571 $this->preventClickjacking();
572 $topmarktext .= ' '.$sk->generateRollback( $rev );
575 # Is there a visible previous revision?
576 if( $rev->userCan( Revision
::DELETED_TEXT
) && $rev->getParentId() !== 0 ) {
577 $difftext = $sk->linkKnown(
579 $this->messages
['diff'],
583 'oldid' => $row->rev_id
587 $difftext = $this->messages
['diff'];
589 $histlink = $sk->linkKnown(
591 $this->messages
['hist'],
593 array( 'action' => 'history' )
596 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
597 $date = $wgLang->timeanddate( wfTimestamp( TS_MW
, $row->rev_timestamp
), true );
598 if( $rev->userCan( Revision
::DELETED_TEXT
) ) {
601 htmlspecialchars($date),
603 array( 'oldid' => intval( $row->rev_id
) )
606 $d = htmlspecialchars( $date );
608 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
609 $d = '<span class="history-deleted">' . $d . '</span>';
612 if( $this->target
== 'newbies' ) {
613 $userlink = ' . . ' . $sk->userLink( $row->rev_user
, $row->rev_user_text
);
614 $userlink .= ' ' . wfMsg( 'parentheses', $sk->userTalkLink( $row->rev_user
, $row->rev_user_text
) ) . ' ';
619 if( $rev->getParentId() === 0 ) {
620 $nflag = ChangesList
::flag( 'newpage' );
625 if( $rev->isMinor() ) {
626 $mflag = ChangesList
::flag( 'minor' );
631 // Don't show useless link to people who cannot hide revisions
632 $canHide = $wgUser->isAllowed( 'deleterevision' );
633 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
634 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
635 $del = $this->mSkin
->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
638 'type' => 'revision',
639 'target' => $page->getPrefixedDbkey(),
640 'ids' => $rev->getId()
642 $del = $this->mSkin
->revDeleteLink( $query,
643 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
650 $diffHistLinks = '(' . $difftext . $this->messages
['pipe-separator'] . $histlink . ')';
653 $diffOut = ( $this->showSizeDiff
) ?
' . . '.ChangesList
::showCharacterDifference( $row->rc_old_len
, $row->rc_new_len
) : ' . . '.Linker
::formatRevisionSize( $row->rev_len
);
655 $ret = "{$del}{$d} {$diffHistLinks} {$nflag}{$mflag} {$link}{$diffOut}{$userlink} {$comment} {$topmarktext}";
657 # Denote if username is redacted for this edit
658 if( $rev->isDeleted( Revision
::DELETED_USER
) ) {
659 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
663 list($tagSummary, $newClasses) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'contributions' );
664 $classes = array_merge( $classes, $newClasses );
665 $ret .= " $tagSummary";
667 // Let extensions add data
668 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
670 $classes = implode( ' ', $classes );
671 $ret = "<li class=\"$classes\">$ret</li>\n";
672 wfProfileOut( __METHOD__
);
677 * Get the Database object in use
679 * @return DatabaseBase
681 public function getDatabase() {
686 * Overwrite Pager function and return a helpful comment
688 function getSqlComment() {
689 if ( $this->namespace ||
$this->deletedOnly
) {
690 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
692 return 'contributions page unfiltered';
696 protected function preventClickjacking() {
697 $this->preventClickjacking
= true;
700 public function getPreventClickjacking() {
701 return $this->preventClickjacking
;