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