Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.35% covered (danger)
19.35%
6 / 31
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserMergeHooks
19.35% covered (danger)
19.35%
6 / 31
50.00% covered (danger)
50.00%
1 / 2
41.57
0.00% covered (danger)
0.00%
0 / 1
 onUserGetReservedNames
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 onContributionsToolLinks
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3use MediaWiki\Hook\ContributionsToolLinksHook;
4use MediaWiki\MediaWikiServices;
5use MediaWiki\User\Hook\UserGetReservedNamesHook;
6
7class UserMergeHooks implements
8    UserGetReservedNamesHook,
9    ContributionsToolLinksHook
10{
11    /**
12     * UserGetReservedNames hook handler
13     *
14     * @param string[] &$reservedUsernames Already registered reserved names
15     */
16    public function onUserGetReservedNames( &$reservedUsernames ) {
17        $deleteEnabled = MediaWikiServices::getInstance()
18            ->getConfigFactory()
19            ->makeConfig( 'usermerge' )
20            ->get( 'UserMergeEnableDelete' );
21
22        if ( $deleteEnabled ) {
23            $reservedUsernames[] = 'Anonymous';
24        }
25    }
26
27    /**
28     * Handler for ContributionsToolLinks hook
29     *
30     * @param int $id
31     * @param Title $nt
32     * @param array &$tools
33     * @param SpecialPage $sp for context
34     */
35    public function onContributionsToolLinks(
36        $id, Title $nt, array &$tools, SpecialPage $sp
37    ) {
38        if ( $id === 0 || $id === $sp->getUser()->getId() ) {
39            return;
40        }
41        if ( !$sp->getAuthority()->isAllowed( 'usermerge' ) ) {
42            return;
43        }
44        $targetUser = User::newFromId( $id );
45        if ( array_intersect(
46            MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $targetUser ),
47            $sp->getConfig()->get( 'UserMergeProtectedGroups' )
48        ) ) {
49            return;
50        }
51
52        $username = $nt->getText();
53        $linkRenderer = $sp->getLinkRenderer();
54        $tools['usermerge-merge'] = $linkRenderer->makeKnownLink(
55            SpecialPage::getTitleFor( 'UserMerge' ),
56            $sp->msg( 'usermerge-merge-linkoncontribs', $username )->text(),
57            [],
58            [ 'wpolduser' => $username ]
59        );
60        if ( $sp->getConfig()->get( 'UserMergeEnableDelete' ) ) {
61            $tools['usermerge-delete'] = $linkRenderer->makeKnownLink(
62                SpecialPage::getTitleFor( 'UserMerge' ),
63                $sp->msg( 'usermerge-delete-linkoncontribs', $username )->text(),
64                [],
65                [ 'wpolduser' => $username, 'wpdelete' => '1' ]
66            );
67        }
68    }
69
70}