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