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