Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 125
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryGlobalUserInfo
0.00% covered (danger)
0.00%
0 / 125
0.00% covered (danger)
0.00%
0 / 5
1332
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 1
930
 getCacheMode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Created on Jan 30, 2010
4 *
5 * CentralAuth extension
6 *
7 * Copyright (C) 2010 Roan Kattouw roan DOT kattouw AT gmail DOT com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25namespace MediaWiki\Extension\CentralAuth\Api;
26
27use ApiBase;
28use ApiQuery;
29use ApiQueryBase;
30use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
31use MediaWiki\ParamValidator\TypeDef\UserDef;
32use MediaWiki\User\UserNameUtils;
33use MediaWiki\WikiMap\WikiMap;
34use Wikimedia\ParamValidator\ParamValidator;
35
36/**
37 * Query module to list global user info and attachments
38 *
39 * @ingroup API
40 * @ingroup Extensions
41 */
42class ApiQueryGlobalUserInfo extends ApiQueryBase {
43
44    /** @var UserNameUtils */
45    private $userNameUtils;
46
47    /**
48     * @param ApiQuery $query
49     * @param string $moduleName
50     * @param UserNameUtils $userNameUtils
51     */
52    public function __construct( ApiQuery $query, $moduleName, UserNameUtils $userNameUtils ) {
53        parent::__construct( $query, $moduleName, 'gui' );
54        $this->userNameUtils = $userNameUtils;
55    }
56
57    public function execute() {
58        $params = $this->extractRequestParams();
59        $prop = array_flip( (array)$params['prop'] );
60        if ( $params['user'] === null && $params['id'] === null ) {
61            $params['user'] = $this->getUser()->getName();
62        }
63
64        if ( $params['user'] === null ) {
65            $user = CentralAuthUser::newFromId( $params['id'] );
66            if ( $user === false ) {
67                $this->dieWithError( [ 'apierror-invaliduserid', wfEscapeWikiText( $params['id'] ) ] );
68            }
69        } else {
70            $username = $this->userNameUtils->getCanonical( $params['user'] );
71            if ( $username === false ) {
72                $this->dieWithError( [ 'apierror-invaliduser', wfEscapeWikiText( $params['user'] ) ] );
73            }
74            $user = CentralAuthUser::getInstanceByName( $username );
75        }
76
77        // Add basic info
78        $result = $this->getResult();
79        $data = [];
80        $userExists = $user->exists();
81
82        if ( $userExists &&
83            ( $user->getHiddenLevelInt() === CentralAuthUser::HIDDEN_LEVEL_NONE ||
84            $this->getAuthority()->isAllowed( 'centralauth-suppress' ) )
85        ) {
86            // The global user exists and it's not hidden or the current user is allowed to see it
87            $data['home'] = $user->getHomeWiki();
88            $data['id'] = $user->getId();
89            $data['registration'] = wfTimestamp( TS_ISO_8601, $user->getRegistration() );
90            $data['name'] = $user->getName();
91
92            if ( $user->isLocked() ) {
93                $data['locked'] = true;
94            }
95            if ( $user->isHidden() ) {
96                $data['hidden'] = true;
97            }
98        } else {
99            // The user doesn't exist or we pretend it doesn't if it's hidden
100            $data['missing'] = true;
101
102            // If we are pretending that the user doesn't exist because it is hidden,
103            // do not add any more information
104            $userExists = false;
105        }
106        $result->addValue( 'query', $this->getModuleName(), $data );
107
108        // Add requested info
109        if ( $userExists && isset( $prop['groups'] ) ) {
110            $groups = $user->getGlobalGroups();
111            $result->setIndexedTagName( $groups, 'g' );
112            $result->addValue( [ 'query', $this->getModuleName() ], 'groups', $groups );
113        }
114        if ( $userExists && isset( $prop['rights'] ) ) {
115            $rights = $user->getGlobalRights();
116            $result->setIndexedTagName( $rights, 'r' );
117            $result->addValue( [ 'query', $this->getModuleName() ], 'rights', $rights );
118        }
119
120        $attachedAccounts = null;
121        if ( $userExists && ( isset( $prop['merged'] ) || isset( $prop['editcount'] ) ) ) {
122            $attachedAccounts = $user->queryAttached();
123        }
124
125        if ( $userExists && isset( $prop['merged'] ) ) {
126            foreach ( $attachedAccounts as $account ) {
127                $dbname = $account['wiki'];
128                $wiki = WikiMap::getWiki( $dbname );
129                $a = [
130                    'wiki' => $dbname,
131                    'url' => $wiki->getCanonicalServer(),
132                    'id' => intval( $account['id'] ),
133                    'timestamp' => wfTimestamp( TS_ISO_8601, $account['attachedTimestamp'] ),
134                    'method' => $account['attachedMethod'],
135                    'editcount' => intval( $account['editCount'] ),
136                    'registration' => wfTimestamp( TS_ISO_8601, $account['registration'] ),
137                ];
138                if ( $account['groupMemberships'] ) {
139                    $a['groups'] = array_keys( $account['groupMemberships'] );
140                    $result->setIndexedTagName( $a['groups'], 'group' );
141                }
142
143                if ( $account['blocked'] ) {
144                    $a['blocked'] = [
145                        'expiry' => $this->getLanguage()->formatExpiry(
146                            $account['block-expiry'], TS_ISO_8601 ),
147                        'reason' => $account['block-reason']
148                    ];
149                }
150                $result->addValue( [ 'query', $this->getModuleName(), 'merged' ], null, $a );
151            }
152            $result->addIndexedTagName( [ 'query', $this->getModuleName(), 'merged' ], 'account' );
153        }
154        if ( $userExists && isset( $prop['editcount'] ) ) {
155            $editcount = 0;
156            foreach ( $attachedAccounts as $account ) {
157                $editcount += $account['editCount'];
158            }
159            $result->addValue( 'query', $this->getModuleName(), [ 'editcount' => $editcount ] );
160        }
161        if ( isset( $prop['unattached'] ) ) {
162            $accounts = $user->queryUnattached();
163            foreach ( $accounts as $account ) {
164                $a = [
165                    'wiki' => $account['wiki'],
166                    'editcount' => $account['editCount'],
167                    'registration' => wfTimestamp( TS_ISO_8601, $account['registration'] ),
168                ];
169
170                if ( $account['groupMemberships'] ) {
171                    $a['groups'] = array_keys( $account['groupMemberships'] );
172                    $result->setIndexedTagName( $a['groups'], 'group' );
173                }
174
175                if ( $account['blocked'] ) {
176                    $a['blocked'] = [
177                        'expiry' => $this->getLanguage()->formatExpiry(
178                            $account['block-expiry'], TS_ISO_8601 ),
179                        'reason' => $account['block-reason']
180                    ];
181                }
182                $result->addValue( [ 'query', $this->getModuleName(), 'unattached' ], null, $a );
183            }
184            $result->addIndexedTagName(
185                [ 'query', $this->getModuleName(), 'unattached' ], 'account'
186            );
187        }
188    }
189
190    /** @inheritDoc */
191    public function getCacheMode( $params ) {
192        if ( $params['user'] !== null || $params['id'] !== null ) {
193            // URL determines user, public caching is fine
194            return 'public';
195        } else {
196            // Code will fall back to the context user, don't cache
197            return 'private';
198        }
199    }
200
201    /** @inheritDoc */
202    public function getAllowedParams() {
203        return [
204            'user' => [
205                ParamValidator::PARAM_TYPE => 'user',
206                UserDef::PARAM_ALLOWED_USER_TYPES => [
207                    'name',
208                    'temp',
209                    'interwiki',
210                ],
211            ],
212            'id' => [
213                ParamValidator::PARAM_TYPE => 'integer',
214            ],
215            'prop' => [
216                ParamValidator::PARAM_TYPE => [
217                    'groups',
218                    'rights',
219                    'merged',
220                    'unattached',
221                    'editcount'
222                ],
223                ParamValidator::PARAM_ISMULTI => true,
224                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
225            ]
226        ];
227    }
228
229    /** @inheritDoc */
230    protected function getExamplesMessages() {
231        return [
232            'action=query&meta=globaluserinfo'
233                => 'apihelp-query+globaluserinfo-example-1',
234            'action=query&meta=globaluserinfo&guiuser=Example&guiprop=groups|merged|unattached'
235                => 'apihelp-query+globaluserinfo-example-2',
236        ];
237    }
238}