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