Merge "Convert Special:NewFiles to use OOUI."
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
1 <?php
2 /**
3 * Implements Special:Contributions
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special:Contributions, show user contributions in a paged list
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialContributions extends IncludableSpecialPage {
30 protected $opts;
31
32 public function __construct() {
33 parent::__construct( 'Contributions' );
34 }
35
36 public function execute( $par ) {
37 $this->setHeaders();
38 $this->outputHeader();
39 $out = $this->getOutput();
40 $out->addModuleStyles( [
41 'mediawiki.special',
42 'mediawiki.special.changeslist',
43 ] );
44 $this->addHelpLink( 'Help:User contributions' );
45
46 $this->opts = [];
47 $request = $this->getRequest();
48
49 if ( $par !== null ) {
50 $target = $par;
51 } else {
52 $target = $request->getVal( 'target' );
53 }
54
55 if ( $request->getVal( 'contribs' ) == 'newbie' || $par === 'newbies' ) {
56 $target = 'newbies';
57 $this->opts['contribs'] = 'newbie';
58 } else {
59 $this->opts['contribs'] = 'user';
60 }
61
62 $this->opts['deletedOnly'] = $request->getBool( 'deletedOnly' );
63
64 if ( !strlen( $target ) ) {
65 if ( !$this->including() ) {
66 $out->addHTML( $this->getForm() );
67 }
68
69 return;
70 }
71
72 $user = $this->getUser();
73
74 $this->opts['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
75 $this->opts['target'] = $target;
76 $this->opts['topOnly'] = $request->getBool( 'topOnly' );
77 $this->opts['newOnly'] = $request->getBool( 'newOnly' );
78 $this->opts['hideMinor'] = $request->getBool( 'hideMinor' );
79
80 $nt = Title::makeTitleSafe( NS_USER, $target );
81 if ( !$nt ) {
82 $out->addHTML( $this->getForm() );
83
84 return;
85 }
86 $userObj = User::newFromName( $nt->getText(), false );
87 if ( !$userObj ) {
88 $out->addHTML( $this->getForm() );
89
90 return;
91 }
92 $id = $userObj->getId();
93
94 if ( $this->opts['contribs'] != 'newbie' ) {
95 $target = $nt->getText();
96 $out->addSubtitle( $this->contributionsSub( $userObj ) );
97 $out->setHTMLTitle( $this->msg(
98 'pagetitle',
99 $this->msg( 'contributions-title', $target )->plain()
100 )->inContentLanguage() );
101 $this->getSkin()->setRelevantUser( $userObj );
102 } else {
103 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
104 $out->setHTMLTitle( $this->msg(
105 'pagetitle',
106 $this->msg( 'sp-contributions-newbies-title' )->plain()
107 )->inContentLanguage() );
108 }
109
110 $ns = $request->getVal( 'namespace', null );
111 if ( $ns !== null && $ns !== '' ) {
112 $this->opts['namespace'] = intval( $ns );
113 } else {
114 $this->opts['namespace'] = '';
115 }
116
117 $this->opts['associated'] = $request->getBool( 'associated' );
118 $this->opts['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
119 $this->opts['tagfilter'] = (string)$request->getVal( 'tagfilter' );
120
121 // Allows reverts to have the bot flag in recent changes. It is just here to
122 // be passed in the form at the top of the page
123 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
124 $this->opts['bot'] = '1';
125 }
126
127 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
128 # Offset overrides year/month selection
129 if ( $skip ) {
130 $this->opts['year'] = '';
131 $this->opts['month'] = '';
132 } else {
133 $this->opts['year'] = $request->getIntOrNull( 'year' );
134 $this->opts['month'] = $request->getIntOrNull( 'month' );
135 }
136
137 $feedType = $request->getVal( 'feed' );
138
139 $feedParams = [
140 'action' => 'feedcontributions',
141 'user' => $target,
142 ];
143 if ( $this->opts['topOnly'] ) {
144 $feedParams['toponly'] = true;
145 }
146 if ( $this->opts['newOnly'] ) {
147 $feedParams['newonly'] = true;
148 }
149 if ( $this->opts['hideMinor'] ) {
150 $feedParams['hideminor'] = true;
151 }
152 if ( $this->opts['deletedOnly'] ) {
153 $feedParams['deletedonly'] = true;
154 }
155 if ( $this->opts['tagfilter'] !== '' ) {
156 $feedParams['tagfilter'] = $this->opts['tagfilter'];
157 }
158 if ( $this->opts['namespace'] !== '' ) {
159 $feedParams['namespace'] = $this->opts['namespace'];
160 }
161 // Don't use year and month for the feed URL, but pass them on if
162 // we redirect to API (if $feedType is specified)
163 if ( $feedType && $this->opts['year'] !== null ) {
164 $feedParams['year'] = $this->opts['year'];
165 }
166 if ( $feedType && $this->opts['month'] !== null ) {
167 $feedParams['month'] = $this->opts['month'];
168 }
169
170 if ( $feedType ) {
171 // Maintain some level of backwards compatibility
172 // If people request feeds using the old parameters, redirect to API
173 $feedParams['feedformat'] = $feedType;
174 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
175
176 $out->redirect( $url, '301' );
177
178 return;
179 }
180
181 // Add RSS/atom links
182 $this->addFeedLinks( $feedParams );
183
184 if ( Hooks::run( 'SpecialContributionsBeforeMainOutput', [ $id, $userObj, $this ] ) ) {
185 if ( !$this->including() ) {
186 $out->addHTML( $this->getForm() );
187 }
188 $pager = new ContribsPager( $this->getContext(), [
189 'target' => $target,
190 'contribs' => $this->opts['contribs'],
191 'namespace' => $this->opts['namespace'],
192 'tagfilter' => $this->opts['tagfilter'],
193 'year' => $this->opts['year'],
194 'month' => $this->opts['month'],
195 'deletedOnly' => $this->opts['deletedOnly'],
196 'topOnly' => $this->opts['topOnly'],
197 'newOnly' => $this->opts['newOnly'],
198 'hideMinor' => $this->opts['hideMinor'],
199 'nsInvert' => $this->opts['nsInvert'],
200 'associated' => $this->opts['associated'],
201 ] );
202
203 if ( !$pager->getNumRows() ) {
204 $out->addWikiMsg( 'nocontribs', $target );
205 } else {
206 # Show a message about slave lag, if applicable
207 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
208 if ( $lag > 0 ) {
209 $out->showLagWarning( $lag );
210 }
211
212 $output = $pager->getBody();
213 if ( !$this->including() ) {
214 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
215 $output .
216 '<p>' . $pager->getNavigationBar() . '</p>';
217 }
218 $out->addHTML( $output );
219 }
220 $out->preventClickjacking( $pager->getPreventClickjacking() );
221
222 # Show the appropriate "footer" message - WHOIS tools, etc.
223 if ( $this->opts['contribs'] == 'newbie' ) {
224 $message = 'sp-contributions-footer-newbies';
225 } elseif ( IP::isIPAddress( $target ) ) {
226 $message = 'sp-contributions-footer-anon';
227 } elseif ( $userObj->isAnon() ) {
228 // No message for non-existing users
229 $message = '';
230 } else {
231 $message = 'sp-contributions-footer';
232 }
233
234 if ( $message ) {
235 if ( !$this->including() ) {
236 if ( !$this->msg( $message, $target )->isDisabled() ) {
237 $out->wrapWikiMsg(
238 "<div class='mw-contributions-footer'>\n$1\n</div>",
239 [ $message, $target ] );
240 }
241 }
242 }
243 }
244 }
245
246 /**
247 * Generates the subheading with links
248 * @param User $userObj User object for the target
249 * @return string Appropriately-escaped HTML to be output literally
250 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
251 * Could be combined.
252 */
253 protected function contributionsSub( $userObj ) {
254 if ( $userObj->isAnon() ) {
255 // Show a warning message that the user being searched for doesn't exists
256 if ( !User::isIP( $userObj->getName() ) ) {
257 $this->getOutput()->wrapWikiMsg(
258 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
259 [
260 'contributions-userdoesnotexist',
261 wfEscapeWikiText( $userObj->getName() ),
262 ]
263 );
264 if ( !$this->including() ) {
265 $this->getOutput()->setStatusCode( 404 );
266 }
267 }
268 $user = htmlspecialchars( $userObj->getName() );
269 } else {
270 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
271 }
272 $nt = $userObj->getUserPage();
273 $talk = $userObj->getTalkPage();
274 $links = '';
275 if ( $talk ) {
276 $tools = $this->getUserLinks( $nt, $talk, $userObj );
277 $links = $this->getLanguage()->pipeList( $tools );
278
279 // Show a note if the user is blocked and display the last block log entry.
280 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
281 // and also this will display a totally irrelevant log entry as a current block.
282 if ( !$this->including() ) {
283 $block = Block::newFromTarget( $userObj, $userObj );
284 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
285 if ( $block->getType() == Block::TYPE_RANGE ) {
286 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
287 }
288
289 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
290 LogEventsList::showLogExtract(
291 $out,
292 'block',
293 $nt,
294 '',
295 [
296 'lim' => 1,
297 'showIfEmpty' => false,
298 'msgKey' => [
299 $userObj->isAnon() ?
300 'sp-contributions-blocked-notice-anon' :
301 'sp-contributions-blocked-notice',
302 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
303 ],
304 'offset' => '' # don't use WebRequest parameter offset
305 ]
306 );
307 }
308 }
309 }
310
311 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
312 }
313
314 /**
315 * Links to different places.
316 * @param Title $userpage Target user page
317 * @param Title $talkpage Talk page
318 * @param User $target Target user object
319 * @return array
320 */
321 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
322
323 $id = $target->getId();
324 $username = $target->getName();
325
326 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
327
328 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
329 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
330 if ( $target->isBlocked() && $target->getBlock()->getType() != Block::TYPE_AUTO ) {
331 $tools[] = Linker::linkKnown( # Change block link
332 SpecialPage::getTitleFor( 'Block', $username ),
333 $this->msg( 'change-blocklink' )->escaped()
334 );
335 $tools[] = Linker::linkKnown( # Unblock link
336 SpecialPage::getTitleFor( 'Unblock', $username ),
337 $this->msg( 'unblocklink' )->escaped()
338 );
339 } else { # User is not blocked
340 $tools[] = Linker::linkKnown( # Block link
341 SpecialPage::getTitleFor( 'Block', $username ),
342 $this->msg( 'blocklink' )->escaped()
343 );
344 }
345 }
346
347 # Block log link
348 $tools[] = Linker::linkKnown(
349 SpecialPage::getTitleFor( 'Log', 'block' ),
350 $this->msg( 'sp-contributions-blocklog' )->escaped(),
351 [],
352 [ 'page' => $userpage->getPrefixedText() ]
353 );
354
355 # Suppression log link (bug 59120)
356 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
357 $tools[] = Linker::linkKnown(
358 SpecialPage::getTitleFor( 'Log', 'suppress' ),
359 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
360 [],
361 [ 'offender' => $username ]
362 );
363 }
364 }
365 # Uploads
366 $tools[] = Linker::linkKnown(
367 SpecialPage::getTitleFor( 'Listfiles', $username ),
368 $this->msg( 'sp-contributions-uploads' )->escaped()
369 );
370
371 # Other logs link
372 $tools[] = Linker::linkKnown(
373 SpecialPage::getTitleFor( 'Log', $username ),
374 $this->msg( 'sp-contributions-logs' )->escaped()
375 );
376
377 # Add link to deleted user contributions for priviledged users
378 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
379 $tools[] = Linker::linkKnown(
380 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
381 $this->msg( 'sp-contributions-deleted' )->escaped()
382 );
383 }
384
385 # Add a link to change user rights for privileged users
386 $userrightsPage = new UserrightsPage();
387 $userrightsPage->setContext( $this->getContext() );
388 if ( $userrightsPage->userCanChangeRights( $target ) ) {
389 $tools[] = Linker::linkKnown(
390 SpecialPage::getTitleFor( 'Userrights', $username ),
391 $this->msg( 'sp-contributions-userrights' )->escaped()
392 );
393 }
394
395 Hooks::run( 'ContributionsToolLinks', [ $id, $userpage, &$tools ] );
396
397 return $tools;
398 }
399
400 /**
401 * Generates the namespace selector form with hidden attributes.
402 * @return string HTML fragment
403 */
404 protected function getForm() {
405 $this->opts['title'] = $this->getPageTitle()->getPrefixedText();
406 if ( !isset( $this->opts['target'] ) ) {
407 $this->opts['target'] = '';
408 } else {
409 $this->opts['target'] = str_replace( '_', ' ', $this->opts['target'] );
410 }
411
412 if ( !isset( $this->opts['namespace'] ) ) {
413 $this->opts['namespace'] = '';
414 }
415
416 if ( !isset( $this->opts['nsInvert'] ) ) {
417 $this->opts['nsInvert'] = '';
418 }
419
420 if ( !isset( $this->opts['associated'] ) ) {
421 $this->opts['associated'] = false;
422 }
423
424 if ( !isset( $this->opts['contribs'] ) ) {
425 $this->opts['contribs'] = 'user';
426 }
427
428 if ( !isset( $this->opts['year'] ) ) {
429 $this->opts['year'] = '';
430 }
431
432 if ( !isset( $this->opts['month'] ) ) {
433 $this->opts['month'] = '';
434 }
435
436 if ( $this->opts['contribs'] == 'newbie' ) {
437 $this->opts['target'] = '';
438 }
439
440 if ( !isset( $this->opts['tagfilter'] ) ) {
441 $this->opts['tagfilter'] = '';
442 }
443
444 if ( !isset( $this->opts['topOnly'] ) ) {
445 $this->opts['topOnly'] = false;
446 }
447
448 if ( !isset( $this->opts['newOnly'] ) ) {
449 $this->opts['newOnly'] = false;
450 }
451
452 if ( !isset( $this->opts['hideMinor'] ) ) {
453 $this->opts['hideMinor'] = false;
454 }
455
456 $form = Html::openElement(
457 'form',
458 [
459 'method' => 'get',
460 'action' => wfScript(),
461 'class' => 'mw-contributions-form'
462 ]
463 );
464
465 # Add hidden params for tracking except for parameters in $skipParameters
466 $skipParameters = [
467 'namespace',
468 'nsInvert',
469 'deletedOnly',
470 'target',
471 'contribs',
472 'year',
473 'month',
474 'topOnly',
475 'newOnly',
476 'hideMinor',
477 'associated',
478 'tagfilter'
479 ];
480
481 foreach ( $this->opts as $name => $value ) {
482 if ( in_array( $name, $skipParameters ) ) {
483 continue;
484 }
485 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
486 }
487
488 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
489
490 if ( $tagFilter ) {
491 $filterSelection = Html::rawElement(
492 'td',
493 [],
494 implode( '&#160;', $tagFilter )
495 );
496 } else {
497 $filterSelection = Html::rawElement( 'td', [ 'colspan' => 2 ], '' );
498 }
499
500 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
501
502 $labelNewbies = Xml::radioLabel(
503 $this->msg( 'sp-contributions-newbies' )->text(),
504 'contribs',
505 'newbie',
506 'newbie',
507 $this->opts['contribs'] == 'newbie',
508 [ 'class' => 'mw-input' ]
509 );
510 $labelUsername = Xml::radioLabel(
511 $this->msg( 'sp-contributions-username' )->text(),
512 'contribs',
513 'user',
514 'user',
515 $this->opts['contribs'] == 'user',
516 [ 'class' => 'mw-input' ]
517 );
518 $input = Html::input(
519 'target',
520 $this->opts['target'],
521 'text',
522 [
523 'size' => '40',
524 'required' => '',
525 'class' => [
526 'mw-input',
527 'mw-ui-input-inline',
528 'mw-autocomplete-user', // used by mediawiki.userSuggest
529 ],
530 ] + (
531 // Only autofocus if target hasn't been specified or in non-newbies mode
532 ( $this->opts['contribs'] === 'newbie' || $this->opts['target'] )
533 ? [] : [ 'autofocus' => true ]
534 )
535 );
536
537 $targetSelection = Html::rawElement(
538 'td',
539 [ 'colspan' => 2 ],
540 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
541 );
542
543 $namespaceSelection = Xml::tags(
544 'td',
545 [],
546 Xml::label(
547 $this->msg( 'namespace' )->text(),
548 'namespace',
549 ''
550 ) .
551 Html::namespaceSelector(
552 [ 'selected' => $this->opts['namespace'], 'all' => '' ],
553 [
554 'name' => 'namespace',
555 'id' => 'namespace',
556 'class' => 'namespaceselector',
557 ]
558 ) . '&#160;' .
559 Html::rawElement(
560 'span',
561 [ 'class' => 'mw-input-with-label' ],
562 Xml::checkLabel(
563 $this->msg( 'invert' )->text(),
564 'nsInvert',
565 'nsInvert',
566 $this->opts['nsInvert'],
567 [
568 'title' => $this->msg( 'tooltip-invert' )->text(),
569 'class' => 'mw-input'
570 ]
571 ) . '&#160;'
572 ) .
573 Html::rawElement( 'span', [ 'class' => 'mw-input-with-label' ],
574 Xml::checkLabel(
575 $this->msg( 'namespace_association' )->text(),
576 'associated',
577 'associated',
578 $this->opts['associated'],
579 [
580 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
581 'class' => 'mw-input'
582 ]
583 ) . '&#160;'
584 )
585 );
586
587 $filters = [];
588
589 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
590 $filters[] = Html::rawElement(
591 'span',
592 [ 'class' => 'mw-input-with-label' ],
593 Xml::checkLabel(
594 $this->msg( 'history-show-deleted' )->text(),
595 'deletedOnly',
596 'mw-show-deleted-only',
597 $this->opts['deletedOnly'],
598 [ 'class' => 'mw-input' ]
599 )
600 );
601 }
602
603 $filters[] = Html::rawElement(
604 'span',
605 [ 'class' => 'mw-input-with-label' ],
606 Xml::checkLabel(
607 $this->msg( 'sp-contributions-toponly' )->text(),
608 'topOnly',
609 'mw-show-top-only',
610 $this->opts['topOnly'],
611 [ 'class' => 'mw-input' ]
612 )
613 );
614 $filters[] = Html::rawElement(
615 'span',
616 [ 'class' => 'mw-input-with-label' ],
617 Xml::checkLabel(
618 $this->msg( 'sp-contributions-newonly' )->text(),
619 'newOnly',
620 'mw-show-new-only',
621 $this->opts['newOnly'],
622 [ 'class' => 'mw-input' ]
623 )
624 );
625 $filters[] = Html::rawElement(
626 'span',
627 [ 'class' => 'mw-input-with-label' ],
628 Xml::checkLabel(
629 $this->msg( 'sp-contributions-hideminor' )->text(),
630 'hideMinor',
631 'mw-hide-minor-edits',
632 $this->opts['hideMinor'],
633 [ 'class' => 'mw-input' ]
634 )
635 );
636
637 Hooks::run(
638 'SpecialContributions::getForm::filters',
639 [ $this, &$filters ]
640 );
641
642 $extraOptions = Html::rawElement(
643 'td',
644 [ 'colspan' => 2 ],
645 implode( '', $filters )
646 );
647
648 $dateSelectionAndSubmit = Xml::tags( 'td', [ 'colspan' => 2 ],
649 Xml::dateMenu(
650 $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
651 $this->opts['month']
652 ) . ' ' .
653 Html::submitButton(
654 $this->msg( 'sp-contributions-submit' )->text(),
655 [ 'class' => 'mw-submit' ], [ 'mw-ui-progressive' ]
656 )
657 );
658
659 $form .= Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() );
660 $form .= Html::rawElement( 'table', [ 'class' => 'mw-contributions-table' ], "\n" .
661 Html::rawElement( 'tr', [], $targetSelection ) . "\n" .
662 Html::rawElement( 'tr', [], $namespaceSelection ) . "\n" .
663 Html::rawElement( 'tr', [], $filterSelection ) . "\n" .
664 Html::rawElement( 'tr', [], $extraOptions ) . "\n" .
665 Html::rawElement( 'tr', [], $dateSelectionAndSubmit ) . "\n"
666 );
667
668 $explain = $this->msg( 'sp-contributions-explain' );
669 if ( !$explain->isBlank() ) {
670 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
671 }
672
673 $form .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
674
675 return $form;
676 }
677
678 /**
679 * Return an array of subpages beginning with $search that this special page will accept.
680 *
681 * @param string $search Prefix to search for
682 * @param int $limit Maximum number of results to return (usually 10)
683 * @param int $offset Number of results to skip (usually 0)
684 * @return string[] Matching subpages
685 */
686 public function prefixSearchSubpages( $search, $limit, $offset ) {
687 $user = User::newFromName( $search );
688 if ( !$user ) {
689 // No prefix suggestion for invalid user
690 return [];
691 }
692 // Autocomplete subpage as user list - public to allow caching
693 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
694 }
695
696 protected function getGroupName() {
697 return 'users';
698 }
699 }