Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryGadgetCategories
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getList
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
90
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 16
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 * 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\Gadgets\Api;
22
23use MediaWiki\Api\ApiBase;
24use MediaWiki\Api\ApiQuery;
25use MediaWiki\Api\ApiQueryBase;
26use MediaWiki\Api\ApiResult;
27use MediaWiki\Extension\Gadgets\GadgetRepo;
28use Wikimedia\ParamValidator\ParamValidator;
29
30/**
31 * API for Gadgets extension
32 */
33class ApiQueryGadgetCategories extends ApiQueryBase {
34    /**
35     * @var array
36     */
37    private $props;
38
39    /**
40     * @var array|bool
41     */
42    private $neededNames;
43
44    public function __construct(
45        ApiQuery $queryModule,
46        string $moduleName,
47        private readonly GadgetRepo $gadgetRepo,
48    ) {
49        parent::__construct( $queryModule, $moduleName, 'gc' );
50    }
51
52    public function execute() {
53        $params = $this->extractRequestParams();
54        $this->props = array_flip( $params['prop'] );
55        $this->neededNames = isset( $params['names'] )
56            ? array_flip( $params['names'] )
57            : false;
58
59        $this->getMain()->setCacheMode( 'public' );
60
61        $this->getList();
62    }
63
64    /**
65     * @return void
66     */
67    private function getList() {
68        $data = [];
69        $result = $this->getResult();
70        $gadgets = $this->gadgetRepo->getStructuredList();
71
72        if ( $gadgets ) {
73            foreach ( $gadgets as $section => $list ) {
74                if ( $this->neededNames && !isset( $this->neededNames[$section] ) ) {
75                    continue;
76                }
77                $row = [];
78                if ( isset( $this->props['name'] ) ) {
79                    $row['name'] = $section;
80                }
81
82                if ( ( $section !== "" ) && isset( $this->props['title'] ) ) {
83                    $row['desc'] = $this->msg( "gadget-section-$section" )->parse();
84                }
85
86                if ( isset( $this->props['members'] ) ) {
87                    $row['members'] = count( $list );
88                }
89
90                $data[] = $row;
91            }
92        }
93        ApiResult::setIndexedTagName( $data, 'category' );
94        $result->addValue( 'query', $this->getModuleName(), $data );
95    }
96
97    /** @inheritDoc */
98    public function getAllowedParams() {
99        return [
100            'prop' => [
101                ParamValidator::PARAM_DEFAULT => 'name',
102                ParamValidator::PARAM_ISMULTI => true,
103                ParamValidator::PARAM_TYPE => [
104                    'name',
105                    'title',
106                    'members',
107                ],
108                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
109            ],
110            'names' => [
111                ParamValidator::PARAM_TYPE => 'string',
112                ParamValidator::PARAM_ISMULTI => true,
113            ],
114        ];
115    }
116
117    /**
118     * @see ApiBase::getExamplesMessages()
119     * @return array
120     */
121    protected function getExamplesMessages() {
122        return [
123            'action=query&list=gadgetcategories'
124                => 'apihelp-query+gadgetcategories-example-1',
125            'action=query&list=gadgetcategories&gcnames=foo|bar&gcprop=name|title|members'
126                => 'apihelp-query+gadgetcategories-example-2',
127        ];
128    }
129}