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