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