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