Merge "Update cached user ID after user is added to the database"
[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( 'mediawiki.special' );
41 $this->addHelpLink( 'Help:User contributions' );
42
43 $this->opts = array();
44 $request = $this->getRequest();
45
46 if ( $par !== null ) {
47 $target = $par;
48 } else {
49 $target = $request->getVal( 'target' );
50 }
51
52 // check for radiobox
53 if ( $request->getVal( 'contribs' ) == 'newbie' ) {
54 $target = 'newbies';
55 $this->opts['contribs'] = 'newbie';
56 } elseif ( $par === 'newbies' ) { // b/c for WMF
57 $target = 'newbies';
58 $this->opts['contribs'] = 'newbie';
59 } else {
60 $this->opts['contribs'] = 'user';
61 }
62
63 $this->opts['deletedOnly'] = $request->getBool( 'deletedOnly' );
64
65 if ( !strlen( $target ) ) {
66 if ( !$this->including() ) {
67 $out->addHTML( $this->getForm() );
68 }
69
70 return;
71 }
72
73 $user = $this->getUser();
74
75 $this->opts['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
76 $this->opts['target'] = $target;
77 $this->opts['topOnly'] = $request->getBool( 'topOnly' );
78 $this->opts['newOnly'] = $request->getBool( 'newOnly' );
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 = array(
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['deletedOnly'] ) {
150 $feedParams['deletedonly'] = true;
151 }
152 if ( $this->opts['tagfilter'] !== '' ) {
153 $feedParams['tagfilter'] = $this->opts['tagfilter'];
154 }
155 if ( $this->opts['namespace'] !== '' ) {
156 $feedParams['namespace'] = $this->opts['namespace'];
157 }
158 // Don't use year and month for the feed URL, but pass them on if
159 // we redirect to API (if $feedType is specified)
160 if ( $feedType && $this->opts['year'] !== null ) {
161 $feedParams['year'] = $this->opts['year'];
162 }
163 if ( $feedType && $this->opts['month'] !== null ) {
164 $feedParams['month'] = $this->opts['month'];
165 }
166
167 if ( $feedType ) {
168 // Maintain some level of backwards compatibility
169 // If people request feeds using the old parameters, redirect to API
170 $feedParams['feedformat'] = $feedType;
171 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
172
173 $out->redirect( $url, '301' );
174
175 return;
176 }
177
178 // Add RSS/atom links
179 $this->addFeedLinks( $feedParams );
180
181 if ( Hooks::run( 'SpecialContributionsBeforeMainOutput', array( $id, $userObj, $this ) ) ) {
182 if ( !$this->including() ) {
183 $out->addHTML( $this->getForm() );
184 }
185 $pager = new ContribsPager( $this->getContext(), array(
186 'target' => $target,
187 'contribs' => $this->opts['contribs'],
188 'namespace' => $this->opts['namespace'],
189 'tagfilter' => $this->opts['tagfilter'],
190 'year' => $this->opts['year'],
191 'month' => $this->opts['month'],
192 'deletedOnly' => $this->opts['deletedOnly'],
193 'topOnly' => $this->opts['topOnly'],
194 'newOnly' => $this->opts['newOnly'],
195 'nsInvert' => $this->opts['nsInvert'],
196 'associated' => $this->opts['associated'],
197 ) );
198
199 if ( !$pager->getNumRows() ) {
200 $out->addWikiMsg( 'nocontribs', $target );
201 } else {
202 # Show a message about slave lag, if applicable
203 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
204 if ( $lag > 0 ) {
205 $out->showLagWarning( $lag );
206 }
207
208 $output = $pager->getBody();
209 if ( !$this->including() ) {
210 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
211 $output .
212 '<p>' . $pager->getNavigationBar() . '</p>';
213 }
214 $out->addHTML( $output );
215 }
216 $out->preventClickjacking( $pager->getPreventClickjacking() );
217
218 # Show the appropriate "footer" message - WHOIS tools, etc.
219 if ( $this->opts['contribs'] == 'newbie' ) {
220 $message = 'sp-contributions-footer-newbies';
221 } elseif ( IP::isIPAddress( $target ) ) {
222 $message = 'sp-contributions-footer-anon';
223 } elseif ( $userObj->isAnon() ) {
224 // No message for non-existing users
225 $message = '';
226 } else {
227 $message = 'sp-contributions-footer';
228 }
229
230 if ( $message ) {
231 if ( !$this->including() ) {
232 if ( !$this->msg( $message, $target )->isDisabled() ) {
233 $out->wrapWikiMsg(
234 "<div class='mw-contributions-footer'>\n$1\n</div>",
235 array( $message, $target ) );
236 }
237 }
238 }
239 }
240 }
241
242 /**
243 * Generates the subheading with links
244 * @param User $userObj User object for the target
245 * @return string Appropriately-escaped HTML to be output literally
246 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
247 * Could be combined.
248 */
249 protected function contributionsSub( $userObj ) {
250 if ( $userObj->isAnon() ) {
251 // Show a warning message that the user being searched for doesn't exists
252 if ( !User::isIP( $userObj->getName() ) ) {
253 $this->getOutput()->wrapWikiMsg(
254 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
255 array(
256 'contributions-userdoesnotexist',
257 wfEscapeWikiText( $userObj->getName() ),
258 )
259 );
260 if ( !$this->including() ) {
261 $this->getOutput()->setStatusCode( 404 );
262 }
263 }
264 $user = htmlspecialchars( $userObj->getName() );
265 } else {
266 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
267 }
268 $nt = $userObj->getUserPage();
269 $talk = $userObj->getTalkPage();
270 $links = '';
271 if ( $talk ) {
272 $tools = $this->getUserLinks( $nt, $talk, $userObj );
273 $links = $this->getLanguage()->pipeList( $tools );
274
275 // Show a note if the user is blocked and display the last block log entry.
276 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
277 // and also this will display a totally irrelevant log entry as a current block.
278 if ( !$this->including() ) {
279 $block = Block::newFromTarget( $userObj, $userObj );
280 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
281 if ( $block->getType() == Block::TYPE_RANGE ) {
282 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
283 }
284
285 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
286 LogEventsList::showLogExtract(
287 $out,
288 'block',
289 $nt,
290 '',
291 array(
292 'lim' => 1,
293 'showIfEmpty' => false,
294 'msgKey' => array(
295 $userObj->isAnon() ?
296 'sp-contributions-blocked-notice-anon' :
297 'sp-contributions-blocked-notice',
298 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
299 ),
300 'offset' => '' # don't use WebRequest parameter offset
301 )
302 );
303 }
304 }
305 }
306
307 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
308 }
309
310 /**
311 * Links to different places.
312 * @param Title $userpage Target user page
313 * @param Title $talkpage Talk page
314 * @param User $target Target user object
315 * @return array
316 */
317 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
318
319 $id = $target->getId();
320 $username = $target->getName();
321
322 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
323
324 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
325 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
326 if ( $target->isBlocked() && $target->getBlock()->getType() != Block::TYPE_AUTO ) {
327 $tools[] = Linker::linkKnown( # Change block link
328 SpecialPage::getTitleFor( 'Block', $username ),
329 $this->msg( 'change-blocklink' )->escaped()
330 );
331 $tools[] = Linker::linkKnown( # Unblock link
332 SpecialPage::getTitleFor( 'Unblock', $username ),
333 $this->msg( 'unblocklink' )->escaped()
334 );
335 } else { # User is not blocked
336 $tools[] = Linker::linkKnown( # Block link
337 SpecialPage::getTitleFor( 'Block', $username ),
338 $this->msg( 'blocklink' )->escaped()
339 );
340 }
341 }
342
343 # Block log link
344 $tools[] = Linker::linkKnown(
345 SpecialPage::getTitleFor( 'Log', 'block' ),
346 $this->msg( 'sp-contributions-blocklog' )->escaped(),
347 array(),
348 array( 'page' => $userpage->getPrefixedText() )
349 );
350
351 # Suppression log link (bug 59120)
352 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
353 $tools[] = Linker::linkKnown(
354 SpecialPage::getTitleFor( 'Log', 'suppress' ),
355 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
356 array(),
357 array( 'offender' => $username )
358 );
359 }
360 }
361 # Uploads
362 $tools[] = Linker::linkKnown(
363 SpecialPage::getTitleFor( 'Listfiles', $username ),
364 $this->msg( 'sp-contributions-uploads' )->escaped()
365 );
366
367 # Other logs link
368 $tools[] = Linker::linkKnown(
369 SpecialPage::getTitleFor( 'Log', $username ),
370 $this->msg( 'sp-contributions-logs' )->escaped()
371 );
372
373 # Add link to deleted user contributions for priviledged users
374 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
375 $tools[] = Linker::linkKnown(
376 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
377 $this->msg( 'sp-contributions-deleted' )->escaped()
378 );
379 }
380
381 # Add a link to change user rights for privileged users
382 $userrightsPage = new UserrightsPage();
383 $userrightsPage->setContext( $this->getContext() );
384 if ( $userrightsPage->userCanChangeRights( $target ) ) {
385 $tools[] = Linker::linkKnown(
386 SpecialPage::getTitleFor( 'Userrights', $username ),
387 $this->msg( 'sp-contributions-userrights' )->escaped()
388 );
389 }
390
391 Hooks::run( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
392
393 return $tools;
394 }
395
396 /**
397 * Generates the namespace selector form with hidden attributes.
398 * @return string HTML fragment
399 */
400 protected function getForm() {
401 $this->opts['title'] = $this->getPageTitle()->getPrefixedText();
402 if ( !isset( $this->opts['target'] ) ) {
403 $this->opts['target'] = '';
404 } else {
405 $this->opts['target'] = str_replace( '_', ' ', $this->opts['target'] );
406 }
407
408 if ( !isset( $this->opts['namespace'] ) ) {
409 $this->opts['namespace'] = '';
410 }
411
412 if ( !isset( $this->opts['nsInvert'] ) ) {
413 $this->opts['nsInvert'] = '';
414 }
415
416 if ( !isset( $this->opts['associated'] ) ) {
417 $this->opts['associated'] = false;
418 }
419
420 if ( !isset( $this->opts['contribs'] ) ) {
421 $this->opts['contribs'] = 'user';
422 }
423
424 if ( !isset( $this->opts['year'] ) ) {
425 $this->opts['year'] = '';
426 }
427
428 if ( !isset( $this->opts['month'] ) ) {
429 $this->opts['month'] = '';
430 }
431
432 if ( $this->opts['contribs'] == 'newbie' ) {
433 $this->opts['target'] = '';
434 }
435
436 if ( !isset( $this->opts['tagfilter'] ) ) {
437 $this->opts['tagfilter'] = '';
438 }
439
440 if ( !isset( $this->opts['topOnly'] ) ) {
441 $this->opts['topOnly'] = false;
442 }
443
444 if ( !isset( $this->opts['newOnly'] ) ) {
445 $this->opts['newOnly'] = false;
446 }
447
448 $form = Html::openElement(
449 'form',
450 array(
451 'method' => 'get',
452 'action' => wfScript(),
453 'class' => 'mw-contributions-form'
454 )
455 );
456
457 # Add hidden params for tracking except for parameters in $skipParameters
458 $skipParameters = array(
459 'namespace',
460 'nsInvert',
461 'deletedOnly',
462 'target',
463 'contribs',
464 'year',
465 'month',
466 'topOnly',
467 'newOnly',
468 'associated'
469 );
470
471 foreach ( $this->opts as $name => $value ) {
472 if ( in_array( $name, $skipParameters ) ) {
473 continue;
474 }
475 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
476 }
477
478 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
479
480 if ( $tagFilter ) {
481 $filterSelection = Html::rawElement(
482 'td',
483 array(),
484 array_shift( $tagFilter ) . implode( '&#160', $tagFilter )
485 );
486 } else {
487 $filterSelection = Html::rawElement( 'td', array( 'colspan' => 2 ), '' );
488 }
489
490 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
491
492 $labelNewbies = Xml::radioLabel(
493 $this->msg( 'sp-contributions-newbies' )->text(),
494 'contribs',
495 'newbie',
496 'newbie',
497 $this->opts['contribs'] == 'newbie',
498 array( 'class' => 'mw-input' )
499 );
500 $labelUsername = Xml::radioLabel(
501 $this->msg( 'sp-contributions-username' )->text(),
502 'contribs',
503 'user',
504 'user',
505 $this->opts['contribs'] == 'user',
506 array( 'class' => 'mw-input' )
507 );
508 $input = Html::input(
509 'target',
510 $this->opts['target'],
511 'text',
512 array(
513 'size' => '40',
514 'required' => '',
515 'class' => array(
516 'mw-input',
517 'mw-ui-input-inline',
518 'mw-autocomplete-user', // used by mediawiki.userSuggest
519 ),
520 ) + ( $this->opts['target'] ? array() : array( 'autofocus' ) )
521 );
522 $targetSelection = Html::rawElement(
523 'td',
524 array( 'colspan' => 2 ),
525 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
526 );
527
528 $namespaceSelection = Xml::tags(
529 'td',
530 array(),
531 Xml::label(
532 $this->msg( 'namespace' )->text(),
533 'namespace',
534 ''
535 ) .
536 Html::namespaceSelector(
537 array( 'selected' => $this->opts['namespace'], 'all' => '' ),
538 array(
539 'name' => 'namespace',
540 'id' => 'namespace',
541 'class' => 'namespaceselector',
542 )
543 ) . '&#160;' .
544 Html::rawElement(
545 'span',
546 array( 'class' => 'mw-input-with-label' ),
547 Xml::checkLabel(
548 $this->msg( 'invert' )->text(),
549 'nsInvert',
550 'nsInvert',
551 $this->opts['nsInvert'],
552 array(
553 'title' => $this->msg( 'tooltip-invert' )->text(),
554 'class' => 'mw-input'
555 )
556 ) . '&#160;'
557 ) .
558 Html::rawElement( 'span', array( 'class' => 'mw-input-with-label' ),
559 Xml::checkLabel(
560 $this->msg( 'namespace_association' )->text(),
561 'associated',
562 'associated',
563 $this->opts['associated'],
564 array(
565 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
566 'class' => 'mw-input'
567 )
568 ) . '&#160;'
569 )
570 );
571
572 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
573 $deletedOnlyCheck = Html::rawElement(
574 'span',
575 array( 'class' => 'mw-input-with-label' ),
576 Xml::checkLabel(
577 $this->msg( 'history-show-deleted' )->text(),
578 'deletedOnly',
579 'mw-show-deleted-only',
580 $this->opts['deletedOnly'],
581 array( 'class' => 'mw-input' )
582 )
583 );
584 } else {
585 $deletedOnlyCheck = '';
586 }
587
588 $checkLabelTopOnly = Html::rawElement(
589 'span',
590 array( 'class' => 'mw-input-with-label' ),
591 Xml::checkLabel(
592 $this->msg( 'sp-contributions-toponly' )->text(),
593 'topOnly',
594 'mw-show-top-only',
595 $this->opts['topOnly'],
596 array( 'class' => 'mw-input' )
597 )
598 );
599 $checkLabelNewOnly = Html::rawElement(
600 'span',
601 array( 'class' => 'mw-input-with-label' ),
602 Xml::checkLabel(
603 $this->msg( 'sp-contributions-newonly' )->text(),
604 'newOnly',
605 'mw-show-new-only',
606 $this->opts['newOnly'],
607 array( 'class' => 'mw-input' )
608 )
609 );
610 $extraOptions = Html::rawElement(
611 'td',
612 array( 'colspan' => 2 ),
613 $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly
614 );
615
616 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
617 Xml::dateMenu(
618 $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
619 $this->opts['month']
620 ) . ' ' .
621 Html::submitButton(
622 $this->msg( 'sp-contributions-submit' )->text(),
623 array( 'class' => 'mw-submit' ), array( 'mw-ui-progressive' )
624 )
625 );
626
627 $form .= Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() );
628 $form .= Html::rawElement( 'table', array( 'class' => 'mw-contributions-table' ), "\n" .
629 Html::rawElement( 'tr', array(), $targetSelection ) . "\n" .
630 Html::rawElement( 'tr', array(), $namespaceSelection ) . "\n" .
631 Html::rawElement( 'tr', array(), $filterSelection ) . "\n" .
632 Html::rawElement( 'tr', array(), $extraOptions ) . "\n" .
633 Html::rawElement( 'tr', array(), $dateSelectionAndSubmit ) . "\n"
634 );
635
636 $explain = $this->msg( 'sp-contributions-explain' );
637 if ( !$explain->isBlank() ) {
638 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
639 }
640
641 $form .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
642
643 return $form;
644 }
645
646 protected function getGroupName() {
647 return 'users';
648 }
649 }
650
651 /**
652 * Pager for Special:Contributions
653 * @ingroup SpecialPage Pager
654 */
655 class ContribsPager extends ReverseChronologicalPager {
656 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
657 public $messages;
658 public $target;
659 public $namespace = '';
660 public $mDb;
661 public $preventClickjacking = false;
662
663 /** @var IDatabase */
664 public $mDbSecondary;
665
666 /**
667 * @var array
668 */
669 protected $mParentLens;
670
671 function __construct( IContextSource $context, array $options ) {
672 parent::__construct( $context );
673
674 $msgs = array(
675 'diff',
676 'hist',
677 'pipe-separator',
678 'uctop'
679 );
680
681 foreach ( $msgs as $msg ) {
682 $this->messages[$msg] = $this->msg( $msg )->escaped();
683 }
684
685 $this->target = isset( $options['target'] ) ? $options['target'] : '';
686 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
687 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
688 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
689 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
690 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
691
692 $this->deletedOnly = !empty( $options['deletedOnly'] );
693 $this->topOnly = !empty( $options['topOnly'] );
694 $this->newOnly = !empty( $options['newOnly'] );
695
696 $year = isset( $options['year'] ) ? $options['year'] : false;
697 $month = isset( $options['month'] ) ? $options['month'] : false;
698 $this->getDateCond( $year, $month );
699
700 // Most of this code will use the 'contributions' group DB, which can map to slaves
701 // with extra user based indexes or partioning by user. The additional metadata
702 // queries should use a regular slave since the lookup pattern is not all by user.
703 $this->mDbSecondary = wfGetDB( DB_SLAVE ); // any random slave
704 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
705 }
706
707 function getDefaultQuery() {
708 $query = parent::getDefaultQuery();
709 $query['target'] = $this->target;
710
711 return $query;
712 }
713
714 /**
715 * This method basically executes the exact same code as the parent class, though with
716 * a hook added, to allow extensions to add additional queries.
717 *
718 * @param string $offset Index offset, inclusive
719 * @param int $limit Exact query limit
720 * @param bool $descending Query direction, false for ascending, true for descending
721 * @return ResultWrapper
722 */
723 function reallyDoQuery( $offset, $limit, $descending ) {
724 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
725 $offset,
726 $limit,
727 $descending
728 );
729
730 /*
731 * This hook will allow extensions to add in additional queries, so they can get their data
732 * in My Contributions as well. Extensions should append their results to the $data array.
733 *
734 * Extension queries have to implement the navbar requirement as well. They should
735 * - have a column aliased as $pager->getIndexField()
736 * - have LIMIT set
737 * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
738 * - have the ORDER BY specified based upon the details provided by the navbar
739 *
740 * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
741 *
742 * &$data: an array of results of all contribs queries
743 * $pager: the ContribsPager object hooked into
744 * $offset: see phpdoc above
745 * $limit: see phpdoc above
746 * $descending: see phpdoc above
747 */
748 $data = array( $this->mDb->select(
749 $tables, $fields, $conds, $fname, $options, $join_conds
750 ) );
751 Hooks::run(
752 'ContribsPager::reallyDoQuery',
753 array( &$data, $this, $offset, $limit, $descending )
754 );
755
756 $result = array();
757
758 // loop all results and collect them in an array
759 foreach ( $data as $query ) {
760 foreach ( $query as $i => $row ) {
761 // use index column as key, allowing us to easily sort in PHP
762 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
763 }
764 }
765
766 // sort results
767 if ( $descending ) {
768 ksort( $result );
769 } else {
770 krsort( $result );
771 }
772
773 // enforce limit
774 $result = array_slice( $result, 0, $limit );
775
776 // get rid of array keys
777 $result = array_values( $result );
778
779 return new FakeResultWrapper( $result );
780 }
781
782 function getQueryInfo() {
783 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
784
785 $user = $this->getUser();
786 $conds = array_merge( $userCond, $this->getNamespaceCond() );
787
788 // Paranoia: avoid brute force searches (bug 17342)
789 if ( !$user->isAllowed( 'deletedhistory' ) ) {
790 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
791 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
792 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
793 ' != ' . Revision::SUPPRESSED_USER;
794 }
795
796 # Don't include orphaned revisions
797 $join_cond['page'] = Revision::pageJoinCond();
798 # Get the current user name for accounts
799 $join_cond['user'] = Revision::userJoinCond();
800
801 $options = array();
802 if ( $index ) {
803 $options['USE INDEX'] = array( 'revision' => $index );
804 }
805
806 $queryInfo = array(
807 'tables' => $tables,
808 'fields' => array_merge(
809 Revision::selectFields(),
810 Revision::selectUserFields(),
811 array( 'page_namespace', 'page_title', 'page_is_new',
812 'page_latest', 'page_is_redirect', 'page_len' )
813 ),
814 'conds' => $conds,
815 'options' => $options,
816 'join_conds' => $join_cond
817 );
818
819 ChangeTags::modifyDisplayQuery(
820 $queryInfo['tables'],
821 $queryInfo['fields'],
822 $queryInfo['conds'],
823 $queryInfo['join_conds'],
824 $queryInfo['options'],
825 $this->tagFilter
826 );
827
828 Hooks::run( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
829
830 return $queryInfo;
831 }
832
833 function getUserCond() {
834 $condition = array();
835 $join_conds = array();
836 $tables = array( 'revision', 'page', 'user' );
837 $index = false;
838 if ( $this->contribs == 'newbie' ) {
839 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
840 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
841 # ignore local groups with the bot right
842 # @todo FIXME: Global groups may have 'bot' rights
843 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
844 if ( count( $groupsWithBotPermission ) ) {
845 $tables[] = 'user_groups';
846 $condition[] = 'ug_group IS NULL';
847 $join_conds['user_groups'] = array(
848 'LEFT JOIN', array(
849 'ug_user = rev_user',
850 'ug_group' => $groupsWithBotPermission
851 )
852 );
853 }
854 } else {
855 $uid = User::idFromName( $this->target );
856 if ( $uid ) {
857 $condition['rev_user'] = $uid;
858 $index = 'user_timestamp';
859 } else {
860 $condition['rev_user_text'] = $this->target;
861 $index = 'usertext_timestamp';
862 }
863 }
864
865 if ( $this->deletedOnly ) {
866 $condition[] = 'rev_deleted != 0';
867 }
868
869 if ( $this->topOnly ) {
870 $condition[] = 'rev_id = page_latest';
871 }
872
873 if ( $this->newOnly ) {
874 $condition[] = 'rev_parent_id = 0';
875 }
876
877 return array( $tables, $index, $condition, $join_conds );
878 }
879
880 function getNamespaceCond() {
881 if ( $this->namespace !== '' ) {
882 $selectedNS = $this->mDb->addQuotes( $this->namespace );
883 $eq_op = $this->nsInvert ? '!=' : '=';
884 $bool_op = $this->nsInvert ? 'AND' : 'OR';
885
886 if ( !$this->associated ) {
887 return array( "page_namespace $eq_op $selectedNS" );
888 }
889
890 $associatedNS = $this->mDb->addQuotes(
891 MWNamespace::getAssociated( $this->namespace )
892 );
893
894 return array(
895 "page_namespace $eq_op $selectedNS " .
896 $bool_op .
897 " page_namespace $eq_op $associatedNS"
898 );
899 }
900
901 return array();
902 }
903
904 function getIndexField() {
905 return 'rev_timestamp';
906 }
907
908 function doBatchLookups() {
909 # Do a link batch query
910 $this->mResult->seek( 0 );
911 $revIds = array();
912 $batch = new LinkBatch();
913 # Give some pointers to make (last) links
914 foreach ( $this->mResult as $row ) {
915 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
916 $revIds[] = $row->rev_parent_id;
917 }
918 if ( isset( $row->rev_id ) ) {
919 if ( $this->contribs === 'newbie' ) { // multiple users
920 $batch->add( NS_USER, $row->user_name );
921 $batch->add( NS_USER_TALK, $row->user_name );
922 }
923 $batch->add( $row->page_namespace, $row->page_title );
924 }
925 }
926 $this->mParentLens = Revision::getParentLengths( $this->mDbSecondary, $revIds );
927 $batch->execute();
928 $this->mResult->seek( 0 );
929 }
930
931 /**
932 * @return string
933 */
934 function getStartBody() {
935 return "<ul class=\"mw-contributions-list\">\n";
936 }
937
938 /**
939 * @return string
940 */
941 function getEndBody() {
942 return "</ul>\n";
943 }
944
945 /**
946 * Generates each row in the contributions list.
947 *
948 * Contributions which are marked "top" are currently on top of the history.
949 * For these contributions, a [rollback] link is shown for users with roll-
950 * back privileges. The rollback link restores the most recent version that
951 * was not written by the target user.
952 *
953 * @todo This would probably look a lot nicer in a table.
954 * @param object $row
955 * @return string
956 */
957 function formatRow( $row ) {
958
959 $ret = '';
960 $classes = array();
961
962 /*
963 * There may be more than just revision rows. To make sure that we'll only be processing
964 * revisions here, let's _try_ to build a revision out of our row (without displaying
965 * notices though) and then trying to grab data from the built object. If we succeed,
966 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
967 * to extensions to subscribe to the hook to parse the row.
968 */
969 MediaWiki\suppressWarnings();
970 try {
971 $rev = new Revision( $row );
972 $validRevision = (bool)$rev->getId();
973 } catch ( Exception $e ) {
974 $validRevision = false;
975 }
976 MediaWiki\restoreWarnings();
977
978 if ( $validRevision ) {
979 $classes = array();
980
981 $page = Title::newFromRow( $row );
982 $link = Linker::link(
983 $page,
984 htmlspecialchars( $page->getPrefixedText() ),
985 array( 'class' => 'mw-contributions-title' ),
986 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
987 );
988 # Mark current revisions
989 $topmarktext = '';
990 $user = $this->getUser();
991 if ( $row->rev_id == $row->page_latest ) {
992 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
993 # Add rollback link
994 if ( !$row->page_is_new && $page->quickUserCan( 'rollback', $user )
995 && $page->quickUserCan( 'edit', $user )
996 ) {
997 $this->preventClickjacking();
998 $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
999 }
1000 }
1001 # Is there a visible previous revision?
1002 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
1003 $difftext = Linker::linkKnown(
1004 $page,
1005 $this->messages['diff'],
1006 array(),
1007 array(
1008 'diff' => 'prev',
1009 'oldid' => $row->rev_id
1010 )
1011 );
1012 } else {
1013 $difftext = $this->messages['diff'];
1014 }
1015 $histlink = Linker::linkKnown(
1016 $page,
1017 $this->messages['hist'],
1018 array(),
1019 array( 'action' => 'history' )
1020 );
1021
1022 if ( $row->rev_parent_id === null ) {
1023 // For some reason rev_parent_id isn't populated for this row.
1024 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
1025 // Next best thing is to have the total number of bytes.
1026 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1027 $chardiff .= Linker::formatRevisionSize( $row->rev_len );
1028 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1029 } else {
1030 $parentLen = 0;
1031 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
1032 $parentLen = $this->mParentLens[$row->rev_parent_id];
1033 }
1034
1035 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1036 $chardiff .= ChangesList::showCharacterDifference(
1037 $parentLen,
1038 $row->rev_len,
1039 $this->getContext()
1040 );
1041 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1042 }
1043
1044 $lang = $this->getLanguage();
1045 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
1046 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
1047 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1048 $d = Linker::linkKnown(
1049 $page,
1050 htmlspecialchars( $date ),
1051 array( 'class' => 'mw-changeslist-date' ),
1052 array( 'oldid' => intval( $row->rev_id ) )
1053 );
1054 } else {
1055 $d = htmlspecialchars( $date );
1056 }
1057 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1058 $d = '<span class="history-deleted">' . $d . '</span>';
1059 }
1060
1061 # Show user names for /newbies as there may be different users.
1062 # Note that we already excluded rows with hidden user names.
1063 if ( $this->contribs == 'newbie' ) {
1064 $userlink = ' . . ' . $lang->getDirMark()
1065 . Linker::userLink( $rev->getUser(), $rev->getUserText() );
1066 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
1067 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
1068 } else {
1069 $userlink = '';
1070 }
1071
1072 if ( $rev->getParentId() === 0 ) {
1073 $nflag = ChangesList::flag( 'newpage' );
1074 } else {
1075 $nflag = '';
1076 }
1077
1078 if ( $rev->isMinor() ) {
1079 $mflag = ChangesList::flag( 'minor' );
1080 } else {
1081 $mflag = '';
1082 }
1083
1084 $del = Linker::getRevDeleteLink( $user, $rev, $page );
1085 if ( $del !== '' ) {
1086 $del .= ' ';
1087 }
1088
1089 $diffHistLinks = $this->msg( 'parentheses' )
1090 ->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )
1091 ->escaped();
1092 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} ";
1093 $ret .= "{$link}{$userlink} {$comment} {$topmarktext}";
1094
1095 # Denote if username is redacted for this edit
1096 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
1097 $ret .= " <strong>" .
1098 $this->msg( 'rev-deleted-user-contribs' )->escaped() .
1099 "</strong>";
1100 }
1101
1102 # Tags, if any.
1103 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
1104 $row->ts_tags,
1105 'contributions'
1106 );
1107 $classes = array_merge( $classes, $newClasses );
1108 $ret .= " $tagSummary";
1109 }
1110
1111 // Let extensions add data
1112 Hooks::run( 'ContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
1113
1114 if ( $classes === array() && $ret === '' ) {
1115 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
1116 $ret = "<!-- Could not format Special:Contribution row. -->\n";
1117 } else {
1118 $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
1119 }
1120
1121 return $ret;
1122 }
1123
1124 /**
1125 * Overwrite Pager function and return a helpful comment
1126 * @return string
1127 */
1128 function getSqlComment() {
1129 if ( $this->namespace || $this->deletedOnly ) {
1130 // potentially slow, see CR r58153
1131 return 'contributions page filtered for namespace or RevisionDeleted edits';
1132 } else {
1133 return 'contributions page unfiltered';
1134 }
1135 }
1136
1137 protected function preventClickjacking() {
1138 $this->preventClickjacking = true;
1139 }
1140
1141 /**
1142 * @return bool
1143 */
1144 public function getPreventClickjacking() {
1145 return $this->preventClickjacking;
1146 }
1147 }