Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 * Implements Special:DeletedContributions
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 * Implements Special:DeletedContributions to display archived revisions
26 * @ingroup SpecialPage
27 */
28 class DeletedContribsPager extends IndexPager {
29 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
30 public $messages;
31 public $target;
32 public $namespace = '';
33 public $mDb;
34
35 /**
36 * @var string Navigation bar with paging links.
37 */
38 protected $mNavigationBar;
39
40 function __construct( IContextSource $context, $target, $namespace = false ) {
41 parent::__construct( $context );
42 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
43 foreach ( $msgs as $msg ) {
44 $this->messages[$msg] = $this->msg( $msg )->escaped();
45 }
46 $this->target = $target;
47 $this->namespace = $namespace;
48 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
49 }
50
51 function getDefaultQuery() {
52 $query = parent::getDefaultQuery();
53 $query['target'] = $this->target;
54
55 return $query;
56 }
57
58 function getQueryInfo() {
59 list( $index, $userCond ) = $this->getUserCond();
60 $conds = array_merge( $userCond, $this->getNamespaceCond() );
61 $user = $this->getUser();
62 // Paranoia: avoid brute force searches (bug 17792)
63 if ( !$user->isAllowed( 'deletedhistory' ) ) {
64 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::DELETED_USER ) . ' = 0';
65 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
66 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::SUPPRESSED_USER ) .
67 ' != ' . Revision::SUPPRESSED_USER;
68 }
69
70 return [
71 'tables' => [ 'archive' ],
72 'fields' => [
73 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment',
74 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
75 ],
76 'conds' => $conds,
77 'options' => [ 'USE INDEX' => $index ]
78 ];
79 }
80
81 /**
82 * This method basically executes the exact same code as the parent class, though with
83 * a hook added, to allow extensions to add additional queries.
84 *
85 * @param string $offset Index offset, inclusive
86 * @param int $limit Exact query limit
87 * @param bool $descending Query direction, false for ascending, true for descending
88 * @return ResultWrapper
89 */
90 function reallyDoQuery( $offset, $limit, $descending ) {
91 $data = [ parent::reallyDoQuery( $offset, $limit, $descending ) ];
92
93 // This hook will allow extensions to add in additional queries, nearly
94 // identical to ContribsPager::reallyDoQuery.
95 Hooks::run(
96 'DeletedContribsPager::reallyDoQuery',
97 [ &$data, $this, $offset, $limit, $descending ]
98 );
99
100 $result = [];
101
102 // loop all results and collect them in an array
103 foreach ( $data as $query ) {
104 foreach ( $query as $i => $row ) {
105 // use index column as key, allowing us to easily sort in PHP
106 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
107 }
108 }
109
110 // sort results
111 if ( $descending ) {
112 ksort( $result );
113 } else {
114 krsort( $result );
115 }
116
117 // enforce limit
118 $result = array_slice( $result, 0, $limit );
119
120 // get rid of array keys
121 $result = array_values( $result );
122
123 return new FakeResultWrapper( $result );
124 }
125
126 function getUserCond() {
127 $condition = [];
128
129 $condition['ar_user_text'] = $this->target;
130 $index = 'usertext_timestamp';
131
132 return [ $index, $condition ];
133 }
134
135 function getIndexField() {
136 return 'ar_timestamp';
137 }
138
139 function getStartBody() {
140 return "<ul>\n";
141 }
142
143 function getEndBody() {
144 return "</ul>\n";
145 }
146
147 function getNavigationBar() {
148 if ( isset( $this->mNavigationBar ) ) {
149 return $this->mNavigationBar;
150 }
151
152 $linkTexts = [
153 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
154 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
155 'first' => $this->msg( 'histlast' )->escaped(),
156 'last' => $this->msg( 'histfirst' )->escaped()
157 ];
158
159 $pagingLinks = $this->getPagingLinks( $linkTexts );
160 $limitLinks = $this->getLimitLinks();
161 $lang = $this->getLanguage();
162 $limits = $lang->pipeList( $limitLinks );
163
164 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
165 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
166 $prevNext = $this->msg( 'viewprevnext' )
167 ->rawParams(
168 $pagingLinks['prev'],
169 $pagingLinks['next'],
170 $limits
171 )->escaped();
172 $separator = $this->msg( 'word-separator' )->escaped();
173 $this->mNavigationBar = $firstLast . $separator . $prevNext;
174
175 return $this->mNavigationBar;
176 }
177
178 function getNamespaceCond() {
179 if ( $this->namespace !== '' ) {
180 return [ 'ar_namespace' => (int)$this->namespace ];
181 } else {
182 return [];
183 }
184 }
185
186 /**
187 * Generates each row in the contributions list.
188 *
189 * @todo This would probably look a lot nicer in a table.
190 * @param stdClass $row
191 * @return string
192 */
193 function formatRow( $row ) {
194 $ret = '';
195 $classes = [];
196
197 /*
198 * There may be more than just revision rows. To make sure that we'll only be processing
199 * revisions here, let's _try_ to build a revision out of our row (without displaying
200 * notices though) and then trying to grab data from the built object. If we succeed,
201 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
202 * to extensions to subscribe to the hook to parse the row.
203 */
204 MediaWiki\suppressWarnings();
205 try {
206 $rev = Revision::newFromArchiveRow( $row );
207 $validRevision = (bool)$rev->getId();
208 } catch ( Exception $e ) {
209 $validRevision = false;
210 }
211 MediaWiki\restoreWarnings();
212
213 if ( $validRevision ) {
214 $ret = $this->formatRevisionRow( $row );
215 }
216
217 // Let extensions add data
218 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes ] );
219
220 if ( $classes === [] && $ret === '' ) {
221 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
222 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
223 } else {
224 $ret = Html::rawElement( 'li', [ 'class' => $classes ], $ret ) . "\n";
225 }
226
227 return $ret;
228 }
229
230 /**
231 * Generates each row in the contributions list for archive entries.
232 *
233 * Contributions which are marked "top" are currently on top of the history.
234 * For these contributions, a [rollback] link is shown for users with sysop
235 * privileges. The rollback link restores the most recent version that was not
236 * written by the target user.
237 *
238 * @todo This would probably look a lot nicer in a table.
239 * @param stdClass $row
240 * @return string
241 */
242 function formatRevisionRow( $row ) {
243 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
244
245 $rev = new Revision( [
246 'title' => $page,
247 'id' => $row->ar_rev_id,
248 'comment' => $row->ar_comment,
249 'user' => $row->ar_user,
250 'user_text' => $row->ar_user_text,
251 'timestamp' => $row->ar_timestamp,
252 'minor_edit' => $row->ar_minor_edit,
253 'deleted' => $row->ar_deleted,
254 ] );
255
256 $undelete = SpecialPage::getTitleFor( 'Undelete' );
257
258 $logs = SpecialPage::getTitleFor( 'Log' );
259 $dellog = Linker::linkKnown(
260 $logs,
261 $this->messages['deletionlog'],
262 [],
263 [
264 'type' => 'delete',
265 'page' => $page->getPrefixedText()
266 ]
267 );
268
269 $reviewlink = Linker::linkKnown(
270 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
271 $this->messages['undeleteviewlink']
272 );
273
274 $user = $this->getUser();
275
276 if ( $user->isAllowed( 'deletedtext' ) ) {
277 $last = Linker::linkKnown(
278 $undelete,
279 $this->messages['diff'],
280 [],
281 [
282 'target' => $page->getPrefixedText(),
283 'timestamp' => $rev->getTimestamp(),
284 'diff' => 'prev'
285 ]
286 );
287 } else {
288 $last = $this->messages['diff'];
289 }
290
291 $comment = Linker::revComment( $rev );
292 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
293 $date = htmlspecialchars( $date );
294
295 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
296 $link = $date; // unusable link
297 } else {
298 $link = Linker::linkKnown(
299 $undelete,
300 $date,
301 [ 'class' => 'mw-changeslist-date' ],
302 [
303 'target' => $page->getPrefixedText(),
304 'timestamp' => $rev->getTimestamp()
305 ]
306 );
307 }
308 // Style deleted items
309 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
310 $link = '<span class="history-deleted">' . $link . '</span>';
311 }
312
313 $pagelink = Linker::link(
314 $page,
315 null,
316 [ 'class' => 'mw-changeslist-title' ]
317 );
318
319 if ( $rev->isMinor() ) {
320 $mflag = ChangesList::flag( 'minor' );
321 } else {
322 $mflag = '';
323 }
324
325 // Revision delete link
326 $del = Linker::getRevDeleteLink( $user, $rev, $page );
327 if ( $del ) {
328 $del .= ' ';
329 }
330
331 $tools = Html::rawElement(
332 'span',
333 [ 'class' => 'mw-deletedcontribs-tools' ],
334 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
335 [ $last, $dellog, $reviewlink ] ) )->escaped()
336 );
337
338 $separator = '<span class="mw-changeslist-separator">. .</span>';
339 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
340
341 # Denote if username is redacted for this edit
342 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
343 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
344 }
345
346 return $ret;
347 }
348
349 /**
350 * Get the Database object in use
351 *
352 * @return IDatabase
353 */
354 public function getDatabase() {
355 return $this->mDb;
356 }
357 }
358
359 class DeletedContributionsPage extends SpecialPage {
360 function __construct() {
361 parent::__construct( 'DeletedContributions', 'deletedhistory',
362 /*listed*/true, /*function*/false, /*file*/false );
363 }
364
365 /**
366 * Special page "deleted user contributions".
367 * Shows a list of the deleted contributions of a user.
368 *
369 * @param string $par (optional) user name of the user for which to show the contributions
370 */
371 function execute( $par ) {
372 $this->setHeaders();
373 $this->outputHeader();
374
375 $user = $this->getUser();
376
377 if ( !$this->userCanExecute( $user ) ) {
378 $this->displayRestrictionError();
379
380 return;
381 }
382
383 $request = $this->getRequest();
384 $out = $this->getOutput();
385 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
386
387 $options = [];
388
389 if ( $par !== null ) {
390 $target = $par;
391 } else {
392 $target = $request->getVal( 'target' );
393 }
394
395 if ( !strlen( $target ) ) {
396 $out->addHTML( $this->getForm( '' ) );
397
398 return;
399 }
400
401 $options['limit'] = $request->getInt( 'limit',
402 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
403 $options['target'] = $target;
404
405 $userObj = User::newFromName( $target, false );
406 if ( !$userObj ) {
407 $out->addHTML( $this->getForm( '' ) );
408
409 return;
410 }
411 $this->getSkin()->setRelevantUser( $userObj );
412
413 $target = $userObj->getName();
414 $out->addSubtitle( $this->getSubTitle( $userObj ) );
415
416 $ns = $request->getVal( 'namespace', null );
417 if ( $ns !== null && $ns !== '' ) {
418 $options['namespace'] = intval( $ns );
419 } else {
420 $options['namespace'] = '';
421 }
422
423 $out->addHTML( $this->getForm( $options ) );
424
425 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
426 if ( !$pager->getNumRows() ) {
427 $out->addWikiMsg( 'nocontribs' );
428
429 return;
430 }
431
432 # Show a message about slave lag, if applicable
433 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
434 if ( $lag > 0 ) {
435 $out->showLagWarning( $lag );
436 }
437
438 $out->addHTML(
439 '<p>' . $pager->getNavigationBar() . '</p>' .
440 $pager->getBody() .
441 '<p>' . $pager->getNavigationBar() . '</p>' );
442
443 # If there were contributions, and it was a valid user or IP, show
444 # the appropriate "footer" message - WHOIS tools, etc.
445 if ( $target != 'newbies' ) {
446 $message = IP::isIPAddress( $target ) ?
447 'sp-contributions-footer-anon' :
448 'sp-contributions-footer';
449
450 if ( !$this->msg( $message )->isDisabled() ) {
451 $out->wrapWikiMsg(
452 "<div class='mw-contributions-footer'>\n$1\n</div>",
453 [ $message, $target ]
454 );
455 }
456 }
457 }
458
459 /**
460 * Generates the subheading with links
461 * @param User $userObj User object for the target
462 * @return string Appropriately-escaped HTML to be output literally
463 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
464 */
465 function getSubTitle( $userObj ) {
466 if ( $userObj->isAnon() ) {
467 $user = htmlspecialchars( $userObj->getName() );
468 } else {
469 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
470 }
471 $links = '';
472 $nt = $userObj->getUserPage();
473 $id = $userObj->getId();
474 $talk = $nt->getTalkPage();
475 if ( $talk ) {
476 # Talk page link
477 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
478 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
479 # Block / Change block / Unblock links
480 if ( $this->getUser()->isAllowed( 'block' ) ) {
481 if ( $userObj->isBlocked() && $userObj->getBlock()->getType() !== Block::TYPE_AUTO ) {
482 $tools[] = Linker::linkKnown( # Change block link
483 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
484 $this->msg( 'change-blocklink' )->escaped()
485 );
486 $tools[] = Linker::linkKnown( # Unblock link
487 SpecialPage::getTitleFor( 'BlockList' ),
488 $this->msg( 'unblocklink' )->escaped(),
489 [],
490 [
491 'action' => 'unblock',
492 'ip' => $nt->getDBkey()
493 ]
494 );
495 } else {
496 # User is not blocked
497 $tools[] = Linker::linkKnown( # Block link
498 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
499 $this->msg( 'blocklink' )->escaped()
500 );
501 }
502 }
503 # Block log link
504 $tools[] = Linker::linkKnown(
505 SpecialPage::getTitleFor( 'Log' ),
506 $this->msg( 'sp-contributions-blocklog' )->escaped(),
507 [],
508 [
509 'type' => 'block',
510 'page' => $nt->getPrefixedText()
511 ]
512 );
513 # Suppression log link (bug 59120)
514 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
515 $tools[] = Linker::linkKnown(
516 SpecialPage::getTitleFor( 'Log', 'suppress' ),
517 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
518 [],
519 [ 'offender' => $userObj->getName() ]
520 );
521 }
522 }
523
524 # Uploads
525 $tools[] = Linker::linkKnown(
526 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
527 $this->msg( 'sp-contributions-uploads' )->escaped()
528 );
529
530 # Other logs link
531 $tools[] = Linker::linkKnown(
532 SpecialPage::getTitleFor( 'Log' ),
533 $this->msg( 'sp-contributions-logs' )->escaped(),
534 [],
535 [ 'user' => $nt->getText() ]
536 );
537 # Link to contributions
538 $tools[] = Linker::linkKnown(
539 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
540 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
541 );
542
543 # Add a link to change user rights for privileged users
544 $userrightsPage = new UserrightsPage();
545 $userrightsPage->setContext( $this->getContext() );
546 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
547 $tools[] = Linker::linkKnown(
548 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
549 $this->msg( 'sp-contributions-userrights' )->escaped()
550 );
551 }
552
553 Hooks::run( 'ContributionsToolLinks', [ $id, $nt, &$tools ] );
554
555 $links = $this->getLanguage()->pipeList( $tools );
556
557 // Show a note if the user is blocked and display the last block log entry.
558 $block = Block::newFromTarget( $userObj, $userObj );
559 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
560 if ( $block->getType() == Block::TYPE_RANGE ) {
561 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
562 }
563
564 // LogEventsList::showLogExtract() wants the first parameter by ref
565 $out = $this->getOutput();
566 LogEventsList::showLogExtract(
567 $out,
568 'block',
569 $nt,
570 '',
571 [
572 'lim' => 1,
573 'showIfEmpty' => false,
574 'msgKey' => [
575 'sp-contributions-blocked-notice',
576 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
577 ],
578 'offset' => '' # don't use $this->getRequest() parameter offset
579 ]
580 );
581 }
582 }
583
584 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
585 }
586
587 /**
588 * Generates the namespace selector form with hidden attributes.
589 * @param array $options The options to be included.
590 * @return string
591 */
592 function getForm( $options ) {
593 $options['title'] = $this->getPageTitle()->getPrefixedText();
594 if ( !isset( $options['target'] ) ) {
595 $options['target'] = '';
596 } else {
597 $options['target'] = str_replace( '_', ' ', $options['target'] );
598 }
599
600 if ( !isset( $options['namespace'] ) ) {
601 $options['namespace'] = '';
602 }
603
604 if ( !isset( $options['contribs'] ) ) {
605 $options['contribs'] = 'user';
606 }
607
608 if ( $options['contribs'] == 'newbie' ) {
609 $options['target'] = '';
610 }
611
612 $f = Xml::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] );
613
614 foreach ( $options as $name => $value ) {
615 if ( in_array( $name, [ 'namespace', 'target', 'contribs' ] ) ) {
616 continue;
617 }
618 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
619 }
620
621 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
622
623 $f .= Xml::openElement( 'fieldset' );
624 $f .= Xml::element( 'legend', [], $this->msg( 'sp-contributions-search' )->text() );
625 $f .= Xml::tags(
626 'label',
627 [ 'for' => 'target' ],
628 $this->msg( 'sp-contributions-username' )->parse()
629 ) . ' ';
630 $f .= Html::input(
631 'target',
632 $options['target'],
633 'text',
634 [
635 'size' => '20',
636 'required' => '',
637 'class' => [
638 'mw-autocomplete-user', // used by mediawiki.userSuggest
639 ],
640 ] + ( $options['target'] ? [] : [ 'autofocus' ] )
641 ) . ' ';
642 $f .= Html::namespaceSelector(
643 [
644 'selected' => $options['namespace'],
645 'all' => '',
646 'label' => $this->msg( 'namespace' )->text()
647 ],
648 [
649 'name' => 'namespace',
650 'id' => 'namespace',
651 'class' => 'namespaceselector',
652 ]
653 ) . ' ';
654 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
655 $f .= Xml::closeElement( 'fieldset' );
656 $f .= Xml::closeElement( 'form' );
657
658 return $f;
659 }
660
661 /**
662 * Return an array of subpages beginning with $search that this special page will accept.
663 *
664 * @param string $search Prefix to search for
665 * @param int $limit Maximum number of results to return (usually 10)
666 * @param int $offset Number of results to skip (usually 0)
667 * @return string[] Matching subpages
668 */
669 public function prefixSearchSubpages( $search, $limit, $offset ) {
670 $user = User::newFromName( $search );
671 if ( !$user ) {
672 // No prefix suggestion for invalid user
673 return [];
674 }
675 // Autocomplete subpage as user list - public to allow caching
676 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
677 }
678
679 protected function getGroupName() {
680 return 'users';
681 }
682 }