]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/Gadgets/api/ApiQueryGadgetCategories.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / Gadgets / api / ApiQueryGadgetCategories.php
1 <?php
2 /**
3  * Created on 16 April 2011
4  * API for Gadgets extension
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  * http://www.gnu.org/copyleft/gpl.html
20  */
21
22 class ApiQueryGadgetCategories extends ApiQueryBase {
23         /**
24          * @var array
25          */
26         private $props;
27
28         /**
29          * @var array|bool
30          */
31         private $neededNames;
32
33         public function __construct( ApiQuery $queryModule, $moduleName ) {
34                 parent::__construct( $queryModule, $moduleName, 'gc' );
35         }
36
37         public function execute() {
38                 $params = $this->extractRequestParams();
39                 $this->props = array_flip( $params['prop'] );
40                 $this->neededNames = isset( $params['names'] )
41                         ? array_flip( $params['names'] )
42                         : false;
43
44                 $this->getMain()->setCacheMode( 'public' );
45
46                 $this->getList();
47         }
48
49         private function getList() {
50                 $data = [];
51                 $result = $this->getResult();
52                 $gadgets = GadgetRepo::singleton()->getStructuredList();
53
54                 if ( $gadgets ) {
55                         foreach ( $gadgets as $category => $list ) {
56                                 if ( !$this->neededNames || isset( $this->neededNames[$category] ) ) {
57                                         $row = [];
58                                         if ( isset( $this->props['name'] ) ) {
59                                                 $row['name'] = $category;
60                                         }
61
62                                         if ( $category !== "" ) {
63                                                 if ( isset( $this->props['title'] ) ) {
64                                                         $row['desc'] = $this->msg( "gadget-section-$category" )->parse();
65                                                 }
66                                         }
67
68                                         if ( isset( $this->props['members'] ) ) {
69                                                 $row['members'] = count( $list );
70                                         }
71
72                                         $data[] = $row;
73                                 }
74                         }
75                 }
76                 $result->setIndexedTagName( $data, 'category' );
77                 $result->addValue( 'query', $this->getModuleName(), $data );
78         }
79
80         public function getAllowedParams() {
81                 return [
82                         'prop' => [
83                                 ApiBase::PARAM_DFLT => 'name',
84                                 ApiBase::PARAM_ISMULTI => true,
85                                 ApiBase::PARAM_TYPE => [
86                                         'name',
87                                         'title',
88                                         'members',
89                                 ],
90                         ],
91                         'names' => [
92                                 ApiBase::PARAM_TYPE => 'string',
93                                 ApiBase::PARAM_ISMULTI => true,
94                         ],
95                 ];
96         }
97
98         /**
99          * @see ApiBase::getExamplesMessages()
100          * @return array
101          */
102         protected function getExamplesMessages() {
103                 return [
104                         'action=query&list=gadgetcategories'
105                                 => 'apihelp-query+gadgetcategories-example-1',
106                         'action=query&list=gadgetcategories&gcnames=foo|bar&gcprop=name|title|members'
107                                 => 'apihelp-query+gadgetcategories-example-2',
108                 ];
109         }
110 }