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