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