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