Debian Codegraph @

extensions/Gadgets/includes/Api/ApiQueryGadgetCategories.php

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