Use local context to get messages
[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 * @return array
702 */
703 private function getParentLengths( array $revIds ) {
704 $revLens = array();
705 if ( !$revIds ) {
706 return $revLens; // empty
707 }
708 wfProfileIn( __METHOD__ );
709 $res = $this->getDatabase()->select( 'revision',
710 array( 'rev_id', 'rev_len' ),
711 array( 'rev_id' => $revIds ),
712 __METHOD__ );
713 foreach ( $res as $row ) {
714 $revLens[$row->rev_id] = $row->rev_len;
715 }
716 wfProfileOut( __METHOD__ );
717 return $revLens;
718 }
719
720 /**
721 * @return string
722 */
723 function getStartBody() {
724 return "<ul>\n";
725 }
726
727 /**
728 * @return string
729 */
730 function getEndBody() {
731 return "</ul>\n";
732 }
733
734 /**
735 * Generates each row in the contributions list.
736 *
737 * Contributions which are marked "top" are currently on top of the history.
738 * For these contributions, a [rollback] link is shown for users with roll-
739 * back privileges. The rollback link restores the most recent version that
740 * was not written by the target user.
741 *
742 * @todo This would probably look a lot nicer in a table.
743 * @return string
744 */
745 function formatRow( $row ) {
746 wfProfileIn( __METHOD__ );
747
748 $rev = new Revision( $row );
749 $classes = array();
750
751 $page = Title::newFromRow( $row );
752 $link = Linker::link(
753 $page,
754 htmlspecialchars( $page->getPrefixedText() ),
755 array(),
756 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
757 );
758 # Mark current revisions
759 $topmarktext = '';
760 if ( $row->rev_id == $row->page_latest ) {
761 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
762 # Add rollback link
763 if ( !$row->page_is_new && $page->quickUserCan( 'rollback' )
764 && $page->quickUserCan( 'edit' ) )
765 {
766 $this->preventClickjacking();
767 $topmarktext .= ' ' . Linker::generateRollback( $rev );
768 }
769 }
770 $user = $this->getUser();
771 # Is there a visible previous revision?
772 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
773 $difftext = Linker::linkKnown(
774 $page,
775 $this->messages['diff'],
776 array(),
777 array(
778 'diff' => 'prev',
779 'oldid' => $row->rev_id
780 )
781 );
782 } else {
783 $difftext = $this->messages['diff'];
784 }
785 $histlink = Linker::linkKnown(
786 $page,
787 $this->messages['hist'],
788 array(),
789 array( 'action' => 'history' )
790 );
791
792 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
793 $chardiff = ' . . ' . ChangesList::showCharacterDifference(
794 $this->mParentLens[$row->rev_parent_id], $row->rev_len ) . ' . . ';
795 } else {
796 $chardiff = ' ';
797 }
798
799 $lang = $this->getLanguage();
800 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
801 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
802 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
803 $d = Linker::linkKnown(
804 $page,
805 htmlspecialchars( $date ),
806 array(),
807 array( 'oldid' => intval( $row->rev_id ) )
808 );
809 } else {
810 $d = htmlspecialchars( $date );
811 }
812 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
813 $d = '<span class="history-deleted">' . $d . '</span>';
814 }
815
816 # Show user names for /newbies as there may be different users.
817 # Note that we already excluded rows with hidden user names.
818 if ( $this->contribs == 'newbie' ) {
819 $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
820 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
821 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
822 } else {
823 $userlink = '';
824 }
825
826 if ( $rev->getParentId() === 0 ) {
827 $nflag = ChangesList::flag( 'newpage' );
828 } else {
829 $nflag = '';
830 }
831
832 if ( $rev->isMinor() ) {
833 $mflag = ChangesList::flag( 'minor' );
834 } else {
835 $mflag = '';
836 }
837
838 $del = Linker::getRevDeleteLink( $user, $rev, $page );
839 if ( $del !== '' ) {
840 $del .= ' ';
841 }
842
843 $diffHistLinks = '(' . $difftext . $this->messages['pipe-separator'] . $histlink . ')';
844 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
845
846 # Denote if username is redacted for this edit
847 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
848 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
849 }
850
851 # Tags, if any.
852 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
853 $classes = array_merge( $classes, $newClasses );
854 $ret .= " $tagSummary";
855
856 // Let extensions add data
857 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
858
859 $classes = implode( ' ', $classes );
860 $ret = "<li class=\"$classes\">$ret</li>\n";
861 wfProfileOut( __METHOD__ );
862 return $ret;
863 }
864
865 /**
866 * Get the Database object in use
867 *
868 * @return DatabaseBase
869 */
870 public function getDatabase() {
871 return $this->mDb;
872 }
873
874 /**
875 * Overwrite Pager function and return a helpful comment
876 * @return string
877 */
878 function getSqlComment() {
879 if ( $this->namespace || $this->deletedOnly ) {
880 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
881 } else {
882 return 'contributions page unfiltered';
883 }
884 }
885
886 protected function preventClickjacking() {
887 $this->preventClickjacking = true;
888 }
889
890 public function getPreventClickjacking() {
891 return $this->preventClickjacking;
892 }
893 }