Merge "Add some casts for sanity before BagOStuff::add() calls"
[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
42 $this->opts = array();
43 $request = $this->getRequest();
44
45 if ( $par !== null ) {
46 $target = $par;
47 } else {
48 $target = $request->getVal( 'target' );
49 }
50
51 // check for radiobox
52 if ( $request->getVal( 'contribs' ) == 'newbie' ) {
53 $target = 'newbies';
54 $this->opts['contribs'] = 'newbie';
55 } elseif ( $par === 'newbies' ) { // b/c for WMF
56 $target = 'newbies';
57 $this->opts['contribs'] = 'newbie';
58 } else {
59 $this->opts['contribs'] = 'user';
60 }
61
62 $this->opts['deletedOnly'] = $request->getBool( 'deletedOnly' );
63
64 if ( !strlen( $target ) ) {
65 if ( !$this->including() ) {
66 $out->addHTML( $this->getForm() );
67 }
68
69 return;
70 }
71
72 $user = $this->getUser();
73
74 $this->opts['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
75 $this->opts['target'] = $target;
76 $this->opts['topOnly'] = $request->getBool( 'topOnly' );
77 $this->opts['newOnly'] = $request->getBool( 'newOnly' );
78
79 $nt = Title::makeTitleSafe( NS_USER, $target );
80 if ( !$nt ) {
81 $out->addHTML( $this->getForm() );
82
83 return;
84 }
85 $userObj = User::newFromName( $nt->getText(), false );
86 if ( !$userObj ) {
87 $out->addHTML( $this->getForm() );
88
89 return;
90 }
91 $id = $userObj->getID();
92
93 if ( $this->opts['contribs'] != 'newbie' ) {
94 $target = $nt->getText();
95 $out->addSubtitle( $this->contributionsSub( $userObj ) );
96 $out->setHTMLTitle( $this->msg(
97 'pagetitle',
98 $this->msg( 'contributions-title', $target )->plain()
99 )->inContentLanguage() );
100 $this->getSkin()->setRelevantUser( $userObj );
101 } else {
102 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
103 $out->setHTMLTitle( $this->msg(
104 'pagetitle',
105 $this->msg( 'sp-contributions-newbies-title' )->plain()
106 )->inContentLanguage() );
107 }
108
109 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
110 $this->opts['namespace'] = intval( $ns );
111 } else {
112 $this->opts['namespace'] = '';
113 }
114
115 $this->opts['associated'] = $request->getBool( 'associated' );
116 $this->opts['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
117 $this->opts['tagfilter'] = (string)$request->getVal( 'tagfilter' );
118
119 // Allows reverts to have the bot flag in recent changes. It is just here to
120 // be passed in the form at the top of the page
121 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
122 $this->opts['bot'] = '1';
123 }
124
125 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
126 # Offset overrides year/month selection
127 if ( $skip ) {
128 $this->opts['year'] = '';
129 $this->opts['month'] = '';
130 } else {
131 $this->opts['year'] = $request->getIntOrNull( 'year' );
132 $this->opts['month'] = $request->getIntOrNull( 'month' );
133 }
134
135 $feedType = $request->getVal( 'feed' );
136
137 $feedParams = array(
138 'action' => 'feedcontributions',
139 'user' => $target,
140 );
141 if ( $this->opts['topOnly'] ) {
142 $feedParams['toponly'] = true;
143 }
144 if ( $this->opts['newOnly'] ) {
145 $feedParams['newonly'] = true;
146 }
147 if ( $this->opts['deletedOnly'] ) {
148 $feedParams['deletedonly'] = true;
149 }
150 if ( $this->opts['tagfilter'] !== '' ) {
151 $feedParams['tagfilter'] = $this->opts['tagfilter'];
152 }
153 if ( $this->opts['namespace'] !== '' ) {
154 $feedParams['namespace'] = $this->opts['namespace'];
155 }
156 // Don't use year and month for the feed URL, but pass them on if
157 // we redirect to API (if $feedType is specified)
158 if ( $feedType && $this->opts['year'] !== null ) {
159 $feedParams['year'] = $this->opts['year'];
160 }
161 if ( $feedType && $this->opts['month'] !== null ) {
162 $feedParams['month'] = $this->opts['month'];
163 }
164
165 if ( $feedType ) {
166 // Maintain some level of backwards compatability
167 // If people request feeds using the old parameters, redirect to API
168 $feedParams['feedformat'] = $feedType;
169 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
170
171 $out->redirect( $url, '301' );
172
173 return;
174 }
175
176 // Add RSS/atom links
177 $this->addFeedLinks( $feedParams );
178
179 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id, $userObj, $this ) ) ) {
180 if ( !$this->including() ) {
181 $out->addHTML( $this->getForm() );
182 }
183 $pager = new ContribsPager( $this->getContext(), array(
184 'target' => $target,
185 'contribs' => $this->opts['contribs'],
186 'namespace' => $this->opts['namespace'],
187 'tagfilter' => $this->opts['tagfilter'],
188 'year' => $this->opts['year'],
189 'month' => $this->opts['month'],
190 'deletedOnly' => $this->opts['deletedOnly'],
191 'topOnly' => $this->opts['topOnly'],
192 'newOnly' => $this->opts['newOnly'],
193 'nsInvert' => $this->opts['nsInvert'],
194 'associated' => $this->opts['associated'],
195 ) );
196
197 if ( !$pager->getNumRows() ) {
198 $out->addWikiMsg( 'nocontribs', $target );
199 } else {
200 # Show a message about slave lag, if applicable
201 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
202 if ( $lag > 0 ) {
203 $out->showLagWarning( $lag );
204 }
205
206 $output = $pager->getBody();
207 if ( !$this->including() ) {
208 $output = '<p>' . $pager->getNavigationBar() . '</p>' .
209 $output.
210 '<p>' . $pager->getNavigationBar() . '</p>';
211 }
212 $out->addHTML( $output );
213 }
214 $out->preventClickjacking( $pager->getPreventClickjacking() );
215
216 # Show the appropriate "footer" message - WHOIS tools, etc.
217 if ( $this->opts['contribs'] == 'newbie' ) {
218 $message = 'sp-contributions-footer-newbies';
219 } elseif ( IP::isIPAddress( $target ) ) {
220 $message = 'sp-contributions-footer-anon';
221 } elseif ( $userObj->isAnon() ) {
222 // No message for non-existing users
223 $message = '';
224 } else {
225 $message = 'sp-contributions-footer';
226 }
227
228 if ( $message ) {
229 if ( !$this->including() ) {
230 if ( !$this->msg( $message, $target )->isDisabled() ) {
231 $out->wrapWikiMsg(
232 "<div class='mw-contributions-footer'>\n$1\n</div>",
233 array( $message, $target ) );
234 }
235 }
236 }
237 }
238 }
239
240 /**
241 * Generates the subheading with links
242 * @param User $userObj User object for the target
243 * @return string Appropriately-escaped HTML to be output literally
244 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
245 * Could be combined.
246 */
247 protected function contributionsSub( $userObj ) {
248 if ( $userObj->isAnon() ) {
249 // Show a warning message that the user being searched for doesn't exists
250 if ( !User::isIP( $userObj->getName() ) ) {
251 $this->getOutput()->wrapWikiMsg(
252 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
253 array(
254 'contributions-userdoesnotexist',
255 wfEscapeWikiText( $userObj->getName() ),
256 )
257 );
258 }
259 $user = htmlspecialchars( $userObj->getName() );
260 } else {
261 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
262 }
263 $nt = $userObj->getUserPage();
264 $talk = $userObj->getTalkPage();
265 $links = '';
266 if ( $talk ) {
267 $tools = $this->getUserLinks( $nt, $talk, $userObj );
268 $links = $this->getLanguage()->pipeList( $tools );
269
270 // Show a note if the user is blocked and display the last block log entry.
271 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
272 // and also this will display a totally irrelevant log entry as a current block.
273 $block = Block::newFromTarget( $userObj, $userObj );
274 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
275 if ( $block->getType() == Block::TYPE_RANGE ) {
276 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
277 }
278
279 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
280 LogEventsList::showLogExtract(
281 $out,
282 'block',
283 $nt,
284 '',
285 array(
286 'lim' => 1,
287 'showIfEmpty' => false,
288 'msgKey' => array(
289 $userObj->isAnon() ?
290 'sp-contributions-blocked-notice-anon' :
291 'sp-contributions-blocked-notice',
292 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
293 ),
294 'offset' => '' # don't use WebRequest parameter offset
295 )
296 );
297 }
298 }
299
300 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
301 }
302
303 /**
304 * Links to different places.
305 * @param Title $userpage Target user page
306 * @param Title $talkpage Talk page
307 * @param User $target Target user object
308 * @return array
309 */
310 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
311
312 $id = $target->getId();
313 $username = $target->getName();
314
315 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
316
317 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
318 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
319 if ( $target->isBlocked() && $target->getBlock()->getType() != Block::TYPE_AUTO ) {
320 $tools[] = Linker::linkKnown( # Change block link
321 SpecialPage::getTitleFor( 'Block', $username ),
322 $this->msg( 'change-blocklink' )->escaped()
323 );
324 $tools[] = Linker::linkKnown( # Unblock link
325 SpecialPage::getTitleFor( 'Unblock', $username ),
326 $this->msg( 'unblocklink' )->escaped()
327 );
328 } else { # User is not blocked
329 $tools[] = Linker::linkKnown( # Block link
330 SpecialPage::getTitleFor( 'Block', $username ),
331 $this->msg( 'blocklink' )->escaped()
332 );
333 }
334 }
335
336 # Block log link
337 $tools[] = Linker::linkKnown(
338 SpecialPage::getTitleFor( 'Log', 'block' ),
339 $this->msg( 'sp-contributions-blocklog' )->escaped(),
340 array(),
341 array( 'page' => $userpage->getPrefixedText() )
342 );
343
344 # Suppression log link (bug 59120)
345 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
346 $tools[] = Linker::linkKnown(
347 SpecialPage::getTitleFor( 'Log', 'suppress' ),
348 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
349 array(),
350 array( 'offender' => $username )
351 );
352 }
353 }
354 # Uploads
355 $tools[] = Linker::linkKnown(
356 SpecialPage::getTitleFor( 'Listfiles', $username ),
357 $this->msg( 'sp-contributions-uploads' )->escaped()
358 );
359
360 # Other logs link
361 $tools[] = Linker::linkKnown(
362 SpecialPage::getTitleFor( 'Log', $username ),
363 $this->msg( 'sp-contributions-logs' )->escaped()
364 );
365
366 # Add link to deleted user contributions for priviledged users
367 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
368 $tools[] = Linker::linkKnown(
369 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
370 $this->msg( 'sp-contributions-deleted' )->escaped()
371 );
372 }
373
374 # Add a link to change user rights for privileged users
375 $userrightsPage = new UserrightsPage();
376 $userrightsPage->setContext( $this->getContext() );
377 if ( $userrightsPage->userCanChangeRights( $target ) ) {
378 $tools[] = Linker::linkKnown(
379 SpecialPage::getTitleFor( 'Userrights', $username ),
380 $this->msg( 'sp-contributions-userrights' )->escaped()
381 );
382 }
383
384 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
385
386 return $tools;
387 }
388
389 /**
390 * Generates the namespace selector form with hidden attributes.
391 * @return string HTML fragment
392 */
393 protected function getForm() {
394 global $wgScript;
395
396 $this->opts['title'] = $this->getPageTitle()->getPrefixedText();
397 if ( !isset( $this->opts['target'] ) ) {
398 $this->opts['target'] = '';
399 } else {
400 $this->opts['target'] = str_replace( '_', ' ', $this->opts['target'] );
401 }
402
403 if ( !isset( $this->opts['namespace'] ) ) {
404 $this->opts['namespace'] = '';
405 }
406
407 if ( !isset( $this->opts['nsInvert'] ) ) {
408 $this->opts['nsInvert'] = '';
409 }
410
411 if ( !isset( $this->opts['associated'] ) ) {
412 $this->opts['associated'] = false;
413 }
414
415 if ( !isset( $this->opts['contribs'] ) ) {
416 $this->opts['contribs'] = 'user';
417 }
418
419 if ( !isset( $this->opts['year'] ) ) {
420 $this->opts['year'] = '';
421 }
422
423 if ( !isset( $this->opts['month'] ) ) {
424 $this->opts['month'] = '';
425 }
426
427 if ( $this->opts['contribs'] == 'newbie' ) {
428 $this->opts['target'] = '';
429 }
430
431 if ( !isset( $this->opts['tagfilter'] ) ) {
432 $this->opts['tagfilter'] = '';
433 }
434
435 if ( !isset( $this->opts['topOnly'] ) ) {
436 $this->opts['topOnly'] = false;
437 }
438
439 if ( !isset( $this->opts['newOnly'] ) ) {
440 $this->opts['newOnly'] = false;
441 }
442
443 $form = Html::openElement(
444 'form',
445 array(
446 'method' => 'get',
447 'action' => $wgScript,
448 'class' => 'mw-contributions-form'
449 )
450 );
451
452 # Add hidden params for tracking except for parameters in $skipParameters
453 $skipParameters = array(
454 'namespace',
455 'nsInvert',
456 'deletedOnly',
457 'target',
458 'contribs',
459 'year',
460 'month',
461 'topOnly',
462 'newOnly',
463 'associated'
464 );
465
466 foreach ( $this->opts as $name => $value ) {
467 if ( in_array( $name, $skipParameters ) ) {
468 continue;
469 }
470 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
471 }
472
473 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
474
475 if ( $tagFilter ) {
476 $filterSelection = Html::rawElement(
477 'td',
478 array( 'class' => 'mw-label' ),
479 array_shift( $tagFilter )
480 );
481 $filterSelection .= Html::rawElement(
482 'td',
483 array( 'class' => 'mw-input' ),
484 implode( '&#160', $tagFilter )
485 );
486 } else {
487 $filterSelection = Html::rawElement( 'td', array( 'colspan' => 2 ), '' );
488 }
489
490 $labelNewbies = Xml::radioLabel(
491 $this->msg( 'sp-contributions-newbies' )->text(),
492 'contribs',
493 'newbie',
494 'newbie',
495 $this->opts['contribs'] == 'newbie',
496 array( 'class' => 'mw-input' )
497 );
498 $labelUsername = Xml::radioLabel(
499 $this->msg( 'sp-contributions-username' )->text(),
500 'contribs',
501 'user',
502 'user',
503 $this->opts['contribs'] == 'user',
504 array( 'class' => 'mw-input' )
505 );
506 $input = Html::input(
507 'target',
508 $this->opts['target'],
509 'text',
510 array( 'size' => '40', 'required' => '', 'class' => 'mw-input' ) +
511 ( $this->opts['target'] ? array() : array( 'autofocus' )
512 )
513 );
514 $targetSelection = Html::rawElement(
515 'td',
516 array( 'colspan' => 2 ),
517 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
518 );
519
520 $namespaceSelection = Xml::tags(
521 'td',
522 array( 'class' => 'mw-label' ),
523 Xml::label(
524 $this->msg( 'namespace' )->text(),
525 'namespace',
526 ''
527 )
528 );
529 $namespaceSelection .= Html::rawElement(
530 'td',
531 null,
532 Html::namespaceSelector(
533 array( 'selected' => $this->opts['namespace'], 'all' => '' ),
534 array(
535 'name' => 'namespace',
536 'id' => 'namespace',
537 'class' => 'namespaceselector',
538 )
539 ) . '&#160;' .
540 Html::rawElement(
541 'span',
542 array( 'style' => 'white-space: nowrap' ),
543 Xml::checkLabel(
544 $this->msg( 'invert' )->text(),
545 'nsInvert',
546 'nsInvert',
547 $this->opts['nsInvert'],
548 array(
549 'title' => $this->msg( 'tooltip-invert' )->text(),
550 'class' => 'mw-input'
551 )
552 ) . '&#160;'
553 ) .
554 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
555 Xml::checkLabel(
556 $this->msg( 'namespace_association' )->text(),
557 'associated',
558 'associated',
559 $this->opts['associated'],
560 array(
561 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
562 'class' => 'mw-input'
563 )
564 ) . '&#160;'
565 )
566 );
567
568 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
569 $deletedOnlyCheck = Html::rawElement(
570 'span',
571 array( 'style' => 'white-space: nowrap' ),
572 Xml::checkLabel(
573 $this->msg( 'history-show-deleted' )->text(),
574 'deletedOnly',
575 'mw-show-deleted-only',
576 $this->opts['deletedOnly'],
577 array( 'class' => 'mw-input' )
578 )
579 );
580 } else {
581 $deletedOnlyCheck = '';
582 }
583
584 $checkLabelTopOnly = Html::rawElement(
585 'span',
586 array( 'style' => 'white-space: nowrap' ),
587 Xml::checkLabel(
588 $this->msg( 'sp-contributions-toponly' )->text(),
589 'topOnly',
590 'mw-show-top-only',
591 $this->opts['topOnly'],
592 array( 'class' => 'mw-input' )
593 )
594 );
595 $checkLabelNewOnly = Html::rawElement(
596 'span',
597 array( 'style' => 'white-space: nowrap' ),
598 Xml::checkLabel(
599 $this->msg( 'sp-contributions-newonly' )->text(),
600 'newOnly',
601 'mw-show-new-only',
602 $this->opts['newOnly'],
603 array( 'class' => 'mw-input' )
604 )
605 );
606 $extraOptions = Html::rawElement(
607 'td',
608 array( 'colspan' => 2 ),
609 $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly
610 );
611
612 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
613 Xml::dateMenu(
614 $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
615 $this->opts['month']
616 ) . ' ' .
617 Xml::submitButton(
618 $this->msg( 'sp-contributions-submit' )->text(),
619 array( 'class' => 'mw-submit' )
620 )
621 );
622
623 $form .= Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() );
624 $form .= Html::rawElement( 'table', array( 'class' => 'mw-contributions-table' ), "\n" .
625 Html::rawElement( 'tr', array(), $targetSelection ) . "\n" .
626 Html::rawElement( 'tr', array(), $namespaceSelection ) . "\n" .
627 Html::rawElement( 'tr', array(), $filterSelection ) . "\n" .
628 Html::rawElement( 'tr', array(), $extraOptions ) . "\n" .
629 Html::rawElement( 'tr', array(), $dateSelectionAndSubmit ) . "\n"
630 );
631
632 $explain = $this->msg( 'sp-contributions-explain' );
633 if ( !$explain->isBlank() ) {
634 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
635 }
636
637 $form .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
638
639 return $form;
640 }
641
642 protected function getGroupName() {
643 return 'users';
644 }
645 }
646
647 /**
648 * Pager for Special:Contributions
649 * @ingroup SpecialPage Pager
650 */
651 class ContribsPager extends ReverseChronologicalPager {
652 public $mDefaultDirection = true;
653 public $messages;
654 public $target;
655 public $namespace = '';
656 public $mDb;
657 public $preventClickjacking = false;
658
659 /** @var DatabaseBase */
660 public $mDbSecondary;
661
662 /**
663 * @var array
664 */
665 protected $mParentLens;
666
667 function __construct( IContextSource $context, array $options ) {
668 parent::__construct( $context );
669
670 $msgs = array(
671 'diff',
672 'hist',
673 'newarticle',
674 'pipe-separator',
675 'rev-delundel',
676 'rollbacklink',
677 'uctop'
678 );
679
680 foreach ( $msgs as $msg ) {
681 $this->messages[$msg] = $this->msg( $msg )->escaped();
682 }
683
684 $this->target = isset( $options['target'] ) ? $options['target'] : '';
685 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
686 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
687 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
688 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
689 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
690
691 $this->deletedOnly = !empty( $options['deletedOnly'] );
692 $this->topOnly = !empty( $options['topOnly'] );
693 $this->newOnly = !empty( $options['newOnly'] );
694
695 $year = isset( $options['year'] ) ? $options['year'] : false;
696 $month = isset( $options['month'] ) ? $options['month'] : false;
697 $this->getDateCond( $year, $month );
698
699 // Most of this code will use the 'contributions' group DB, which can map to slaves
700 // with extra user based indexes or partioning by user. The additional metadata
701 // queries should use a regular slave since the lookup pattern is not all by user.
702 $this->mDbSecondary = wfGetDB( DB_SLAVE ); // any random slave
703 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
704 }
705
706 function getDefaultQuery() {
707 $query = parent::getDefaultQuery();
708 $query['target'] = $this->target;
709
710 return $query;
711 }
712
713 /**
714 * This method basically executes the exact same code as the parent class, though with
715 * a hook added, to allow extentions to add additional queries.
716 *
717 * @param string $offset Index offset, inclusive
718 * @param int $limit Exact query limit
719 * @param bool $descending Query direction, false for ascending, true for descending
720 * @return ResultWrapper
721 */
722 function reallyDoQuery( $offset, $limit, $descending ) {
723 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
724 $offset,
725 $limit,
726 $descending
727 );
728 $pager = $this;
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 wfRunHooks(
752 'ContribsPager::reallyDoQuery',
753 array( &$data, $pager, $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->isAllowed( 'suppressrevision' ) ) {
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 wfRunHooks( '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>\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 wfProfileIn( __METHOD__ );
959
960 $ret = '';
961 $classes = array();
962
963 /*
964 * There may be more than just revision rows. To make sure that we'll only be processing
965 * revisions here, let's _try_ to build a revision out of our row (without displaying
966 * notices though) and then trying to grab data from the built object. If we succeed,
967 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
968 * to extensions to subscribe to the hook to parse the row.
969 */
970 wfSuppressWarnings();
971 try {
972 $rev = new Revision( $row );
973 $validRevision = (bool)$rev->getId();
974 } catch ( MWException $e ) {
975 $validRevision = false;
976 }
977 wfRestoreWarnings();
978
979 if ( $validRevision ) {
980 $classes = array();
981
982 $page = Title::newFromRow( $row );
983 $link = Linker::link(
984 $page,
985 htmlspecialchars( $page->getPrefixedText() ),
986 array( 'class' => 'mw-contributions-title' ),
987 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
988 );
989 # Mark current revisions
990 $topmarktext = '';
991 $user = $this->getUser();
992 if ( $row->rev_id == $row->page_latest ) {
993 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
994 # Add rollback link
995 if ( !$row->page_is_new && $page->quickUserCan( 'rollback', $user )
996 && $page->quickUserCan( 'edit', $user )
997 ) {
998 $this->preventClickjacking();
999 $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
1000 }
1001 }
1002 # Is there a visible previous revision?
1003 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
1004 $difftext = Linker::linkKnown(
1005 $page,
1006 $this->messages['diff'],
1007 array(),
1008 array(
1009 'diff' => 'prev',
1010 'oldid' => $row->rev_id
1011 )
1012 );
1013 } else {
1014 $difftext = $this->messages['diff'];
1015 }
1016 $histlink = Linker::linkKnown(
1017 $page,
1018 $this->messages['hist'],
1019 array(),
1020 array( 'action' => 'history' )
1021 );
1022
1023 if ( $row->rev_parent_id === null ) {
1024 // For some reason rev_parent_id isn't populated for this row.
1025 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
1026 // Next best thing is to have the total number of bytes.
1027 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1028 $chardiff .= Linker::formatRevisionSize( $row->rev_len );
1029 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1030 } else {
1031 $parentLen = 0;
1032 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
1033 $parentLen = $this->mParentLens[$row->rev_parent_id];
1034 }
1035
1036 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1037 $chardiff .= ChangesList::showCharacterDifference(
1038 $parentLen,
1039 $row->rev_len,
1040 $this->getContext()
1041 );
1042 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1043 }
1044
1045 $lang = $this->getLanguage();
1046 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
1047 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
1048 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1049 $d = Linker::linkKnown(
1050 $page,
1051 htmlspecialchars( $date ),
1052 array( 'class' => 'mw-changeslist-date' ),
1053 array( 'oldid' => intval( $row->rev_id ) )
1054 );
1055 } else {
1056 $d = htmlspecialchars( $date );
1057 }
1058 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1059 $d = '<span class="history-deleted">' . $d . '</span>';
1060 }
1061
1062 # Show user names for /newbies as there may be different users.
1063 # Note that we already excluded rows with hidden user names.
1064 if ( $this->contribs == 'newbie' ) {
1065 $userlink = ' . . ' . $lang->getDirMark()
1066 . Linker::userLink( $rev->getUser(), $rev->getUserText() );
1067 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
1068 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
1069 } else {
1070 $userlink = '';
1071 }
1072
1073 if ( $rev->getParentId() === 0 ) {
1074 $nflag = ChangesList::flag( 'newpage' );
1075 } else {
1076 $nflag = '';
1077 }
1078
1079 if ( $rev->isMinor() ) {
1080 $mflag = ChangesList::flag( 'minor' );
1081 } else {
1082 $mflag = '';
1083 }
1084
1085 $del = Linker::getRevDeleteLink( $user, $rev, $page );
1086 if ( $del !== '' ) {
1087 $del .= ' ';
1088 }
1089
1090 $diffHistLinks = $this->msg( 'parentheses' )
1091 ->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )
1092 ->escaped();
1093 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} ";
1094 $ret .= "{$link}{$userlink} {$comment} {$topmarktext}";
1095
1096 # Denote if username is redacted for this edit
1097 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
1098 $ret .= " <strong>" .
1099 $this->msg( 'rev-deleted-user-contribs' )->escaped() .
1100 "</strong>";
1101 }
1102
1103 # Tags, if any.
1104 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
1105 $row->ts_tags,
1106 'contributions'
1107 );
1108 $classes = array_merge( $classes, $newClasses );
1109 $ret .= " $tagSummary";
1110 }
1111
1112 // Let extensions add data
1113 wfRunHooks( 'ContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
1114
1115 if ( $classes === array() && $ret === '' ) {
1116 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
1117 $ret = "<!-- Could not format Special:Contribution row. -->\n";
1118 } else {
1119 $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
1120 }
1121
1122 wfProfileOut( __METHOD__ );
1123
1124 return $ret;
1125 }
1126
1127 /**
1128 * Overwrite Pager function and return a helpful comment
1129 * @return string
1130 */
1131 function getSqlComment() {
1132 if ( $this->namespace || $this->deletedOnly ) {
1133 // potentially slow, see CR r58153
1134 return 'contributions page filtered for namespace or RevisionDeleted edits';
1135 } else {
1136 return 'contributions page unfiltered';
1137 }
1138 }
1139
1140 protected function preventClickjacking() {
1141 $this->preventClickjacking = true;
1142 }
1143
1144 /**
1145 * @return bool
1146 */
1147 public function getPreventClickjacking() {
1148 return $this->preventClickjacking;
1149 }
1150 }