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