Documentation, spaces, braces, explicit member variables
[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 $links = '';
224 if ( $talk ) {
225 $tools = $this->getUserLinks( $nt, $talk, $userObj );
226 $links = $this->getLanguage()->pipeList( $tools );
227
228 // Show a note if the user is blocked and display the last block log entry.
229 if ( $userObj->isBlocked() ) {
230 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
231 LogEventsList::showLogExtract(
232 $out,
233 'block',
234 $nt,
235 '',
236 array(
237 'lim' => 1,
238 'showIfEmpty' => false,
239 'msgKey' => array(
240 $userObj->isAnon() ?
241 'sp-contributions-blocked-notice-anon' :
242 'sp-contributions-blocked-notice',
243 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
244 ),
245 'offset' => '' # don't use WebRequest parameter offset
246 )
247 );
248 }
249 }
250
251 // Old message 'contribsub' had one parameter, but that doesn't work for
252 // languages that want to put the "for" bit right after $user but before
253 // $links. If 'contribsub' is around, use it for reverse compatibility,
254 // otherwise use 'contribsub2'.
255 // @todo Should this be removed at some point?
256 $oldMsg = $this->msg( 'contribsub' );
257 if ( $oldMsg->exists() ) {
258 $linksWithParentheses = $this->msg( 'parentheses' )->rawParams( $links )->escaped();
259 return $oldMsg->rawParams( "$user $linksWithParentheses" );
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 Html::namespaceSelector( array(
449 'selected' => $this->opts['namespace'],
450 'all' => '',
451 ), array(
452 'name' => 'namespace',
453 'id' => 'namespace',
454 'class' => 'namespaceselector',
455 ) ) .
456 '&#160;' .
457 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
458 Xml::checkLabel(
459 $this->msg( 'invert' )->text(),
460 'nsInvert',
461 'nsInvert',
462 $this->opts['nsInvert'],
463 array( 'title' => $this->msg( 'tooltip-invert' )->text(), 'class' => 'mw-input' )
464 ) . '&#160;'
465 ) .
466 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
467 Xml::checkLabel(
468 $this->msg( 'namespace_association' )->text(),
469 'associated',
470 'associated',
471 $this->opts['associated'],
472 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text(), 'class' => 'mw-input' )
473 ) . '&#160;'
474 )
475 ) ;
476
477 $extraOptions = Xml::tags( 'td', array( 'colspan' => 2 ),
478 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
479 Xml::checkLabel(
480 $this->msg( 'history-show-deleted' )->text(),
481 'deletedOnly',
482 'mw-show-deleted-only',
483 $this->opts['deletedOnly'],
484 array( 'class' => 'mw-input' )
485 )
486 ) .
487 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
488 Xml::checkLabel(
489 $this->msg( 'sp-contributions-toponly' )->text(),
490 'topOnly',
491 'mw-show-top-only',
492 $this->opts['topOnly'],
493 array( 'class' => 'mw-input' )
494 )
495 )
496 ) ;
497
498 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
499 Xml::dateMenu(
500 $this->opts['year'],
501 $this->opts['month']
502 ) . ' ' .
503 Xml::submitButton(
504 $this->msg( 'sp-contributions-submit' )->text(),
505 array( 'class' => 'mw-submit' )
506 )
507 ) ;
508
509 $form .=
510 Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() ) .
511 Xml::openElement( 'table', array( 'class' => 'mw-contributions-table' ) ) .
512 Xml::openElement( 'tr' ) .
513 $targetSelection .
514 Xml::closeElement( 'tr' ) .
515 Xml::openElement( 'tr' ) .
516 $namespaceSelection .
517 Xml::closeElement( 'tr' ) .
518 Xml::openElement( 'tr' ) .
519 $filterSelection .
520 Xml::closeElement( 'tr' ) .
521 Xml::openElement( 'tr' ) .
522 $extraOptions .
523 Xml::closeElement( 'tr' ) .
524 Xml::openElement( 'tr' ) .
525 $dateSelectionAndSubmit .
526 Xml::closeElement( 'tr' ) .
527 Xml::closeElement( 'table' );
528
529 $explain = $this->msg( 'sp-contributions-explain' );
530 if ( $explain->exists() ) {
531 $form .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
532 }
533 $form .= Xml::closeElement( 'fieldset' ) .
534 Xml::closeElement( 'form' );
535 return $form;
536 }
537 }
538
539 /**
540 * Pager for Special:Contributions
541 * @ingroup SpecialPage Pager
542 */
543 class ContribsPager extends ReverseChronologicalPager {
544 public $mDefaultDirection = true;
545 var $messages, $target;
546 var $namespace = '', $mDb;
547 var $preventClickjacking = false;
548
549 /**
550 * @var array
551 */
552 protected $mParentLens;
553
554 function __construct( IContextSource $context, array $options ) {
555 parent::__construct( $context );
556
557 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
558
559 foreach ( $msgs as $msg ) {
560 $this->messages[$msg] = $this->msg( $msg )->escaped();
561 }
562
563 $this->target = isset( $options['target'] ) ? $options['target'] : '';
564 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
565 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
566 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
567 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
568 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
569
570 $this->deletedOnly = !empty( $options['deletedOnly'] );
571 $this->topOnly = !empty( $options['topOnly'] );
572
573 $year = isset( $options['year'] ) ? $options['year'] : false;
574 $month = isset( $options['month'] ) ? $options['month'] : false;
575 $this->getDateCond( $year, $month );
576
577 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
578 }
579
580 function getDefaultQuery() {
581 $query = parent::getDefaultQuery();
582 $query['target'] = $this->target;
583 return $query;
584 }
585
586 function getQueryInfo() {
587 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
588
589 $user = $this->getUser();
590 $conds = array_merge( $userCond, $this->getNamespaceCond() );
591
592 // Paranoia: avoid brute force searches (bug 17342)
593 if ( !$user->isAllowed( 'deletedhistory' ) ) {
594 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
595 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
596 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
597 ' != ' . Revision::SUPPRESSED_USER;
598 }
599
600 # Don't include orphaned revisions
601 $join_cond['page'] = Revision::pageJoinCond();
602 # Get the current user name for accounts
603 $join_cond['user'] = Revision::userJoinCond();
604
605 $queryInfo = array(
606 'tables' => $tables,
607 'fields' => array_merge(
608 Revision::selectFields(),
609 Revision::selectUserFields(),
610 array( 'page_namespace', 'page_title', 'page_is_new',
611 'page_latest', 'page_is_redirect', 'page_len' )
612 ),
613 'conds' => $conds,
614 'options' => array( 'USE INDEX' => array( 'revision' => $index ) ),
615 'join_conds' => $join_cond
616 );
617
618 ChangeTags::modifyDisplayQuery(
619 $queryInfo['tables'],
620 $queryInfo['fields'],
621 $queryInfo['conds'],
622 $queryInfo['join_conds'],
623 $queryInfo['options'],
624 $this->tagFilter
625 );
626
627 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
628 return $queryInfo;
629 }
630
631 function getUserCond() {
632 $condition = array();
633 $join_conds = array();
634 $tables = array( 'revision', 'page', 'user' );
635 if ( $this->contribs == 'newbie' ) {
636 $tables[] = 'user_groups';
637 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
638 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
639 $condition[] = 'ug_group IS NULL';
640 $index = 'user_timestamp';
641 # @todo FIXME: Other groups may have 'bot' rights
642 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
643 } else {
644 $uid = User::idFromName( $this->target );
645 if ( $uid ) {
646 $condition['rev_user'] = $uid;
647 $index = 'user_timestamp';
648 } else {
649 $condition['rev_user_text'] = $this->target;
650 $index = 'usertext_timestamp';
651 }
652 }
653 if ( $this->deletedOnly ) {
654 $condition[] = "rev_deleted != '0'";
655 }
656 if ( $this->topOnly ) {
657 $condition[] = "rev_id = page_latest";
658 }
659 return array( $tables, $index, $condition, $join_conds );
660 }
661
662 function getNamespaceCond() {
663 if ( $this->namespace !== '' ) {
664 $selectedNS = $this->mDb->addQuotes( $this->namespace );
665 $eq_op = $this->nsInvert ? '!=' : '=';
666 $bool_op = $this->nsInvert ? 'AND' : 'OR';
667
668 if ( !$this->associated ) {
669 return array( "page_namespace $eq_op $selectedNS" );
670 } else {
671 $associatedNS = $this->mDb->addQuotes (
672 MWNamespace::getAssociated( $this->namespace )
673 );
674 return array(
675 "page_namespace $eq_op $selectedNS " .
676 $bool_op .
677 " page_namespace $eq_op $associatedNS"
678 );
679 }
680
681 } else {
682 return array();
683 }
684 }
685
686 function getIndexField() {
687 return 'rev_timestamp';
688 }
689
690 function doBatchLookups() {
691 $this->mResult->rewind();
692 $revIds = array();
693 foreach ( $this->mResult as $row ) {
694 if( $row->rev_parent_id ) {
695 $revIds[] = $row->rev_parent_id;
696 }
697 }
698 $this->mParentLens = $this->getParentLengths( $revIds );
699 $this->mResult->rewind(); // reset
700
701 # Do a link batch query
702 $this->mResult->seek( 0 );
703 $batch = new LinkBatch();
704 # Give some pointers to make (last) links
705 foreach ( $this->mResult as $row ) {
706 if ( $this->contribs === 'newbie' ) { // multiple users
707 $batch->add( NS_USER, $row->user_name );
708 $batch->add( NS_USER_TALK, $row->user_name );
709 }
710 $batch->add( $row->page_namespace, $row->page_title );
711 }
712 $batch->execute();
713 $this->mResult->seek( 0 );
714 }
715
716 /**
717 * Do a batched query to get the parent revision lengths
718 * @param $revIds array
719 * @return array
720 */
721 private function getParentLengths( array $revIds ) {
722 $revLens = array();
723 if ( !$revIds ) {
724 return $revLens; // empty
725 }
726 wfProfileIn( __METHOD__ );
727 $res = $this->getDatabase()->select( 'revision',
728 array( 'rev_id', 'rev_len' ),
729 array( 'rev_id' => $revIds ),
730 __METHOD__ );
731 foreach ( $res as $row ) {
732 $revLens[$row->rev_id] = $row->rev_len;
733 }
734 wfProfileOut( __METHOD__ );
735 return $revLens;
736 }
737
738 /**
739 * @return string
740 */
741 function getStartBody() {
742 return "<ul>\n";
743 }
744
745 /**
746 * @return string
747 */
748 function getEndBody() {
749 return "</ul>\n";
750 }
751
752 /**
753 * Generates each row in the contributions list.
754 *
755 * Contributions which are marked "top" are currently on top of the history.
756 * For these contributions, a [rollback] link is shown for users with roll-
757 * back privileges. The rollback link restores the most recent version that
758 * was not written by the target user.
759 *
760 * @todo This would probably look a lot nicer in a table.
761 * @param $row
762 * @return string
763 */
764 function formatRow( $row ) {
765 wfProfileIn( __METHOD__ );
766
767 $rev = new Revision( $row );
768 $classes = array();
769
770 $page = Title::newFromRow( $row );
771 $link = Linker::link(
772 $page,
773 htmlspecialchars( $page->getPrefixedText() ),
774 array(),
775 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
776 );
777 # Mark current revisions
778 $topmarktext = '';
779 if ( $row->rev_id == $row->page_latest ) {
780 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
781 # Add rollback link
782 if ( !$row->page_is_new && $page->quickUserCan( 'rollback' )
783 && $page->quickUserCan( 'edit' ) )
784 {
785 $this->preventClickjacking();
786 $topmarktext .= ' ' . Linker::generateRollback( $rev );
787 }
788 }
789 $user = $this->getUser();
790 # Is there a visible previous revision?
791 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
792 $difftext = Linker::linkKnown(
793 $page,
794 $this->messages['diff'],
795 array(),
796 array(
797 'diff' => 'prev',
798 'oldid' => $row->rev_id
799 )
800 );
801 } else {
802 $difftext = $this->messages['diff'];
803 }
804 $histlink = Linker::linkKnown(
805 $page,
806 $this->messages['hist'],
807 array(),
808 array( 'action' => 'history' )
809 );
810
811 if ( $row->rev_parent_id === null ) {
812 // For some reason rev_parent_id isn't populated for this row.
813 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
814 // Next best thing is to have the total number of bytes.
815 $chardiff = ' . . ' . Linker::formatRevisionSize( $row->rev_len ) . ' . . ';
816 } else {
817 $parentLen = isset( $this->mParentLens[$row->rev_parent_id] ) ? $this->mParentLens[$row->rev_parent_id] : 0;
818 $chardiff = ' . . ' . ChangesList::showCharacterDifference(
819 $parentLen, $row->rev_len ) . ' . . ';
820 }
821
822 $lang = $this->getLanguage();
823 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
824 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
825 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
826 $d = Linker::linkKnown(
827 $page,
828 htmlspecialchars( $date ),
829 array(),
830 array( 'oldid' => intval( $row->rev_id ) )
831 );
832 } else {
833 $d = htmlspecialchars( $date );
834 }
835 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
836 $d = '<span class="history-deleted">' . $d . '</span>';
837 }
838
839 # Show user names for /newbies as there may be different users.
840 # Note that we already excluded rows with hidden user names.
841 if ( $this->contribs == 'newbie' ) {
842 $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
843 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
844 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
845 } else {
846 $userlink = '';
847 }
848
849 if ( $rev->getParentId() === 0 ) {
850 $nflag = ChangesList::flag( 'newpage' );
851 } else {
852 $nflag = '';
853 }
854
855 if ( $rev->isMinor() ) {
856 $mflag = ChangesList::flag( 'minor' );
857 } else {
858 $mflag = '';
859 }
860
861 $del = Linker::getRevDeleteLink( $user, $rev, $page );
862 if ( $del !== '' ) {
863 $del .= ' ';
864 }
865
866 $diffHistLinks = $this->msg( 'parentheses' )->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )->escaped();
867 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
868
869 # Denote if username is redacted for this edit
870 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
871 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
872 }
873
874 # Tags, if any.
875 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
876 $classes = array_merge( $classes, $newClasses );
877 $ret .= " $tagSummary";
878
879 // Let extensions add data
880 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
881
882 $classes = implode( ' ', $classes );
883 $ret = "<li class=\"$classes\">$ret</li>\n";
884 wfProfileOut( __METHOD__ );
885 return $ret;
886 }
887
888 /**
889 * Get the Database object in use
890 *
891 * @return DatabaseBase
892 */
893 public function getDatabase() {
894 return $this->mDb;
895 }
896
897 /**
898 * Overwrite Pager function and return a helpful comment
899 * @return string
900 */
901 function getSqlComment() {
902 if ( $this->namespace || $this->deletedOnly ) {
903 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
904 } else {
905 return 'contributions page unfiltered';
906 }
907 }
908
909 protected function preventClickjacking() {
910 $this->preventClickjacking = true;
911 }
912
913 /**
914 * @return bool
915 */
916 public function getPreventClickjacking() {
917 return $this->preventClickjacking;
918 }
919 }