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