Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiAntiSpoof
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
42
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types=1 );
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20namespace MediaWiki\Extension\AntiSpoof;
21
22use MediaWiki\Api\ApiBase;
23use MediaWiki\Api\ApiResult;
24use MediaWiki\User\User;
25use Wikimedia\ParamValidator\ParamValidator;
26
27/**
28 * API module to check a username against the AntiSpoof normalisation checks
29 *
30 * @ingroup API
31 * @ingroup Extensions
32 */
33class ApiAntiSpoof extends ApiBase {
34
35    public function execute() {
36        $params = $this->extractRequestParams();
37
38        $res = $this->getResult();
39        $res->addValue( null, $this->getModuleName(), [ 'username' => $params['username'] ] );
40
41        $spoof = new SpoofUser( $params['username'] );
42
43        if ( $spoof->isLegal() ) {
44            $normalized = $spoof->getNormalized();
45            $res->addValue( null, $this->getModuleName(), [ 'normalised' => $normalized ] );
46
47            $unfilteredConflicts = $spoof->getConflicts();
48            if ( !$unfilteredConflicts ) {
49                $res->addValue( null, $this->getModuleName(), [ 'result' => 'pass' ] );
50            } else {
51                $hasSuppressed = false;
52                $conflicts = [];
53                foreach ( $unfilteredConflicts as $conflict ) {
54                    if ( !User::newFromName( $conflict )->isHidden() ) {
55                        $conflicts[] = $conflict;
56                    } else {
57                        $hasSuppressed = true;
58                    }
59                }
60
61                if ( $hasSuppressed ) {
62                    $res->addValue( null, $this->getModuleName(), [ 'suppressed' => 'true' ] );
63                }
64
65                $res->addValue( null, $this->getModuleName(), [ 'result' => 'conflict' ] );
66
67                ApiResult::setIndexedTagName( $conflicts, 'u' );
68                $res->addValue( [ $this->getModuleName() ], 'users', $conflicts );
69            }
70        } else {
71            $errorStatus = $spoof->getErrorStatus();
72            $res->addValue( 'antispoof', 'result', 'error' );
73            $res->addValue(
74                'antispoof',
75                'error',
76                $errorStatus->getMessage( false, false, $this->getLanguage() )->text()
77            );
78        }
79    }
80
81    /** @inheritDoc */
82    public function getAllowedParams() {
83        return [
84            'username' => [
85                ParamValidator::PARAM_REQUIRED => true,
86            ],
87        ];
88    }
89
90    /** @inheritDoc */
91    protected function getExamplesMessages() {
92        return [
93            'action=antispoof&username=Foo'
94                => 'apihelp-antispoof-example-1',
95        ];
96    }
97}