501ccee517e9670e73e35a933af5ce4edb718103
[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 // @todo Should this be removed at some point?
255 $oldMsg = $this->msg( 'contribsub' );
256 if ( $oldMsg->exists() ) {
257 $linksWithParentheses = $this->msg( 'parentheses' )->rawParams( $links )->escaped();
258 return $oldMsg->rawParams( "$user $linksWithParentheses" );
259 } else {
260 return $this->msg( 'contribsub2' )->rawParams( $user, $links );
261 }
262 }
263
264 /**
265 * Links to different places.
266 * @param $userpage Title: Target user page
267 * @param $talkpage Title: Talk page
268 * @param $target User: Target user object
269 * @return array
270 */
271 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
272
273 $id = $target->getId();
274 $username = $target->getName();
275
276 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
277
278 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
279 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
280 if ( $target->isBlocked() ) {
281 $tools[] = Linker::linkKnown( # Change block link
282 SpecialPage::getTitleFor( 'Block', $username ),
283 $this->msg( 'change-blocklink' )->escaped()
284 );
285 $tools[] = Linker::linkKnown( # Unblock link
286 SpecialPage::getTitleFor( 'Unblock', $username ),
287 $this->msg( 'unblocklink' )->escaped()
288 );
289 } else { # User is not blocked
290 $tools[] = Linker::linkKnown( # Block link
291 SpecialPage::getTitleFor( 'Block', $username ),
292 $this->msg( 'blocklink' )->escaped()
293 );
294 }
295 }
296 # Block log link
297 $tools[] = Linker::linkKnown(
298 SpecialPage::getTitleFor( 'Log', 'block' ),
299 $this->msg( 'sp-contributions-blocklog' )->escaped(),
300 array(),
301 array(
302 'page' => $userpage->getPrefixedText()
303 )
304 );
305 }
306 # Uploads
307 $tools[] = Linker::linkKnown(
308 SpecialPage::getTitleFor( 'Listfiles', $username ),
309 $this->msg( 'sp-contributions-uploads' )->escaped()
310 );
311
312 # Other logs link
313 $tools[] = Linker::linkKnown(
314 SpecialPage::getTitleFor( 'Log', $username ),
315 $this->msg( 'sp-contributions-logs' )->escaped()
316 );
317
318 # Add link to deleted user contributions for priviledged users
319 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
320 $tools[] = Linker::linkKnown(
321 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
322 $this->msg( 'sp-contributions-deleted' )->escaped()
323 );
324 }
325
326 # Add a link to change user rights for privileged users
327 $userrightsPage = new UserrightsPage();
328 $userrightsPage->setContext( $this->getContext() );
329 if ( $id !== null && $userrightsPage->userCanChangeRights( $target ) ) {
330 $tools[] = Linker::linkKnown(
331 SpecialPage::getTitleFor( 'Userrights', $username ),
332 $this->msg( 'sp-contributions-userrights' )->escaped()
333 );
334 }
335
336 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
337 return $tools;
338 }
339
340 /**
341 * Generates the namespace selector form with hidden attributes.
342 * @return String: HTML fragment
343 */
344 protected function getForm() {
345 global $wgScript;
346
347 $this->opts['title'] = $this->getTitle()->getPrefixedText();
348 if ( !isset( $this->opts['target'] ) ) {
349 $this->opts['target'] = '';
350 } else {
351 $this->opts['target'] = str_replace( '_' , ' ' , $this->opts['target'] );
352 }
353
354 if ( !isset( $this->opts['namespace'] ) ) {
355 $this->opts['namespace'] = '';
356 }
357
358 if ( !isset( $this->opts['nsInvert'] ) ) {
359 $this->opts['nsInvert'] = '';
360 }
361
362 if ( !isset( $this->opts['associated'] ) ) {
363 $this->opts['associated'] = false;
364 }
365
366 if ( !isset( $this->opts['contribs'] ) ) {
367 $this->opts['contribs'] = 'user';
368 }
369
370 if ( !isset( $this->opts['year'] ) ) {
371 $this->opts['year'] = '';
372 }
373
374 if ( !isset( $this->opts['month'] ) ) {
375 $this->opts['month'] = '';
376 }
377
378 if ( $this->opts['contribs'] == 'newbie' ) {
379 $this->opts['target'] = '';
380 }
381
382 if ( !isset( $this->opts['tagfilter'] ) ) {
383 $this->opts['tagfilter'] = '';
384 }
385
386 if ( !isset( $this->opts['topOnly'] ) ) {
387 $this->opts['topOnly'] = false;
388 }
389
390 $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'class' => 'mw-contributions-form' ) );
391
392 # Add hidden params for tracking except for parameters in $skipParameters
393 $skipParameters = array( 'namespace', 'nsInvert', 'deletedOnly', 'target', 'contribs', 'year', 'month', 'topOnly', 'associated' );
394 foreach ( $this->opts as $name => $value ) {
395 if ( in_array( $name, $skipParameters ) ) {
396 continue;
397 }
398 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
399 }
400
401 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
402
403 if ( $tagFilter ) {
404 $filterSelection =
405 Xml::tags( 'td', array( 'class' => 'mw-label' ), array_shift( $tagFilter ) ) .
406 Xml::tags( 'td', array( 'class' => 'mw-input' ), implode( '&#160', $tagFilter ) );
407 } else {
408 $filterSelection = Xml::tags( 'td', array( 'colspan' => 2 ), '' );
409 }
410
411 $targetSelection = Xml::tags( 'td', array( 'colspan' => 2 ),
412 Xml::radioLabel(
413 $this->msg( 'sp-contributions-newbies' )->text(),
414 'contribs',
415 'newbie' ,
416 'newbie',
417 $this->opts['contribs'] == 'newbie',
418 array( 'class' => 'mw-input' )
419 ) . '<br />' .
420 Xml::radioLabel(
421 $this->msg( 'sp-contributions-username' )->text(),
422 'contribs',
423 'user',
424 'user',
425 $this->opts['contribs'] == 'user',
426 array( 'class' => 'mw-input' )
427 ) . ' ' .
428 Html::input(
429 'target',
430 $this->opts['target'],
431 'text',
432 array( 'size' => '20', 'required' => '', 'class' => 'mw-input' ) +
433 ( $this->opts['target'] ? array() : array( 'autofocus' )
434 )
435 ) . ' '
436 ) ;
437
438 $namespaceSelection =
439 Xml::tags( 'td', array( 'class' => 'mw-label' ),
440 Xml::label(
441 $this->msg( 'namespace' )->text(),
442 'namespace',
443 ''
444 )
445 ) .
446 Xml::tags( 'td', null,
447 Html::namespaceSelector( array(
448 'selected' => $this->opts['namespace'],
449 'all' => '',
450 ), array(
451 'name' => 'namespace',
452 'id' => 'namespace',
453 'class' => 'namespaceselector',
454 ) ) .
455 '&#160;' .
456 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
457 Xml::checkLabel(
458 $this->msg( 'invert' )->text(),
459 'nsInvert',
460 'nsInvert',
461 $this->opts['nsInvert'],
462 array( 'title' => $this->msg( 'tooltip-invert' )->text(), 'class' => 'mw-input' )
463 ) . '&#160;'
464 ) .
465 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
466 Xml::checkLabel(
467 $this->msg( 'namespace_association' )->text(),
468 'associated',
469 'associated',
470 $this->opts['associated'],
471 array( 'title' => $this->msg( 'tooltip-namespace_association' )->text(), 'class' => 'mw-input' )
472 ) . '&#160;'
473 )
474 ) ;
475
476 $extraOptions = Xml::tags( 'td', array( 'colspan' => 2 ),
477 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
478 Xml::checkLabel(
479 $this->msg( 'history-show-deleted' )->text(),
480 'deletedOnly',
481 'mw-show-deleted-only',
482 $this->opts['deletedOnly'],
483 array( 'class' => 'mw-input' )
484 )
485 ) .
486 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
487 Xml::checkLabel(
488 $this->msg( 'sp-contributions-toponly' )->text(),
489 'topOnly',
490 'mw-show-top-only',
491 $this->opts['topOnly'],
492 array( 'class' => 'mw-input' )
493 )
494 )
495 ) ;
496
497 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
498 Xml::dateMenu(
499 $this->opts['year'],
500 $this->opts['month']
501 ) . ' ' .
502 Xml::submitButton(
503 $this->msg( 'sp-contributions-submit' )->text(),
504 array( 'class' => 'mw-submit' )
505 )
506 ) ;
507
508 $form .=
509 Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() ) .
510 Xml::openElement( 'table', array( 'class' => 'mw-contributions-table' ) ) .
511 Xml::openElement( 'tr' ) .
512 $targetSelection .
513 Xml::closeElement( 'tr' ) .
514 Xml::openElement( 'tr' ) .
515 $namespaceSelection .
516 Xml::closeElement( 'tr' ) .
517 Xml::openElement( 'tr' ) .
518 $filterSelection .
519 Xml::closeElement( 'tr' ) .
520 Xml::openElement( 'tr' ) .
521 $extraOptions .
522 Xml::closeElement( 'tr' ) .
523 Xml::openElement( 'tr' ) .
524 $dateSelectionAndSubmit .
525 Xml::closeElement( 'tr' ) .
526 Xml::closeElement( 'table' );
527
528 $explain = $this->msg( 'sp-contributions-explain' );
529 if ( $explain->exists() ) {
530 $form .= "<p id='mw-sp-contributions-explain'>{$explain}</p>";
531 }
532 $form .= Xml::closeElement( 'fieldset' ) .
533 Xml::closeElement( 'form' );
534 return $form;
535 }
536 }
537
538 /**
539 * Pager for Special:Contributions
540 * @ingroup SpecialPage Pager
541 */
542 class ContribsPager extends ReverseChronologicalPager {
543 public $mDefaultDirection = true;
544 var $messages, $target;
545 var $namespace = '', $mDb;
546 var $preventClickjacking = false;
547
548 function __construct( IContextSource $context, array $options ) {
549 parent::__construct( $context );
550
551 $msgs = array( 'uctop', 'diff', 'newarticle', 'rollbacklink', 'diff', 'hist', 'rev-delundel', 'pipe-separator' );
552
553 foreach ( $msgs as $msg ) {
554 $this->messages[$msg] = $this->msg( $msg )->escaped();
555 }
556
557 $this->target = isset( $options['target'] ) ? $options['target'] : '';
558 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
559 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
560 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
561 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
562 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
563
564 $this->deletedOnly = !empty( $options['deletedOnly'] );
565 $this->topOnly = !empty( $options['topOnly'] );
566
567 $year = isset( $options['year'] ) ? $options['year'] : false;
568 $month = isset( $options['month'] ) ? $options['month'] : false;
569 $this->getDateCond( $year, $month );
570
571 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
572 }
573
574 function getDefaultQuery() {
575 $query = parent::getDefaultQuery();
576 $query['target'] = $this->target;
577 return $query;
578 }
579
580 function getQueryInfo() {
581 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
582
583 $user = $this->getUser();
584 $conds = array_merge( $userCond, $this->getNamespaceCond() );
585
586 // Paranoia: avoid brute force searches (bug 17342)
587 if ( !$user->isAllowed( 'deletedhistory' ) ) {
588 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
589 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
590 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
591 ' != ' . Revision::SUPPRESSED_USER;
592 }
593
594 # Don't include orphaned revisions
595 $join_cond['page'] = Revision::pageJoinCond();
596 # Get the current user name for accounts
597 $join_cond['user'] = Revision::userJoinCond();
598
599 $queryInfo = array(
600 'tables' => $tables,
601 'fields' => array_merge(
602 Revision::selectFields(),
603 Revision::selectUserFields(),
604 array( 'page_namespace', 'page_title', 'page_is_new',
605 'page_latest', 'page_is_redirect', 'page_len' )
606 ),
607 'conds' => $conds,
608 'options' => array( 'USE INDEX' => array( 'revision' => $index ) ),
609 'join_conds' => $join_cond
610 );
611
612 ChangeTags::modifyDisplayQuery(
613 $queryInfo['tables'],
614 $queryInfo['fields'],
615 $queryInfo['conds'],
616 $queryInfo['join_conds'],
617 $queryInfo['options'],
618 $this->tagFilter
619 );
620
621 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
622 return $queryInfo;
623 }
624
625 function getUserCond() {
626 $condition = array();
627 $join_conds = array();
628 $tables = array( 'revision', 'page', 'user' );
629 if ( $this->contribs == 'newbie' ) {
630 $tables[] = 'user_groups';
631 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
632 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
633 $condition[] = 'ug_group IS NULL';
634 $index = 'user_timestamp';
635 # @todo FIXME: Other groups may have 'bot' rights
636 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
637 } else {
638 $uid = User::idFromName( $this->target );
639 if ( $uid ) {
640 $condition['rev_user'] = $uid;
641 $index = 'user_timestamp';
642 } else {
643 $condition['rev_user_text'] = $this->target;
644 $index = 'usertext_timestamp';
645 }
646 }
647 if ( $this->deletedOnly ) {
648 $condition[] = "rev_deleted != '0'";
649 }
650 if ( $this->topOnly ) {
651 $condition[] = "rev_id = page_latest";
652 }
653 return array( $tables, $index, $condition, $join_conds );
654 }
655
656 function getNamespaceCond() {
657 if ( $this->namespace !== '' ) {
658 $selectedNS = $this->mDb->addQuotes( $this->namespace );
659 $eq_op = $this->nsInvert ? '!=' : '=';
660 $bool_op = $this->nsInvert ? 'AND' : 'OR';
661
662 if ( !$this->associated ) {
663 return array( "page_namespace $eq_op $selectedNS" );
664 } else {
665 $associatedNS = $this->mDb->addQuotes (
666 MWNamespace::getAssociated( $this->namespace )
667 );
668 return array(
669 "page_namespace $eq_op $selectedNS " .
670 $bool_op .
671 " page_namespace $eq_op $associatedNS"
672 );
673 }
674
675 } else {
676 return array();
677 }
678 }
679
680 function getIndexField() {
681 return 'rev_timestamp';
682 }
683
684 function doBatchLookups() {
685 $this->mResult->rewind();
686 $revIds = array();
687 foreach ( $this->mResult as $row ) {
688 if( $row->rev_parent_id ) {
689 $revIds[] = $row->rev_parent_id;
690 }
691 }
692 $this->mParentLens = $this->getParentLengths( $revIds );
693 $this->mResult->rewind(); // reset
694
695 # Do a link batch query
696 $this->mResult->seek( 0 );
697 $batch = new LinkBatch();
698 # Give some pointers to make (last) links
699 foreach ( $this->mResult as $row ) {
700 if ( $this->contribs === 'newbie' ) { // multiple users
701 $batch->add( NS_USER, $row->user_name );
702 $batch->add( NS_USER_TALK, $row->user_name );
703 }
704 $batch->add( $row->page_namespace, $row->page_title );
705 }
706 $batch->execute();
707 $this->mResult->seek( 0 );
708 }
709
710 /**
711 * Do a batched query to get the parent revision lengths
712 * @return array
713 */
714 private function getParentLengths( array $revIds ) {
715 $revLens = array();
716 if ( !$revIds ) {
717 return $revLens; // empty
718 }
719 wfProfileIn( __METHOD__ );
720 $res = $this->getDatabase()->select( 'revision',
721 array( 'rev_id', 'rev_len' ),
722 array( 'rev_id' => $revIds ),
723 __METHOD__ );
724 foreach ( $res as $row ) {
725 $revLens[$row->rev_id] = $row->rev_len;
726 }
727 wfProfileOut( __METHOD__ );
728 return $revLens;
729 }
730
731 /**
732 * @return string
733 */
734 function getStartBody() {
735 return "<ul>\n";
736 }
737
738 /**
739 * @return string
740 */
741 function getEndBody() {
742 return "</ul>\n";
743 }
744
745 /**
746 * Generates each row in the contributions list.
747 *
748 * Contributions which are marked "top" are currently on top of the history.
749 * For these contributions, a [rollback] link is shown for users with roll-
750 * back privileges. The rollback link restores the most recent version that
751 * was not written by the target user.
752 *
753 * @todo This would probably look a lot nicer in a table.
754 * @return string
755 */
756 function formatRow( $row ) {
757 wfProfileIn( __METHOD__ );
758
759 $rev = new Revision( $row );
760 $classes = array();
761
762 $page = Title::newFromRow( $row );
763 $link = Linker::link(
764 $page,
765 htmlspecialchars( $page->getPrefixedText() ),
766 array(),
767 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
768 );
769 # Mark current revisions
770 $topmarktext = '';
771 if ( $row->rev_id == $row->page_latest ) {
772 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
773 # Add rollback link
774 if ( !$row->page_is_new && $page->quickUserCan( 'rollback' )
775 && $page->quickUserCan( 'edit' ) )
776 {
777 $this->preventClickjacking();
778 $topmarktext .= ' ' . Linker::generateRollback( $rev );
779 }
780 }
781 $user = $this->getUser();
782 # Is there a visible previous revision?
783 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
784 $difftext = Linker::linkKnown(
785 $page,
786 $this->messages['diff'],
787 array(),
788 array(
789 'diff' => 'prev',
790 'oldid' => $row->rev_id
791 )
792 );
793 } else {
794 $difftext = $this->messages['diff'];
795 }
796 $histlink = Linker::linkKnown(
797 $page,
798 $this->messages['hist'],
799 array(),
800 array( 'action' => 'history' )
801 );
802
803 if ( $row->rev_parent_id === null ) {
804 // For some reason rev_parent_id isn't populated for this row.
805 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
806 // Next best thing is to have the total number of bytes.
807 $chardiff = ' . . ' . Linker::formatRevisionSize( $row->rev_len ) . ' . . ';
808 } else {
809 $parentLen = isset( $this->mParentLens[$row->rev_parent_id] ) ? $this->mParentLens[$row->rev_parent_id] : 0;
810 $chardiff = ' . . ' . ChangesList::showCharacterDifference(
811 $parentLen, $row->rev_len ) . ' . . ';
812 }
813
814 $lang = $this->getLanguage();
815 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
816 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
817 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
818 $d = Linker::linkKnown(
819 $page,
820 htmlspecialchars( $date ),
821 array(),
822 array( 'oldid' => intval( $row->rev_id ) )
823 );
824 } else {
825 $d = htmlspecialchars( $date );
826 }
827 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
828 $d = '<span class="history-deleted">' . $d . '</span>';
829 }
830
831 # Show user names for /newbies as there may be different users.
832 # Note that we already excluded rows with hidden user names.
833 if ( $this->contribs == 'newbie' ) {
834 $userlink = ' . . ' . Linker::userLink( $rev->getUser(), $rev->getUserText() );
835 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
836 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
837 } else {
838 $userlink = '';
839 }
840
841 if ( $rev->getParentId() === 0 ) {
842 $nflag = ChangesList::flag( 'newpage' );
843 } else {
844 $nflag = '';
845 }
846
847 if ( $rev->isMinor() ) {
848 $mflag = ChangesList::flag( 'minor' );
849 } else {
850 $mflag = '';
851 }
852
853 $del = Linker::getRevDeleteLink( $user, $rev, $page );
854 if ( $del !== '' ) {
855 $del .= ' ';
856 }
857
858 $diffHistLinks = $this->msg( 'parentheses' )->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )->escaped();
859 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
860
861 # Denote if username is redacted for this edit
862 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
863 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
864 }
865
866 # Tags, if any.
867 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'contributions' );
868 $classes = array_merge( $classes, $newClasses );
869 $ret .= " $tagSummary";
870
871 // Let extensions add data
872 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
873
874 $classes = implode( ' ', $classes );
875 $ret = "<li class=\"$classes\">$ret</li>\n";
876 wfProfileOut( __METHOD__ );
877 return $ret;
878 }
879
880 /**
881 * Get the Database object in use
882 *
883 * @return DatabaseBase
884 */
885 public function getDatabase() {
886 return $this->mDb;
887 }
888
889 /**
890 * Overwrite Pager function and return a helpful comment
891 * @return string
892 */
893 function getSqlComment() {
894 if ( $this->namespace || $this->deletedOnly ) {
895 return 'contributions page filtered for namespace or RevisionDeleted edits'; // potentially slow, see CR r58153
896 } else {
897 return 'contributions page unfiltered';
898 }
899 }
900
901 protected function preventClickjacking() {
902 $this->preventClickjacking = true;
903 }
904
905 public function getPreventClickjacking() {
906 return $this->preventClickjacking;
907 }
908 }