Merge "Move ArticleSaveComplete hook to doCreate()/doModify() methods"
[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 'tagfilter'
470 );
471
472 foreach ( $this->opts as $name => $value ) {
473 if ( in_array( $name, $skipParameters ) ) {
474 continue;
475 }
476 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
477 }
478
479 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
480
481 if ( $tagFilter ) {
482 $filterSelection = Html::rawElement(
483 'td',
484 array(),
485 implode( '&#160;', $tagFilter )
486 );
487 } else {
488 $filterSelection = Html::rawElement( 'td', array( 'colspan' => 2 ), '' );
489 }
490
491 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
492
493 $labelNewbies = Xml::radioLabel(
494 $this->msg( 'sp-contributions-newbies' )->text(),
495 'contribs',
496 'newbie',
497 'newbie',
498 $this->opts['contribs'] == 'newbie',
499 array( 'class' => 'mw-input' )
500 );
501 $labelUsername = Xml::radioLabel(
502 $this->msg( 'sp-contributions-username' )->text(),
503 'contribs',
504 'user',
505 'user',
506 $this->opts['contribs'] == 'user',
507 array( 'class' => 'mw-input' )
508 );
509 $input = Html::input(
510 'target',
511 $this->opts['target'],
512 'text',
513 array(
514 'size' => '40',
515 'required' => '',
516 'class' => array(
517 'mw-input',
518 'mw-ui-input-inline',
519 'mw-autocomplete-user', // used by mediawiki.userSuggest
520 ),
521 ) + (
522 // Only autofocus if target hasn't been specified or in non-newbies mode
523 ( $this->opts['contribs'] === 'newbie' || $this->opts['target'] )
524 ? array() : array( 'autofocus' => true )
525 )
526 );
527
528 $targetSelection = Html::rawElement(
529 'td',
530 array( 'colspan' => 2 ),
531 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
532 );
533
534 $namespaceSelection = Xml::tags(
535 'td',
536 array(),
537 Xml::label(
538 $this->msg( 'namespace' )->text(),
539 'namespace',
540 ''
541 ) .
542 Html::namespaceSelector(
543 array( 'selected' => $this->opts['namespace'], 'all' => '' ),
544 array(
545 'name' => 'namespace',
546 'id' => 'namespace',
547 'class' => 'namespaceselector',
548 )
549 ) . '&#160;' .
550 Html::rawElement(
551 'span',
552 array( 'class' => 'mw-input-with-label' ),
553 Xml::checkLabel(
554 $this->msg( 'invert' )->text(),
555 'nsInvert',
556 'nsInvert',
557 $this->opts['nsInvert'],
558 array(
559 'title' => $this->msg( 'tooltip-invert' )->text(),
560 'class' => 'mw-input'
561 )
562 ) . '&#160;'
563 ) .
564 Html::rawElement( 'span', array( 'class' => 'mw-input-with-label' ),
565 Xml::checkLabel(
566 $this->msg( 'namespace_association' )->text(),
567 'associated',
568 'associated',
569 $this->opts['associated'],
570 array(
571 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
572 'class' => 'mw-input'
573 )
574 ) . '&#160;'
575 )
576 );
577
578 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
579 $deletedOnlyCheck = Html::rawElement(
580 'span',
581 array( 'class' => 'mw-input-with-label' ),
582 Xml::checkLabel(
583 $this->msg( 'history-show-deleted' )->text(),
584 'deletedOnly',
585 'mw-show-deleted-only',
586 $this->opts['deletedOnly'],
587 array( 'class' => 'mw-input' )
588 )
589 );
590 } else {
591 $deletedOnlyCheck = '';
592 }
593
594 $checkLabelTopOnly = Html::rawElement(
595 'span',
596 array( 'class' => 'mw-input-with-label' ),
597 Xml::checkLabel(
598 $this->msg( 'sp-contributions-toponly' )->text(),
599 'topOnly',
600 'mw-show-top-only',
601 $this->opts['topOnly'],
602 array( 'class' => 'mw-input' )
603 )
604 );
605 $checkLabelNewOnly = Html::rawElement(
606 'span',
607 array( 'class' => 'mw-input-with-label' ),
608 Xml::checkLabel(
609 $this->msg( 'sp-contributions-newonly' )->text(),
610 'newOnly',
611 'mw-show-new-only',
612 $this->opts['newOnly'],
613 array( 'class' => 'mw-input' )
614 )
615 );
616 $extraOptions = Html::rawElement(
617 'td',
618 array( 'colspan' => 2 ),
619 $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly
620 );
621
622 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
623 Xml::dateMenu(
624 $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
625 $this->opts['month']
626 ) . ' ' .
627 Html::submitButton(
628 $this->msg( 'sp-contributions-submit' )->text(),
629 array( 'class' => 'mw-submit' ), array( 'mw-ui-progressive' )
630 )
631 );
632
633 $form .= Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() );
634 $form .= Html::rawElement( 'table', array( 'class' => 'mw-contributions-table' ), "\n" .
635 Html::rawElement( 'tr', array(), $targetSelection ) . "\n" .
636 Html::rawElement( 'tr', array(), $namespaceSelection ) . "\n" .
637 Html::rawElement( 'tr', array(), $filterSelection ) . "\n" .
638 Html::rawElement( 'tr', array(), $extraOptions ) . "\n" .
639 Html::rawElement( 'tr', array(), $dateSelectionAndSubmit ) . "\n"
640 );
641
642 $explain = $this->msg( 'sp-contributions-explain' );
643 if ( !$explain->isBlank() ) {
644 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
645 }
646
647 $form .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
648
649 return $form;
650 }
651
652 protected function getGroupName() {
653 return 'users';
654 }
655 }
656
657 /**
658 * Pager for Special:Contributions
659 * @ingroup SpecialPage Pager
660 */
661 class ContribsPager extends ReverseChronologicalPager {
662 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
663 public $messages;
664 public $target;
665 public $namespace = '';
666 public $mDb;
667 public $preventClickjacking = false;
668
669 /** @var IDatabase */
670 public $mDbSecondary;
671
672 /**
673 * @var array
674 */
675 protected $mParentLens;
676
677 function __construct( IContextSource $context, array $options ) {
678 parent::__construct( $context );
679
680 $msgs = array(
681 'diff',
682 'hist',
683 'pipe-separator',
684 'uctop'
685 );
686
687 foreach ( $msgs as $msg ) {
688 $this->messages[$msg] = $this->msg( $msg )->escaped();
689 }
690
691 $this->target = isset( $options['target'] ) ? $options['target'] : '';
692 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
693 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
694 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
695 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
696 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
697
698 $this->deletedOnly = !empty( $options['deletedOnly'] );
699 $this->topOnly = !empty( $options['topOnly'] );
700 $this->newOnly = !empty( $options['newOnly'] );
701
702 $year = isset( $options['year'] ) ? $options['year'] : false;
703 $month = isset( $options['month'] ) ? $options['month'] : false;
704 $this->getDateCond( $year, $month );
705
706 // Most of this code will use the 'contributions' group DB, which can map to slaves
707 // with extra user based indexes or partioning by user. The additional metadata
708 // queries should use a regular slave since the lookup pattern is not all by user.
709 $this->mDbSecondary = wfGetDB( DB_SLAVE ); // any random slave
710 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
711 }
712
713 function getDefaultQuery() {
714 $query = parent::getDefaultQuery();
715 $query['target'] = $this->target;
716
717 return $query;
718 }
719
720 /**
721 * This method basically executes the exact same code as the parent class, though with
722 * a hook added, to allow extensions to add additional queries.
723 *
724 * @param string $offset Index offset, inclusive
725 * @param int $limit Exact query limit
726 * @param bool $descending Query direction, false for ascending, true for descending
727 * @return ResultWrapper
728 */
729 function reallyDoQuery( $offset, $limit, $descending ) {
730 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
731 $offset,
732 $limit,
733 $descending
734 );
735
736 /*
737 * This hook will allow extensions to add in additional queries, so they can get their data
738 * in My Contributions as well. Extensions should append their results to the $data array.
739 *
740 * Extension queries have to implement the navbar requirement as well. They should
741 * - have a column aliased as $pager->getIndexField()
742 * - have LIMIT set
743 * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
744 * - have the ORDER BY specified based upon the details provided by the navbar
745 *
746 * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
747 *
748 * &$data: an array of results of all contribs queries
749 * $pager: the ContribsPager object hooked into
750 * $offset: see phpdoc above
751 * $limit: see phpdoc above
752 * $descending: see phpdoc above
753 */
754 $data = array( $this->mDb->select(
755 $tables, $fields, $conds, $fname, $options, $join_conds
756 ) );
757 Hooks::run(
758 'ContribsPager::reallyDoQuery',
759 array( &$data, $this, $offset, $limit, $descending )
760 );
761
762 $result = array();
763
764 // loop all results and collect them in an array
765 foreach ( $data as $query ) {
766 foreach ( $query as $i => $row ) {
767 // use index column as key, allowing us to easily sort in PHP
768 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
769 }
770 }
771
772 // sort results
773 if ( $descending ) {
774 ksort( $result );
775 } else {
776 krsort( $result );
777 }
778
779 // enforce limit
780 $result = array_slice( $result, 0, $limit );
781
782 // get rid of array keys
783 $result = array_values( $result );
784
785 return new FakeResultWrapper( $result );
786 }
787
788 function getQueryInfo() {
789 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
790
791 $user = $this->getUser();
792 $conds = array_merge( $userCond, $this->getNamespaceCond() );
793
794 // Paranoia: avoid brute force searches (bug 17342)
795 if ( !$user->isAllowed( 'deletedhistory' ) ) {
796 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
797 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
798 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
799 ' != ' . Revision::SUPPRESSED_USER;
800 }
801
802 # Don't include orphaned revisions
803 $join_cond['page'] = Revision::pageJoinCond();
804 # Get the current user name for accounts
805 $join_cond['user'] = Revision::userJoinCond();
806
807 $options = array();
808 if ( $index ) {
809 $options['USE INDEX'] = array( 'revision' => $index );
810 }
811
812 $queryInfo = array(
813 'tables' => $tables,
814 'fields' => array_merge(
815 Revision::selectFields(),
816 Revision::selectUserFields(),
817 array( 'page_namespace', 'page_title', 'page_is_new',
818 'page_latest', 'page_is_redirect', 'page_len' )
819 ),
820 'conds' => $conds,
821 'options' => $options,
822 'join_conds' => $join_cond
823 );
824
825 ChangeTags::modifyDisplayQuery(
826 $queryInfo['tables'],
827 $queryInfo['fields'],
828 $queryInfo['conds'],
829 $queryInfo['join_conds'],
830 $queryInfo['options'],
831 $this->tagFilter
832 );
833
834 Hooks::run( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
835
836 return $queryInfo;
837 }
838
839 function getUserCond() {
840 $condition = array();
841 $join_conds = array();
842 $tables = array( 'revision', 'page', 'user' );
843 $index = false;
844 if ( $this->contribs == 'newbie' ) {
845 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
846 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
847 # ignore local groups with the bot right
848 # @todo FIXME: Global groups may have 'bot' rights
849 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
850 if ( count( $groupsWithBotPermission ) ) {
851 $tables[] = 'user_groups';
852 $condition[] = 'ug_group IS NULL';
853 $join_conds['user_groups'] = array(
854 'LEFT JOIN', array(
855 'ug_user = rev_user',
856 'ug_group' => $groupsWithBotPermission
857 )
858 );
859 }
860 } else {
861 $uid = User::idFromName( $this->target );
862 if ( $uid ) {
863 $condition['rev_user'] = $uid;
864 $index = 'user_timestamp';
865 } else {
866 $condition['rev_user_text'] = $this->target;
867 $index = 'usertext_timestamp';
868 }
869 }
870
871 if ( $this->deletedOnly ) {
872 $condition[] = 'rev_deleted != 0';
873 }
874
875 if ( $this->topOnly ) {
876 $condition[] = 'rev_id = page_latest';
877 }
878
879 if ( $this->newOnly ) {
880 $condition[] = 'rev_parent_id = 0';
881 }
882
883 return array( $tables, $index, $condition, $join_conds );
884 }
885
886 function getNamespaceCond() {
887 if ( $this->namespace !== '' ) {
888 $selectedNS = $this->mDb->addQuotes( $this->namespace );
889 $eq_op = $this->nsInvert ? '!=' : '=';
890 $bool_op = $this->nsInvert ? 'AND' : 'OR';
891
892 if ( !$this->associated ) {
893 return array( "page_namespace $eq_op $selectedNS" );
894 }
895
896 $associatedNS = $this->mDb->addQuotes(
897 MWNamespace::getAssociated( $this->namespace )
898 );
899
900 return array(
901 "page_namespace $eq_op $selectedNS " .
902 $bool_op .
903 " page_namespace $eq_op $associatedNS"
904 );
905 }
906
907 return array();
908 }
909
910 function getIndexField() {
911 return 'rev_timestamp';
912 }
913
914 function doBatchLookups() {
915 # Do a link batch query
916 $this->mResult->seek( 0 );
917 $revIds = array();
918 $batch = new LinkBatch();
919 # Give some pointers to make (last) links
920 foreach ( $this->mResult as $row ) {
921 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
922 $revIds[] = $row->rev_parent_id;
923 }
924 if ( isset( $row->rev_id ) ) {
925 if ( $this->contribs === 'newbie' ) { // multiple users
926 $batch->add( NS_USER, $row->user_name );
927 $batch->add( NS_USER_TALK, $row->user_name );
928 }
929 $batch->add( $row->page_namespace, $row->page_title );
930 }
931 }
932 $this->mParentLens = Revision::getParentLengths( $this->mDbSecondary, $revIds );
933 $batch->execute();
934 $this->mResult->seek( 0 );
935 }
936
937 /**
938 * @return string
939 */
940 function getStartBody() {
941 return "<ul class=\"mw-contributions-list\">\n";
942 }
943
944 /**
945 * @return string
946 */
947 function getEndBody() {
948 return "</ul>\n";
949 }
950
951 /**
952 * Generates each row in the contributions list.
953 *
954 * Contributions which are marked "top" are currently on top of the history.
955 * For these contributions, a [rollback] link is shown for users with roll-
956 * back privileges. The rollback link restores the most recent version that
957 * was not written by the target user.
958 *
959 * @todo This would probably look a lot nicer in a table.
960 * @param object $row
961 * @return string
962 */
963 function formatRow( $row ) {
964
965 $ret = '';
966 $classes = array();
967
968 /*
969 * There may be more than just revision rows. To make sure that we'll only be processing
970 * revisions here, let's _try_ to build a revision out of our row (without displaying
971 * notices though) and then trying to grab data from the built object. If we succeed,
972 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
973 * to extensions to subscribe to the hook to parse the row.
974 */
975 MediaWiki\suppressWarnings();
976 try {
977 $rev = new Revision( $row );
978 $validRevision = (bool)$rev->getId();
979 } catch ( Exception $e ) {
980 $validRevision = false;
981 }
982 MediaWiki\restoreWarnings();
983
984 if ( $validRevision ) {
985 $classes = array();
986
987 $page = Title::newFromRow( $row );
988 $link = Linker::link(
989 $page,
990 htmlspecialchars( $page->getPrefixedText() ),
991 array( 'class' => 'mw-contributions-title' ),
992 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
993 );
994 # Mark current revisions
995 $topmarktext = '';
996 $user = $this->getUser();
997 if ( $row->rev_id == $row->page_latest ) {
998 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
999 # Add rollback link
1000 if ( !$row->page_is_new && $page->quickUserCan( 'rollback', $user )
1001 && $page->quickUserCan( 'edit', $user )
1002 ) {
1003 $this->preventClickjacking();
1004 $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
1005 }
1006 }
1007 # Is there a visible previous revision?
1008 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
1009 $difftext = Linker::linkKnown(
1010 $page,
1011 $this->messages['diff'],
1012 array(),
1013 array(
1014 'diff' => 'prev',
1015 'oldid' => $row->rev_id
1016 )
1017 );
1018 } else {
1019 $difftext = $this->messages['diff'];
1020 }
1021 $histlink = Linker::linkKnown(
1022 $page,
1023 $this->messages['hist'],
1024 array(),
1025 array( 'action' => 'history' )
1026 );
1027
1028 if ( $row->rev_parent_id === null ) {
1029 // For some reason rev_parent_id isn't populated for this row.
1030 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
1031 // Next best thing is to have the total number of bytes.
1032 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1033 $chardiff .= Linker::formatRevisionSize( $row->rev_len );
1034 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1035 } else {
1036 $parentLen = 0;
1037 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
1038 $parentLen = $this->mParentLens[$row->rev_parent_id];
1039 }
1040
1041 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1042 $chardiff .= ChangesList::showCharacterDifference(
1043 $parentLen,
1044 $row->rev_len,
1045 $this->getContext()
1046 );
1047 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1048 }
1049
1050 $lang = $this->getLanguage();
1051 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
1052 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
1053 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1054 $d = Linker::linkKnown(
1055 $page,
1056 htmlspecialchars( $date ),
1057 array( 'class' => 'mw-changeslist-date' ),
1058 array( 'oldid' => intval( $row->rev_id ) )
1059 );
1060 } else {
1061 $d = htmlspecialchars( $date );
1062 }
1063 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1064 $d = '<span class="history-deleted">' . $d . '</span>';
1065 }
1066
1067 # Show user names for /newbies as there may be different users.
1068 # Note that we already excluded rows with hidden user names.
1069 if ( $this->contribs == 'newbie' ) {
1070 $userlink = ' . . ' . $lang->getDirMark()
1071 . Linker::userLink( $rev->getUser(), $rev->getUserText() );
1072 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
1073 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
1074 } else {
1075 $userlink = '';
1076 }
1077
1078 if ( $rev->getParentId() === 0 ) {
1079 $nflag = ChangesList::flag( 'newpage' );
1080 } else {
1081 $nflag = '';
1082 }
1083
1084 if ( $rev->isMinor() ) {
1085 $mflag = ChangesList::flag( 'minor' );
1086 } else {
1087 $mflag = '';
1088 }
1089
1090 $del = Linker::getRevDeleteLink( $user, $rev, $page );
1091 if ( $del !== '' ) {
1092 $del .= ' ';
1093 }
1094
1095 $diffHistLinks = $this->msg( 'parentheses' )
1096 ->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )
1097 ->escaped();
1098 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} ";
1099 $ret .= "{$link}{$userlink} {$comment} {$topmarktext}";
1100
1101 # Denote if username is redacted for this edit
1102 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
1103 $ret .= " <strong>" .
1104 $this->msg( 'rev-deleted-user-contribs' )->escaped() .
1105 "</strong>";
1106 }
1107
1108 # Tags, if any.
1109 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
1110 $row->ts_tags,
1111 'contributions'
1112 );
1113 $classes = array_merge( $classes, $newClasses );
1114 $ret .= " $tagSummary";
1115 }
1116
1117 // Let extensions add data
1118 Hooks::run( 'ContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
1119
1120 if ( $classes === array() && $ret === '' ) {
1121 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
1122 $ret = "<!-- Could not format Special:Contribution row. -->\n";
1123 } else {
1124 $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
1125 }
1126
1127 return $ret;
1128 }
1129
1130 /**
1131 * Overwrite Pager function and return a helpful comment
1132 * @return string
1133 */
1134 function getSqlComment() {
1135 if ( $this->namespace || $this->deletedOnly ) {
1136 // potentially slow, see CR r58153
1137 return 'contributions page filtered for namespace or RevisionDeleted edits';
1138 } else {
1139 return 'contributions page unfiltered';
1140 }
1141 }
1142
1143 protected function preventClickjacking() {
1144 $this->preventClickjacking = true;
1145 }
1146
1147 /**
1148 * @return bool
1149 */
1150 public function getPreventClickjacking() {
1151 return $this->preventClickjacking;
1152 }
1153 }