* (bug 32512) Include 'associated namespace' checkbox on Special:Contributions
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
1 <?php
2 /**
3 * Implements Special:Contributions
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special:Contributions, show user contributions in a paged list
26 *
27 * @ingroup SpecialPage
28 */
29
30 class SpecialContributions extends SpecialPage {
31
32 protected $opts;
33
34 public function __construct() {
35 parent::__construct( 'Contributions' );
36 }
37
38 public function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->addModuleStyles( 'mediawiki.special' );
43
44 $this->opts = array();
45 $request = $this->getRequest();
46
47 if( $par == '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', wfMsgExt( 'contributions-title', array( 'parsemag' ), $target ) ) );
89 $this->getSkin()->setRelevantUser( $userObj );
90 } else {
91 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub') );
92 $out->setHTMLTitle( $this->msg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
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( 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( !wfMessage( $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 = self::getUserLinks( $nt, $talk, $userObj, $this->getUser() );
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 * @param $subject User: The viewing user ($wgUser might be still checked in some cases)
271 */
272 public static function getUserLinks( Title $userpage, Title $talkpage, User $target, User $subject ) {
273
274 $id = $target->getId();
275 $username = $target->getName();
276
277 $tools[] = Linker::link( $talkpage, wfMsgHtml( 'sp-contributions-talk' ) );
278
279 if( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
280 if( $subject->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
281 if ( $target->isBlocked() ) {
282 $tools[] = Linker::linkKnown( # Change block link
283 SpecialPage::getTitleFor( 'Block', $username ),
284 wfMsgHtml( 'change-blocklink' )
285 );
286 $tools[] = Linker::linkKnown( # Unblock link
287 SpecialPage::getTitleFor( 'Unblock', $username ),
288 wfMsgHtml( 'unblocklink' )
289 );
290 } else { # User is not blocked
291 $tools[] = Linker::linkKnown( # Block link
292 SpecialPage::getTitleFor( 'Block', $username ),
293 wfMsgHtml( 'blocklink' )
294 );
295 }
296 }
297 # Block log link
298 $tools[] = Linker::linkKnown(
299 SpecialPage::getTitleFor( 'Log', 'block' ),
300 wfMsgHtml( 'sp-contributions-blocklog' ),
301 array(),
302 array(
303 'page' => $userpage->getPrefixedText()
304 )
305 );
306 }
307 # Uploads
308 $tools[] = Linker::linkKnown(
309 SpecialPage::getTitleFor( 'Listfiles', $username ),
310 wfMsgHtml( 'sp-contributions-uploads' )
311 );
312
313 # Other logs link
314 $tools[] = Linker::linkKnown(
315 SpecialPage::getTitleFor( 'Log', $username ),
316 wfMsgHtml( 'sp-contributions-logs' )
317 );
318
319 # Add link to deleted user contributions for priviledged users
320 if( $subject->isAllowed( 'deletedhistory' ) ) {
321 $tools[] = Linker::linkKnown(
322 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
323 wfMsgHtml( 'sp-contributions-deleted' )
324 );
325 }
326
327 # Add a link to change user rights for privileged users
328 $userrightsPage = new UserrightsPage();
329 $userrightsPage->getContext()->setUser( $subject );
330 if( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
331 $tools[] = Linker::linkKnown(
332 SpecialPage::getTitleFor( 'Userrights', $username ),
333 wfMsgHtml( 'sp-contributions-userrights' )
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 wfMsgExt( 'sp-contributions-newbies', array( 'parsemag' ) ),
415 'contribs',
416 'newbie' ,
417 'newbie',
418 $this->opts['contribs'] == 'newbie',
419 array( 'class' => 'mw-input' )
420 ) . '<br />' .
421 Xml::radioLabel(
422 wfMsgExt( 'sp-contributions-username', array( 'parsemag' ) ),
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 wfMsg( 'namespace' ),
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 wfMsg('invert'),
452 'nsInvert',
453 'nsInvert',
454 $this->opts['nsInvert'],
455 array( 'title' => wfMsg( 'tooltip-invert' ), 'class' => 'mw-input' )
456 ) . '&#160;'
457 ) .
458 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
459 Xml::checkLabel(
460 wfMsg('namespace_association'),
461 'associated',
462 'associated',
463 $this->opts['associated'],
464 array( 'title' => wfMsg( 'tooltip-namespace_association' ), '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 wfMsg( 'history-show-deleted' ),
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 wfMsg( 'sp-contributions-toponly' ),
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 wfMsg( 'sp-contributions-submit' ),
497 array( 'class' => 'mw-submit' )
498 )
499 ) ;
500
501 $form .=
502 Xml::fieldset( wfMsg( 'sp-contributions-search' ) ) .
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 = wfMessage( '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( $options ) {
542 parent::__construct();
543
544 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
545
546 foreach( $msgs as $msg ) {
547 $this->messages[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
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 function getStartBody() {
720 return "<ul>\n";
721 }
722
723 function getEndBody() {
724 return "</ul>\n";
725 }
726
727 /**
728 * Generates each row in the contributions list.
729 *
730 * Contributions which are marked "top" are currently on top of the history.
731 * For these contributions, a [rollback] link is shown for users with roll-
732 * back privileges. The rollback link restores the most recent version that
733 * was not written by the target user.
734 *
735 * @todo This would probably look a lot nicer in a table.
736 */
737 function formatRow( $row ) {
738 wfProfileIn( __METHOD__ );
739
740 $rev = new Revision( $row );
741 $classes = array();
742
743 $page = Title::newFromRow( $row );
744 $link = Linker::link(
745 $page,
746 htmlspecialchars( $page->getPrefixedText() ),
747 array(),
748 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
749 );
750 # Mark current revisions
751 $topmarktext = '';
752 if( $row->rev_id == $row->page_latest ) {
753 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
754 # Add rollback link
755 if( !$row->page_is_new && $page->quickUserCan( 'rollback' )
756 && $page->quickUserCan( 'edit' ) )
757 {
758 $this->preventClickjacking();
759 $topmarktext .= ' '.Linker::generateRollback( $rev );
760 }
761 }
762 $user = $this->getUser();
763 # Is there a visible previous revision?
764 if( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
765 $difftext = Linker::linkKnown(
766 $page,
767 $this->messages['diff'],
768 array(),
769 array(
770 'diff' => 'prev',
771 'oldid' => $row->rev_id
772 )
773 );
774 } else {
775 $difftext = $this->messages['diff'];
776 }
777 $histlink = Linker::linkKnown(
778 $page,
779 $this->messages['hist'],
780 array(),
781 array( 'action' => 'history' )
782 );
783
784 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
785 $chardiff = ' . . ' . ChangesList::showCharacterDifference(
786 $this->mParentLens[$row->rev_parent_id], $row->rev_len ) . ' . . ';
787 } else {
788 $chardiff = ' ';
789 }
790
791 $comment = $this->getLanguage()->getDirMark() . Linker::revComment( $rev, false, true );
792 $date = $this->getLanguage()->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
793 if( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
794 $d = Linker::linkKnown(
795 $page,
796 htmlspecialchars($date),
797 array(),
798 array( 'oldid' => intval( $row->rev_id ) )
799 );
800 } else {
801 $d = htmlspecialchars( $date );
802 }
803 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
804 $d = '<span class="history-deleted">' . $d . '</span>';
805 }
806
807 # Show user names for /newbies as there may be different users.
808 # Note that we already excluded rows with hidden user names.
809 if( $this->contribs == 'newbie' ) {
810 $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
811 $userlink .= ' ' . wfMsg( 'parentheses',
812 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) ) . ' ';
813 } else {
814 $userlink = '';
815 }
816
817 if( $rev->getParentId() === 0 ) {
818 $nflag = ChangesList::flag( 'newpage' );
819 } else {
820 $nflag = '';
821 }
822
823 if( $rev->isMinor() ) {
824 $mflag = ChangesList::flag( 'minor' );
825 } else {
826 $mflag = '';
827 }
828
829 // Don't show useless link to people who cannot hide revisions
830 $canHide = $user->isAllowed( 'deleterevision' );
831 if( $canHide || ($rev->getVisibility() && $user->isAllowed('deletedhistory')) ) {
832 if( !$rev->userCan( Revision::DELETED_RESTRICTED, $user ) ) {
833 $del = Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
834 } else {
835 $query = array(
836 'type' => 'revision',
837 'target' => $page->getPrefixedDbkey(),
838 'ids' => $rev->getId()
839 );
840 $del = Linker::revDeleteLink( $query,
841 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
842 }
843 $del .= ' ';
844 } else {
845 $del = '';
846 }
847
848 $diffHistLinks = '(' . $difftext . $this->messages['pipe-separator'] . $histlink . ')';
849 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
850
851 # Denote if username is redacted for this edit
852 if( $rev->isDeleted( Revision::DELETED_USER ) ) {
853 $ret .= " <strong>" . wfMsgHtml('rev-deleted-user-contribs') . "</strong>";
854 }
855
856 # Tags, if any.
857 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
858 $classes = array_merge( $classes, $newClasses );
859 $ret .= " $tagSummary";
860
861 // Let extensions add data
862 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
863
864 $classes = implode( ' ', $classes );
865 $ret = "<li class=\"$classes\">$ret</li>\n";
866 wfProfileOut( __METHOD__ );
867 return $ret;
868 }
869
870 /**
871 * Get the Database object in use
872 *
873 * @return DatabaseBase
874 */
875 public function getDatabase() {
876 return $this->mDb;
877 }
878
879 /**
880 * Overwrite Pager function and return a helpful comment
881 */
882 function getSqlComment() {
883 if ( $this->namespace || $this->deletedOnly ) {
884 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
885 } else {
886 return 'contributions page unfiltered';
887 }
888 }
889
890 protected function preventClickjacking() {
891 $this->preventClickjacking = true;
892 }
893
894 public function getPreventClickjacking() {
895 return $this->preventClickjacking;
896 }
897 }