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