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
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
19namespace MediaWiki\Extension\AntiSpoof;
20
21use ApiBase;
22use MediaWiki\User\User;
23use Wikimedia\ParamValidator\ParamValidator;
24
25/**
26 * API module to check a username against the AntiSpoof normalisation checks
27 *
28 * @ingroup API
29 * @ingroup Extensions
30 */
31class ApiAntiSpoof extends ApiBase {
32
33    public function execute() {
34        $params = $this->extractRequestParams();
35
36        $res = $this->getResult();
37        $res->addValue( null, $this->getModuleName(), [ 'username' => $params['username'] ] );
38
39        $spoof = new SpoofUser( $params['username'] );
40
41        if ( $spoof->isLegal() ) {
42            $normalized = $spoof->getNormalized();
43            $res->addValue( null, $this->getModuleName(), [ 'normalised' => $normalized ] );
44
45            $unfilteredConflicts = $spoof->getConflicts();
46            if ( !$unfilteredConflicts ) {
47                $res->addValue( null, $this->getModuleName(), [ 'result' => 'pass' ] );
48            } else {
49                $hasSuppressed = false;
50                $conflicts = [];
51                foreach ( $unfilteredConflicts as $conflict ) {
52                    if ( !User::newFromName( $conflict )->isHidden() ) {
53                        $conflicts[] = $conflict;
54                    } else {
55                        $hasSuppressed = true;
56                    }
57                }
58
59                if ( $hasSuppressed ) {
60                    $res->addValue( null, $this->getModuleName(), [ 'suppressed' => 'true' ] );
61                }
62
63                $res->addValue( null, $this->getModuleName(), [ 'result' => 'conflict' ] );
64
65                $res->setIndexedTagName( $conflicts, 'u' );
66                $res->addValue( [ $this->getModuleName() ], 'users', $conflicts );
67            }
68        } else {
69            $errorStatus = $spoof->getErrorStatus();
70            $res->addValue( 'antispoof', 'result', 'error' );
71            $res->addValue(
72                'antispoof',
73                'error',
74                $errorStatus->getMessage( false, false, $this->getLanguage() )->text()
75            );
76        }
77    }
78
79    /** @inheritDoc */
80    public function getAllowedParams() {
81        return [
82            'username' => [
83                ParamValidator::PARAM_REQUIRED => true,
84            ],
85        ];
86    }
87
88    /**
89     * @see ApiBase::getExamplesMessages()
90     * @return array
91     */
92    protected function getExamplesMessages() {
93        return [
94            'action=antispoof&username=Foo'
95                => 'apihelp-antispoof-example-1',
96        ];
97    }
98}