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