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