Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryGlobalRenameStatus
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
72
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 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 addUser
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 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 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\Api;
22
23use ApiQuery;
24use ApiQueryBase;
25use ApiResult;
26use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameFactory;
27use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserStatus;
28use Wikimedia\ParamValidator\ParamValidator;
29
30/**
31 * API module equivalent of Special:GlobalRenameStatus
32 */
33class ApiQueryGlobalRenameStatus extends ApiQueryBase {
34
35    private GlobalRenameFactory $globalRenameFactory;
36
37    /** @inheritDoc */
38    public function __construct(
39        ApiQuery $queryModule,
40        $moduleName,
41        GlobalRenameFactory $globalRenameFactory
42    ) {
43        parent::__construct( $queryModule, $moduleName, 'grs' );
44        $this->globalRenameFactory = $globalRenameFactory;
45    }
46
47    /**
48     * If a 'user' parameter is provided, get the details for that
49     * user, otherwise output details for all current renames
50     */
51    public function execute() {
52        $params = $this->extractRequestParams();
53        if ( $params['user'] ) {
54            $this->addUser( $params['user'] );
55        } else {
56            $renames = GlobalRenameUserStatus::getInProgressRenames( $this->getUser() );
57            foreach ( $renames as $user ) {
58                $this->addUser( $user );
59            }
60        }
61    }
62
63    /**
64     * Look up and add info for a rename
65     *
66     * @param string $name Username (old or new)
67     */
68    private function addUser( $name ) {
69        $statuses = $this->globalRenameFactory->newGlobalRenameUserStatus( $name );
70        $names = $statuses->getNames();
71        if ( !$names ) {
72            return;
73        }
74        $info = [
75            'from' => $names[0],
76            'to' => $names[1],
77            'status' => $statuses->getStatuses(),
78        ];
79        ApiResult::setArrayType( $info['status'], 'assoc' );
80        $this->getResult()->addValue( [ 'query', 'globalrenamestatus' ], $name, $info );
81    }
82
83    /**
84     * @see ApiBase::getAllowedParams()
85     * @return array[]
86     */
87    public function getAllowedParams() {
88        return [
89            'user' => [
90                ParamValidator::PARAM_TYPE => 'user',
91            ]
92        ];
93    }
94
95    /** @inheritDoc */
96    protected function getExamplesMessages() {
97        return [
98            'action=query&meta=globalrenamestatus'
99                => 'apihelp-query+globalrenamestatus-example-1',
100        ];
101    }
102}