Use camel case for variable names in Article.php
[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 if ( !$this->including() ) {
259 $this->getOutput()->setStatusCode( 404 );
260 }
261 }
262 $user = htmlspecialchars( $userObj->getName() );
263 } else {
264 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
265 }
266 $nt = $userObj->getUserPage();
267 $talk = $userObj->getTalkPage();
268 $links = '';
269 if ( $talk ) {
270 $tools = $this->getUserLinks( $nt, $talk, $userObj );
271 $links = $this->getLanguage()->pipeList( $tools );
272
273 // Show a note if the user is blocked and display the last block log entry.
274 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
275 // and also this will display a totally irrelevant log entry as a current block.
276 if ( !$this->including() ) {
277 $block = Block::newFromTarget( $userObj, $userObj );
278 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
279 if ( $block->getType() == Block::TYPE_RANGE ) {
280 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
281 }
282
283 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
284 LogEventsList::showLogExtract(
285 $out,
286 'block',
287 $nt,
288 '',
289 array(
290 'lim' => 1,
291 'showIfEmpty' => false,
292 'msgKey' => array(
293 $userObj->isAnon() ?
294 'sp-contributions-blocked-notice-anon' :
295 'sp-contributions-blocked-notice',
296 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
297 ),
298 'offset' => '' # don't use WebRequest parameter offset
299 )
300 );
301 }
302 }
303 }
304
305 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
306 }
307
308 /**
309 * Links to different places.
310 * @param Title $userpage Target user page
311 * @param Title $talkpage Talk page
312 * @param User $target Target user object
313 * @return array
314 */
315 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
316
317 $id = $target->getId();
318 $username = $target->getName();
319
320 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
321
322 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
323 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
324 if ( $target->isBlocked() && $target->getBlock()->getType() != Block::TYPE_AUTO ) {
325 $tools[] = Linker::linkKnown( # Change block link
326 SpecialPage::getTitleFor( 'Block', $username ),
327 $this->msg( 'change-blocklink' )->escaped()
328 );
329 $tools[] = Linker::linkKnown( # Unblock link
330 SpecialPage::getTitleFor( 'Unblock', $username ),
331 $this->msg( 'unblocklink' )->escaped()
332 );
333 } else { # User is not blocked
334 $tools[] = Linker::linkKnown( # Block link
335 SpecialPage::getTitleFor( 'Block', $username ),
336 $this->msg( 'blocklink' )->escaped()
337 );
338 }
339 }
340
341 # Block log link
342 $tools[] = Linker::linkKnown(
343 SpecialPage::getTitleFor( 'Log', 'block' ),
344 $this->msg( 'sp-contributions-blocklog' )->escaped(),
345 array(),
346 array( 'page' => $userpage->getPrefixedText() )
347 );
348
349 # Suppression log link (bug 59120)
350 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
351 $tools[] = Linker::linkKnown(
352 SpecialPage::getTitleFor( 'Log', 'suppress' ),
353 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
354 array(),
355 array( 'offender' => $username )
356 );
357 }
358 }
359 # Uploads
360 $tools[] = Linker::linkKnown(
361 SpecialPage::getTitleFor( 'Listfiles', $username ),
362 $this->msg( 'sp-contributions-uploads' )->escaped()
363 );
364
365 # Other logs link
366 $tools[] = Linker::linkKnown(
367 SpecialPage::getTitleFor( 'Log', $username ),
368 $this->msg( 'sp-contributions-logs' )->escaped()
369 );
370
371 # Add link to deleted user contributions for priviledged users
372 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
373 $tools[] = Linker::linkKnown(
374 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
375 $this->msg( 'sp-contributions-deleted' )->escaped()
376 );
377 }
378
379 # Add a link to change user rights for privileged users
380 $userrightsPage = new UserrightsPage();
381 $userrightsPage->setContext( $this->getContext() );
382 if ( $userrightsPage->userCanChangeRights( $target ) ) {
383 $tools[] = Linker::linkKnown(
384 SpecialPage::getTitleFor( 'Userrights', $username ),
385 $this->msg( 'sp-contributions-userrights' )->escaped()
386 );
387 }
388
389 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
390
391 return $tools;
392 }
393
394 /**
395 * Generates the namespace selector form with hidden attributes.
396 * @return string HTML fragment
397 */
398 protected function getForm() {
399 global $wgScript;
400
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' => $wgScript,
453 'class' => 'mw-contributions-form'
454 )
455 );
456
457 # Add hidden params for tracking except for parameters in $skipParameters
458 $skipParameters = array(
459 'namespace',
460 'nsInvert',
461 'deletedOnly',
462 'target',
463 'contribs',
464 'year',
465 'month',
466 'topOnly',
467 'newOnly',
468 'associated'
469 );
470
471 foreach ( $this->opts as $name => $value ) {
472 if ( in_array( $name, $skipParameters ) ) {
473 continue;
474 }
475 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
476 }
477
478 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
479
480 if ( $tagFilter ) {
481 $filterSelection = Html::rawElement(
482 'td',
483 array( 'class' => 'mw-label' ),
484 array_shift( $tagFilter )
485 );
486 $filterSelection .= Html::rawElement(
487 'td',
488 array( 'class' => 'mw-input' ),
489 implode( '&#160', $tagFilter )
490 );
491 } else {
492 $filterSelection = Html::rawElement( 'td', array( 'colspan' => 2 ), '' );
493 }
494
495 $labelNewbies = Xml::radioLabel(
496 $this->msg( 'sp-contributions-newbies' )->text(),
497 'contribs',
498 'newbie',
499 'newbie',
500 $this->opts['contribs'] == 'newbie',
501 array( 'class' => 'mw-input' )
502 );
503 $labelUsername = Xml::radioLabel(
504 $this->msg( 'sp-contributions-username' )->text(),
505 'contribs',
506 'user',
507 'user',
508 $this->opts['contribs'] == 'user',
509 array( 'class' => 'mw-input' )
510 );
511 $input = Html::input(
512 'target',
513 $this->opts['target'],
514 'text',
515 array( 'size' => '40', 'required' => '', 'class' => 'mw-input' ) +
516 ( $this->opts['target'] ? array() : array( 'autofocus' )
517 )
518 );
519 $targetSelection = Html::rawElement(
520 'td',
521 array( 'colspan' => 2 ),
522 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
523 );
524
525 $namespaceSelection = Xml::tags(
526 'td',
527 array( 'class' => 'mw-label' ),
528 Xml::label(
529 $this->msg( 'namespace' )->text(),
530 'namespace',
531 ''
532 )
533 );
534 $namespaceSelection .= Html::rawElement(
535 'td',
536 null,
537 Html::namespaceSelector(
538 array( 'selected' => $this->opts['namespace'], 'all' => '' ),
539 array(
540 'name' => 'namespace',
541 'id' => 'namespace',
542 'class' => 'namespaceselector',
543 )
544 ) . '&#160;' .
545 Html::rawElement(
546 'span',
547 array( 'style' => 'white-space: nowrap' ),
548 Xml::checkLabel(
549 $this->msg( 'invert' )->text(),
550 'nsInvert',
551 'nsInvert',
552 $this->opts['nsInvert'],
553 array(
554 'title' => $this->msg( 'tooltip-invert' )->text(),
555 'class' => 'mw-input'
556 )
557 ) . '&#160;'
558 ) .
559 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
560 Xml::checkLabel(
561 $this->msg( 'namespace_association' )->text(),
562 'associated',
563 'associated',
564 $this->opts['associated'],
565 array(
566 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
567 'class' => 'mw-input'
568 )
569 ) . '&#160;'
570 )
571 );
572
573 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
574 $deletedOnlyCheck = Html::rawElement(
575 'span',
576 array( 'style' => 'white-space: nowrap' ),
577 Xml::checkLabel(
578 $this->msg( 'history-show-deleted' )->text(),
579 'deletedOnly',
580 'mw-show-deleted-only',
581 $this->opts['deletedOnly'],
582 array( 'class' => 'mw-input' )
583 )
584 );
585 } else {
586 $deletedOnlyCheck = '';
587 }
588
589 $checkLabelTopOnly = Html::rawElement(
590 'span',
591 array( 'style' => 'white-space: nowrap' ),
592 Xml::checkLabel(
593 $this->msg( 'sp-contributions-toponly' )->text(),
594 'topOnly',
595 'mw-show-top-only',
596 $this->opts['topOnly'],
597 array( 'class' => 'mw-input' )
598 )
599 );
600 $checkLabelNewOnly = Html::rawElement(
601 'span',
602 array( 'style' => 'white-space: nowrap' ),
603 Xml::checkLabel(
604 $this->msg( 'sp-contributions-newonly' )->text(),
605 'newOnly',
606 'mw-show-new-only',
607 $this->opts['newOnly'],
608 array( 'class' => 'mw-input' )
609 )
610 );
611 $extraOptions = Html::rawElement(
612 'td',
613 array( 'colspan' => 2 ),
614 $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly
615 );
616
617 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
618 Xml::dateMenu(
619 $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
620 $this->opts['month']
621 ) . ' ' .
622 Xml::submitButton(
623 $this->msg( 'sp-contributions-submit' )->text(),
624 array( 'class' => 'mw-submit' )
625 )
626 );
627
628 $form .= Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() );
629 $form .= Html::rawElement( 'table', array( 'class' => 'mw-contributions-table' ), "\n" .
630 Html::rawElement( 'tr', array(), $targetSelection ) . "\n" .
631 Html::rawElement( 'tr', array(), $namespaceSelection ) . "\n" .
632 Html::rawElement( 'tr', array(), $filterSelection ) . "\n" .
633 Html::rawElement( 'tr', array(), $extraOptions ) . "\n" .
634 Html::rawElement( 'tr', array(), $dateSelectionAndSubmit ) . "\n"
635 );
636
637 $explain = $this->msg( 'sp-contributions-explain' );
638 if ( !$explain->isBlank() ) {
639 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
640 }
641
642 $form .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
643
644 return $form;
645 }
646
647 protected function getGroupName() {
648 return 'users';
649 }
650 }
651
652 /**
653 * Pager for Special:Contributions
654 * @ingroup SpecialPage Pager
655 */
656 class ContribsPager extends ReverseChronologicalPager {
657 public $mDefaultDirection = true;
658 public $messages;
659 public $target;
660 public $namespace = '';
661 public $mDb;
662 public $preventClickjacking = false;
663
664 /** @var DatabaseBase */
665 public $mDbSecondary;
666
667 /**
668 * @var array
669 */
670 protected $mParentLens;
671
672 function __construct( IContextSource $context, array $options ) {
673 parent::__construct( $context );
674
675 $msgs = array(
676 'diff',
677 'hist',
678 'newarticle',
679 'pipe-separator',
680 'rev-delundel',
681 'rollbacklink',
682 'uctop'
683 );
684
685 foreach ( $msgs as $msg ) {
686 $this->messages[$msg] = $this->msg( $msg )->escaped();
687 }
688
689 $this->target = isset( $options['target'] ) ? $options['target'] : '';
690 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
691 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
692 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
693 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
694 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
695
696 $this->deletedOnly = !empty( $options['deletedOnly'] );
697 $this->topOnly = !empty( $options['topOnly'] );
698 $this->newOnly = !empty( $options['newOnly'] );
699
700 $year = isset( $options['year'] ) ? $options['year'] : false;
701 $month = isset( $options['month'] ) ? $options['month'] : false;
702 $this->getDateCond( $year, $month );
703
704 // Most of this code will use the 'contributions' group DB, which can map to slaves
705 // with extra user based indexes or partioning by user. The additional metadata
706 // queries should use a regular slave since the lookup pattern is not all by user.
707 $this->mDbSecondary = wfGetDB( DB_SLAVE ); // any random slave
708 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
709 }
710
711 function getDefaultQuery() {
712 $query = parent::getDefaultQuery();
713 $query['target'] = $this->target;
714
715 return $query;
716 }
717
718 /**
719 * This method basically executes the exact same code as the parent class, though with
720 * a hook added, to allow extentions to add additional queries.
721 *
722 * @param string $offset Index offset, inclusive
723 * @param int $limit Exact query limit
724 * @param bool $descending Query direction, false for ascending, true for descending
725 * @return ResultWrapper
726 */
727 function reallyDoQuery( $offset, $limit, $descending ) {
728 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
729 $offset,
730 $limit,
731 $descending
732 );
733 $pager = $this;
734
735 /*
736 * This hook will allow extensions to add in additional queries, so they can get their data
737 * in My Contributions as well. Extensions should append their results to the $data array.
738 *
739 * Extension queries have to implement the navbar requirement as well. They should
740 * - have a column aliased as $pager->getIndexField()
741 * - have LIMIT set
742 * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
743 * - have the ORDER BY specified based upon the details provided by the navbar
744 *
745 * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
746 *
747 * &$data: an array of results of all contribs queries
748 * $pager: the ContribsPager object hooked into
749 * $offset: see phpdoc above
750 * $limit: see phpdoc above
751 * $descending: see phpdoc above
752 */
753 $data = array( $this->mDb->select(
754 $tables, $fields, $conds, $fname, $options, $join_conds
755 ) );
756 wfRunHooks(
757 'ContribsPager::reallyDoQuery',
758 array( &$data, $pager, $offset, $limit, $descending )
759 );
760
761 $result = array();
762
763 // loop all results and collect them in an array
764 foreach ( $data as $query ) {
765 foreach ( $query as $i => $row ) {
766 // use index column as key, allowing us to easily sort in PHP
767 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
768 }
769 }
770
771 // sort results
772 if ( $descending ) {
773 ksort( $result );
774 } else {
775 krsort( $result );
776 }
777
778 // enforce limit
779 $result = array_slice( $result, 0, $limit );
780
781 // get rid of array keys
782 $result = array_values( $result );
783
784 return new FakeResultWrapper( $result );
785 }
786
787 function getQueryInfo() {
788 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
789
790 $user = $this->getUser();
791 $conds = array_merge( $userCond, $this->getNamespaceCond() );
792
793 // Paranoia: avoid brute force searches (bug 17342)
794 if ( !$user->isAllowed( 'deletedhistory' ) ) {
795 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
796 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
797 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
798 ' != ' . Revision::SUPPRESSED_USER;
799 }
800
801 # Don't include orphaned revisions
802 $join_cond['page'] = Revision::pageJoinCond();
803 # Get the current user name for accounts
804 $join_cond['user'] = Revision::userJoinCond();
805
806 $options = array();
807 if ( $index ) {
808 $options['USE INDEX'] = array( 'revision' => $index );
809 }
810
811 $queryInfo = array(
812 'tables' => $tables,
813 'fields' => array_merge(
814 Revision::selectFields(),
815 Revision::selectUserFields(),
816 array( 'page_namespace', 'page_title', 'page_is_new',
817 'page_latest', 'page_is_redirect', 'page_len' )
818 ),
819 'conds' => $conds,
820 'options' => $options,
821 'join_conds' => $join_cond
822 );
823
824 ChangeTags::modifyDisplayQuery(
825 $queryInfo['tables'],
826 $queryInfo['fields'],
827 $queryInfo['conds'],
828 $queryInfo['join_conds'],
829 $queryInfo['options'],
830 $this->tagFilter
831 );
832
833 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
834
835 return $queryInfo;
836 }
837
838 function getUserCond() {
839 $condition = array();
840 $join_conds = array();
841 $tables = array( 'revision', 'page', 'user' );
842 $index = false;
843 if ( $this->contribs == 'newbie' ) {
844 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
845 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
846 # ignore local groups with the bot right
847 # @todo FIXME: Global groups may have 'bot' rights
848 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
849 if ( count( $groupsWithBotPermission ) ) {
850 $tables[] = 'user_groups';
851 $condition[] = 'ug_group IS NULL';
852 $join_conds['user_groups'] = array(
853 'LEFT JOIN', array(
854 'ug_user = rev_user',
855 'ug_group' => $groupsWithBotPermission
856 )
857 );
858 }
859 } else {
860 $uid = User::idFromName( $this->target );
861 if ( $uid ) {
862 $condition['rev_user'] = $uid;
863 $index = 'user_timestamp';
864 } else {
865 $condition['rev_user_text'] = $this->target;
866 $index = 'usertext_timestamp';
867 }
868 }
869
870 if ( $this->deletedOnly ) {
871 $condition[] = 'rev_deleted != 0';
872 }
873
874 if ( $this->topOnly ) {
875 $condition[] = 'rev_id = page_latest';
876 }
877
878 if ( $this->newOnly ) {
879 $condition[] = 'rev_parent_id = 0';
880 }
881
882 return array( $tables, $index, $condition, $join_conds );
883 }
884
885 function getNamespaceCond() {
886 if ( $this->namespace !== '' ) {
887 $selectedNS = $this->mDb->addQuotes( $this->namespace );
888 $eq_op = $this->nsInvert ? '!=' : '=';
889 $bool_op = $this->nsInvert ? 'AND' : 'OR';
890
891 if ( !$this->associated ) {
892 return array( "page_namespace $eq_op $selectedNS" );
893 }
894
895 $associatedNS = $this->mDb->addQuotes(
896 MWNamespace::getAssociated( $this->namespace )
897 );
898
899 return array(
900 "page_namespace $eq_op $selectedNS " .
901 $bool_op .
902 " page_namespace $eq_op $associatedNS"
903 );
904 }
905
906 return array();
907 }
908
909 function getIndexField() {
910 return 'rev_timestamp';
911 }
912
913 function doBatchLookups() {
914 # Do a link batch query
915 $this->mResult->seek( 0 );
916 $revIds = array();
917 $batch = new LinkBatch();
918 # Give some pointers to make (last) links
919 foreach ( $this->mResult as $row ) {
920 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
921 $revIds[] = $row->rev_parent_id;
922 }
923 if ( isset( $row->rev_id ) ) {
924 if ( $this->contribs === 'newbie' ) { // multiple users
925 $batch->add( NS_USER, $row->user_name );
926 $batch->add( NS_USER_TALK, $row->user_name );
927 }
928 $batch->add( $row->page_namespace, $row->page_title );
929 }
930 }
931 $this->mParentLens = Revision::getParentLengths( $this->mDbSecondary, $revIds );
932 $batch->execute();
933 $this->mResult->seek( 0 );
934 }
935
936 /**
937 * @return string
938 */
939 function getStartBody() {
940 return "<ul>\n";
941 }
942
943 /**
944 * @return string
945 */
946 function getEndBody() {
947 return "</ul>\n";
948 }
949
950 /**
951 * Generates each row in the contributions list.
952 *
953 * Contributions which are marked "top" are currently on top of the history.
954 * For these contributions, a [rollback] link is shown for users with roll-
955 * back privileges. The rollback link restores the most recent version that
956 * was not written by the target user.
957 *
958 * @todo This would probably look a lot nicer in a table.
959 * @param object $row
960 * @return string
961 */
962 function formatRow( $row ) {
963 wfProfileIn( __METHOD__ );
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 wfSuppressWarnings();
976 try {
977 $rev = new Revision( $row );
978 $validRevision = (bool)$rev->getId();
979 } catch ( MWException $e ) {
980 $validRevision = false;
981 }
982 wfRestoreWarnings();
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 wfRunHooks( '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 wfProfileOut( __METHOD__ );
1128
1129 return $ret;
1130 }
1131
1132 /**
1133 * Overwrite Pager function and return a helpful comment
1134 * @return string
1135 */
1136 function getSqlComment() {
1137 if ( $this->namespace || $this->deletedOnly ) {
1138 // potentially slow, see CR r58153
1139 return 'contributions page filtered for namespace or RevisionDeleted edits';
1140 } else {
1141 return 'contributions page unfiltered';
1142 }
1143 }
1144
1145 protected function preventClickjacking() {
1146 $this->preventClickjacking = true;
1147 }
1148
1149 /**
1150 * @return bool
1151 */
1152 public function getPreventClickjacking() {
1153 return $this->preventClickjacking;
1154 }
1155 }