a0482045e67e039e065781a1afe0ed04d26494ec
[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 == 'newbies' ) {
48 $target = 'newbies';
49 $this->opts['contribs'] = 'newbie';
50 } elseif ( $par !== null ) {
51 $target = $par;
52 } else {
53 $target = $request->getVal( 'target' );
54 }
55
56 // check for radiobox
57 if ( $request->getVal( 'contribs' ) == 'newbie' ) {
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 $userObj = User::newFromName( $target, false );
78 if ( !$userObj ) {
79 $out->addHTML( $this->getForm() );
80 return;
81 }
82 $nt = $userObj->getUserPage();
83 $id = $userObj->getID();
84
85 if ( $this->opts['contribs'] != 'newbie' ) {
86 $target = $nt->getText();
87 $out->addSubtitle( $this->contributionsSub( $userObj ) );
88 $out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'contributions-title', $target )->plain() ) );
89 $this->getSkin()->setRelevantUser( $userObj );
90 } else {
91 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
92 $out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'sp-contributions-newbies-title' )->plain() ) );
93 }
94
95 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
96 $this->opts['namespace'] = intval( $ns );
97 } else {
98 $this->opts['namespace'] = '';
99 }
100
101 $this->opts['associated'] = $request->getBool( 'associated' );
102
103 $this->opts['nsInvert'] = (bool) $request->getVal( 'nsInvert' );
104
105 $this->opts['tagfilter'] = (string) $request->getVal( 'tagfilter' );
106 $this->opts['tagfilterdropdown'] = (string) $request->getVal( 'tagfilterdropdown' );
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['tagfilterdropdown'] !== '' && $this->opts['tagfilterdropdown'] !== 'other' ) {
143 $apiParams['tagfilter'] = $this->opts['tagfilterdropdown'];
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';
198 if ( IP::isIPAddress( $target ) ) {
199 $message = 'sp-contributions-footer-anon';
200 } else {
201 if ( $userObj->isAnon() ) {
202 // No message for non-existing users
203 return;
204 }
205 }
206
207 if ( !$this->msg( $message, $target )->isDisabled() ) {
208 $out->wrapWikiMsg(
209 "<div class='mw-contributions-footer'>\n$1\n</div>",
210 array( $message, $target ) );
211 }
212 }
213 }
214 }
215
216 /**
217 * Generates the subheading with links
218 * @param $userObj User object for the target
219 * @return String: appropriately-escaped HTML to be output literally
220 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
221 */
222 protected function contributionsSub( $userObj ) {
223 if ( $userObj->isAnon() ) {
224 $user = htmlspecialchars( $userObj->getName() );
225 } else {
226 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
227 }
228 $nt = $userObj->getUserPage();
229 $talk = $userObj->getTalkPage();
230 if ( $talk ) {
231 $tools = $this->getUserLinks( $nt, $talk, $userObj );
232 $links = $this->getLanguage()->pipeList( $tools );
233
234 // Show a note if the user is blocked and display the last block log entry.
235 if ( $userObj->isBlocked() ) {
236 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
237 LogEventsList::showLogExtract(
238 $out,
239 'block',
240 $nt,
241 '',
242 array(
243 'lim' => 1,
244 'showIfEmpty' => false,
245 'msgKey' => array(
246 $userObj->isAnon() ?
247 'sp-contributions-blocked-notice-anon' :
248 'sp-contributions-blocked-notice',
249 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
250 ),
251 'offset' => '' # don't use WebRequest parameter offset
252 )
253 );
254 }
255 }
256
257 // Old message 'contribsub' had one parameter, but that doesn't work for
258 // languages that want to put the "for" bit right after $user but before
259 // $links. If 'contribsub' is around, use it for reverse compatibility,
260 // otherwise use 'contribsub2'.
261 $oldMsg = $this->msg( 'contribsub' );
262 if ( $oldMsg->exists() ) {
263 return $oldMsg->rawParams( "$user ($links)" );
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::buildTagFilterWithDropdown(
407 'tag-filter-dropdown-list',
408 $this->opts['tagfilter'],
409 $this->opts['tagfilterdropdown']
410 );
411
412 if ( $tagFilter ) {
413 $filterSelection =
414 Xml::tags( 'td', array( 'class' => 'mw-label' ), array_shift( $tagFilter ) ) .
415 Xml::tags( 'td', array( 'class' => 'mw-input' ), implode( '&#160', $tagFilter ) );
416 } else {
417 $filterSelection = Xml::tags( 'td', array( 'colspan' => 2 ), '' );
418 }
419
420 $targetSelection = Xml::tags( 'td', array( 'colspan' => 2 ),
421 Xml::radioLabel(
422 $this->msg( 'sp-contributions-newbies' )->text(),
423 'contribs',
424 'newbie' ,
425 'newbie',
426 $this->opts['contribs'] == 'newbie',
427 array( 'class' => 'mw-input' )
428 ) . '<br />' .
429 Xml::radioLabel(
430 $this->msg( 'sp-contributions-username' )->text(),
431 'contribs',
432 'user',
433 'user',
434 $this->opts['contribs'] == 'user',
435 array( 'class' => 'mw-input' )
436 ) . ' ' .
437 Html::input(
438 'target',
439 $this->opts['target'],
440 'text',
441 array( 'size' => '20', 'required' => '', 'class' => 'mw-input' ) +
442 ( $this->opts['target'] ? array() : array( 'autofocus' )
443 )
444 ) . ' '
445 ) ;
446
447 $namespaceSelection =
448 Xml::tags( 'td', array( 'class' => 'mw-label' ),
449 Xml::label(
450 $this->msg( 'namespace' )->text(),
451 'namespace',
452 ''
453 )
454 ) .
455 Xml::tags( 'td', null,
456 Xml::namespaceSelector( $this->opts['namespace'], '' ) . '&#160;' .
457 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
458 Xml::checkLabel(
459 $this->msg( 'invert' )->text(),
460 'nsInvert',
461 'nsInvert',
462 $this->opts['nsInvert'],
463 array( 'title' => $this->msg( 'tooltip-invert' )->text(), 'class' => 'mw-input' )
464 ) . '&#160;'
465 ) .
466 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
467 Xml::checkLabel(
468 $this->msg( 'namespace_association' )->text(),
469 'associated',
470 'associated',
471 $this->opts['associated'],
472 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text(), 'class' => 'mw-input' )
473 ) . '&#160;'
474 )
475 ) ;
476
477 $extraOptions = Xml::tags( 'td', array( 'colspan' => 2 ),
478 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
479 Xml::checkLabel(
480 $this->msg( 'history-show-deleted' )->text(),
481 'deletedOnly',
482 'mw-show-deleted-only',
483 $this->opts['deletedOnly'],
484 array( 'class' => 'mw-input' )
485 )
486 ) .
487 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
488 Xml::checkLabel(
489 $this->msg( 'sp-contributions-toponly' )->text(),
490 'topOnly',
491 'mw-show-top-only',
492 $this->opts['topOnly'],
493 array( 'class' => 'mw-input' )
494 )
495 )
496 ) ;
497
498 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
499 Xml::dateMenu(
500 $this->opts['year'],
501 $this->opts['month']
502 ) . ' ' .
503 Xml::submitButton(
504 $this->msg( 'sp-contributions-submit' )->text(),
505 array( 'class' => 'mw-submit' )
506 )
507 ) ;
508
509 $form .=
510 Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() ) .
511 Xml::openElement( 'table', array( 'class' => 'mw-contributions-table' ) ) .
512 Xml::openElement( 'tr' ) .
513 $targetSelection .
514 Xml::closeElement( 'tr' ) .
515 Xml::openElement( 'tr' ) .
516 $namespaceSelection .
517 Xml::closeElement( 'tr' ) .
518 Xml::openElement( 'tr' ) .
519 $filterSelection .
520 Xml::closeElement( 'tr' ) .
521 Xml::openElement( 'tr' ) .
522 $extraOptions .
523 Xml::closeElement( 'tr' ) .
524 Xml::openElement( 'tr' ) .
525 $dateSelectionAndSubmit .
526 Xml::closeElement( 'tr' ) .
527 Xml::closeElement( 'table' );
528
529 $explain = $this->msg( 'sp-contributions-explain' );
530 if ( $explain->exists() ) {
531 $form .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
532 }
533 $form .= Xml::closeElement( 'fieldset' ) .
534 Xml::closeElement( 'form' );
535 return $form;
536 }
537 }
538
539 /**
540 * Pager for Special:Contributions
541 * @ingroup SpecialPage Pager
542 */
543 class ContribsPager extends ReverseChronologicalPager {
544 public $mDefaultDirection = true;
545 var $messages, $target;
546 var $namespace = '', $mDb;
547 var $preventClickjacking = false;
548
549 function __construct( IContextSource $context, array $options ) {
550 parent::__construct( $context );
551
552 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
553
554 foreach ( $msgs as $msg ) {
555 $this->messages[$msg] = $this->msg( $msg )->escaped();
556 }
557
558 $this->target = isset( $options['target'] ) ? $options['target'] : '';
559 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
560 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
561 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
562 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
563 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
564
565 $this->deletedOnly = !empty( $options['deletedOnly'] );
566 $this->topOnly = !empty( $options['topOnly'] );
567
568 $year = isset( $options['year'] ) ? $options['year'] : false;
569 $month = isset( $options['month'] ) ? $options['month'] : false;
570 $this->getDateCond( $year, $month );
571
572 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
573 }
574
575 function getDefaultQuery() {
576 $query = parent::getDefaultQuery();
577 $query['target'] = $this->target;
578 return $query;
579 }
580
581 function getQueryInfo() {
582 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
583
584 $user = $this->getUser();
585 $conds = array_merge( $userCond, $this->getNamespaceCond() );
586
587 // Paranoia: avoid brute force searches (bug 17342)
588 if ( !$user->isAllowed( 'deletedhistory' ) ) {
589 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
590 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
591 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
592 ' != ' . Revision::SUPPRESSED_USER;
593 }
594
595 # Don't include orphaned revisions
596 $join_cond['page'] = Revision::pageJoinCond();
597 # Get the current user name for accounts
598 $join_cond['user'] = Revision::userJoinCond();
599
600 $queryInfo = array(
601 'tables' => $tables,
602 'fields' => array_merge(
603 Revision::selectFields(),
604 Revision::selectUserFields(),
605 array( 'page_namespace', 'page_title', 'page_is_new',
606 'page_latest', 'page_is_redirect', 'page_len' )
607 ),
608 'conds' => $conds,
609 'options' => array( 'USE INDEX' => array( 'revision' => $index ) ),
610 'join_conds' => $join_cond
611 );
612
613 ChangeTags::modifyDisplayQuery(
614 $queryInfo['tables'],
615 $queryInfo['fields'],
616 $queryInfo['conds'],
617 $queryInfo['join_conds'],
618 $queryInfo['options'],
619 $this->tagFilter
620 );
621
622 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
623 return $queryInfo;
624 }
625
626 function getUserCond() {
627 $condition = array();
628 $join_conds = array();
629 $tables = array( 'revision', 'page', 'user' );
630 if ( $this->contribs == 'newbie' ) {
631 $tables[] = 'user_groups';
632 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
633 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
634 $condition[] = 'ug_group IS NULL';
635 $index = 'user_timestamp';
636 # @todo FIXME: Other groups may have 'bot' rights
637 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
638 } else {
639 if ( IP::isIPAddress( $this->target ) ) {
640 $condition['rev_user_text'] = $this->target;
641 $index = 'usertext_timestamp';
642 } else {
643 $condition['rev_user'] = User::idFromName( $this->target );
644 $index = 'user_timestamp';
645 }
646 }
647 if ( $this->deletedOnly ) {
648 $condition[] = "rev_deleted != '0'";
649 }
650 if ( $this->topOnly ) {
651 $condition[] = "rev_id = page_latest";
652 }
653 return array( $tables, $index, $condition, $join_conds );
654 }
655
656 function getNamespaceCond() {
657 if ( $this->namespace !== '' ) {
658 $selectedNS = $this->mDb->addQuotes( $this->namespace );
659 $eq_op = $this->nsInvert ? '!=' : '=';
660 $bool_op = $this->nsInvert ? 'AND' : 'OR';
661
662 if ( !$this->associated ) {
663 return array( "page_namespace $eq_op $selectedNS" );
664 } else {
665 $associatedNS = $this->mDb->addQuotes (
666 MWNamespace::getAssociated( $this->namespace )
667 );
668 return array(
669 "page_namespace $eq_op $selectedNS " .
670 $bool_op .
671 " page_namespace $eq_op $associatedNS"
672 );
673 }
674
675 } else {
676 return array();
677 }
678 }
679
680 function getIndexField() {
681 return 'rev_timestamp';
682 }
683
684 function doBatchLookups() {
685 $this->mResult->rewind();
686 $revIds = array();
687 foreach ( $this->mResult as $row ) {
688 $revIds[] = $row->rev_parent_id;
689 }
690 $this->mParentLens = $this->getParentLengths( $revIds );
691 $this->mResult->rewind(); // reset
692
693 if ( $this->contribs === 'newbie' ) { // multiple users
694 # Do a link batch query
695 $this->mResult->seek( 0 );
696 $batch = new LinkBatch();
697 # Give some pointers to make (last) links
698 foreach ( $this->mResult as $row ) {
699 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
700 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
701 }
702 $batch->execute();
703 $this->mResult->seek( 0 );
704 }
705 }
706
707 /**
708 * Do a batched query to get the parent revision lengths
709 */
710 private function getParentLengths( array $revIds ) {
711 $revLens = array();
712 if ( !$revIds ) {
713 return $revLens; // empty
714 }
715 wfProfileIn( __METHOD__ );
716 $res = $this->getDatabase()->select( 'revision',
717 array( 'rev_id', 'rev_len' ),
718 array( 'rev_id' => $revIds ),
719 __METHOD__ );
720 foreach ( $res as $row ) {
721 $revLens[$row->rev_id] = $row->rev_len;
722 }
723 wfProfileOut( __METHOD__ );
724 return $revLens;
725 }
726
727 /**
728 * @return string
729 */
730 function getStartBody() {
731 return "<ul>\n";
732 }
733
734 /**
735 * @return string
736 */
737 function getEndBody() {
738 return "</ul>\n";
739 }
740
741 /**
742 * Generates each row in the contributions list.
743 *
744 * Contributions which are marked "top" are currently on top of the history.
745 * For these contributions, a [rollback] link is shown for users with roll-
746 * back privileges. The rollback link restores the most recent version that
747 * was not written by the target user.
748 *
749 * @todo This would probably look a lot nicer in a table.
750 */
751 function formatRow( $row ) {
752 wfProfileIn( __METHOD__ );
753
754 $rev = new Revision( $row );
755 $classes = array();
756
757 $page = Title::newFromRow( $row );
758 $link = Linker::link(
759 $page,
760 htmlspecialchars( $page->getPrefixedText() ),
761 array(),
762 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
763 );
764 # Mark current revisions
765 $topmarktext = '';
766 if ( $row->rev_id == $row->page_latest ) {
767 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
768 # Add rollback link
769 if ( !$row->page_is_new && $page->quickUserCan( 'rollback' )
770 && $page->quickUserCan( 'edit' ) )
771 {
772 $this->preventClickjacking();
773 $topmarktext .= ' ' . Linker::generateRollback( $rev );
774 }
775 }
776 $user = $this->getUser();
777 # Is there a visible previous revision?
778 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
779 $difftext = Linker::linkKnown(
780 $page,
781 $this->messages['diff'],
782 array(),
783 array(
784 'diff' => 'prev',
785 'oldid' => $row->rev_id
786 )
787 );
788 } else {
789 $difftext = $this->messages['diff'];
790 }
791 $histlink = Linker::linkKnown(
792 $page,
793 $this->messages['hist'],
794 array(),
795 array( 'action' => 'history' )
796 );
797
798 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
799 $chardiff = ' . . ' . ChangesList::showCharacterDifference(
800 $this->mParentLens[$row->rev_parent_id], $row->rev_len ) . ' . . ';
801 } else {
802 $chardiff = ' ';
803 }
804
805 $lang = $this->getLanguage();
806 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
807 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
808 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
809 $d = Linker::linkKnown(
810 $page,
811 htmlspecialchars( $date ),
812 array(),
813 array( 'oldid' => intval( $row->rev_id ) )
814 );
815 } else {
816 $d = htmlspecialchars( $date );
817 }
818 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
819 $d = '<span class="history-deleted">' . $d . '</span>';
820 }
821
822 # Show user names for /newbies as there may be different users.
823 # Note that we already excluded rows with hidden user names.
824 if ( $this->contribs == 'newbie' ) {
825 $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
826 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
827 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
828 } else {
829 $userlink = '';
830 }
831
832 if ( $rev->getParentId() === 0 ) {
833 $nflag = ChangesList::flag( 'newpage' );
834 } else {
835 $nflag = '';
836 }
837
838 if ( $rev->isMinor() ) {
839 $mflag = ChangesList::flag( 'minor' );
840 } else {
841 $mflag = '';
842 }
843
844 $del = Linker::getRevDeleteLink( $user, $rev, $page );
845 if ( $del !== '' ) {
846 $del .= ' ';
847 }
848
849 $diffHistLinks = '(' . $difftext . $this->messages['pipe-separator'] . $histlink . ')';
850 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
851
852 # Denote if username is redacted for this edit
853 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
854 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
855 }
856
857 # Tags, if any.
858 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
859 $classes = array_merge( $classes, $newClasses );
860 $ret .= " $tagSummary";
861
862 // Let extensions add data
863 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
864
865 $classes = implode( ' ', $classes );
866 $ret = "<li class=\"$classes\">$ret</li>\n";
867 wfProfileOut( __METHOD__ );
868 return $ret;
869 }
870
871 /**
872 * Get the Database object in use
873 *
874 * @return DatabaseBase
875 */
876 public function getDatabase() {
877 return $this->mDb;
878 }
879
880 /**
881 * Overwrite Pager function and return a helpful comment
882 */
883 function getSqlComment() {
884 if ( $this->namespace || $this->deletedOnly ) {
885 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
886 } else {
887 return 'contributions page unfiltered';
888 }
889 }
890
891 protected function preventClickjacking() {
892 $this->preventClickjacking = true;
893 }
894
895 public function getPreventClickjacking() {
896 return $this->preventClickjacking;
897 }
898 }