Remove useless comments from search output
[mediawiki.git] / includes / specials / SpecialContributions.php
blob6a4467c1277d95e1d1c6f35dbbf0dff0794b74c8
1 <?php
2 /**
3 * Implements Special:Contributions
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special:Contributions, show user contributions in a paged list
27 * @ingroup SpecialPage
29 class SpecialContributions extends IncludableSpecialPage {
30 protected $opts;
32 public function __construct() {
33 parent::__construct( 'Contributions' );
36 public function execute( $par ) {
37 $this->setHeaders();
38 $this->outputHeader();
39 $out = $this->getOutput();
40 $out->addModuleStyles( 'mediawiki.special' );
42 $this->opts = array();
43 $request = $this->getRequest();
45 if ( $par !== null ) {
46 $target = $par;
47 } else {
48 $target = $request->getVal( 'target' );
51 // check for radiobox
52 if ( $request->getVal( 'contribs' ) == 'newbie' ) {
53 $target = 'newbies';
54 $this->opts['contribs'] = 'newbie';
55 } elseif ( $par === 'newbies' ) { // b/c for WMF
56 $target = 'newbies';
57 $this->opts['contribs'] = 'newbie';
58 } else {
59 $this->opts['contribs'] = 'user';
62 $this->opts['deletedOnly'] = $request->getBool( 'deletedOnly' );
64 if ( !strlen( $target ) ) {
65 if ( !$this->including() ) {
66 $out->addHTML( $this->getForm() );
69 return;
72 $user = $this->getUser();
74 $this->opts['limit'] = $request->getInt( 'limit', $user->getOption( 'rclimit' ) );
75 $this->opts['target'] = $target;
76 $this->opts['topOnly'] = $request->getBool( 'topOnly' );
77 $this->opts['newOnly'] = $request->getBool( 'newOnly' );
79 $nt = Title::makeTitleSafe( NS_USER, $target );
80 if ( !$nt ) {
81 $out->addHTML( $this->getForm() );
83 return;
85 $userObj = User::newFromName( $nt->getText(), false );
86 if ( !$userObj ) {
87 $out->addHTML( $this->getForm() );
89 return;
91 $id = $userObj->getID();
93 if ( $this->opts['contribs'] != 'newbie' ) {
94 $target = $nt->getText();
95 $out->addSubtitle( $this->contributionsSub( $userObj ) );
96 $out->setHTMLTitle( $this->msg(
97 'pagetitle',
98 $this->msg( 'contributions-title', $target )->plain()
99 )->inContentLanguage() );
100 $this->getSkin()->setRelevantUser( $userObj );
101 } else {
102 $out->addSubtitle( $this->msg( 'sp-contributions-newbies-sub' ) );
103 $out->setHTMLTitle( $this->msg(
104 'pagetitle',
105 $this->msg( 'sp-contributions-newbies-title' )->plain()
106 )->inContentLanguage() );
109 if ( ( $ns = $request->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
110 $this->opts['namespace'] = intval( $ns );
111 } else {
112 $this->opts['namespace'] = '';
115 $this->opts['associated'] = $request->getBool( 'associated' );
116 $this->opts['nsInvert'] = (bool)$request->getVal( 'nsInvert' );
117 $this->opts['tagfilter'] = (string)$request->getVal( 'tagfilter' );
119 // Allows reverts to have the bot flag in recent changes. It is just here to
120 // be passed in the form at the top of the page
121 if ( $user->isAllowed( 'markbotedits' ) && $request->getBool( 'bot' ) ) {
122 $this->opts['bot'] = '1';
125 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
126 # Offset overrides year/month selection
127 if ( $skip ) {
128 $this->opts['year'] = '';
129 $this->opts['month'] = '';
130 } else {
131 $this->opts['year'] = $request->getIntOrNull( 'year' );
132 $this->opts['month'] = $request->getIntOrNull( 'month' );
135 $feedType = $request->getVal( 'feed' );
137 $feedParams = array(
138 'action' => 'feedcontributions',
139 'user' => $target,
141 if ( $this->opts['topOnly'] ) {
142 $feedParams['toponly'] = true;
144 if ( $this->opts['newOnly'] ) {
145 $feedParams['newonly'] = true;
147 if ( $this->opts['deletedOnly'] ) {
148 $feedParams['deletedonly'] = true;
150 if ( $this->opts['tagfilter'] !== '' ) {
151 $feedParams['tagfilter'] = $this->opts['tagfilter'];
153 if ( $this->opts['namespace'] !== '' ) {
154 $feedParams['namespace'] = $this->opts['namespace'];
156 // Don't use year and month for the feed URL, but pass them on if
157 // we redirect to API (if $feedType is specified)
158 if ( $feedType && $this->opts['year'] !== null ) {
159 $feedParams['year'] = $this->opts['year'];
161 if ( $feedType && $this->opts['month'] !== null ) {
162 $feedParams['month'] = $this->opts['month'];
165 if ( $feedType ) {
166 // Maintain some level of backwards compatability
167 // If people request feeds using the old parameters, redirect to API
168 $feedParams['feedformat'] = $feedType;
169 $url = wfAppendQuery( wfScript( 'api' ), $feedParams );
171 $out->redirect( $url, '301' );
173 return;
176 // Add RSS/atom links
177 $this->addFeedLinks( $feedParams );
179 if ( wfRunHooks( 'SpecialContributionsBeforeMainOutput', array( $id, $userObj, $this ) ) ) {
180 if ( !$this->including() ) {
181 $out->addHTML( $this->getForm() );
183 $pager = new ContribsPager( $this->getContext(), array(
184 'target' => $target,
185 'contribs' => $this->opts['contribs'],
186 'namespace' => $this->opts['namespace'],
187 'tagfilter' => $this->opts['tagfilter'],
188 'year' => $this->opts['year'],
189 'month' => $this->opts['month'],
190 'deletedOnly' => $this->opts['deletedOnly'],
191 'topOnly' => $this->opts['topOnly'],
192 'newOnly' => $this->opts['newOnly'],
193 'nsInvert' => $this->opts['nsInvert'],
194 'associated' => $this->opts['associated'],
195 ) );
197 if ( !$pager->getNumRows() ) {
198 $out->addWikiMsg( 'nocontribs', $target );
199 } else {
200 # Show a message about slave lag, if applicable
201 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
202 if ( $lag > 0 ) {
203 $out->showLagWarning( $lag );
206 $out->addHTML(
207 '<p>' . $pager->getNavigationBar() . '</p>' .
208 $pager->getBody() .
209 '<p>' . $pager->getNavigationBar() . '</p>'
212 $out->preventClickjacking( $pager->getPreventClickjacking() );
214 # Show the appropriate "footer" message - WHOIS tools, etc.
215 if ( $this->opts['contribs'] == 'newbie' ) {
216 $message = 'sp-contributions-footer-newbies';
217 } elseif ( IP::isIPAddress( $target ) ) {
218 $message = 'sp-contributions-footer-anon';
219 } elseif ( $userObj->isAnon() ) {
220 // No message for non-existing users
221 $message = '';
222 } else {
223 $message = 'sp-contributions-footer';
226 if ( $message ) {
227 if ( !$this->including() ) {
228 if ( !$this->msg( $message, $target )->isDisabled() ) {
229 $out->wrapWikiMsg(
230 "<div class='mw-contributions-footer'>\n$1\n</div>",
231 array( $message, $target ) );
239 * Generates the subheading with links
240 * @param User $userObj User object for the target
241 * @return string Appropriately-escaped HTML to be output literally
242 * @todo FIXME: Almost the same as getSubTitle in SpecialDeletedContributions.php.
243 * Could be combined.
245 protected function contributionsSub( $userObj ) {
246 if ( $userObj->isAnon() ) {
247 // Show a warning message that the user being searched for doesn't exists
248 if ( !User::isIP( $userObj->getName() ) ) {
249 $this->getOutput()->wrapWikiMsg(
250 "<div class=\"mw-userpage-userdoesnotexist error\">\n\$1\n</div>",
251 array(
252 'contributions-userdoesnotexist',
253 wfEscapeWikiText( $userObj->getName() ),
257 $user = htmlspecialchars( $userObj->getName() );
258 } else {
259 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
261 $nt = $userObj->getUserPage();
262 $talk = $userObj->getTalkPage();
263 $links = '';
264 if ( $talk ) {
265 $tools = $this->getUserLinks( $nt, $talk, $userObj );
266 $links = $this->getLanguage()->pipeList( $tools );
268 // Show a note if the user is blocked and display the last block log entry.
269 // Do not expose the autoblocks, since that may lead to a leak of accounts' IPs,
270 // and also this will display a totally irrelevant log entry as a current block.
271 if ( $userObj->isBlocked() && $userObj->getBlock()->getType() != Block::TYPE_AUTO ) {
272 $out = $this->getOutput(); // showLogExtract() wants first parameter by reference
273 LogEventsList::showLogExtract(
274 $out,
275 'block',
276 $nt,
278 array(
279 'lim' => 1,
280 'showIfEmpty' => false,
281 'msgKey' => array(
282 $userObj->isAnon() ?
283 'sp-contributions-blocked-notice-anon' :
284 'sp-contributions-blocked-notice',
285 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
287 'offset' => '' # don't use WebRequest parameter offset
293 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
297 * Links to different places.
298 * @param Title $userpage Target user page
299 * @param Title $talkpage Talk page
300 * @param User $target Target user object
301 * @return array
303 public function getUserLinks( Title $userpage, Title $talkpage, User $target ) {
305 $id = $target->getId();
306 $username = $target->getName();
308 $tools[] = Linker::link( $talkpage, $this->msg( 'sp-contributions-talk' )->escaped() );
310 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $username ) ) ) {
311 if ( $this->getUser()->isAllowed( 'block' ) ) { # Block / Change block / Unblock links
312 if ( $target->isBlocked() && $target->getBlock()->getType() != Block::TYPE_AUTO ) {
313 $tools[] = Linker::linkKnown( # Change block link
314 SpecialPage::getTitleFor( 'Block', $username ),
315 $this->msg( 'change-blocklink' )->escaped()
317 $tools[] = Linker::linkKnown( # Unblock link
318 SpecialPage::getTitleFor( 'Unblock', $username ),
319 $this->msg( 'unblocklink' )->escaped()
321 } else { # User is not blocked
322 $tools[] = Linker::linkKnown( # Block link
323 SpecialPage::getTitleFor( 'Block', $username ),
324 $this->msg( 'blocklink' )->escaped()
329 # Block log link
330 $tools[] = Linker::linkKnown(
331 SpecialPage::getTitleFor( 'Log', 'block' ),
332 $this->msg( 'sp-contributions-blocklog' )->escaped(),
333 array(),
334 array( 'page' => $userpage->getPrefixedText() )
337 # Suppression log link (bug 59120)
338 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
339 $tools[] = Linker::linkKnown(
340 SpecialPage::getTitleFor( 'Log', 'suppress' ),
341 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
342 array(),
343 array( 'offender' => $username )
347 # Uploads
348 $tools[] = Linker::linkKnown(
349 SpecialPage::getTitleFor( 'Listfiles', $username ),
350 $this->msg( 'sp-contributions-uploads' )->escaped()
353 # Other logs link
354 $tools[] = Linker::linkKnown(
355 SpecialPage::getTitleFor( 'Log', $username ),
356 $this->msg( 'sp-contributions-logs' )->escaped()
359 # Add link to deleted user contributions for priviledged users
360 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
361 $tools[] = Linker::linkKnown(
362 SpecialPage::getTitleFor( 'DeletedContributions', $username ),
363 $this->msg( 'sp-contributions-deleted' )->escaped()
367 # Add a link to change user rights for privileged users
368 $userrightsPage = new UserrightsPage();
369 $userrightsPage->setContext( $this->getContext() );
370 if ( $userrightsPage->userCanChangeRights( $target ) ) {
371 $tools[] = Linker::linkKnown(
372 SpecialPage::getTitleFor( 'Userrights', $username ),
373 $this->msg( 'sp-contributions-userrights' )->escaped()
377 wfRunHooks( 'ContributionsToolLinks', array( $id, $userpage, &$tools ) );
379 return $tools;
383 * Generates the namespace selector form with hidden attributes.
384 * @return string HTML fragment
386 protected function getForm() {
387 global $wgScript;
389 $this->opts['title'] = $this->getPageTitle()->getPrefixedText();
390 if ( !isset( $this->opts['target'] ) ) {
391 $this->opts['target'] = '';
392 } else {
393 $this->opts['target'] = str_replace( '_', ' ', $this->opts['target'] );
396 if ( !isset( $this->opts['namespace'] ) ) {
397 $this->opts['namespace'] = '';
400 if ( !isset( $this->opts['nsInvert'] ) ) {
401 $this->opts['nsInvert'] = '';
404 if ( !isset( $this->opts['associated'] ) ) {
405 $this->opts['associated'] = false;
408 if ( !isset( $this->opts['contribs'] ) ) {
409 $this->opts['contribs'] = 'user';
412 if ( !isset( $this->opts['year'] ) ) {
413 $this->opts['year'] = '';
416 if ( !isset( $this->opts['month'] ) ) {
417 $this->opts['month'] = '';
420 if ( $this->opts['contribs'] == 'newbie' ) {
421 $this->opts['target'] = '';
424 if ( !isset( $this->opts['tagfilter'] ) ) {
425 $this->opts['tagfilter'] = '';
428 if ( !isset( $this->opts['topOnly'] ) ) {
429 $this->opts['topOnly'] = false;
432 if ( !isset( $this->opts['newOnly'] ) ) {
433 $this->opts['newOnly'] = false;
436 $form = Html::openElement(
437 'form',
438 array(
439 'method' => 'get',
440 'action' => $wgScript,
441 'class' => 'mw-contributions-form'
445 # Add hidden params for tracking except for parameters in $skipParameters
446 $skipParameters = array(
447 'namespace',
448 'nsInvert',
449 'deletedOnly',
450 'target',
451 'contribs',
452 'year',
453 'month',
454 'topOnly',
455 'newOnly',
456 'associated'
459 foreach ( $this->opts as $name => $value ) {
460 if ( in_array( $name, $skipParameters ) ) {
461 continue;
463 $form .= "\t" . Html::hidden( $name, $value ) . "\n";
466 $tagFilter = ChangeTags::buildTagFilterSelector( $this->opts['tagfilter'] );
468 if ( $tagFilter ) {
469 $filterSelection = Html::rawElement(
470 'td',
471 array( 'class' => 'mw-label' ),
472 array_shift( $tagFilter )
474 $filterSelection .= Html::rawElement(
475 'td',
476 array( 'class' => 'mw-input' ),
477 implode( '&#160', $tagFilter )
479 } else {
480 $filterSelection = Html::rawElement( 'td', array( 'colspan' => 2 ), '' );
483 $labelNewbies = Xml::radioLabel(
484 $this->msg( 'sp-contributions-newbies' )->text(),
485 'contribs',
486 'newbie',
487 'newbie',
488 $this->opts['contribs'] == 'newbie',
489 array( 'class' => 'mw-input' )
491 $labelUsername = Xml::radioLabel(
492 $this->msg( 'sp-contributions-username' )->text(),
493 'contribs',
494 'user',
495 'user',
496 $this->opts['contribs'] == 'user',
497 array( 'class' => 'mw-input' )
499 $input = Html::input(
500 'target',
501 $this->opts['target'],
502 'text',
503 array( 'size' => '40', 'required' => '', 'class' => 'mw-input' ) +
504 ( $this->opts['target'] ? array() : array( 'autofocus' )
507 $targetSelection = Html::rawElement(
508 'td',
509 array( 'colspan' => 2 ),
510 $labelNewbies . '<br />' . $labelUsername . ' ' . $input . ' '
513 $namespaceSelection = Xml::tags(
514 'td',
515 array( 'class' => 'mw-label' ),
516 Xml::label(
517 $this->msg( 'namespace' )->text(),
518 'namespace',
522 $namespaceSelection .= Html::rawElement(
523 'td',
524 null,
525 Html::namespaceSelector(
526 array( 'selected' => $this->opts['namespace'], 'all' => '' ),
527 array(
528 'name' => 'namespace',
529 'id' => 'namespace',
530 'class' => 'namespaceselector',
532 ) . '&#160;' .
533 Html::rawElement(
534 'span',
535 array( 'style' => 'white-space: nowrap' ),
536 Xml::checkLabel(
537 $this->msg( 'invert' )->text(),
538 'nsInvert',
539 'nsInvert',
540 $this->opts['nsInvert'],
541 array(
542 'title' => $this->msg( 'tooltip-invert' )->text(),
543 'class' => 'mw-input'
545 ) . '&#160;'
547 Html::rawElement( 'span', array( 'style' => 'white-space: nowrap' ),
548 Xml::checkLabel(
549 $this->msg( 'namespace_association' )->text(),
550 'associated',
551 'associated',
552 $this->opts['associated'],
553 array(
554 'title' => $this->msg( 'tooltip-namespace_association' )->text(),
555 'class' => 'mw-input'
557 ) . '&#160;'
561 if ( $this->getUser()->isAllowed( 'deletedhistory' ) ) {
562 $deletedOnlyCheck = Html::rawElement(
563 'span',
564 array( 'style' => 'white-space: nowrap' ),
565 Xml::checkLabel(
566 $this->msg( 'history-show-deleted' )->text(),
567 'deletedOnly',
568 'mw-show-deleted-only',
569 $this->opts['deletedOnly'],
570 array( 'class' => 'mw-input' )
573 } else {
574 $deletedOnlyCheck = '';
577 $checkLabelTopOnly = Html::rawElement(
578 'span',
579 array( 'style' => 'white-space: nowrap' ),
580 Xml::checkLabel(
581 $this->msg( 'sp-contributions-toponly' )->text(),
582 'topOnly',
583 'mw-show-top-only',
584 $this->opts['topOnly'],
585 array( 'class' => 'mw-input' )
588 $checkLabelNewOnly = Html::rawElement(
589 'span',
590 array( 'style' => 'white-space: nowrap' ),
591 Xml::checkLabel(
592 $this->msg( 'sp-contributions-newonly' )->text(),
593 'newOnly',
594 'mw-show-new-only',
595 $this->opts['newOnly'],
596 array( 'class' => 'mw-input' )
599 $extraOptions = Html::rawElement(
600 'td',
601 array( 'colspan' => 2 ),
602 $deletedOnlyCheck . $checkLabelTopOnly . $checkLabelNewOnly
605 $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 2 ),
606 Xml::dateMenu(
607 $this->opts['year'] === '' ? MWTimestamp::getInstance()->format( 'Y' ) : $this->opts['year'],
608 $this->opts['month']
609 ) . ' ' .
610 Xml::submitButton(
611 $this->msg( 'sp-contributions-submit' )->text(),
612 array( 'class' => 'mw-submit' )
616 $form .= Xml::fieldset( $this->msg( 'sp-contributions-search' )->text() );
617 $form .= Html::rawElement( 'table', array( 'class' => 'mw-contributions-table' ), "\n" .
618 Html::rawElement( 'tr', array(), $targetSelection ) . "\n" .
619 Html::rawElement( 'tr', array(), $namespaceSelection ) . "\n" .
620 Html::rawElement( 'tr', array(), $filterSelection ) . "\n" .
621 Html::rawElement( 'tr', array(), $extraOptions ) . "\n" .
622 Html::rawElement( 'tr', array(), $dateSelectionAndSubmit ) . "\n"
625 $explain = $this->msg( 'sp-contributions-explain' );
626 if ( !$explain->isBlank() ) {
627 $form .= "<p id='mw-sp-contributions-explain'>{$explain->parse()}</p>";
630 $form .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' );
632 return $form;
635 protected function getGroupName() {
636 return 'users';
641 * Pager for Special:Contributions
642 * @ingroup SpecialPage Pager
644 class ContribsPager extends ReverseChronologicalPager {
645 public $mDefaultDirection = true;
646 public $messages;
647 public $target;
648 public $namespace = '';
649 public $mDb;
650 public $preventClickjacking = false;
652 /** @var DatabaseBase */
653 public $mDbSecondary;
656 * @var array
658 protected $mParentLens;
660 function __construct( IContextSource $context, array $options ) {
661 parent::__construct( $context );
663 $msgs = array(
664 'diff',
665 'hist',
666 'newarticle',
667 'pipe-separator',
668 'rev-delundel',
669 'rollbacklink',
670 'uctop'
673 foreach ( $msgs as $msg ) {
674 $this->messages[$msg] = $this->msg( $msg )->escaped();
677 $this->target = isset( $options['target'] ) ? $options['target'] : '';
678 $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users';
679 $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : '';
680 $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false;
681 $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false;
682 $this->associated = isset( $options['associated'] ) ? $options['associated'] : false;
684 $this->deletedOnly = !empty( $options['deletedOnly'] );
685 $this->topOnly = !empty( $options['topOnly'] );
686 $this->newOnly = !empty( $options['newOnly'] );
688 $year = isset( $options['year'] ) ? $options['year'] : false;
689 $month = isset( $options['month'] ) ? $options['month'] : false;
690 $this->getDateCond( $year, $month );
692 // Most of this code will use the 'contributions' group DB, which can map to slaves
693 // with extra user based indexes or partioning by user. The additional metadata
694 // queries should use a regular slave since the lookup pattern is not all by user.
695 $this->mDbSecondary = wfGetDB( DB_SLAVE ); // any random slave
696 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
699 function getDefaultQuery() {
700 $query = parent::getDefaultQuery();
701 $query['target'] = $this->target;
703 return $query;
707 * This method basically executes the exact same code as the parent class, though with
708 * a hook added, to allow extentions to add additional queries.
710 * @param string $offset Index offset, inclusive
711 * @param int $limit Exact query limit
712 * @param bool $descending Query direction, false for ascending, true for descending
713 * @return ResultWrapper
715 function reallyDoQuery( $offset, $limit, $descending ) {
716 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
717 $offset,
718 $limit,
719 $descending
721 $pager = $this;
724 * This hook will allow extensions to add in additional queries, so they can get their data
725 * in My Contributions as well. Extensions should append their results to the $data array.
727 * Extension queries have to implement the navbar requirement as well. They should
728 * - have a column aliased as $pager->getIndexField()
729 * - have LIMIT set
730 * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
731 * - have the ORDER BY specified based upon the details provided by the navbar
733 * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
735 * &$data: an array of results of all contribs queries
736 * $pager: the ContribsPager object hooked into
737 * $offset: see phpdoc above
738 * $limit: see phpdoc above
739 * $descending: see phpdoc above
741 $data = array( $this->mDb->select(
742 $tables, $fields, $conds, $fname, $options, $join_conds
743 ) );
744 wfRunHooks(
745 'ContribsPager::reallyDoQuery',
746 array( &$data, $pager, $offset, $limit, $descending )
749 $result = array();
751 // loop all results and collect them in an array
752 foreach ( $data as $query ) {
753 foreach ( $query as $i => $row ) {
754 // use index column as key, allowing us to easily sort in PHP
755 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
759 // sort results
760 if ( $descending ) {
761 ksort( $result );
762 } else {
763 krsort( $result );
766 // enforce limit
767 $result = array_slice( $result, 0, $limit );
769 // get rid of array keys
770 $result = array_values( $result );
772 return new FakeResultWrapper( $result );
775 function getQueryInfo() {
776 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
778 $user = $this->getUser();
779 $conds = array_merge( $userCond, $this->getNamespaceCond() );
781 // Paranoia: avoid brute force searches (bug 17342)
782 if ( !$user->isAllowed( 'deletedhistory' ) ) {
783 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
784 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
785 $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
786 ' != ' . Revision::SUPPRESSED_USER;
789 # Don't include orphaned revisions
790 $join_cond['page'] = Revision::pageJoinCond();
791 # Get the current user name for accounts
792 $join_cond['user'] = Revision::userJoinCond();
794 $options = array();
795 if ( $index ) {
796 $options['USE INDEX'] = array( 'revision' => $index );
799 $queryInfo = array(
800 'tables' => $tables,
801 'fields' => array_merge(
802 Revision::selectFields(),
803 Revision::selectUserFields(),
804 array( 'page_namespace', 'page_title', 'page_is_new',
805 'page_latest', 'page_is_redirect', 'page_len' )
807 'conds' => $conds,
808 'options' => $options,
809 'join_conds' => $join_cond
812 ChangeTags::modifyDisplayQuery(
813 $queryInfo['tables'],
814 $queryInfo['fields'],
815 $queryInfo['conds'],
816 $queryInfo['join_conds'],
817 $queryInfo['options'],
818 $this->tagFilter
821 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
823 return $queryInfo;
826 function getUserCond() {
827 $condition = array();
828 $join_conds = array();
829 $tables = array( 'revision', 'page', 'user' );
830 $index = false;
831 if ( $this->contribs == 'newbie' ) {
832 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
833 $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
834 # ignore local groups with the bot right
835 # @todo FIXME: Global groups may have 'bot' rights
836 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
837 if ( count( $groupsWithBotPermission ) ) {
838 $tables[] = 'user_groups';
839 $condition[] = 'ug_group IS NULL';
840 $join_conds['user_groups'] = array(
841 'LEFT JOIN', array(
842 'ug_user = rev_user',
843 'ug_group' => $groupsWithBotPermission
847 } else {
848 $uid = User::idFromName( $this->target );
849 if ( $uid ) {
850 $condition['rev_user'] = $uid;
851 $index = 'user_timestamp';
852 } else {
853 $condition['rev_user_text'] = $this->target;
854 $index = 'usertext_timestamp';
858 if ( $this->deletedOnly ) {
859 $condition[] = 'rev_deleted != 0';
862 if ( $this->topOnly ) {
863 $condition[] = 'rev_id = page_latest';
866 if ( $this->newOnly ) {
867 $condition[] = 'rev_parent_id = 0';
870 return array( $tables, $index, $condition, $join_conds );
873 function getNamespaceCond() {
874 if ( $this->namespace !== '' ) {
875 $selectedNS = $this->mDb->addQuotes( $this->namespace );
876 $eq_op = $this->nsInvert ? '!=' : '=';
877 $bool_op = $this->nsInvert ? 'AND' : 'OR';
879 if ( !$this->associated ) {
880 return array( "page_namespace $eq_op $selectedNS" );
883 $associatedNS = $this->mDb->addQuotes(
884 MWNamespace::getAssociated( $this->namespace )
887 return array(
888 "page_namespace $eq_op $selectedNS " .
889 $bool_op .
890 " page_namespace $eq_op $associatedNS"
894 return array();
897 function getIndexField() {
898 return 'rev_timestamp';
901 function doBatchLookups() {
902 # Do a link batch query
903 $this->mResult->seek( 0 );
904 $revIds = array();
905 $batch = new LinkBatch();
906 # Give some pointers to make (last) links
907 foreach ( $this->mResult as $row ) {
908 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
909 $revIds[] = $row->rev_parent_id;
911 if ( isset( $row->rev_id ) ) {
912 if ( $this->contribs === 'newbie' ) { // multiple users
913 $batch->add( NS_USER, $row->user_name );
914 $batch->add( NS_USER_TALK, $row->user_name );
916 $batch->add( $row->page_namespace, $row->page_title );
919 $this->mParentLens = Revision::getParentLengths( $this->mDbSecondary, $revIds );
920 $batch->execute();
921 $this->mResult->seek( 0 );
925 * @return string
927 function getStartBody() {
928 return "<ul>\n";
932 * @return string
934 function getEndBody() {
935 return "</ul>\n";
939 * Generates each row in the contributions list.
941 * Contributions which are marked "top" are currently on top of the history.
942 * For these contributions, a [rollback] link is shown for users with roll-
943 * back privileges. The rollback link restores the most recent version that
944 * was not written by the target user.
946 * @todo This would probably look a lot nicer in a table.
947 * @param object $row
948 * @return string
950 function formatRow( $row ) {
951 wfProfileIn( __METHOD__ );
953 $ret = '';
954 $classes = array();
957 * There may be more than just revision rows. To make sure that we'll only be processing
958 * revisions here, let's _try_ to build a revision out of our row (without displaying
959 * notices though) and then trying to grab data from the built object. If we succeed,
960 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
961 * to extensions to subscribe to the hook to parse the row.
963 wfSuppressWarnings();
964 try {
965 $rev = new Revision( $row );
966 $validRevision = (bool)$rev->getId();
967 } catch ( MWException $e ) {
968 $validRevision = false;
970 wfRestoreWarnings();
972 if ( $validRevision ) {
973 $classes = array();
975 $page = Title::newFromRow( $row );
976 $link = Linker::link(
977 $page,
978 htmlspecialchars( $page->getPrefixedText() ),
979 array( 'class' => 'mw-contributions-title' ),
980 $page->isRedirect() ? array( 'redirect' => 'no' ) : array()
982 # Mark current revisions
983 $topmarktext = '';
984 $user = $this->getUser();
985 if ( $row->rev_id == $row->page_latest ) {
986 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
987 # Add rollback link
988 if ( !$row->page_is_new && $page->quickUserCan( 'rollback', $user )
989 && $page->quickUserCan( 'edit', $user )
991 $this->preventClickjacking();
992 $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
995 # Is there a visible previous revision?
996 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
997 $difftext = Linker::linkKnown(
998 $page,
999 $this->messages['diff'],
1000 array(),
1001 array(
1002 'diff' => 'prev',
1003 'oldid' => $row->rev_id
1006 } else {
1007 $difftext = $this->messages['diff'];
1009 $histlink = Linker::linkKnown(
1010 $page,
1011 $this->messages['hist'],
1012 array(),
1013 array( 'action' => 'history' )
1016 if ( $row->rev_parent_id === null ) {
1017 // For some reason rev_parent_id isn't populated for this row.
1018 // Its rumoured this is true on wikipedia for some revisions (bug 34922).
1019 // Next best thing is to have the total number of bytes.
1020 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1021 $chardiff .= Linker::formatRevisionSize( $row->rev_len );
1022 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1023 } else {
1024 $parentLen = 0;
1025 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
1026 $parentLen = $this->mParentLens[$row->rev_parent_id];
1029 $chardiff = ' <span class="mw-changeslist-separator">. .</span> ';
1030 $chardiff .= ChangesList::showCharacterDifference(
1031 $parentLen,
1032 $row->rev_len,
1033 $this->getContext()
1035 $chardiff .= ' <span class="mw-changeslist-separator">. .</span> ';
1038 $lang = $this->getLanguage();
1039 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true );
1040 $date = $lang->userTimeAndDate( $row->rev_timestamp, $user );
1041 if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1042 $d = Linker::linkKnown(
1043 $page,
1044 htmlspecialchars( $date ),
1045 array( 'class' => 'mw-changeslist-date' ),
1046 array( 'oldid' => intval( $row->rev_id ) )
1048 } else {
1049 $d = htmlspecialchars( $date );
1051 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1052 $d = '<span class="history-deleted">' . $d . '</span>';
1055 # Show user names for /newbies as there may be different users.
1056 # Note that we already excluded rows with hidden user names.
1057 if ( $this->contribs == 'newbie' ) {
1058 $userlink = ' . . ' . $lang->getDirMark() . Linker::userLink( $rev->getUser(), $rev->getUserText() );
1059 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
1060 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
1061 } else {
1062 $userlink = '';
1065 if ( $rev->getParentId() === 0 ) {
1066 $nflag = ChangesList::flag( 'newpage' );
1067 } else {
1068 $nflag = '';
1071 if ( $rev->isMinor() ) {
1072 $mflag = ChangesList::flag( 'minor' );
1073 } else {
1074 $mflag = '';
1077 $del = Linker::getRevDeleteLink( $user, $rev, $page );
1078 if ( $del !== '' ) {
1079 $del .= ' ';
1082 $diffHistLinks = $this->msg( 'parentheses' )
1083 ->rawParams( $difftext . $this->messages['pipe-separator'] . $histlink )
1084 ->escaped();
1085 $ret = "{$del}{$d} {$diffHistLinks}{$chardiff}{$nflag}{$mflag} ";
1086 $ret .= "{$link}{$userlink} {$comment} {$topmarktext}";
1088 # Denote if username is redacted for this edit
1089 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
1090 $ret .= " <strong>" .
1091 $this->msg( 'rev-deleted-user-contribs' )->escaped() .
1092 "</strong>";
1095 # Tags, if any.
1096 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
1097 $row->ts_tags,
1098 'contributions'
1100 $classes = array_merge( $classes, $newClasses );
1101 $ret .= " $tagSummary";
1104 // Let extensions add data
1105 wfRunHooks( 'ContributionsLineEnding', array( $this, &$ret, $row, &$classes ) );
1107 if ( $classes === array() && $ret === '' ) {
1108 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
1109 $ret = "<!-- Could not format Special:Contribution row. -->\n";
1110 } else {
1111 $ret = Html::rawElement( 'li', array( 'class' => $classes ), $ret ) . "\n";
1114 wfProfileOut( __METHOD__ );
1116 return $ret;
1120 * Overwrite Pager function and return a helpful comment
1121 * @return string
1123 function getSqlComment() {
1124 if ( $this->namespace || $this->deletedOnly ) {
1125 // potentially slow, see CR r58153
1126 return 'contributions page filtered for namespace or RevisionDeleted edits';
1127 } else {
1128 return 'contributions page unfiltered';
1132 protected function preventClickjacking() {
1133 $this->preventClickjacking = true;
1137 * @return bool
1139 public function getPreventClickjacking() {
1140 return $this->preventClickjacking;