r100315: removed now redundant code
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
1 <?php
2 /**
3 * Implements Special:Contributions
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special:Contributions, show user contributions in a paged list
26 *
27 * @ingroup SpecialPage
28 */
29
30 class SpecialContributions extends SpecialPage {
31
32 protected $opts;
33
34 public function __construct() {
35 parent::__construct( 'Contributions' );
36 }
37
38 public function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->addModuleStyles( 'mediawiki.special' );
43
44 $this->opts = array();
45 $request = $this->getRequest();
46
47 if ( $par !== null ) {
48 $target = $par;
49 } else {
50 $target = $request->getVal( 'target' );
51 }
52
53 // check for radiobox
54 if ( $request->getVal( 'contribs' ) == 'newbie' ) {
55 $target = 'newbies';
56 $this->opts['contribs'] = 'newbie';
57 } else {
58 $this->opts['contribs'] = 'user';
59 }
60
61 $this->opts['deletedOnly'] = $request->getBool( 'deletedOnly' );
62
63 if ( !strlen( $target ) ) {
64 $out->addHTML( $this->getForm() );
65 return;
66 }
67
68 $user = $this->getUser();
69
70 $this->opts['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
71 $this->opts['target'] = $target;
72 $this->opts['topOnly'] = $request->getBool( 'topOnly' );
73
74 $userObj = User::newFromName( $target, false );
75 if ( !$userObj ) {
76 $out->addHTML( $this->getForm() );
77 return;
78 }
79 $nt = $userObj->getUserPage();
80 $id = $userObj->getID();
81
82 if ( $this->opts['contribs'] != 'newbie' ) {
83 $target = $nt->getText();
84 $out->addSubtitle( $this->contributionsSub( $userObj ) );
85 $out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'contributions-title', $target )->plain() ) );
86 $this->getSkin()->setRelevantUser( $userObj );
87 } else {
88 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
89 $out->setHTMLTitle( $this->msg( 'pagetitle', $this->msg( 'sp-contributions-newbies-title' )->plain() ) );
90 }
91
92 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
93 $this->opts['namespace'] = intval( $ns );
94 } else {
95 $this->opts['namespace'] = '';
96 }
97
98 $this->opts['associated'] = $request->getBool( 'associated' );
99
100 $this->opts['nsInvert'] = (bool) $request->getVal( 'nsInvert' );
101
102 $this->opts['tagfilter'] = (string) $request->getVal( 'tagfilter' );
103
104 // Allows reverts to have the bot flag in recent changes. It is just here to
105 // be passed in the form at the top of the page
106 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
107 $this->opts['bot'] = '1';
108 }
109
110 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
111 # Offset overrides year/month selection
112 if ( $skip ) {
113 $this->opts['year'] = '';
114 $this->opts['month'] = '';
115 } else {
116 $this->opts['year'] = $request->getIntOrNull( 'year' );
117 $this->opts['month'] = $request->getIntOrNull( 'month' );
118 }
119
120 $feedType = $request->getVal( 'feed' );
121 if ( $feedType ) {
122 // Maintain some level of backwards compatability
123 // If people request feeds using the old parameters, redirect to API
124 $apiParams = array(
125 'action' => 'feedcontributions',
126 'feedformat' => $feedType,
127 'user' => $target,
128 );
129 if ( $this->opts['topOnly'] ) {
130 $apiParams['toponly'] = true;
131 }
132 if ( $this->opts['deletedOnly'] ) {
133 $apiParams['deletedonly'] = true;
134 }
135 if ( $this->opts['tagfilter'] !== '' ) {
136 $apiParams['tagfilter'] = $this->opts['tagfilter'];
137 }
138 if ( $this->opts['namespace'] !== '' ) {
139 $apiParams['namespace'] = $this->opts['namespace'];
140 }
141 if ( $this->opts['year'] !== null ) {
142 $apiParams['year'] = $this->opts['year'];
143 }
144 if ( $this->opts['month'] !== null ) {
145 $apiParams['month'] = $this->opts['month'];
146 }
147
148 $url = wfScript( 'api' ) . '?' . wfArrayToCGI( $apiParams );
149
150 $out->redirect( $url, '301' );
151 return;
152 }
153
154 // Add RSS/atom links
155 $this->addFeedLinks( array( 'action' => 'feedcontributions', 'user' => $target ) );
156
157 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id ) ) ) {
158
159 $out->addHTML( $this->getForm() );
160
161 $pager = new ContribsPager( $this->getContext(), array(
162 'target' => $target,
163 'contribs' => $this->opts['contribs'],
164 'namespace' => $this->opts['namespace'],
165 'year' => $this->opts['year'],
166 'month' => $this->opts['month'],
167 'deletedOnly' => $this->opts['deletedOnly'],
168 'topOnly' => $this->opts['topOnly'],
169 'nsInvert' => $this->opts['nsInvert'],
170 'associated' => $this->opts['associated'],
171 ) );
172 if ( !$pager->getNumRows() ) {
173 $out->addWikiMsg( 'nocontribs', $target );
174 } else {
175 # Show a message about slave lag, if applicable
176 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
177 if ( $lag > 0 )
178 $out->showLagWarning( $lag );
179
180 $out->addHTML(
181 '<p>' . $pager->getNavigationBar() . '</p>' .
182 $pager->getBody() .
183 '<p>' . $pager->getNavigationBar() . '</p>'
184 );
185 }
186 $out->preventClickjacking( $pager->getPreventClickjacking() );
187
188 # Show the appropriate "footer" message - WHOIS tools, etc.
189 if ( $this->opts['contribs'] != 'newbie' ) {
190 $message = 'sp-contributions-footer';
191 if ( IP::isIPAddress( $target ) ) {
192 $message = 'sp-contributions-footer-anon';
193 } else {
194 if ( $userObj->isAnon() ) {
195 // No message for non-existing users
196 return;
197 }
198 }
199
200 if ( !$this->msg( $message, $target )->isDisabled() ) {
201 $out->wrapWikiMsg(
202 "<div class='mw-contributions-footer'>\n$1\n</div>",
203 array( $message, $target ) );
204 }
205 }
206 }
207 }
208
209 /**
210 * Generates the subheading with links
211 * @param $userObj User object for the target
212 * @return String: appropriately-escaped HTML to be output literally
213 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php. Could be combined.
214 */
215 protected function contributionsSub( $userObj ) {
216 if ( $userObj->isAnon() ) {
217 $user = htmlspecialchars( $userObj->getName() );
218 } else {
219 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
220 }
221 $nt = $userObj->getUserPage();
222 $talk = $userObj->getTalkPage();
223 if ( $talk ) {
224 $tools = $this->getUserLinks( $nt, $talk, $userObj );
225 $links = $this->getLanguage()->pipeList( $tools );
226
227 // Show a note if the user is blocked and display the last block log entry.
228 if ( $userObj->isBlocked() ) {
229 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
230 LogEventsList::showLogExtract(
231 $out,
232 'block',
233 $nt,
234 '',
235 array(
236 'lim' => 1,
237 'showIfEmpty' => false,
238 'msgKey' => array(
239 $userObj->isAnon() ?
240 'sp-contributions-blocked-notice-anon' :
241 'sp-contributions-blocked-notice',
242 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
243 ),
244 'offset' => '' # don't use WebRequest parameter offset
245 )
246 );
247 }
248 }
249
250 // Old message 'contribsub' had one parameter, but that doesn't work for
251 // languages that want to put the "for" bit right after $user but before
252 // $links. If 'contribsub' is around, use it for reverse compatibility,
253 // otherwise use 'contribsub2'.
254 $oldMsg = $this->msg( 'contribsub' );
255 if ( $oldMsg->exists() ) {
256 return $oldMsg->rawParams( "$user ($links)" );
257 } else {
258 return $this->msg( 'contribsub2' )->rawParams( $user, $links );
259 }
260 }
261
262 /**
263 * Links to different places.
264 * @param $userpage Title: Target user page
265 * @param $talkpage Title: Talk page
266 * @param $target User: Target user object
267 * @return array
268 */
269 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
270
271 $id = $target->getId();
272 $username = $target->getName();
273
274 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
275
276 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
277 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
278 if ( $target->isBlocked() ) {
279 $tools[] = Linker::linkKnown( # Change block link
280 SpecialPage::getTitleFor( 'Block', $username ),
281 $this->msg( 'change-blocklink' )->escaped()
282 );
283 $tools[] = Linker::linkKnown( # Unblock link
284 SpecialPage::getTitleFor( 'Unblock', $username ),
285 $this->msg( 'unblocklink' )->escaped()
286 );
287 } else { # User is not blocked
288 $tools[] = Linker::linkKnown( # Block link
289 SpecialPage::getTitleFor( 'Block', $username ),
290 $this->msg( 'blocklink' )->escaped()
291 );
292 }
293 }
294 # Block log link
295 $tools[] = Linker::linkKnown(
296 SpecialPage::getTitleFor( 'Log', 'block' ),
297 $this->msg( 'sp-contributions-blocklog' )->escaped(),
298 array(),
299 array(
300 'page' => $userpage->getPrefixedText()
301 )
302 );
303 }
304 # Uploads
305 $tools[] = Linker::linkKnown(
306 SpecialPage::getTitleFor( 'Listfiles', $username ),
307 $this->msg( 'sp-contributions-uploads' )->escaped()
308 );
309
310 # Other logs link
311 $tools[] = Linker::linkKnown(
312 SpecialPage::getTitleFor( 'Log', $username ),
313 $this->msg( 'sp-contributions-logs' )->escaped()
314 );
315
316 # Add link to deleted user contributions for priviledged users
317 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
318 $tools[] = Linker::linkKnown(
319 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
320 $this->msg( 'sp-contributions-deleted' )->escaped()
321 );
322 }
323
324 # Add a link to change user rights for privileged users
325 $userrightsPage = new UserrightsPage();
326 $userrightsPage->setContext( $this->getContext() );
327 if ( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
328 $tools[] = Linker::linkKnown(
329 SpecialPage::getTitleFor( 'Userrights', $username ),
330 $this->msg( 'sp-contributions-userrights' )->escaped()
331 );
332 }
333
334 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
335 return $tools;
336 }
337
338 /**
339 * Generates the namespace selector form with hidden attributes.
340 * @return String: HTML fragment
341 */
342 protected function getForm() {
343 global $wgScript;
344
345 $this->opts['title'] = $this->getTitle()->getPrefixedText();
346 if ( !isset( $this->opts['target'] ) ) {
347 $this->opts['target'] = '';
348 } else {
349 $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
350 }
351
352 if ( !isset( $this->opts['namespace'] ) ) {
353 $this->opts['namespace'] = '';
354 }
355
356 if ( !isset( $this->opts['nsInvert'] ) ) {
357 $this->opts['nsInvert'] = '';
358 }
359
360 if ( !isset( $this->opts['associated'] ) ) {
361 $this->opts['associated'] = false;
362 }
363
364 if ( !isset( $this->opts['contribs'] ) ) {
365 $this->opts['contribs'] = 'user';
366 }
367
368 if ( !isset( $this->opts['year'] ) ) {
369 $this->opts['year'] = '';
370 }
371
372 if ( !isset( $this->opts['month'] ) ) {
373 $this->opts['month'] = '';
374 }
375
376 if ( $this->opts['contribs'] == 'newbie' ) {
377 $this->opts['target'] = '';
378 }
379
380 if ( !isset( $this->opts['tagfilter'] ) ) {
381 $this->opts['tagfilter'] = '';
382 }
383
384 if ( !isset( $this->opts['topOnly'] ) ) {
385 $this->opts['topOnly'] = false;
386 }
387
388 $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
389
390 # Add hidden params for tracking except for parameters in $skipParameters
391 $skipParameters = array( 'namespace', 'nsInvert', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly', 'associated' );
392 foreach ( $this->opts as $name => $value ) {
393 if ( in_array( $name, $skipParameters ) ) {
394 continue;
395 }
396 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
397 }
398
399 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
400
401 if ( $tagFilter ) {
402 $filterSelection =
403 Xml::tags( 'td', array( 'class' => 'mw-label' ), array_shift( $tagFilter ) ) .
404 Xml::tags( 'td', array( 'class' => 'mw-input' ), implode( '&#160', $tagFilter ) );
405 } else {
406 $filterSelection = Xml::tags( 'td', array( 'colspan' => 2 ), '' );
407 }
408
409 $targetSelection = Xml::tags( 'td', array( 'colspan' => 2 ),
410 Xml::radioLabel(
411 $this->msg( 'sp-contributions-newbies' )->text(),
412 'contribs',
413 'newbie' ,
414 'newbie',
415 $this->opts['contribs'] == 'newbie',
416 array( 'class' => 'mw-input' )
417 ) . '<br />' .
418 Xml::radioLabel(
419 $this->msg( 'sp-contributions-username' )->text(),
420 'contribs',
421 'user',
422 'user',
423 $this->opts['contribs'] == 'user',
424 array( 'class' => 'mw-input' )
425 ) . ' ' .
426 Html::input(
427 'target',
428 $this->opts['target'],
429 'text',
430 array( 'size' => '20', 'required' => '', 'class' => 'mw-input' ) +
431 ( $this->opts['target'] ? array() : array( 'autofocus' )
432 )
433 ) . ' '
434 ) ;
435
436 $namespaceSelection =
437 Xml::tags( 'td', array( 'class' => 'mw-label' ),
438 Xml::label(
439 $this->msg( 'namespace' )->text(),
440 'namespace',
441 ''
442 )
443 ) .
444 Xml::tags( 'td', null,
445 Xml::namespaceSelector( $this->opts['namespace'], '' ) . '&#160;' .
446 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
447 Xml::checkLabel(
448 $this->msg( 'invert' )->text(),
449 'nsInvert',
450 'nsInvert',
451 $this->opts['nsInvert'],
452 array( 'title' => $this->msg( 'tooltip-invert' )->text(), 'class' => 'mw-input' )
453 ) . '&#160;'
454 ) .
455 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
456 Xml::checkLabel(
457 $this->msg( 'namespace_association' )->text(),
458 'associated',
459 'associated',
460 $this->opts['associated'],
461 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text(), 'class' => 'mw-input' )
462 ) . '&#160;'
463 )
464 ) ;
465
466 $extraOptions = Xml::tags( 'td', array( 'colspan' => 2 ),
467 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
468 Xml::checkLabel(
469 $this->msg( 'history-show-deleted' )->text(),
470 'deletedOnly',
471 'mw-show-deleted-only',
472 $this->opts['deletedOnly'],
473 array( 'class' => 'mw-input' )
474 )
475 ) .
476 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
477 Xml::checkLabel(
478 $this->msg( 'sp-contributions-toponly' )->text(),
479 'topOnly',
480 'mw-show-top-only',
481 $this->opts['topOnly'],
482 array( 'class' => 'mw-input' )
483 )
484 )
485 ) ;
486
487 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
488 Xml::dateMenu(
489 $this->opts['year'],
490 $this->opts['month']
491 ) . ' ' .
492 Xml::submitButton(
493 $this->msg( 'sp-contributions-submit' )->text(),
494 array( 'class' => 'mw-submit' )
495 )
496 ) ;
497
498 $form .=
499 Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() ) .
500 Xml::openElement( 'table', array( 'class' => 'mw-contributions-table' ) ) .
501 Xml::openElement( 'tr' ) .
502 $targetSelection .
503 Xml::closeElement( 'tr' ) .
504 Xml::openElement( 'tr' ) .
505 $namespaceSelection .
506 Xml::closeElement( 'tr' ) .
507 Xml::openElement( 'tr' ) .
508 $filterSelection .
509 Xml::closeElement( 'tr' ) .
510 Xml::openElement( 'tr' ) .
511 $extraOptions .
512 Xml::closeElement( 'tr' ) .
513 Xml::openElement( 'tr' ) .
514 $dateSelectionAndSubmit .
515 Xml::closeElement( 'tr' ) .
516 Xml::closeElement( 'table' );
517
518 $explain = $this->msg( 'sp-contributions-explain' );
519 if ( $explain->exists() ) {
520 $form .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
521 }
522 $form .= Xml::closeElement( 'fieldset' ) .
523 Xml::closeElement( 'form' );
524 return $form;
525 }
526 }
527
528 /**
529 * Pager for Special:Contributions
530 * @ingroup SpecialPage Pager
531 */
532 class ContribsPager extends ReverseChronologicalPager {
533 public $mDefaultDirection = true;
534 var $messages, $target;
535 var $namespace = '', $mDb;
536 var $preventClickjacking = false;
537
538 function __construct( IContextSource $context, array $options ) {
539 parent::__construct( $context );
540
541 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
542
543 foreach ( $msgs as $msg ) {
544 $this->messages[$msg] = $this->msg( $msg )->escaped();
545 }
546
547 $this->target = isset( $options['target'] ) ? $options['target'] : '';
548 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
549 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
550 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
551 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
552 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
553
554 $this->deletedOnly = !empty( $options['deletedOnly'] );
555 $this->topOnly = !empty( $options['topOnly'] );
556
557 $year = isset( $options['year'] ) ? $options['year'] : false;
558 $month = isset( $options['month'] ) ? $options['month'] : false;
559 $this->getDateCond( $year, $month );
560
561 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
562 }
563
564 function getDefaultQuery() {
565 $query = parent::getDefaultQuery();
566 $query['target'] = $this->target;
567 return $query;
568 }
569
570 function getQueryInfo() {
571 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
572
573 $user = $this->getUser();
574 $conds = array_merge( $userCond, $this->getNamespaceCond() );
575
576 // Paranoia: avoid brute force searches (bug 17342)
577 if ( !$user->isAllowed( 'deletedhistory' ) ) {
578 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
579 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
580 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
581 ' != ' . Revision::SUPPRESSED_USER;
582 }
583
584 # Don't include orphaned revisions
585 $join_cond['page'] = Revision::pageJoinCond();
586 # Get the current user name for accounts
587 $join_cond['user'] = Revision::userJoinCond();
588
589 $queryInfo = array(
590 'tables' => $tables,
591 'fields' => array_merge(
592 Revision::selectFields(),
593 Revision::selectUserFields(),
594 array( 'page_namespace', 'page_title', 'page_is_new',
595 'page_latest', 'page_is_redirect', 'page_len' )
596 ),
597 'conds' => $conds,
598 'options' => array( 'USE INDEX' => array( 'revision' => $index ) ),
599 'join_conds' => $join_cond
600 );
601
602 ChangeTags::modifyDisplayQuery(
603 $queryInfo['tables'],
604 $queryInfo['fields'],
605 $queryInfo['conds'],
606 $queryInfo['join_conds'],
607 $queryInfo['options'],
608 $this->tagFilter
609 );
610
611 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
612 return $queryInfo;
613 }
614
615 function getUserCond() {
616 $condition = array();
617 $join_conds = array();
618 $tables = array( 'revision', 'page', 'user' );
619 if ( $this->contribs == 'newbie' ) {
620 $tables[] = 'user_groups';
621 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
622 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
623 $condition[] = 'ug_group IS NULL';
624 $index = 'user_timestamp';
625 # @todo FIXME: Other groups may have 'bot' rights
626 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
627 } else {
628 if ( IP::isIPAddress( $this->target ) ) {
629 $condition['rev_user_text'] = $this->target;
630 $index = 'usertext_timestamp';
631 } else {
632 $condition['rev_user'] = User::idFromName( $this->target );
633 $index = 'user_timestamp';
634 }
635 }
636 if ( $this->deletedOnly ) {
637 $condition[] = "rev_deleted != '0'";
638 }
639 if ( $this->topOnly ) {
640 $condition[] = "rev_id = page_latest";
641 }
642 return array( $tables, $index, $condition, $join_conds );
643 }
644
645 function getNamespaceCond() {
646 if ( $this->namespace !== '' ) {
647 $selectedNS = $this->mDb->addQuotes( $this->namespace );
648 $eq_op = $this->nsInvert ? '!=' : '=';
649 $bool_op = $this->nsInvert ? 'AND' : 'OR';
650
651 if ( !$this->associated ) {
652 return array( "page_namespace $eq_op $selectedNS" );
653 } else {
654 $associatedNS = $this->mDb->addQuotes (
655 MWNamespace::getAssociated( $this->namespace )
656 );
657 return array(
658 "page_namespace $eq_op $selectedNS " .
659 $bool_op .
660 " page_namespace $eq_op $associatedNS"
661 );
662 }
663
664 } else {
665 return array();
666 }
667 }
668
669 function getIndexField() {
670 return 'rev_timestamp';
671 }
672
673 function doBatchLookups() {
674 $this->mResult->rewind();
675 $revIds = array();
676 foreach ( $this->mResult as $row ) {
677 if( $row->rev_parent_id ) {
678 $revIds[] = $row->rev_parent_id;
679 }
680 }
681 $this->mParentLens = $this->getParentLengths( $revIds );
682 $this->mResult->rewind(); // reset
683
684 # Do a link batch query
685 $this->mResult->seek( 0 );
686 $batch = new LinkBatch();
687 # Give some pointers to make (last) links
688 foreach ( $this->mResult as $row ) {
689 if ( $this->contribs === 'newbie' ) { // multiple users
690 $batch->add( NS_USER, $row->user_name );
691 $batch->add( NS_USER_TALK, $row->user_name );
692 }
693 $batch->add( $row->page_namespace, $row->page_title );
694 }
695 $batch->execute();
696 $this->mResult->seek( 0 );
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 $parentLen = isset( $this->mParentLens[$row->rev_parent_id] ) ? $this->mParentLens[$row->rev_parent_id] : 0;
793 $chardiff = ' . . ' . ChangesList::showCharacterDifference(
794 $parentLen, $row->rev_len ) . ' . . ';
795
796 $lang = $this->getLanguage();
797 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
798 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
799 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
800 $d = Linker::linkKnown(
801 $page,
802 htmlspecialchars( $date ),
803 array(),
804 array( 'oldid' => intval( $row->rev_id ) )
805 );
806 } else {
807 $d = htmlspecialchars( $date );
808 }
809 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
810 $d = '<span class="history-deleted">' . $d . '</span>';
811 }
812
813 # Show user names for /newbies as there may be different users.
814 # Note that we already excluded rows with hidden user names.
815 if ( $this->contribs == 'newbie' ) {
816 $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
817 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
818 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
819 } else {
820 $userlink = '';
821 }
822
823 if ( $rev->getParentId() === 0 ) {
824 $nflag = ChangesList::flag( 'newpage' );
825 } else {
826 $nflag = '';
827 }
828
829 if ( $rev->isMinor() ) {
830 $mflag = ChangesList::flag( 'minor' );
831 } else {
832 $mflag = '';
833 }
834
835 $del = Linker::getRevDeleteLink( $user, $rev, $page );
836 if ( $del !== '' ) {
837 $del .= ' ';
838 }
839
840 $diffHistLinks = '(' . $difftext . $this->messages['pipe-separator'] . $histlink . ')';
841 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
842
843 # Denote if username is redacted for this edit
844 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
845 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
846 }
847
848 # Tags, if any.
849 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
850 $classes = array_merge( $classes, $newClasses );
851 $ret .= " $tagSummary";
852
853 // Let extensions add data
854 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
855
856 $classes = implode( ' ', $classes );
857 $ret = "<li class=\"$classes\">$ret</li>\n";
858 wfProfileOut( __METHOD__ );
859 return $ret;
860 }
861
862 /**
863 * Get the Database object in use
864 *
865 * @return DatabaseBase
866 */
867 public function getDatabase() {
868 return $this->mDb;
869 }
870
871 /**
872 * Overwrite Pager function and return a helpful comment
873 * @return string
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 }