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