Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialContributionsHookHandler
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onContributionsToolLinks
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 onSpecialContributionsBeforeMainOutput
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\Hooks\Handlers;
22
23use LogEventsList;
24use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
25use MediaWiki\Hook\ContributionsToolLinksHook;
26use MediaWiki\Hook\SpecialContributionsBeforeMainOutputHook;
27use MediaWiki\Html\Html;
28use MediaWiki\SpecialPage\SpecialPage;
29use MediaWiki\Title\NamespaceInfo;
30use MediaWiki\Title\Title;
31use MediaWiki\User\User;
32use MediaWiki\User\UserFactory;
33use MediaWiki\User\UserNameUtils;
34
35class SpecialContributionsHookHandler implements
36    ContributionsToolLinksHook,
37    SpecialContributionsBeforeMainOutputHook
38{
39
40    private NamespaceInfo $namespaceInfo;
41    private UserFactory $userFactory;
42    private UserNameUtils $userNameUtils;
43
44    public function __construct(
45        NamespaceInfo $namespaceInfo,
46        UserFactory $userFactory,
47        UserNameUtils $userNameUtils
48    ) {
49        $this->namespaceInfo = $namespaceInfo;
50        $this->userFactory = $userFactory;
51        $this->userNameUtils = $userNameUtils;
52    }
53
54    /**
55     * @param int $id User ID
56     * @param Title $title User page title
57     * @param array &$tools Array of tool links
58     * @param SpecialPage $sp for context
59     * @return bool|void
60     */
61    public function onContributionsToolLinks( $id, Title $title, array &$tools, SpecialPage $sp ) {
62        $user = $this->userFactory->newFromId( $id );
63        if ( !$user->isRegistered() ) {
64            return true;
65        }
66        if ( $this->userNameUtils->getCanonical( $user->getName() ) === false ) {
67            return true;
68        }
69        $centralUser = CentralAuthUser::getInstance( $user );
70        if ( !$centralUser->exists() || !$centralUser->isAttached() ) {
71            return true;
72        }
73        $linkRenderer = $sp->getLinkRenderer();
74        $tools['centralauth'] = $linkRenderer->makeKnownLink(
75            SpecialPage::getTitleFor( 'CentralAuth', $title->getText() ),
76            $sp->msg( 'centralauth-contribs-link' )->text(),
77            [ 'class' => 'mw-contributions-link-centralauth' ]
78        );
79    }
80
81    /**
82     * @param int $id User ID
83     * @param User $user
84     * @param SpecialPage $sp
85     * @return bool|void
86     */
87    public function onSpecialContributionsBeforeMainOutput( $id, $user, $sp ) {
88        if ( !$user->isRegistered() ) {
89            return true;
90        }
91        if ( $this->userNameUtils->getCanonical( $user->getName() ) === false ) {
92            return true;
93        }
94
95        $centralUser = CentralAuthUser::getInstance( $user );
96        if ( !$centralUser->exists() || !$centralUser->isAttached()
97            || !$centralUser->isLocked() || $centralUser->isHidden()
98        ) {
99            return true;
100        }
101
102        $out = $sp->getOutput();
103        $count = LogEventsList::showLogExtract(
104            $out,
105            [ 'globalauth' ],
106            $this->namespaceInfo->getCanonicalName( NS_USER ) . ":{$user}@global",
107            '',
108            [
109                'lim' => 1,
110                'showIfEmpty' => false,
111                'msgKey' => [
112                    'centralauth-contribs-locked-log',
113                    $user->getName()
114                ],
115                'offset' => '',
116            ]
117        );
118
119        if ( $count === 0 ) {
120            // we couldn't load the log entry
121            $out->addHTML(
122                Html::warningBox(
123                    $out->msg( 'centralauth-contribs-locked', $user )->parse(),
124                    'mw-warning-with-logexcerpt'
125                )
126            );
127        }
128    }
129}