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