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